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

8.6 KB
Raw Blame
import { Property } from '@activepieces/pieces-framework';
import { systemeIoCommon } from './client';
import { systemeIoAuth } from './auth';
import { contactDropdownOptions, contactLabel } from './dropdowns';

export const systemeIoProps = {
  contactDropdown: Property.Dropdown({
    auth: systemeIoAuth,
    displayName: 'Contact',
    description: 'Select a contact',
    required: true,
    refreshers: [],
    options: async ({ auth }) => {
      if (!auth) {
        return {
          disabled: true,
          placeholder: 'Please connect your account first',
          options: [],
        };
      }

      try {
        const response = await systemeIoCommon.getContacts({
          auth: auth.secret_text,
          limit: 100,
        });

        let contacts: any[] = [];
        if (Array.isArray(response)) {
          contacts = response;
        } else if (response && typeof response === 'object' && response !== null) {
          const responseAny = response as any;
          if (responseAny.items && Array.isArray(responseAny.items)) {
            contacts = responseAny.items;
          }
        }

        if (contacts.length > 0) {
          return {
            disabled: false,
            options: contacts.map((contact: any) => ({
              label: `${contact.first_name || ''} ${contact.last_name || ''} (${contact.email})`.trim(),
              value: contact.id,
            })),
          };
        }

        return {
          disabled: true,
          placeholder: 'No contacts found',
          options: [],
        };
      } catch (error) {
        console.error('Error fetching contacts:', error);
        return {
          disabled: true,
          placeholder: 'Error loading contacts',
          options: [],
        };
      }
    },
  }),

  tagDropdown: Property.Dropdown({
    auth: systemeIoAuth,
    displayName: 'Tag',
    description: 'Select a tag',
    required: true,
    refreshers: [],
    options: async ({ auth }) => {
      if (!auth) {
        return {
          disabled: true,
          placeholder: 'Please connect your account first',
          options: [],
        };
      }

      try {
        const response = await systemeIoCommon.getTags({
          auth: auth.secret_text,
        });

        let tags: any[] = [];
        if (Array.isArray(response)) {
          tags = response;
        } else if (response && typeof response === 'object' && response !== null) {
          const responseAny = response as any;
          if (responseAny.items && Array.isArray(responseAny.items)) {
            tags = responseAny.items;
          }
        }

        if (tags.length > 0) {
          return {
            disabled: false,
            options: tags.map((tag: any) => ({
              label: tag.name,
              value: tag.id,
            })),
          };
        }

        return {
          disabled: true,
          placeholder: 'No tags found',
          options: [],
        };
      } catch (error) {
        console.error('Error fetching tags:', error);
        return {
          disabled: true,
          placeholder: 'Error loading tags',
          options: [],
        };
      }
    },
  }),

  tagsMultiSelectDropdown: Property.MultiSelectDropdown({
    auth: systemeIoAuth,
    displayName: 'Tags',
    description: 'Select tags to assign to the contact',
    required: false,
    refreshers: [],
    options: async ({ auth }) => {
      if (!auth) {
        return {
          disabled: true,
          placeholder: 'Please connect your account first',
          options: [],
        };
      }

      try {
        const response = await systemeIoCommon.getTags({
          auth: auth.secret_text,
        });

        let tags: any[] = [];
        if (Array.isArray(response)) {
          tags = response;
        } else if (response && typeof response === 'object' && response !== null) {
          const responseAny = response as any;
          if (responseAny.items && Array.isArray(responseAny.items)) {
            tags = responseAny.items;
          }
        }

        if (tags.length > 0) {
          return {
            disabled: false,
            options: tags.map((tag: any) => ({
              label: tag.name || tag.id,
              value: tag.id,
            })),
          };
        }

        return {
          disabled: true,
          placeholder: 'No tags found',
          options: [],
        };
      } catch (error) {
        console.error('Error fetching tags:', error);
        return {
          disabled: true,
          placeholder: 'Error loading tags',
          options: [],
        };
      }
    },
  }),

  contactIdDropdown: Property.Dropdown({
    auth: systemeIoAuth,
    displayName: 'Contact ID',
    description: 'Select a contact. Type a name (searches the newest 100), a full email address or a contact id.',
    required: true,
    refreshers: [],
    refreshOnSearch: true,
    options: async ({ auth }, ctx) => {
      if (!auth) {
        return {
          disabled: true,
          placeholder: 'Please connect your account first',
          options: [],
        };
      }
      return contactDropdownOptions({
        apiKey: auth.secret_text,
        searchValue: ctx?.searchValue,
        label: (contact) => `ID: ${contact.id} - ${contactLabel(contact)}`,
      });
    },
  }),

  tagNameDropdown: Property.Dropdown({
    auth: systemeIoAuth,
    displayName: 'Tag Name',
    description: 'Select a tag by name',
    required: true,
    refreshers: [],
    options: async ({ auth }) => {
      if (!auth) {
        return {
          disabled: true,
          placeholder: 'Please connect your account first',
          options: [],
        };
      }

      try {
        const response = await systemeIoCommon.getTags({
          auth: auth.secret_text,
        });

        let tags: any[] = [];
        if (Array.isArray(response)) {
          tags = response;
        } else if (response && typeof response === 'object' && response !== null) {
          const responseAny = response as any;
          if (responseAny.items && Array.isArray(responseAny.items)) {
            tags = responseAny.items;
          }
        }

        if (tags.length > 0) {
          return {
            disabled: false,
            options: tags.map((tag: any) => ({
              label: tag.name,
              value: tag.name,
            })),
          };
        }

        return {
          disabled: true,
          placeholder: 'No tags found',
          options: [],
        };
      } catch (error) {
        console.error('Error fetching tags:', error);
        return {
          disabled: true,
          placeholder: 'Error loading tags',
          options: [],
        };
      }
    },
  }),

  contactFields: Property.Array({
    displayName: 'Custom Contact Fields',
    description: 'Add custom contact field values (e.g., country, company, etc.)',
    required: false,
    properties: {
      field: Property.ShortText({
        displayName: 'Field Slug',
        description: 'Enter the field slug (e.g., country, company, custom1)',
        required: true,
      }),
      value: Property.ShortText({
        displayName: 'Value',
        description: 'Enter the field value',
        required: false,
      }),
    },
  }),

  contactFieldDropdown: Property.Dropdown({
    auth: systemeIoAuth,
    displayName: 'Contact Field',
    description: 'Select a contact field',
    required: false,
    refreshers: [],
    options: async ({ auth }) => {
      if (!auth) {
        return {
          disabled: true,
          placeholder: 'Please connect your account first',
          options: [],
        };
      }

      try {
        const response = await systemeIoCommon.getContactFields({
          auth: auth.secret_text,
        });

        let fields: any[] = [];
        if (Array.isArray(response)) {
          fields = response;
        } else if (response && typeof response === 'object' && response !== null) {
          const responseAny = response as any;
          if (responseAny.items && Array.isArray(responseAny.items)) {
            fields = responseAny.items;
          }
        }

        if (fields.length > 0) {
          return {
            disabled: false,
            options: fields.map((field: any) => ({
              label: field.fieldName || field.slug,
              value: field.slug,
            })),
          };
        }

        return {
          disabled: true,
          placeholder: 'No contact fields found',
          options: [],
        };
      } catch (error) {
        console.error('Error fetching contact fields:', error);
        return {
          disabled: true,
          placeholder: 'Error loading contact fields',
          options: [],
        };
      }
    },
  }),
};