Skip to main content
PUT
/
department
/
update
/
{departmentId}
Update an existing department
curl --request PUT \
  --url http://localhost:2000/department/update/{departmentId} \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "name": "Engineering",
  "description": "Handles all software engineering operations",
  "code": "ENG-001",
  "location": "San Francisco HQ",
  "parentDepartment": "64ef3c29f9a1c27e1b2c3aaa"
}
'
import requests

url = "http://localhost:2000/department/update/{departmentId}"

payload = {
"name": "Engineering",
"description": "Handles all software engineering operations",
"code": "ENG-001",
"location": "San Francisco HQ",
"parentDepartment": "64ef3c29f9a1c27e1b2c3aaa"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}

response = requests.put(url, json=payload, headers=headers)

print(response.text)
const options = {
method: 'PUT',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
name: 'Engineering',
description: 'Handles all software engineering operations',
code: 'ENG-001',
location: 'San Francisco HQ',
parentDepartment: '64ef3c29f9a1c27e1b2c3aaa'
})
};

fetch('http://localhost:2000/department/update/{departmentId}', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));
<?php

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_PORT => "2000",
CURLOPT_URL => "http://localhost:2000/department/update/{departmentId}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => json_encode([
'name' => 'Engineering',
'description' => 'Handles all software engineering operations',
'code' => 'ENG-001',
'location' => 'San Francisco HQ',
'parentDepartment' => '64ef3c29f9a1c27e1b2c3aaa'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
package main

import (
"fmt"
"strings"
"net/http"
"io"
)

func main() {

url := "http://localhost:2000/department/update/{departmentId}"

payload := strings.NewReader("{\n \"name\": \"Engineering\",\n \"description\": \"Handles all software engineering operations\",\n \"code\": \"ENG-001\",\n \"location\": \"San Francisco HQ\",\n \"parentDepartment\": \"64ef3c29f9a1c27e1b2c3aaa\"\n}")

req, _ := http.NewRequest("PUT", url, payload)

req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.put("http://localhost:2000/department/update/{departmentId}")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"Engineering\",\n \"description\": \"Handles all software engineering operations\",\n \"code\": \"ENG-001\",\n \"location\": \"San Francisco HQ\",\n \"parentDepartment\": \"64ef3c29f9a1c27e1b2c3aaa\"\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("http://localhost:2000/department/update/{departmentId}")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Put.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"Engineering\",\n \"description\": \"Handles all software engineering operations\",\n \"code\": \"ENG-001\",\n \"location\": \"San Francisco HQ\",\n \"parentDepartment\": \"64ef3c29f9a1c27e1b2c3aaa\"\n}"

response = http.request(request)
puts response.read_body
{
  "message": "Successfully updated Engineering department",
  "data": {
    "department": {
      "_id": "64ef3c29f9a1c27e1b2c3a4d",
      "name": "Engineering",
      "parentDepartment": {
        "_id": "64ef3c29f9a1c27e1b2c3aaa",
        "name": "Technology",
        "description": "Some description for this department",
        "code": "TECH"
      },
      "manager": {
        "_id": "64ef3c29f9a1c27e1b2c3a4d",
        "emails": [
          "user@example.com"
        ],
        "personalInformation": {
          "firstName": "John",
          "lastName": "Doe",
          "name": "John Doe"
        }
      },
      "description": "Handles all software engineering operations"
    }
  }
}
Admin-only endpoint to update department details.

Request

Headers

NameTypeRequiredDescription
AuthorizationstringYesBearer token
Content-TypestringYesapplication/json

Path Parameters

ParameterTypeRequiredDescription
departmentIdstringYesDepartment ID to update

Request Body

{
  "name": "Customer Success",
  "description": "Dedicated to ensuring customer satisfaction and retention",
  "parentId": "64b7f2b3e4b0a5d3f9c54321"
}

Request Body Schema

FieldTypeRequiredDescription
namestringNoNew department name
descriptionstringNoNew department description
parentIdstringNoNew parent department ID

Field Details

  • name: Must be unique if changed
  • description: Update the department description
  • parentId: Can be changed to reorganize hierarchy
  • parentId: Use null to make it a root-level department

Response

200 OK - Successfully updated department

{
  "message": "Successfully updated Engineering department",
  "data": {
    "department": {
      "_id": "64b7f2b3e4b0a5d3f9c54321",
      "name": "Customer Success",
      "description": "Dedicated to ensuring customer satisfaction and retention",
      "parentId": "64b7f2b3e4b0a5d3f9c54321",
      "createdAt": "2024-01-15T10:30:00.000Z",
      "updatedAt": "2024-01-16T14:25:00.000Z"
    }
  }
}

400 Bad Request

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Department name already exists"
  }
}

401 Unauthorized

{
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Missing or invalid token or insufficient permissions"
  }
}

404 Not Found

{
  "error": {
    "code": "DEPARTMENT_NOT_FOUND",
    "message": "Department not found"
  }
}

500 Internal Server Error

{
  "error": {
    "code": "SERVER_ERROR",
    "message": "Internal server error"
  }
}

Examples

Update department name and description

curl -X PUT 'http://localhost:2000/department/update/64b7f2b3e4b0a5d3f9c54321' \
  -H 'Authorization: Bearer your-jwt-token' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "Customer Success",
    "description": "Dedicated to ensuring customer satisfaction and retention"
  }'

Move department to new parent

curl -X PUT 'http://localhost:2000/department/update/64b7f3c4e4b0a5d3f9c98765' \
  -H 'Authorization: Bearer your-jwt-token' \
  -H 'Content-Type: application/json' \
  -d '{
    "parentId": "64b7f7g8h9i0j1k2l3m4n5o"
  }'

Make department root-level

curl -X PUT 'http://localhost:2000/department/update/64b7f3c4e4b0a5d3f9c98765' \
  -H 'Authorization: Bearer your-jwt-token' \
  -H 'Content-Type: application/json' \
  -d '{
    "parentId": null
  }'

Notes

  • This is an admin-only endpoint - requires administrative privileges
  • Department ID must be a valid MongoDB ObjectId
  • Department names must remain unique after update
  • Cannot set a department as its own parent (prevents circular references)
  • Cannot set a child department as parent (prevents circular hierarchy)
  • Moving a department also moves all its sub-departments
  • The updatedAt timestamp is automatically updated
  • Users assigned to the department are not affected by name changes

Hierarchy Considerations

  • Moving Departments: All child departments move with the parent
  • Circular Prevention: System prevents creating circular references
  • Parent Validation: New parent must exist (unless setting to null)
  • Depth Limits: Consider reasonable hierarchy depth limits

Impact on Users

  • User department assignments remain valid
  • Department-based permissions are preserved
  • User profiles will show updated department names
  • No disruption to user access or functionality

Best Practices

  1. Plan Changes: Consider impact on hierarchy before updating
  2. Communicate: Notify users of department name changes
  3. Test: Test hierarchy changes in non-production first
  4. Backup: Document current structure before major changes
  5. Review: Regularly review department structure for optimization

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Path Parameters

departmentId
string
required

Department ID to update

Example:

"64ef3c29f9a1c27e1b2c3a4d"

Body

application/json
name
string

Department name (leave empty string "" if unchanged)

Example:

"Engineering"

description
string

Department description (leave empty string "" if unchanged)

Example:

"Handles all software engineering operations"

code
string

Department code (leave empty string "" if unchanged)

Example:

"ENG-001"

location
string

Department location (leave empty string "" if unchanged)

Example:

"San Francisco HQ"

parentDepartment
string | null

Parent department ID. - Must be a valid MongoDB ObjectId of an existing department. - Can be null if this department has no parent. - Leave empty if unchanged.

Example:

"64ef3c29f9a1c27e1b2c3aaa"

Response

Successfully updated department

message
string
Example:

"Successfully updated Engineering department"

data
object