Skip to main content
PUT
/
department
/
assign-manager
/
{departmentId}
Assign a manager to a department
curl --request PUT \
  --url http://localhost:2000/department/assign-manager/{departmentId} \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "userId": "64ef3c29f9a1c27e1b2c3b5e"
}
'
import requests

url = "http://localhost:2000/department/assign-manager/{departmentId}"

payload = { "userId": "64ef3c29f9a1c27e1b2c3b5e" }
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({userId: '64ef3c29f9a1c27e1b2c3b5e'})
};

fetch('http://localhost:2000/department/assign-manager/{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/assign-manager/{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([
'userId' => '64ef3c29f9a1c27e1b2c3b5e'
]),
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/assign-manager/{departmentId}"

payload := strings.NewReader("{\n \"userId\": \"64ef3c29f9a1c27e1b2c3b5e\"\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/assign-manager/{departmentId}")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"userId\": \"64ef3c29f9a1c27e1b2c3b5e\"\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("http://localhost:2000/department/assign-manager/{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 \"userId\": \"64ef3c29f9a1c27e1b2c3b5e\"\n}"

response = http.request(request)
puts response.read_body
{
  "message": "Successfully assigned manager (John Doe) to Engineering department",
  "data": {}
}
Admin-only endpoint to assign a user as the manager of a department.

Request

Headers

NameTypeRequiredDescription
AuthorizationstringYesBearer token
Content-TypestringYesapplication/json

Path Parameters

ParameterTypeRequiredDescription
departmentIdstringYesDepartment ID to assign manager to

Request Body

{
  "userId": "64b7f1a2e4b0a5d3f9c12345"
}

Request Body Schema

FieldTypeRequiredDescription
userIdstringYesUser ID to assign as department manager

Field Details

  • userId: Valid MongoDB ObjectId of the user to promote to manager

Response

200 OK - Successfully assigned manager

{
  "message": "Successfully assigned manager (John Doe) to Engineering department",
  "data": {
    "departmentId": "64ef3c29f9a1c27e1b2c3a4d",
    "manager": {
      "userId": "64b7f1a2e4b0a5d3f9c12345",
      "name": "John Doe",
      "email": "john.doe@example.com",
      "role": "manager"
    },
    "previousManager": {
      "userId": "64b7f2b3e4b0a5d3f9c54321",
      "name": "Jane Smith",
      "email": "jane.smith@example.com"
    }
  }
}

400 Bad Request

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "User or department not found"
  }
}

401 Unauthorized

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

500 Internal Server Error

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

Example

curl -X PUT 'http://localhost:2000/department/assign-manager/64ef3c29f9a1c27e1b2c3a4d' \
  -H 'Authorization: Bearer your-jwt-token' \
  -H 'Content-Type: application/json' \
  -d '{
    "userId": "64b7f1a2e4b0a5d3f9c12345"
  }'

Notes

  • This is an admin-only endpoint - requires administrative privileges
  • Department ID must be a valid MongoDB ObjectId
  • User ID must be a valid MongoDB ObjectId
  • The assigned user automatically gets “manager” role in the department
  • Previous manager (if any) is demoted to regular member
  • The user must already be a member of the department
  • If user is not in department, they are added first as manager

Manager Privileges

Department managers typically have:
  • Full department access and control
  • Ability to manage department members
  • Access to department analytics and reports
  • Approval authority for department requests
  • Representation in organizational meetings

Use Cases

  • Promotion: Promote a team member to management
  • Replacement: Replace departing or transferred manager
  • Reorganization: Assign new leadership during restructuring
  • Interim Management: Assign temporary manager

Best Practices

  1. Verify Eligibility: Ensure user is suitable for management role
  2. Communicate Changes: Notify team about leadership changes
  3. Training: Provide management training if needed
  4. Access Review: Review manager permissions after assignment
  5. Documentation: Document the reason for management change

Error Handling

  • User Not Found: User ID doesn’t exist in system
  • Department Not Found: Department ID doesn’t exist
  • Invalid Permissions: User lacks required permissions
  • Already Manager: User is already the department manager

Impact on Users

New Manager

  • Gains elevated permissions within department
  • Can view and manage department resources
  • Receives management notifications and reports
  • May see additional dashboard features

Previous Manager

  • Loses manager privileges but remains in department
  • Retains regular department member access
  • May need to hand over responsibilities

Department Members

  • See new manager in organizational charts
  • May receive notifications about leadership change
  • Continue with normal department operations

Security Considerations

  • Manager role grants significant permissions
  • Regularly review manager assignments
  • Consider audit trails for management changes
  • Ensure proper authorization for this endpoint
  • Use /department/add-users to add users to department first
  • Use /user/update-department-role for other role assignments
  • Use /department/list to verify current assignments

Authorizations

Authorization
string
header
required

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

Path Parameters

departmentId
string
required

Unique identifier of the department

Body

application/json
userId
string
required

User ID of the manager to assign

Example:

"64ef3c29f9a1c27e1b2c3b5e"

Response

Successfully assigned manager

message
string
Example:

"Successfully assigned manager (John Doe) to Engineering department"

data
object