Browse Source

fix: avoid throwing from async

nc-fix/meta-first
mertmit 5 months ago
parent
commit
3e28da4d29
  1. 202
      packages/nocodb/src/services/hook-handler.service.ts

202
packages/nocodb/src/services/hook-handler.service.ts

@ -29,117 +29,125 @@ export class HookHandlerService implements OnModuleInit, OnModuleDestroy {
context: NcContext, context: NcContext,
{ hookName, prevData, newData, user, viewId, modelId, tnPath }, { hookName, prevData, newData, user, viewId, modelId, tnPath },
): Promise<void> { ): Promise<void> {
const view = await View.get(context, viewId); try {
const model = await Model.get(context, modelId); const view = await View.get(context, viewId);
const model = await Model.get(context, modelId);
// handle form view data submission // handle form view data submission
if ( if (
(hookName === 'after.insert' || hookName === 'after.bulkInsert') && (hookName === 'after.insert' || hookName === 'after.bulkInsert') &&
view.type === ViewTypes.FORM view.type === ViewTypes.FORM
) { ) {
try { try {
const formView = await view.getView<FormView>(context); const formView = await view.getView<FormView>(context);
const emails = Object.entries(JSON.parse(formView?.email) || {})
.filter((a) => a[1])
.map((a) => a[0]);
if (emails?.length) { const emails = Object.entries(JSON.parse(formView?.email) || {})
const { columns } = await FormView.getWithInfo( .filter((a) => a[1])
context, .map((a) => a[0]);
formView.fk_view_id,
);
const allColumns = await model.getColumns(context);
const fieldById = columns.reduce(
(o: Record<string, FormColumnType>, f: FormColumnType) => {
return Object.assign(o, { [f.fk_column_id]: f });
},
{},
);
let order = 1;
const filteredColumns = allColumns
?.map((c: ColumnType) => {
return {
...c,
fk_column_id: c.id,
fk_view_id: formView.fk_view_id,
...(fieldById[c.id] ? fieldById[c.id] : {}),
order: (fieldById[c.id] && fieldById[c.id].order) || order++,
id: fieldById[c.id] && fieldById[c.id].id,
};
})
.sort((a: ColumnType, b: ColumnType) => a.order - b.order)
.filter(
(f: ColumnType & FormColumnType) =>
f.show &&
f.uidt !== UITypes.Rollup &&
f.uidt !== UITypes.Lookup &&
f.uidt !== UITypes.Formula &&
f.uidt !== UITypes.QrCode &&
f.uidt !== UITypes.Barcode &&
f.uidt !== UITypes.SpecificDBType,
)
.sort((a: ColumnType, b: ColumnType) => a.order - b.order)
.map((c: ColumnType & FormColumnType) => {
c.required = !!(c.required || 0);
return c;
});
const transformedData = _transformSubmittedFormDataForEmail( if (emails?.length) {
newData, const { columns } = await FormView.getWithInfo(
formView, context,
filteredColumns, formView.fk_view_id,
); );
(await NcPluginMgrv2.emailAdapter(false))?.mailSend({ const allColumns = await model.getColumns(context);
to: emails.join(','), const fieldById = columns.reduce(
subject: 'NocoDB Form', (o: Record<string, FormColumnType>, f: FormColumnType) => {
html: ejs.render(formSubmissionEmailTemplate, { return Object.assign(o, { [f.fk_column_id]: f });
data: transformedData, },
tn: tnPath, {},
_tn: model.title, );
}), let order = 1;
}); const filteredColumns = allColumns
} ?.map((c: ColumnType) => {
} catch (e) { return {
this.logger.error({ ...c,
error: e, fk_column_id: c.id,
details: 'Error while sending form submission email', fk_view_id: formView.fk_view_id,
hookName, ...(fieldById[c.id] ? fieldById[c.id] : {}),
}); order: (fieldById[c.id] && fieldById[c.id].order) || order++,
} id: fieldById[c.id] && fieldById[c.id].id,
} };
})
.sort((a: ColumnType, b: ColumnType) => a.order - b.order)
.filter(
(f: ColumnType & FormColumnType) =>
f.show &&
f.uidt !== UITypes.Rollup &&
f.uidt !== UITypes.Lookup &&
f.uidt !== UITypes.Formula &&
f.uidt !== UITypes.QrCode &&
f.uidt !== UITypes.Barcode &&
f.uidt !== UITypes.SpecificDBType,
)
.sort((a: ColumnType, b: ColumnType) => a.order - b.order)
.map((c: ColumnType & FormColumnType) => {
c.required = !!(c.required || 0);
return c;
});
const [event, operation] = hookName.split('.'); const transformedData = _transformSubmittedFormDataForEmail(
const hooks = await Hook.list(context, { newData,
fk_model_id: modelId, formView,
event: event as HookType['event'], filteredColumns,
operation: operation as HookType['operation'], );
}); (await NcPluginMgrv2.emailAdapter(false))?.mailSend({
for (const hook of hooks) { to: emails.join(','),
if (hook.active) { subject: 'NocoDB Form',
try { html: ejs.render(formSubmissionEmailTemplate, {
await this.jobsService.add(JobTypes.HandleWebhook, { data: transformedData,
context, tn: tnPath,
hookId: hook.id, _tn: model.title,
modelId, }),
viewId, });
prevData, }
newData,
user,
});
} catch (e) { } catch (e) {
this.logger.error({ this.logger.error({
error: e, error: e,
details: 'Error while invoking webhook', details: 'Error while sending form submission email',
hook: hook.id, hookName,
}); });
} }
} }
const [event, operation] = hookName.split('.');
const hooks = await Hook.list(context, {
fk_model_id: modelId,
event: event as HookType['event'],
operation: operation as HookType['operation'],
});
for (const hook of hooks) {
if (hook.active) {
try {
await this.jobsService.add(JobTypes.HandleWebhook, {
context,
hookId: hook.id,
modelId,
viewId,
prevData,
newData,
user,
});
} catch (e) {
this.logger.error({
error: e,
details: 'Error while invoking webhook',
hook: hook.id,
});
}
}
}
} catch (e) {
this.logger.error({
error: e,
details: 'Error while handling hook',
hookName,
});
} }
} }
onModuleInit(): any { onModuleInit(): any {
this.unsubscribe = this.eventEmitter.on(HANDLE_WEBHOOK, async (arg) => { this.unsubscribe = this.eventEmitter.on(HANDLE_WEBHOOK, (arg) => {
const { context, ...rest } = arg; const { context, ...rest } = arg;
return this.handleHooks(context, rest); return this.handleHooks(context, rest);
}); });

Loading…
Cancel
Save