forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2248 zapier integration implement typeorm eventsubscribers (twentyhq#…
…3122) * Add new queue to twenty-server * Add triggers to zapier * Rename webhook operation * Use find one or fail * Use logger * Fix typescript templating * Add dedicated call webhook job * Update logging * Fix error handling
- Loading branch information
Showing
36 changed files
with
1,040 additions
and
209 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
packages/twenty-server/src/integrations/message-queue/jobs.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/twenty-server/src/workspace/workspace-query-runner/jobs/call-webhook-jobs.job.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Inject, Injectable, Logger } from '@nestjs/common'; | ||
|
||
import { MessageQueueJob } from 'src/integrations/message-queue/interfaces/message-queue-job.interface'; | ||
|
||
import { WorkspaceDataSourceService } from 'src/workspace/workspace-datasource/workspace-datasource.service'; | ||
import { ObjectMetadataService } from 'src/metadata/object-metadata/object-metadata.service'; | ||
import { DataSourceService } from 'src/metadata/data-source/data-source.service'; | ||
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service'; | ||
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants'; | ||
import { | ||
CallWebhookJob, | ||
CallWebhookJobData, | ||
} from 'src/workspace/workspace-query-runner/jobs/call-webhook.job'; | ||
|
||
export enum CallWebhookJobsJobOperation { | ||
create = 'create', | ||
update = 'update', | ||
delete = 'delete', | ||
} | ||
|
||
export type CallWebhookJobsJobData = { | ||
workspaceId: string; | ||
objectNameSingular: string; | ||
recordData: any; | ||
operation: CallWebhookJobsJobOperation; | ||
}; | ||
|
||
@Injectable() | ||
export class CallWebhookJobsJob | ||
implements MessageQueueJob<CallWebhookJobsJobData> | ||
{ | ||
private readonly logger = new Logger(CallWebhookJobsJob.name); | ||
|
||
constructor( | ||
private readonly workspaceDataSourceService: WorkspaceDataSourceService, | ||
private readonly objectMetadataService: ObjectMetadataService, | ||
private readonly dataSourceService: DataSourceService, | ||
@Inject(MessageQueue.webhookQueue) | ||
private readonly messageQueueService: MessageQueueService, | ||
) {} | ||
|
||
async handle(data: CallWebhookJobsJobData): Promise<void> { | ||
const objectMetadataItem = | ||
await this.objectMetadataService.findOneOrFailWithinWorkspace( | ||
data.workspaceId, | ||
{ where: { nameSingular: data.objectNameSingular } }, | ||
); | ||
const dataSourceMetadata = | ||
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceIdOrFail( | ||
data.workspaceId, | ||
); | ||
const workspaceDataSource = | ||
await this.workspaceDataSourceService.connectToWorkspaceDataSource( | ||
data.workspaceId, | ||
); | ||
const operationName = `${data.operation}.${objectMetadataItem.namePlural}`; | ||
const webhooks: { id: string; targetUrl: string }[] = | ||
await workspaceDataSource?.query( | ||
`SELECT * FROM ${dataSourceMetadata.schema}."webhook" WHERE operation='${operationName}'`, | ||
); | ||
|
||
webhooks.forEach((webhook) => { | ||
this.messageQueueService.add<CallWebhookJobData>( | ||
CallWebhookJob.name, | ||
{ | ||
recordData: data.recordData, | ||
targetUrl: webhook.targetUrl, | ||
}, | ||
{ retryLimit: 3 }, | ||
); | ||
}); | ||
|
||
this.logger.log( | ||
`CallWebhookJobsJob on operation '${operationName}' called on webhooks ids [\n"${webhooks | ||
.map((webhook) => webhook.id) | ||
.join('",\n"')}"\n]`, | ||
); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/twenty-server/src/workspace/workspace-query-runner/jobs/call-webhook.job.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { HttpService } from '@nestjs/axios'; | ||
|
||
import { MessageQueueJob } from 'src/integrations/message-queue/interfaces/message-queue-job.interface'; | ||
|
||
export type CallWebhookJobData = { | ||
targetUrl: string; | ||
recordData: any; | ||
}; | ||
|
||
@Injectable() | ||
export class CallWebhookJob implements MessageQueueJob<CallWebhookJobData> { | ||
private readonly logger = new Logger(CallWebhookJob.name); | ||
|
||
constructor(private readonly httpService: HttpService) {} | ||
|
||
async handle(data: CallWebhookJobData): Promise<void> { | ||
try { | ||
await this.httpService.axiosRef.post(data.targetUrl, data.recordData); | ||
this.logger.log( | ||
`CallWebhookJob successfully called on targetUrl '${ | ||
data.targetUrl | ||
}' with data: ${JSON.stringify(data.recordData)}`, | ||
); | ||
} catch (err) { | ||
throw new Error( | ||
`Error calling webhook on targetUrl '${data.targetUrl}': ${err}`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.