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

5.4 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { outsetaAuth } from '../auth';
import { OutsetaClient } from '../common/client';
import { pipelineDropdown, pipelineStageDropdown } from '../common/dropdowns';
import { customPropertiesProp, mergeCustomProperties } from '../common/custom-properties';

export const updateDealAction = createAction({
  name: 'update_deal',
  classification: 'WRITE',
  auth: outsetaAuth,
  displayName: 'Update Deal',
  description: 'Update an existing deal.',
  audience: 'both',
  aiMetadata: {
    description:
      'Updates an existing Outseta CRM deal identified by UID, setting only the provided fields (name, pipeline stage, amount, assignee, due date, linked account/person) via a PUT. Use to advance a deal stage or edit its details when you have its UID. Idempotent: re-running with the same input leaves the deal in the same state.',
    idempotent: true,
  },
  props: {
    dealUid: Property.ShortText({
      displayName: 'Deal UID',
      description: 'The UID of the deal to update.',
      required: true,
    }),
    name: Property.ShortText({
      displayName: 'Name',
      required: false,
    }),
    pipelineUid: pipelineDropdown({ required: false }),
    dealPipelineStageUid: pipelineStageDropdown({ required: false }),
    amount: Property.Number({
      displayName: 'Amount',
      required: false,
      description: 'The monetary value of the deal.',
    }),
    assignedToPersonClientIdentifier: Property.ShortText({
      displayName: 'Assigned To (Client Identifier)',
      required: false,
      description: "The client identifier of the team member this deal is assigned to. Found in the person's Outseta profile.",
    }),
    dueDate: Property.DateTime({
      displayName: 'Due Date',
      required: false,
    }),
    accountUid: Property.ShortText({
      displayName: 'Account UID',
      description: 'The UID of the account to associate with this deal.',
      required: false,
    }),
    personUid: Property.ShortText({
      displayName: 'Person UID',
      description:
        'UID of a person to add as a contact on this deal. Existing contacts are preserved.',
      required: false,
    }),
    customProperties: customPropertiesProp('Deal'),
  },
  async run(context) {
    const client = new OutsetaClient({
      domain: context.auth.props.domain,
      apiKey: context.auth.props.apiKey,
      apiSecret: context.auth.props.apiSecret,
    });

    // Fetch full deal with DealPeople expanded to avoid wiping fields on PUT
    const deal = await client.get<any>(
      `/api/v1/crm/deals/${context.propsValue.dealUid}?fields=*,DealPeople.*,DealPeople.Person.*,DealPipelineStage.*,Account.*`
    );

    let changed = false;
    if (context.propsValue.name) {
      deal.Name = context.propsValue.name;
      changed = true;
    }
    if (context.propsValue.dealPipelineStageUid) {
      deal.DealPipelineStage = { Uid: context.propsValue.dealPipelineStageUid };
      changed = true;
    }
    if (context.propsValue.amount != null) {
      deal.Amount = context.propsValue.amount;
      changed = true;
    }
    if (context.propsValue.assignedToPersonClientIdentifier) {
      deal.AssignedToPersonClientIdentifier =
        context.propsValue.assignedToPersonClientIdentifier;
      changed = true;
    }
    if (context.propsValue.dueDate) {
      deal.DueDate = context.propsValue.dueDate;
      changed = true;
    }
    if (context.propsValue.accountUid) {
      deal.Account = { Uid: context.propsValue.accountUid };
      changed = true;
    }
    if (context.propsValue.personUid) {
      const existing: any[] =
        deal.DealPeople?.items ?? deal.DealPeople?.Items ?? deal.DealPeople ?? [];
      const alreadyLinked = existing.some(
        (dp: any) => dp.Person?.Uid === context.propsValue.personUid
      );
      if (!alreadyLinked) {
        // Preserve the existing DealPeople join records' Uid so the server
        // treats them as the same entities. Without this, Outseta may delete
        // the old join records and create new ones (changing all the
        // PersonAccount-style references downstream).
        deal.DealPeople = [
          ...existing.map((dp: any) => ({
            Uid: dp.Uid,
            Person: { Uid: dp.Person?.Uid },
          })),
          { Person: { Uid: context.propsValue.personUid } },
        ];
        changed = true;
      }
    }

    if (context.propsValue.customProperties) {
      mergeCustomProperties(deal, context.propsValue.customProperties);
      changed = true;
    }

    if (!changed) {
      throw new Error('At least one field must be provided.');
    }

    // Defensive: ensure DealPeople is a flat array on the way out, so we
    // never PUT back a {items: [...]} envelope that the server might
    // misinterpret as "no contacts" and wipe.
    if (deal.DealPeople && !Array.isArray(deal.DealPeople)) {
      deal.DealPeople =
        deal.DealPeople.items ?? deal.DealPeople.Items ?? [];
    }

    const updated = await client.put<any>(
      `/api/v1/crm/deals/${context.propsValue.dealUid}`,
      deal
    );

    return {
      uid: updated.Uid ?? null,
      name: updated.Name ?? null,
      amount: updated.Amount ?? null,
      due_date: updated.DueDate ?? null,
      pipeline_stage_uid: updated.DealPipelineStage?.Uid ?? null,
      pipeline_stage_name: updated.DealPipelineStage?.Name ?? null,
      account_uid: updated.Account?.Uid ?? null,
      account_name: updated.Account?.Name ?? null,
      updated: updated.Updated ?? null,
    };
  },
});