File size: 1,484 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties } from 'twenty-shared/utils';
import { v4 } from 'uuid';

import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';
import { type FlatWebhook } from 'src/engine/metadata-modules/flat-webhook/types/flat-webhook.type';
import { type CreateWebhookInput } from 'src/engine/metadata-modules/webhook/dtos/create-webhook.input';
import { generateWebhookSecret } from 'src/engine/metadata-modules/webhook/utils/generate-webhook-secret.util';

export const fromCreateWebhookInputToFlatWebhookToCreate = ({
  createWebhookInput,
  workspaceId,
  flatApplication,
}: {
  createWebhookInput: CreateWebhookInput;
  workspaceId: string;
  flatApplication: FlatApplication;
}): FlatWebhook => {
  const now = new Date().toISOString();

  const { targetUrl, description } =
    trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties(
      createWebhookInput,
      ['targetUrl', 'description'],
    );

  const id = createWebhookInput.id ?? v4();
  const secret = createWebhookInput.secret ?? generateWebhookSecret();

  return {
    id,
    targetUrl,
    operations: createWebhookInput.operations,
    description: description ?? null,
    secret,
    workspaceId,
    createdAt: now,
    updatedAt: now,
    deletedAt: null,
    universalIdentifier: id,
    applicationId: flatApplication.id,
    applicationUniversalIdentifier: flatApplication.universalIdentifier,
  };
};