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

4.1 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { fetchContacts, fetchHouseholds, WEALTHBOX_API_BASE, handleApiError } from '../common';
import { wealthboxAuth } from '../..';

export const addHouseholdMember = createAction({
  name: 'add_household_member',
  displayName: 'Add Member to Household',
  description: 'Adds a member to an existing household. Link multiple contacts under one family unit.',
  audience: 'both',
  aiMetadata: { description: 'Adds an existing contact as a member of an existing Wealthbox household, assigning a household role/title (Head, Spouse, Child, etc.). Use to attach a person to a household after both records exist; requires the household id, the contact id, and a title. Not idempotent: it appends the membership on each call.', idempotent: false },
  auth: wealthboxAuth,
  props: {
    household_id: Property.Dropdown({
      auth: wealthboxAuth,
      displayName: 'Household',
      description: 'Select the household that will receive the new member',
      required: true,
      refreshers: [],
      options: async ({ auth }) => {
        if (!auth) return { options: [] };

        try {
          const households = await fetchHouseholds(auth.secret_text);
          return {
            options: households.map((household: any) => ({
              label: household.first_name || `Household ${household.id}`,
              value: household.id
            }))
          };
        } catch (error) {
          return {
            options: [],
            error: 'Failed to load households. Please check your authentication.'
          };
        }
      }
    }),
    contact_id: Property.Dropdown({
      auth: wealthboxAuth,
      displayName: 'Contact',
      description: 'Select the contact to add to the household',
      required: true,
      refreshers: [],
      options: async ({ auth }) => {
        if (!auth) return { options: [] };

        try {
          const contacts = await fetchContacts(auth.secret_text, { active: true, order: 'recent' });
          return {
            options: contacts.map((contact: any) => ({
              label: contact.name || `${contact.first_name} ${contact.last_name}`.trim() || `Contact ${contact.id}`,
              value: contact.id
            }))
          };
        } catch (error) {
          return {
            options: [],
            error: 'Failed to load contacts. Please check your authentication.'
          };
        }
      }
    }),
    title: Property.StaticDropdown({
      displayName: 'Household Title',
      description: 'The household title to assign to the added contact',
      required: true,
      options: {
        options: [
          { label: 'Head', value: 'Head' },
          { label: 'Spouse', value: 'Spouse' },
          { label: 'Partner', value: 'Partner' },
          { label: 'Child', value: 'Child' },
          { label: 'Grandchild', value: 'Grandchild' },
          { label: 'Parent', value: 'Parent' },
          { label: 'Grandparent', value: 'Grandparent' },
          { label: 'Sibling', value: 'Sibling' },
          { label: 'Other', value: 'Other' },
          { label: 'Dependent', value: 'Dependent' }
        ]
      }
    })
  },
  
  async run(context) {
    const { auth, propsValue } = context;

    if (!auth) {
      throw new Error('Authentication is required');
    }

    const requestBody = {
      id: propsValue.contact_id,
      title: propsValue.title
    };

    try {
      const response = await httpClient.sendRequest({
        method: HttpMethod.POST,
        url: `${WEALTHBOX_API_BASE}/households/${propsValue.household_id}/members`,
        headers: {
          'ACCESS_TOKEN': auth.secret_text,
          'Content-Type': 'application/json'
        },
        body: requestBody
      });

      if (response.status >= 400) {
        handleApiError('add household member', response.status, response.body);
      }

      return response.body;
    } catch (error) {
      throw new Error(`Failed to add member to household: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }
});