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.6 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { bokioAuth } from '../common/auth';
import { makeRequest } from '../common/client';
import { HttpMethod } from '@activepieces/pieces-common';
import { invoiceIdDropdown } from '../common/props';

export const updateAnInvoice = createAction({
  auth: bokioAuth,
  name: 'updateAnInvoice',
  classification: 'WRITE',
  displayName: 'Update an invoice',
  description: 'Updates an existing draft invoice in Bokio',
  audience: 'both',
  aiMetadata: { description: 'Updates an existing draft invoice in Bokio, identified by invoice ID, replacing fields such as type, customer, dates, currency, and the full line-items list (passed as a JSON array). Use to modify an invoice that already exists rather than create one. Idempotent — repeating the same update yields the same invoice state.', idempotent: true },
  props: {
    invoiceId: invoiceIdDropdown,
    invoiceType: Property.StaticDropdown({
      displayName: 'Invoice Type',
      description: 'Type of invoice',
      required: false,
      options: {
        options: [
          { label: 'Invoice', value: 'invoice' },
          { label: 'Cash Invoice', value: 'cashInvoice' },
        ],
      },
    }),
    customerRef: Property.ShortText({
      displayName: 'Customer ID',
      description: 'UUID of the customer',
      required: false,
    }),
    invoiceDate: Property.ShortText({
      displayName: 'Invoice Date',
      description: 'Invoice date (YYYY-MM-DD)',
      required: true,
    }),
    dueDate: Property.ShortText({
      displayName: 'Due Date',
      description: 'Due date (YYYY-MM-DD)',
      required: true,
    }),
    orderNumberReference: Property.ShortText({
      displayName: 'Order Number Reference',
      description: 'The order number associated with the invoice',
      required: false,
    }),
    currency: Property.ShortText({
      displayName: 'Currency',
      description: 'ISO 4217 currency code (e.g., SEK, EUR, USD)',
      required: false,
    }),
    currencyRate: Property.Number({
      displayName: 'Currency Rate',
      description: 'Currency rate',
      required: false,
    }),
    lineItemsJson: Property.LongText({
      displayName: 'Line Items (JSON)',
      description:
        'Line items as JSON array. Each item should have: itemRef (object with id), description, quantity, unitPrice, unit, itemType (PRODUCT or SERVICE), vat',
      required: false,
    }),
  },
  async run(context) {
    const {
      invoiceId,
      invoiceType,
      customerRef,
      invoiceDate,
      dueDate,
      orderNumberReference,
      currency,
      currencyRate,
      lineItemsJson,
    } = context.propsValue;
    const { api_key, companyId } = context.auth.props;

    const body: any = {};

    if (invoiceType) {
      body.type = invoiceType;
    }

    if (customerRef) {
      body.customerRef = {
        id: customerRef,
      };
    }

    if (invoiceDate) {
      body.invoiceDate = invoiceDate;
    }

    if (dueDate) {
      body.dueDate = dueDate;
    }

    if (orderNumberReference) {
      body.orderNumberReference = orderNumberReference;
    }

    if (currency) {
      body.currency = currency;
    }

    if (currencyRate !== undefined && currencyRate !== null) {
      body.currencyRate = currencyRate;
    }

    if (lineItemsJson) {
      try {
        body.lineItems = JSON.parse(lineItemsJson);
      } catch {
        throw new Error('Invalid JSON format for line items');
      }
    }

    const response = await makeRequest(
      api_key,
      HttpMethod.PUT,
      `/companies/${companyId}/invoices/${invoiceId}`,
      body
    );

    return response;
  },
});