+249
@@ -0,0 +1,249 @@
|
||||
import type { ObjectRecordEvent } from 'twenty-shared/database-events';
|
||||
|
||||
import { type WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/types/workspace-event-batch.type';
|
||||
import type { WebhookEntity } from 'src/engine/metadata-modules/webhook/entities/webhook.entity';
|
||||
import { transformEventBatchToWebhookEvents } from 'src/engine/metadata-modules/webhook/utils/transform-event-batch-to-webhook-events';
|
||||
import { type FlatObjectMetadata } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata.type';
|
||||
|
||||
const mockObjectMetadata: FlatObjectMetadata = {
|
||||
id: 'id',
|
||||
nameSingular: 'nameSingular',
|
||||
namePlural: 'namePlural',
|
||||
workspaceId: 'workspaceId',
|
||||
labelSingular: 'Label Singular',
|
||||
labelPlural: 'Label Plural',
|
||||
isCustom: false,
|
||||
isRemote: false,
|
||||
isActive: true,
|
||||
isSystem: false,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
universalIdentifier: 'id',
|
||||
fieldIds: [],
|
||||
indexMetadataIds: [],
|
||||
viewIds: [],
|
||||
applicationId: null,
|
||||
} as unknown as FlatObjectMetadata;
|
||||
|
||||
describe('transformEventBatchToWebhookEvents', () => {
|
||||
it('should transform properly', () => {
|
||||
const workspaceEventBatch: WorkspaceEventBatch<ObjectRecordEvent> = {
|
||||
workspaceId: 'workspaceId',
|
||||
objectMetadata: mockObjectMetadata,
|
||||
name: 'objectNameSingular.created',
|
||||
events: [
|
||||
{
|
||||
recordId: 'recordId-1',
|
||||
properties: {
|
||||
after: {
|
||||
id: 'id-1',
|
||||
nameSingular: 'nameSingular-1',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
recordId: 'recordId-2',
|
||||
properties: {
|
||||
before: {
|
||||
id: 'id-2',
|
||||
nameSingular: 'nameSingular-2',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
recordId: 'recordId-3',
|
||||
properties: {
|
||||
after: {
|
||||
id: 'id-3',
|
||||
nameSingular: 'nameSingular-3',
|
||||
secret: 'secret-3',
|
||||
},
|
||||
updatedFields: ['nameSingular'],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const webhooks = [
|
||||
{
|
||||
id: 'webhook-id',
|
||||
targetUrl: 'targetUrl',
|
||||
secret: 'secret',
|
||||
},
|
||||
{
|
||||
id: 'webhook-id-2',
|
||||
targetUrl: 'targetUrl-2',
|
||||
secret: 'secret-2',
|
||||
},
|
||||
] as WebhookEntity[];
|
||||
|
||||
const result = transformEventBatchToWebhookEvents({
|
||||
workspaceEventBatch,
|
||||
webhooks,
|
||||
});
|
||||
const expectedResultWithoutEventDate = [
|
||||
{
|
||||
targetUrl: 'targetUrl',
|
||||
eventName: 'objectNameSingular.created',
|
||||
objectMetadata: {
|
||||
id: mockObjectMetadata.id,
|
||||
nameSingular: mockObjectMetadata.nameSingular,
|
||||
},
|
||||
workspaceId: 'workspaceId',
|
||||
webhookId: 'webhook-id',
|
||||
record: {
|
||||
id: 'id-1',
|
||||
nameSingular: 'nameSingular-1',
|
||||
},
|
||||
secret: 'secret',
|
||||
},
|
||||
{
|
||||
targetUrl: 'targetUrl',
|
||||
eventName: 'objectNameSingular.created',
|
||||
objectMetadata: {
|
||||
id: mockObjectMetadata.id,
|
||||
nameSingular: mockObjectMetadata.nameSingular,
|
||||
},
|
||||
workspaceId: 'workspaceId',
|
||||
webhookId: 'webhook-id',
|
||||
record: {
|
||||
id: 'id-2',
|
||||
nameSingular: 'nameSingular-2',
|
||||
},
|
||||
secret: 'secret',
|
||||
},
|
||||
{
|
||||
targetUrl: 'targetUrl',
|
||||
eventName: 'objectNameSingular.created',
|
||||
objectMetadata: {
|
||||
id: mockObjectMetadata.id,
|
||||
nameSingular: mockObjectMetadata.nameSingular,
|
||||
},
|
||||
workspaceId: 'workspaceId',
|
||||
webhookId: 'webhook-id',
|
||||
record: {
|
||||
id: 'id-3',
|
||||
nameSingular: 'nameSingular-3',
|
||||
secret: 'secret-3',
|
||||
},
|
||||
updatedFields: ['nameSingular'],
|
||||
secret: 'secret',
|
||||
},
|
||||
{
|
||||
targetUrl: 'targetUrl-2',
|
||||
eventName: 'objectNameSingular.created',
|
||||
objectMetadata: {
|
||||
id: mockObjectMetadata.id,
|
||||
nameSingular: mockObjectMetadata.nameSingular,
|
||||
},
|
||||
workspaceId: 'workspaceId',
|
||||
webhookId: 'webhook-id-2',
|
||||
record: {
|
||||
id: 'id-1',
|
||||
nameSingular: 'nameSingular-1',
|
||||
},
|
||||
secret: 'secret-2',
|
||||
},
|
||||
{
|
||||
targetUrl: 'targetUrl-2',
|
||||
eventName: 'objectNameSingular.created',
|
||||
objectMetadata: {
|
||||
id: mockObjectMetadata.id,
|
||||
nameSingular: mockObjectMetadata.nameSingular,
|
||||
},
|
||||
workspaceId: 'workspaceId',
|
||||
webhookId: 'webhook-id-2',
|
||||
record: {
|
||||
id: 'id-2',
|
||||
nameSingular: 'nameSingular-2',
|
||||
},
|
||||
secret: 'secret-2',
|
||||
},
|
||||
{
|
||||
targetUrl: 'targetUrl-2',
|
||||
eventName: 'objectNameSingular.created',
|
||||
objectMetadata: {
|
||||
id: mockObjectMetadata.id,
|
||||
nameSingular: mockObjectMetadata.nameSingular,
|
||||
},
|
||||
workspaceId: 'workspaceId',
|
||||
webhookId: 'webhook-id-2',
|
||||
record: {
|
||||
id: 'id-3',
|
||||
nameSingular: 'nameSingular-3',
|
||||
secret: 'secret-3',
|
||||
},
|
||||
updatedFields: ['nameSingular'],
|
||||
secret: 'secret-2',
|
||||
},
|
||||
];
|
||||
|
||||
const resultWithoutEventDate = result.map((event) => {
|
||||
const { eventDate: _, ...eventWithoutEventDate } = event;
|
||||
|
||||
return eventWithoutEventDate;
|
||||
});
|
||||
|
||||
expect(resultWithoutEventDate).toEqual(expectedResultWithoutEventDate);
|
||||
});
|
||||
|
||||
it('should sanitize records properly', () => {
|
||||
const workspaceEventBatch: WorkspaceEventBatch<ObjectRecordEvent> = {
|
||||
workspaceId: 'workspaceId',
|
||||
objectMetadata: mockObjectMetadata,
|
||||
name: 'webhook.created',
|
||||
events: [
|
||||
{
|
||||
recordId: 'recordId-1',
|
||||
properties: {
|
||||
after: {
|
||||
id: 'id-1',
|
||||
targetUrl: 'targetUrl-1',
|
||||
secret: 'secret-1',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const webhooks = [
|
||||
{
|
||||
id: 'webhook-id',
|
||||
targetUrl: 'targetUrl',
|
||||
secret: 'secret',
|
||||
},
|
||||
] as WebhookEntity[];
|
||||
|
||||
const result = transformEventBatchToWebhookEvents({
|
||||
workspaceEventBatch,
|
||||
webhooks,
|
||||
});
|
||||
|
||||
const expectedResultWithoutEventDate = [
|
||||
{
|
||||
targetUrl: 'targetUrl',
|
||||
eventName: 'webhook.created',
|
||||
objectMetadata: {
|
||||
id: mockObjectMetadata.id,
|
||||
nameSingular: mockObjectMetadata.nameSingular,
|
||||
},
|
||||
workspaceId: 'workspaceId',
|
||||
webhookId: 'webhook-id',
|
||||
record: {
|
||||
id: 'id-1',
|
||||
targetUrl: 'targetUrl-1',
|
||||
// No secret
|
||||
},
|
||||
secret: 'secret',
|
||||
},
|
||||
];
|
||||
|
||||
const resultWithoutEventDate = result.map((event) => {
|
||||
const { eventDate: _, ...eventWithoutEventDate } = event;
|
||||
|
||||
return eventWithoutEventDate;
|
||||
});
|
||||
|
||||
expect(resultWithoutEventDate).toEqual(expectedResultWithoutEventDate);
|
||||
});
|
||||
});
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
import type { ObjectRecordEvent } from 'twenty-shared/database-events';
|
||||
|
||||
import { transformEventToWebhookEvent } from 'src/engine/metadata-modules/webhook/utils/transform-event-to-webhook-event';
|
||||
|
||||
describe('transformEventToWebhookEvent', () => {
|
||||
it('should transform event to webhook event', () => {
|
||||
const record = {
|
||||
recordId: 'recordId',
|
||||
properties: {
|
||||
after: {
|
||||
id: 'id',
|
||||
nameSingular: 'nameSingular',
|
||||
secret: 'secret',
|
||||
},
|
||||
updatedFields: ['nameSingular'],
|
||||
},
|
||||
} as ObjectRecordEvent;
|
||||
|
||||
const expectedResult = {
|
||||
record: {
|
||||
id: 'id',
|
||||
nameSingular: 'nameSingular',
|
||||
secret: 'secret',
|
||||
},
|
||||
updatedFields: ['nameSingular'],
|
||||
};
|
||||
|
||||
expect(
|
||||
transformEventToWebhookEvent({
|
||||
eventName: 'nameSingular.created',
|
||||
event: record,
|
||||
}),
|
||||
).toEqual(expectedResult);
|
||||
});
|
||||
});
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { randomBytes } from 'crypto';
|
||||
|
||||
export const generateWebhookSecret = (): string => {
|
||||
return randomBytes(32).toString('hex');
|
||||
};
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
import type { ObjectRecordEvent } from 'twenty-shared/database-events';
|
||||
|
||||
import { type WebhookEntity } from 'src/engine/metadata-modules/webhook/entities/webhook.entity';
|
||||
import { type CallWebhookJobData } from 'src/engine/metadata-modules/webhook/jobs/call-webhook.job';
|
||||
import { transformEventToWebhookEvent } from 'src/engine/metadata-modules/webhook/utils/transform-event-to-webhook-event';
|
||||
import { type WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/types/workspace-event-batch.type';
|
||||
|
||||
export const transformEventBatchToWebhookEvents = ({
|
||||
workspaceEventBatch,
|
||||
webhooks,
|
||||
}: {
|
||||
workspaceEventBatch: WorkspaceEventBatch<ObjectRecordEvent>;
|
||||
webhooks: WebhookEntity[];
|
||||
}): CallWebhookJobData[] => {
|
||||
const result: CallWebhookJobData[] = [];
|
||||
|
||||
for (const webhook of webhooks) {
|
||||
const targetUrl = webhook.targetUrl;
|
||||
const eventName = workspaceEventBatch.name;
|
||||
const objectMetadataForWebhook = {
|
||||
id: workspaceEventBatch.objectMetadata.id,
|
||||
nameSingular: workspaceEventBatch.objectMetadata.nameSingular,
|
||||
};
|
||||
const workspaceId = workspaceEventBatch.workspaceId;
|
||||
const webhookId = webhook.id;
|
||||
const eventDate = new Date();
|
||||
const secret = webhook.secret;
|
||||
|
||||
for (const eventData of workspaceEventBatch.events) {
|
||||
const { record, updatedFields } = transformEventToWebhookEvent({
|
||||
eventName: workspaceEventBatch.name,
|
||||
event: eventData,
|
||||
});
|
||||
|
||||
result.push({
|
||||
targetUrl,
|
||||
eventName,
|
||||
objectMetadata: objectMetadataForWebhook,
|
||||
workspaceId,
|
||||
webhookId,
|
||||
eventDate,
|
||||
userId: eventData.userId,
|
||||
workspaceMemberId: eventData.workspaceMemberId,
|
||||
record,
|
||||
...(updatedFields && { updatedFields }),
|
||||
secret,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import type { ObjectRecordEvent } from 'twenty-shared/database-events';
|
||||
|
||||
import { removeSecretFromWebhookRecord } from 'src/utils/remove-secret-from-webhook-record';
|
||||
|
||||
export const transformEventToWebhookEvent = ({
|
||||
eventName,
|
||||
event,
|
||||
}: {
|
||||
eventName: string;
|
||||
event: ObjectRecordEvent;
|
||||
}) => {
|
||||
const [nameSingular, _] = eventName.split('.');
|
||||
|
||||
const record =
|
||||
'after' in event.properties && isDefined(event.properties.after)
|
||||
? event.properties.after
|
||||
: 'before' in event.properties && isDefined(event.properties.before)
|
||||
? event.properties.before
|
||||
: {};
|
||||
const updatedFields =
|
||||
'updatedFields' in event.properties
|
||||
? event.properties.updatedFields
|
||||
: undefined;
|
||||
|
||||
const isWebhookEvent = nameSingular === 'webhook';
|
||||
|
||||
const sanitizedRecord = removeSecretFromWebhookRecord(record, isWebhookEvent);
|
||||
|
||||
return {
|
||||
record: sanitizedRecord,
|
||||
...(updatedFields && { updatedFields }),
|
||||
};
|
||||
};
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import { assertUnreachable } from 'twenty-shared/utils';
|
||||
|
||||
import {
|
||||
ConflictError,
|
||||
NotFoundError,
|
||||
UserInputError,
|
||||
} from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
|
||||
import {
|
||||
WebhookException,
|
||||
WebhookExceptionCode,
|
||||
} from 'src/engine/metadata-modules/webhook/webhook.exception';
|
||||
|
||||
export const webhookGraphqlApiExceptionHandler = (error: Error) => {
|
||||
if (error instanceof WebhookException) {
|
||||
switch (error.code) {
|
||||
case WebhookExceptionCode.WEBHOOK_NOT_FOUND:
|
||||
throw new NotFoundError(error);
|
||||
case WebhookExceptionCode.INVALID_WEBHOOK_INPUT:
|
||||
case WebhookExceptionCode.INVALID_TARGET_URL:
|
||||
throw new UserInputError(error);
|
||||
case WebhookExceptionCode.WEBHOOK_ALREADY_EXISTS:
|
||||
throw new ConflictError(error);
|
||||
default: {
|
||||
return assertUnreachable(error.code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw error;
|
||||
};
|
||||
Reference in New Issue
Block a user