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.5 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 updateOOO = createAction({
  auth: assembledAuth,
  name: 'update_OOO',
  classification: 'DESTRUCTIVE',
  displayName: 'Update OOO Request',
  description: 'Updates an existing OOO request.',
  audience: 'both',
  aiMetadata: { description: 'Updates an existing time-off request identified by its OOO ID. Because Assembled has no update endpoint, this cancels the original request and creates a fresh one with the new details, so user ID and activity type ID are required. This mutates state and creates a new request each call, so it is not safe to repeat.', idempotent: false },
  props: {
    OOO_id: Property.ShortText({
      displayName: 'OOO ID',
      required: true,
    }),
    start_date: Property.DateTime({
      displayName: 'Start Date',
      required: false,
    }),
    end_date: Property.DateTime({
      displayName: 'End Date',
      required: false,
    }),
    status: Property.StaticDropdown({
      displayName: 'Status',
      required: false,
      options: {
        options: [
          { label: 'Pending', value: 'pending' },
          { label: 'Approved', value: 'approved' },
          { label: 'Rejected', value: 'rejected' },
          { label: 'Cancelled', value: 'cancelled' },
        ],
      },
    }),
    reason: Property.LongText({
      displayName: 'Reason',
      required: false,
    }),
    user_id: Property.ShortText({
      displayName: 'User ID',
      description: 'ID of the user for the time off request (required for creating new request)',
      required: true,
    }),
    activity_type_id: Property.ShortText({
      displayName: 'Activity Type ID',
      description: 'UUID of the activity type for time off (required for creating new request)',
      required: true,
    }),
  },
  async run(context) {
    const { OOO_id, start_date, end_date, status, reason, user_id, activity_type_id } = context.propsValue;
    
    // no direct endpoint to update a time off request, so need to cancel and create new time off request
    try {
      // cancel the existing time off request
      console.log(`Canceling existing time off request: ${OOO_id}`);
      await assembledCommon.makeRequest(
        context.auth.secret_text,
        HttpMethod.POST,
        `/time_off/${OOO_id}/cancel`
      );
      
      // create new time off request with updated details
      console.log('Creating new time off request with updated details');
      const newRequestData: Record<string, unknown> = {
        user_id,
        activity_type_id,
        all_day: true,
      };
      
      if (start_date) newRequestData['start_time'] = Math.floor(new Date(start_date).getTime() / 1000);
      if (end_date) newRequestData['end_time'] = Math.floor(new Date(end_date).getTime() / 1000);
      if (reason) newRequestData['description'] = reason;
      
      const response = await assembledCommon.makeRequest(
        context.auth.secret_text,
        HttpMethod.POST,
        '/time_off',
        newRequestData
      );
      
      return {
        success: true,
        message: 'Time off request updated successfully (canceled old and created new)',
        canceled_request_id: OOO_id,
        new_request_id: response.body.id,
        data: response.body,
      };
      
    } catch (error) {
      throw new Error(`Failed to update time off request: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  },
});