Skip to content

nicoldr/company-hierarchy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# Company Hierarchy Management

## Description

This project is a web application for managing the hierarchy of departments within a company. It allows for the creation, updating, deletion, and hierarchical visualization of departments. The application is built using Golang and MySQL, with all database operations managed through stored procedures. Authentication is implemented using JWT to ensure secure access.

## Bitwise Representation of `flags`

The `flags` field uses bitwise representation to indicate multiple states of a department:

- **Bit 1 (Least Significant Bit)**: Active (1) / Inactive (0)
- **Bit 2**: Deleted (1) / Not Deleted (0)
- **Bit 3**: Approved (1) / Not Approved (0)

Examples:
- `000` (binary) = `0` (decimal): Inactive, Restored (Not Deleted), Not Approved
- `001` (binary) = `1` (decimal): Active, Restored (Not Deleted), Not Approved
- `010` (binary) = `2` (decimal): Inactive, Deleted, Not Approved
- `011` (binary) = `3` (decimal): Active, Deleted, Not Approved
- `100` (binary) = `4` (decimal): Inactive, Restored (Not Deleted), Approved
- `101` (binary) = `5` (decimal): Active, Restored (Not Deleted), Approved
- `110` (binary) = `6` (decimal): Inactive, Deleted, Approved
- `111` (binary) = `7` (decimal): Active, Deleted, Approved

## Features

- **Department Management**:
  - Create and update department information.
  - Delete existing departments.
- **Hierarchy Visualization**:
  - Display a tree-like structure of departments hierarchycal order, based on a parent department.
- **Authentication**:
  - Secure access using JWT tokens.

## Technologies Used

- **Backend**: Golang
- **Database**: MySQL (with stored procedures)
- **Web Framework**: Gin
- **Authentication**: JWT

## Prerequisites

- **Go**: Version 1.21 or later
- **MySQL**: Ensure MySQL is installed and running

## Setup Instructions:

## 1. Start MySQL Server

Ensure that your MySQL server is running. You can start it using the following command:

```sh
sudo service mysql start

## 2. Clone the Repository

```sh
git clone https://github.com/nicoldr/company-hierarchy.git
cd company-hierarchy

## 3.  Database Setup
**Create the database and tables**:
sudo mysql -u root -p < database/setup.sql (requires sudo permissions)

## 4.  Environment Variables
Create a .env file in the root directory with the following details
PORT=8080
DB_HOST=localhost
DB_PORT=3306
DB_USER=<db_user>
DB_PASSWORD=<db_password>
DB_NAME=company_hierarchy
APP_USER=<app_username>
APP_PASSWORD=<app_password>
JWT_SECRET=<jwt_secret>

## 5. Install Dependencies
go mod tidy

## 6. Run the application
go run main.go

## API Endpoints

### Authentication

- **Login**: `POST /login`
  - Request Body:
    ```json
    {
      "username": "username",
      "password": "password"
    }
    ```
  - Response:
    ```json
    {
      "token": "jwt_token"
    }
    ```

### Department Management

- **Add Department**: `POST /departments`
  - Request Body:
    ```json
    {
      "name": "IT",
      "parent_id": 2,
      "flags": 0
    }
    ```
  - Response:
    ```json
    {
      "id": 5,
      "name": "IT",
      "parent_id": 2,
      "flags": 0
    }
    ```

- **Update Department**: `PUT /departments/:id`
  - Request Body:
    ```json
    {
      "name": "IT",
      "parent_id": 2,
      "flags": 1
    }
    ```
  - Response:
    ```json
    {
      "id": 5,
      "name": "IT",
      "parent_id": 2,
      "flags": 1
    }
    ```
- **Activate Department**: `PUT /departments/:id/activate`
  - Response: `204 No Content`

- **Deactivate Department**: `PUT /departments/:id/deactivate`
  - Response: `204 No Content`

- **Delete Department**: `DELETE /departments/:id`
  - Response: `204 No Content`

- **Restore Department**: `PUT /departments/:id/restore`
  - Response: `204 No Content`

- **Approve Department**: `PUT /departments/:id/approve`
  - Response: `204 No Content`

- **Unapprove Department**: `PUT /departments/:id/unapprove`
  - Response: `204 No Content`

### Hierarchy Visualization

- **Get Hierarchy**: `GET /departments/hierarchy/:parent_id`
  - Response:
    ```json
    [
      {
        "id": 3,
        "name": "Recruitment",
        "parent_id": 1,
        "flags": 0
      }
    ]
    ```
## 7. Test the API Endpoints, example requests
In another terminal use the following curl commands to test the API endpoints.

### Obtain Token
TOKEN=$(curl -s -X POST http://localhost:8080/login \
-H "Content-Type: application/json" \
-d '{"username": "app_username", "password": "app_password"}' | jq -r '.token')

### Add Department
curl -X POST http://localhost:8080/departments/ -H "C
ontent-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"name": "Marketing", "parent_id": 1, "flags": 0}'

### Update Department
curl -X PUT http://localhost:8080/departments/1 -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"flags": 1}'

### Activate Department
curl -X PUT http://localhost:8080/departments/4/activate -H "Authorization: Bearer $TOKEN"

### Deactivate Department
curl -X PUT http://localhost:8080/departments/4/deactivate -H "Authorization: Bearer $TOKEN"

### Delete Department
curl -X DELETE http://localhost:8080/departments/4 -H "Authorization: Bearer $TOKEN"

### Restore Department:
curl -X PUT http://localhost:8080/departments/4/restore -H "Authorization: Bearer $TOKEN"

### Approve Department:
curl -X PUT http://localhost:8080/departments/4/approve -H "Authorization: Bearer $TOKEN"

### Unapprove Department:
curl -X PUT http://localhost:8080/departments/4/unapprove -H "Authorization: Bearer $TOKEN"

### Get Hierarchy
curl -X GET http://localhost:8080/departments/hierarchy/2 -H "Authorization: Bearer $TOKEN"

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages