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.4 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { helpScoutApiRequest } from '../common/api';
import { helpScoutAuth } from '../common/auth';
import { propsValidation } from '@activepieces/pieces-common';
import * as z from 'zod/mini'
import { HttpMethod } from '@activepieces/pieces-common';

export const createCustomer = createAction({
  auth: helpScoutAuth,
  name: 'create_customer',
  classification: 'WRITE',
  displayName: 'Create Customer',
  description: 'Adds a new customer to Help Scout.',
  audience: 'both',
  aiMetadata: {
    description:
      'Creates a new customer record in Help Scout from an email plus optional profile details (name, phone, organization, social profiles, etc.). Use to register a contact before associating conversations. Email is required and must be valid; not idempotent — each call creates a new customer even if one already exists for that email, so look the customer up first if duplicates matter.',
    idempotent: false,
  },
  props: {
    email: Property.ShortText({
      displayName: 'Email',
      required: true,
    }),
    firstName: Property.ShortText({
      displayName: 'First Name',
      required: false,
    }),
    lastName: Property.ShortText({
      displayName: 'Last Name',
      required: false,
    }),
    phone: Property.ShortText({
      displayName: 'Phone',
      required: false,
    }),
    photoUrl: Property.ShortText({
      displayName: 'Photo URL',
      required: false,
    }),
    jobTitle: Property.ShortText({
      displayName: 'Job Title',
      required: false,
    }),
    location: Property.ShortText({
      displayName: 'Location',
      required: false,
    }),
    background: Property.LongText({
      displayName: 'Background',
      required: false,
    }),
    age: Property.ShortText({
      displayName: 'Age',
      required: false,
      description: 'Customer’s age (string, e.g. "30-35")',
    }),
    gender: Property.StaticDropdown({
      displayName: 'Gender',
      required: false,
      options: {
        options: [
          { label: 'Male', value: 'male' },
          { label: 'Female', value: 'female' },
          { label: 'Unknown', value: 'unknown' },
        ],
      },
    }),
    organization: Property.ShortText({
      displayName: 'Organization',
      required: false,
    }),
    socialProfiles: Property.Array({
      displayName: 'Social Profile URLs',
      required: false,
      description: 'URLs for social profiles (type will be set to "other")',
    }),
  },
  async run({ auth, propsValue }){
    await propsValidation.validateZod(propsValue, {
      email: z.string().check(z.email('Please provide a valid email address.')),
      firstName: z.optional(z.string()),
      lastName: z.optional(z.string()),
      phone: z.optional(z.string()),
      photoUrl: z.optional(z.string().check(z.url('Photo URL must be a valid URL.'))),
      jobTitle: z.optional(z.string()),
      location: z.optional(z.string()),
      background: z.optional(z.string()),
      age: z.optional(z.string()),
      gender: z.optional(z.enum(['male', 'female', 'unknown'])),
      organization: z.optional(z.string()),
      socialProfiles: z.optional(z.array(z.string().check(z.url('Each social profile must be a valid URL.')))),
    });
    const payload: Record<string, any> = {
      emails: [{ value: propsValue.email, type: 'work' }],
      firstName: propsValue.firstName,
      lastName: propsValue.lastName,
      phones: propsValue.phone ? [{ value: propsValue.phone, type: 'work' }] : undefined,
      photoUrl: propsValue.photoUrl,
      jobTitle: propsValue.jobTitle,
      location: propsValue.location,
      background: propsValue.background,
      age: propsValue.age,
      gender: propsValue.gender,
      organization: propsValue.organization,
      socialProfiles: Array.isArray(propsValue.socialProfiles)
        ? propsValue.socialProfiles.filter((url): url is string => typeof url === 'string').map((url) => ({ value: url, type: 'other' }))
        : undefined,
    };
    Object.keys(payload).forEach((key) => {
      if (payload[key] === undefined || payload[key] === null) {
        delete payload[key];
      }
    });
    const response =  await helpScoutApiRequest({
      method: HttpMethod.POST,
      url: '/customers',
      auth,
      body: payload,
    });

    const customerId = response.headers?.['resource-id'];

    return {
      id:customerId
    }
  },
});