Skip to main content
PUT
/
department
/
add-users
/
{departmentId}
Add users to a department
curl --request PUT \
  --url http://localhost:2000/department/add-users/{departmentId} \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "users": [
    "64ef3c29f9a1c27e1b2c3b5e",
    "64ef3c29f9a1c27e1b2c3b5f"
  ],
  "departmentRole": "Manager"
}
'
{
  "message": "Successfully added users to Engineering department",
  "data": {}
}
Admin-only endpoint to add multiple users to a department with a specific role.

Request

Headers

NameTypeRequiredDescription
AuthorizationstringYesBearer token
Content-TypestringYesapplication/json

Path Parameters

ParameterTypeRequiredDescription
departmentIdstringYesDepartment ID to add users to

Request Body

{
  "userIds": [
    "64b7f1a2e4b0a5d3f9c12345",
    "64b7f2b3e4b0a5d3f9c54321",
    "64b7f3c4e4b0a5d3f9c98765"
  ],
  "role": "agent"
}

Request Body Schema

FieldTypeRequiredDescription
userIdsarrayYesList of user IDs to add to department
rolestringYesRole to assign to users in department

Field Details

  • userIds: Array of valid MongoDB ObjectIds
  • role: Department role (e.g., “manager”, “supervisor”, “agent”, “admin”)

Response

200 OK - Successfully added users to department

{
  "message": "Successfully added users to Engineering department",
  "data": {
    "addedUsers": [
      {
        "userId": "64b7f1a2e4b0a5d3f9c12345",
        "name": "John Doe",
        "role": "agent"
      },
      {
        "userId": "64b7f2b3e4b0a5d3f9c54321",
        "name": "Jane Smith",
        "role": "agent"
      }
    ],
    "departmentId": "64ef3c29f9a1c27e1b2c3a4d"
  }
}

400 Bad Request

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Department not found or invalid user IDs"
  }
}

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/add-users/64ef3c29f9a1c27e1b2c3a4d' \
  -H 'Authorization: Bearer your-jwt-token' \
  -H 'Content-Type: application/json' \
  -d '{
    "userIds": [
      "64b7f1a2e4b0a5d3f9c12345",
      "64b7f2b3e4b0a5d3f9c54321"
    ],
    "role": "agent"
  }'

Notes

  • This is an admin-only endpoint - requires administrative privileges
  • Department ID must be a valid MongoDB ObjectId
  • All user IDs must be valid MongoDB ObjectIds
  • Users can only be added to one department at a time
  • If users are already in a department, they will be moved
  • The role applies to all users being added
  • Users receive department-based permissions based on their role

Role Definitions

RoleDescriptionPermissions
managerDepartment managerFull department access
supervisorTeam supervisorTeam management access
agentRegular department memberStandard department access
adminDepartment administratorAdministrative access

Use Cases

  • Onboarding: Add new employees to their department
  • Team Transfers: Move users between departments
  • Bulk Assignment: Add multiple users efficiently
  • Role Changes: Assign specific roles to new members

Best Practices

  1. Verify Users: Ensure all user IDs exist before adding
  2. Role Assignment: Choose appropriate roles for users
  3. Communication: Notify users of department changes
  4. Permissions: Review role permissions before assignment
  5. Audit: Track department membership changes

Error Handling

  • Invalid User IDs: Users that don’t exist are skipped
  • Already Assigned: Users are moved from current department
  • Invalid Role: Returns validation error
  • Department Not Found: Returns 404 error

Impact on Users

  • Users gain access to department resources
  • Previous department assignments are replaced
  • Role-based permissions are applied immediately
  • Users may see new teams and projects in their dashboard

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
users
string[]
required

List of user IDs to add to the department

Example:
[
"64ef3c29f9a1c27e1b2c3b5e",
"64ef3c29f9a1c27e1b2c3b5f"
]
departmentRole
string
required

Role to assign to the users in the department

Example:

"Manager"

Response

Successfully added users to department

message
string
Example:

"Successfully added users to Engineering department"

data
object