Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

move constructWebHookData to webhook invoker

Fendy Heryantoc651fc5d8222
diff --git a/packages/nocodb/src/helpers/webhookHelpers.ts b/packages/nocodb/src/helpers/webhookHelpers.ts
index 0b2bd5f126..a0fc5ddb68 100644
--- a/packages/nocodb/src/helpers/webhookHelpers.ts
+++ b/packages/nocodb/src/helpers/webhookHelpers.ts
@@ -12,7 +12,6 @@ import {
isDateMonthFormat,
UITypes,
} from 'nocodb-sdk';
-import { v4 as uuidv4 } from 'uuid';
import type {
ColumnType,
FormColumnType,
@@ -24,10 +23,9 @@ import type {
import type { AxiosResponse } from 'axios';
import type { NcContext } from '~/interface/config';
import type { Column, FormView, Hook, Model, Source, View } from '~/models';
-import { WebhookInvoker } from '~/utils/webhook-invoker';
import { Filter } from '~/models';
-import { isEE, populateUpdatePayloadDiff } from '~/utils';
-import { parseMetaProp } from '~/utils/modelUtils';
+import { populateUpdatePayloadDiff } from '~/utils';
+import { WebhookInvoker } from '~/utils/webhook-invoker';
handlebarsHelpers({ handlebars: Handlebars });
@@ -407,56 +405,15 @@ export function constructWebHookData(
newData: Record<string, unknown>,
user = null,
) {
- // TODO: construct webhook view data
- if (['v2', 'v3'].includes(hook.version)) {
- // extend in the future - currently only support records
- const scope = ['after', 'manual'].includes(hook.event) ? 'records.' : '';
- const isBulkInsert =
- hook.version === 'v2' && (hook.operation as any) === 'bulkInsert';
-
- // Check for include_user in notification object first, fall back to hook.include_user for backward compatibility
- const includeUser = parseMetaProp(hook, 'notification')?.include_user;
-
- return {
- type: `${scope}${hook.event}.${hook.operation}`,
- id: uuidv4(),
- ...(includeUser && isEE && user
- ? { user: sanitizeUserForHook(user) }
- : {}),
- version: hook.version,
- data: {
- table_id: model.id,
- table_name: model.title,
- // webhook are table specific, so no need to send view_id and view_name
- // view_id: view?.id,
- // view_name: view?.title,
- ...(prevData && {
- previous_rows: Array.isArray(prevData)
- ? prevData.map((prev) => ({ ...prev, nc_order: undefined }))
- : [{ ...prevData, nc_order: undefined }],
- }),
- ...(!isBulkInsert &&
- newData && {
- rows: Array.isArray(newData)
- ? newData.map((each) => ({
- ...each,
- nc_order: undefined,
- }))
- : [{ ...newData, nc_order: undefined }],
- }),
- ...(isBulkInsert && {
- rows_inserted: Array.isArray(newData)
- ? newData.length
- : newData
- ? 1
- : 0,
- }),
- },
- };
- }
-
- // for v1, keep it as it is
- return newData;
+ // backward compatibility
+ return new WebhookInvoker().constructWebHookData(
+ hook,
+ model,
+ _view,
+ prevData,
+ newData,
+ user,
+ );
}
function extractReqPayloadForLog(reqPayload, response?: AxiosResponse<any>) {
diff --git a/packages/nocodb/src/utils/webhook-invoker.ts b/packages/nocodb/src/utils/webhook-invoker.ts
index 3b49b72f04..d50b83f0b0 100644
--- a/packages/nocodb/src/utils/webhook-invoker.ts
+++ b/packages/nocodb/src/utils/webhook-invoker.ts
@@ -12,12 +12,13 @@ import type {
ViewType,
} from 'nocodb-sdk';
import type { Filter } from '~/models';
+import { parseMetaProp } from '~/utils/modelUtils';
import { NcError } from '~/helpers/ncError';
import NcPluginMgrv2 from '~/helpers/NcPluginMgrv2';
import {
- constructWebHookData,
handleHttpWebHook,
parseBody,
+ sanitizeUserForHook,
validateCondition,
} from '~/helpers/webhookHelpers';
import { JobTypes } from '~/interface/Jobs';
@@ -83,7 +84,7 @@ export class WebhookInvoker {
});
}
- const webhookData = constructWebHookData(
+ const webhookData = this.constructWebHookData(
hook,
model,
view,
@@ -185,7 +186,7 @@ export class WebhookInvoker {
if (hook.version === 'v2' || hook.version === 'v1') {
return newData;
} else {
- return constructWebHookData(hook, model, view, prevData, newData);
+ return this.constructWebHookData(hook, model, view, prevData, newData);
}
}
@@ -240,6 +241,66 @@ export class WebhookInvoker {
return flattenedFilters;
}
+ constructWebHookData(
+ hook: Hook | HookType,
+ model: Model | TableType,
+ _view: View | ViewType,
+ prevData: Record<string, unknown>,
+ newData: Record<string, unknown>,
+ user = null,
+ ) {
+ // TODO: construct webhook view data
+ if (['v2', 'v3'].includes(hook.version)) {
+ // extend in the future - currently only support records
+ const scope = ['after', 'manual'].includes(hook.event) ? 'records.' : '';
+ const isBulkInsert =
+ hook.version === 'v2' && (hook.operation as any) === 'bulkInsert';
+
+ // Check for include_user in notification object first, fall back to hook.include_user for backward compatibility
+ const includeUser = parseMetaProp(hook, 'notification')?.include_user;
+
+ return {
+ type: `${scope}${hook.event}.${hook.operation}`,
+ id: uuidv4(),
+ ...(includeUser && isEE && user
+ ? { user: sanitizeUserForHook(user) }
+ : {}),
+ version: hook.version,
+ data: {
+ table_id: model.id,
+ table_name: model.title,
+ // webhook are table specific, so no need to send view_id and view_name
+ // view_id: view?.id,
+ // view_name: view?.title,
+ ...(prevData && {
+ previous_rows: Array.isArray(prevData)
+ ? prevData.map((prev) => ({ ...prev, nc_order: undefined }))
+ : [{ ...prevData, nc_order: undefined }],
+ }),
+ ...(!isBulkInsert &&
+ newData && {
+ rows: Array.isArray(newData)
+ ? newData.map((each) => ({
+ ...each,
+ nc_order: undefined,
+ }))
+ : [{ ...newData, nc_order: undefined }],
+ }),
+ ...(isBulkInsert && {
+ rows_inserted: Array.isArray(newData)
+ ? newData.length
+ : newData
+ ? 1
+ : 0,
+ }),
+ },
+ };
+ }
+
+ // for v1, keep it as it is
+ return newData;
+ }
+
public async invoke(
context: NcContext,
param: {