Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.1 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { assembledCommon } from '../common';
import { HttpMethod } from '@activepieces/pieces-common';
import { assembledAuth } from '../common/auth';

export const addShift = createAction({
  auth: assembledAuth,
  name: 'add_shift',
  classification: 'WRITE',
  displayName: 'Add Shift on Assembled',
  description: 'Add a new shift to a user\'s schedule in Assembled',
  audience: 'both',
  aiMetadata: { description: 'Adds a scheduled shift (activity) to an agent\'s schedule in Assembled over a given start/end time. Requires the agent ID (the agent_id field from the people endpoint, not the person ID) and an activity type ID UUID. Each call creates a new activity, so repeating it produces duplicate shifts.', idempotent: false },
  props: {
    agent_id: Property.ShortText({
      displayName: 'Agent ID',
      description: 'Agent ID of the person to assign the shift to (use the agent_id field from /people endpoint, not the person id)',
      required: true,
    }),
    start_time: Property.DateTime({
      displayName: 'Start Time',
      description: 'Start time of the shift',
      required: true,
    }),
    end_time: Property.DateTime({
      displayName: 'End Time', 
      description: 'End time of the shift',
      required: true,
    }),
    shift_type: Property.StaticDropdown({
      displayName: 'Shift Type',
      description: 'Type of shift',
      required: false,
      defaultValue: 'regular',
      options: {
        options: [
          { label: 'Regular', value: 'regular' },
          { label: 'Overtime', value: 'overtime' },
          { label: 'On-call', value: 'on_call' },
          { label: 'Training', value: 'training' },
        ],
      },
    }),
    activity_type_id: Property.ShortText({
      displayName: 'Activity Type ID',
      description: 'UUID of the activity type for this shift (get from /activity_types endpoint)',
      required: true,
    }),
    notes: Property.LongText({
      displayName: 'Notes',
      description: 'Additional notes for the shift',
      required: false,
    }),
  },
  async run(context) {
    const { agent_id, start_time, end_time, shift_type, activity_type_id, notes } = context.propsValue;
    
    try {
      // Convert to Unix timestamps as required by Assembled API
      const startTimestamp = Math.floor(new Date(start_time).getTime() / 1000);
      const endTimestamp = Math.floor(new Date(end_time).getTime() / 1000);
      
      const shiftData = {
        agent_id: agent_id, 
        type_id: activity_type_id, 
        start_time: startTimestamp,
        end_time: endTimestamp,
        description: notes || `${shift_type || 'regular'} shift`,
      };

      const response = await assembledCommon.makeRequest(
        context.auth.secret_text,
        HttpMethod.POST,
        '/activities',
        shiftData
      );

      return {
        success: true,
        shift_id: response.body.id,
        message: 'Shift added successfully',
        data: response.body,
      };
    } catch (error) {
      throw new Error(`Failed to add shift: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  },
});