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

2.4 KB
Raw Blame
import { Property, createAction } from '@activepieces/pieces-framework';
import {
  AuthenticationType,
  HttpMethod,
  HttpRequest,
  httpClient,
} from '@activepieces/pieces-common';
import { xeroAuth } from '../..';
import { props } from '../common/props';
import { xeroOutputSchemas } from '../output-schemas';

export const xeroAllocateCreditNoteToInvoice = createAction({
  auth: xeroAuth,
  name: 'xero_allocate_credit_note_to_invoice',
  classification: 'WRITE',
  displayName: 'Allocate Credit Note to Invoice',
  description: 'Allocates a credit note to a specific invoice.',
  audience: 'human',
  aiMetadata: {
    description:
      'Apply a specified amount of an existing credit note against a specific invoice in Xero, both referenced by ID. Pick this to offset an outstanding invoice with available credit. Not idempotent: each call records another allocation, so re-running over-allocates; resolve the credit-note and invoice IDs and the remaining credit first.',
    idempotent: false,
  },
  outputSchema: xeroOutputSchemas.allocationEnvelope,
  props: {
    tenant_id: props.tenant_id,
    credit_note_id: props.credit_note_id(true),
    invoice_id: props.invoice_id(true),
    amount: Property.Number({
      displayName: 'Amount',
      description: 'The amount of the credit to allocate.',
      required: true,
    }),
    date: Property.ShortText({
      displayName: 'Allocation Date',
      description: 'Date of allocation. Format: YYYY-MM-DD. Optional.',
      required: false,
    }),
  },
  async run(context) {
    const { tenant_id, credit_note_id, invoice_id, amount, date } =
      context.propsValue;

    const url = `https://api.xero.com/api.xro/2.0/CreditNotes/${credit_note_id}/Allocations`;

    const body: Record<string, unknown> = {
      Allocations: [
        {
          Invoice: {
            InvoiceID: invoice_id,
          },
          Amount: amount,
          ...(date ? { Date: date } : {}),
        },
      ],
    };

    const request: HttpRequest = {
      method: HttpMethod.PUT,
      url,
      body,
      authentication: {
        type: AuthenticationType.BEARER_TOKEN,
        token: context.auth.access_token,
      },
      headers: {
        'Xero-Tenant-Id': tenant_id,
      },
    };

    const result = await httpClient.sendRequest(request);
    if (result.status === 200) {
      return result.body;
    }
    return result;
  },
});