
# Service Library - `taskManagement`

This document provides a complete reference of the custom code library for the `taskManagement` service. It includes all library functions, edge functions with their REST endpoints, templates, and assets.


## Library Functions

Library functions are reusable modules available to all business APIs and other custom code within the service via `require("lib/<moduleName>")`.


### `collectUniqueAssignees.js`

```js
module.exports = async (context) => {
  console.log('=== collectUniqueAssignees called ===');
  console.log('Context keys:', Object.keys(context));
  console.log('Task Assignment:', JSON.stringify(context.taskAssignment));
  
  if (!context.taskAssignment) {
    console.error('ERROR: context.taskAssignment is undefined');
    throw new Error('Task assignment not found in context');
  }
  
  const { assigneeUserIds = [], assignedDepartmentIds = [], companyId } = context.taskAssignment;
  
  console.log('assigneeUserIds:', assigneeUserIds);
  console.log('assignedDepartmentIds:', assignedDepartmentIds);
  console.log('companyId:', companyId);
  
  const uniqueAssignees = new Set(assigneeUserIds || []);
  
  // Fetch users from assigned departments
  if (assignedDepartmentIds && assignedDepartmentIds.length > 0) {
    console.log('Fetching users for departments:', assignedDepartmentIds);
    for (const deptId of assignedDepartmentIds) {
      try {
        const deptUsers = await this.listUsersByDepartment({ 
          departmentId: deptId,
          companyId: companyId 
        });
        console.log(`Found ${deptUsers?.length || 0} users in department ${deptId}`);
        if (deptUsers && Array.isArray(deptUsers)) {
          deptUsers.forEach(user => {
            if (user && user.id) {
              uniqueAssignees.add(user.id);
            }
          });
        }
      } catch (error) {
        console.error(`Error fetching users for department ${deptId}:`, error.message);
      }
    }
  }
  
  const result = Array.from(uniqueAssignees);
  console.log('Total unique assignees:', result.length);
  console.log('Unique assignees:', result);
  
  return result;
};
```














---

*This document was generated from the service library configuration and should be kept in sync with design changes.*
