diff --git a/src/__tests__/widgetMutations.test.ts b/src/__tests__/widgetMutations.test.ts index 9e2700476..d9d266fe6 100644 --- a/src/__tests__/widgetMutations.test.ts +++ b/src/__tests__/widgetMutations.test.ts @@ -517,6 +517,38 @@ describe('lead', () => { mock.restore(); }); + test('leadConnect: Already filled', async () => { + const mock = sinon.stub(utils, 'sendRequest').callsFake(() => { + return Promise.resolve('success'); + }); + + const brand = await brandFactory({}); + const form = await formFactory({}); + + const integration = await integrationFactory({ + brandId: brand._id, + formId: form._id, + leadData: { + loadType: 'embedded', + isRequireOnce: true, + }, + }); + + const conversation = await conversationFactory({ customerId: '123123', integrationId: integration._id }); + + const response = await widgetMutations.widgetsLeadConnect( + {}, + { + brandCode: brand.code || '', + formCode: form.code || '', + cachedCustomerId: '123123', + }, + ); + expect(conversation).toBeDefined(); + expect(response).toBeNull(); + mock.restore(); + }); + test('saveLead: form not found', async () => { try { await widgetMutations.widgetsSaveLead( diff --git a/src/data/constants.ts b/src/data/constants.ts index 3321e0402..6ad099cf6 100644 --- a/src/data/constants.ts +++ b/src/data/constants.ts @@ -25,8 +25,9 @@ export const FORM_FIELDS = { BLANK: '', NUMBER: 'number', DATE: 'date', + DATETIME: 'datetime', EMAIL: 'email', - ALL: ['', 'number', 'date', 'email'], + ALL: ['', 'number', 'date', 'datetime', 'email'], }, }; diff --git a/src/data/resolvers/mutations/widgets.ts b/src/data/resolvers/mutations/widgets.ts index 0d1de58ab..1c3cf9fba 100644 --- a/src/data/resolvers/mutations/widgets.ts +++ b/src/data/resolvers/mutations/widgets.ts @@ -84,7 +84,7 @@ export const getMessengerData = async (integration: IIntegrationDocument) => { const widgetMutations = { // Find integrationId by brandCode - async widgetsLeadConnect(_root, args: { brandCode: string; formCode: string }) { + async widgetsLeadConnect(_root, args: { brandCode: string; formCode: string; cachedCustomerId?: string }) { const brand = await Brands.findOne({ code: args.brandCode }); const form = await Forms.findOne({ code: args.formCode }); @@ -112,6 +112,13 @@ const widgetMutations = { registerOnboardHistory({ type: 'leadIntegrationInstalled', user }); } + if (integ.leadData?.isRequireOnce && args.cachedCustomerId) { + const conversation = await Conversations.findOne({ customerId: args.cachedCustomerId, integrationId: integ.id }); + if (conversation) { + return null; + } + } + // return integration details return { integration: integ, diff --git a/src/data/schema/integration.ts b/src/data/schema/integration.ts index 360c1d8f2..4c9081004 100644 --- a/src/data/schema/integration.ts +++ b/src/data/schema/integration.ts @@ -46,6 +46,7 @@ export const types = ` themeColor: String callout: JSON, rules: [InputRule] + isRequireOnce: Boolean } input MessengerOnlineHoursSchema { diff --git a/src/data/schema/widget.ts b/src/data/schema/widget.ts index 200e9164f..c9c0811dd 100644 --- a/src/data/schema/widget.ts +++ b/src/data/schema/widget.ts @@ -94,7 +94,8 @@ export const mutations = ` widgetsLeadConnect( brandCode: String!, - formCode: String! + formCode: String!, + cachedCustomerId: String ): FormConnectResponse widgetsSaveLead( diff --git a/src/db/models/definitions/integrations.ts b/src/db/models/definitions/integrations.ts index 4ea9321ea..2f24ed980 100644 --- a/src/db/models/definitions/integrations.ts +++ b/src/db/models/definitions/integrations.ts @@ -75,6 +75,7 @@ export interface ILeadData { rules?: IRule; viewCount?: number; contactsGathered?: number; + isRequireOnce?: boolean; } export interface ILeadDataDocument extends ILeadData, Document { @@ -259,6 +260,11 @@ export const leadDataSchema = new Schema( optional: true, label: 'Rules', }), + isRequireOnce: field({ + type: Boolean, + optional: true, + label: 'Do now show again if already filled out', + }), }, { _id: false }, );