From b07bd4374fe940537feae894a1f147fec8715d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Mon, 27 Nov 2023 14:28:21 +0300 Subject: [PATCH] feat(zendesk): add create user action --- .../zendesk/actions/create-user/fields.ts | 102 ++++++++++++++++++ .../apps/zendesk/actions/create-user/index.ts | 53 +++++++++ .../backend/src/apps/zendesk/actions/index.ts | 9 +- .../src/apps/zendesk/dynamic-data/index.ts | 2 + .../dynamic-data/list-organizations/index.ts | 38 +++++++ packages/docs/pages/apps/zendesk/actions.md | 2 + 6 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 packages/backend/src/apps/zendesk/actions/create-user/fields.ts create mode 100644 packages/backend/src/apps/zendesk/actions/create-user/index.ts create mode 100644 packages/backend/src/apps/zendesk/dynamic-data/list-organizations/index.ts diff --git a/packages/backend/src/apps/zendesk/actions/create-user/fields.ts b/packages/backend/src/apps/zendesk/actions/create-user/fields.ts new file mode 100644 index 00000000..8bbfa026 --- /dev/null +++ b/packages/backend/src/apps/zendesk/actions/create-user/fields.ts @@ -0,0 +1,102 @@ +export const fields = [ + { + label: 'Name', + key: 'name', + type: 'string' as const, + required: true, + variables: true, + description: '', + }, + { + label: 'Email', + key: 'email', + type: 'string' as const, + required: true, + variables: true, + description: + 'It is essential to be distinctive. Zendesk prohibits the existence of identical users sharing the same email address.', + }, + { + label: 'Details', + key: 'details', + type: 'string' as const, + required: false, + variables: true, + description: '', + }, + { + label: 'Notes', + key: 'notes', + type: 'string' as const, + required: false, + variables: true, + description: + 'Within this field, you have the capability to save any remarks or comments you may have concerning the user.', + }, + { + label: 'Phone', + key: 'phone', + type: 'string' as const, + required: false, + variables: true, + description: + "The user's contact number should be entered in the following format: +1 (555) 123-4567.", + }, + { + label: 'Tags', + key: 'tags', + type: 'string' as const, + required: false, + variables: true, + description: 'A comma separated list of tags.', + }, + { + label: 'Role', + key: 'role', + type: 'string' as const, + required: false, + variables: true, + description: + "It can take on one of the designated roles: 'end-user', 'agent', or 'admin'. If a different value is set or none is specified, the default is 'end-user.'", + }, + { + label: 'Organization', + key: 'organizationId', + type: 'dropdown' as const, + required: false, + variables: true, + description: 'Assign this user to a specific organization.', + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listOrganizations', + }, + ], + }, + }, + { + label: 'External Id', + key: 'externalId', + type: 'string' as const, + required: false, + variables: true, + description: + 'An exclusive external identifier; you can utilize this to link organizations with an external record.', + }, + { + label: 'Verified', + key: 'verified', + type: 'dropdown' as const, + required: false, + description: + "Specify if you can verify that the user's assertion of their identity is accurate.", + variables: true, + options: [ + { label: 'True', value: 'true' }, + { label: 'False', value: 'false' }, + ], + }, +]; diff --git a/packages/backend/src/apps/zendesk/actions/create-user/index.ts b/packages/backend/src/apps/zendesk/actions/create-user/index.ts new file mode 100644 index 00000000..44cfc5c4 --- /dev/null +++ b/packages/backend/src/apps/zendesk/actions/create-user/index.ts @@ -0,0 +1,53 @@ +import { IJSONObject } from '@automatisch/types'; +import defineAction from '../../../../helpers/define-action'; +import { fields } from './fields'; + +type Payload = { + user: IJSONObject; +}; + +export default defineAction({ + name: 'Create user', + key: 'createUser', + description: 'Creates a new user.', + arguments: fields, + + async run($) { + const { + name, + email, + details, + notes, + phone, + role, + organizationId, + externalId, + verified, + } = $.step.parameters; + + const tags = $.step.parameters.tags as string; + const formattedTags = tags.split(','); + + const payload: Payload = { + user: { + name, + email, + details, + notes, + phone, + organization_id: organizationId, + external_id: externalId, + verified: verified || 'false', + tags: formattedTags, + }, + }; + + if (role) { + payload.user.role = role; + } + + const response = await $.http.post('/api/v2/users', payload); + + $.setActionItem({ raw: response.data }); + }, +}); diff --git a/packages/backend/src/apps/zendesk/actions/index.ts b/packages/backend/src/apps/zendesk/actions/index.ts index 752a24c6..ce9e27d0 100644 --- a/packages/backend/src/apps/zendesk/actions/index.ts +++ b/packages/backend/src/apps/zendesk/actions/index.ts @@ -1,6 +1,13 @@ import createTicket from './create-ticket'; +import createUser from './create-user'; import deleteTicket from './delete-ticket'; import findTicket from './find-ticket'; import updateTicket from './update-ticket'; -export default [createTicket, deleteTicket, findTicket, updateTicket]; +export default [ + createTicket, + createUser, + deleteTicket, + findTicket, + updateTicket, +]; diff --git a/packages/backend/src/apps/zendesk/dynamic-data/index.ts b/packages/backend/src/apps/zendesk/dynamic-data/index.ts index 67475704..f5755bc4 100644 --- a/packages/backend/src/apps/zendesk/dynamic-data/index.ts +++ b/packages/backend/src/apps/zendesk/dynamic-data/index.ts @@ -2,6 +2,7 @@ import listUsers from './list-users'; import listBrands from './list-brands'; import listFirstPageOfTickets from './list-first-page-of-tickets'; import listGroups from './list-groups'; +import listOrganizations from './list-organizations'; import listSharingAgreements from './list-sharing-agreements'; import listTicketForms from './list-ticket-forms'; import listViews from './list-views'; @@ -11,6 +12,7 @@ export default [ listBrands, listFirstPageOfTickets, listGroups, + listOrganizations, listSharingAgreements, listFirstPageOfTickets, listTicketForms, diff --git a/packages/backend/src/apps/zendesk/dynamic-data/list-organizations/index.ts b/packages/backend/src/apps/zendesk/dynamic-data/list-organizations/index.ts new file mode 100644 index 00000000..c86ac91c --- /dev/null +++ b/packages/backend/src/apps/zendesk/dynamic-data/list-organizations/index.ts @@ -0,0 +1,38 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +export default { + name: 'List organizations', + key: 'listOrganizations', + + async run($: IGlobalVariable) { + const organizations: { + data: IJSONObject[]; + } = { + data: [], + }; + let hasMore; + + const params = { + 'page[size]': 100, + 'page[after]': undefined as unknown as string, + }; + + do { + const response = await $.http.get('/api/v2/organizations', { params }); + const allOrganizations = response?.data?.organizations; + hasMore = response?.data?.meta?.has_more; + params['page[after]'] = response.data.meta?.after_cursor; + + if (allOrganizations?.length) { + for (const organization of allOrganizations) { + organizations.data.push({ + value: organization.id, + name: organization.name, + }); + } + } + } while (hasMore); + + return organizations; + }, +}; diff --git a/packages/docs/pages/apps/zendesk/actions.md b/packages/docs/pages/apps/zendesk/actions.md index e70e6d67..a0b9273b 100644 --- a/packages/docs/pages/apps/zendesk/actions.md +++ b/packages/docs/pages/apps/zendesk/actions.md @@ -3,6 +3,8 @@ favicon: /favicons/zendesk.svg items: - name: Create ticket desc: Creates a new ticket. + - name: Create user + desc: Creates a new user. - name: Delete ticket desc: Deletes an existing ticket. - name: Find ticket