6071 return only updated fields of records in zapier update trigger (#8193)

- move webhook triggers into `entity-events-to-db.listener.ts`
- refactor event management
- add a `@OnDatabaseEvent` decorator to manage database events
- add updatedFields in updated events
- update openApi webhooks docs
- update zapier integration
This commit is contained in:
martmull
2024-11-04 17:44:36 +01:00
committed by GitHub
parent 741020fbb0
commit 695991881f
62 changed files with 547 additions and 578 deletions

View File

@ -79,7 +79,7 @@ export const requestDbViaRestApi = (
z: ZObject,
bundle: Bundle,
objectNamePlural: string,
) => {
): Promise<Record<string, any>[]> => {
const options = {
url: `${
bundle.authData.apiUrl || process.env.SERVER_BASE_URL

View File

@ -7,48 +7,28 @@ import requestDb, {
requestSchema,
} from '../../utils/requestDb';
export enum Operation {
create = 'create',
update = 'update',
delete = 'delete',
destroy = 'destroy',
export enum DatabaseEventAction {
CREATED = 'created',
UPDATED = 'updated',
DELETED = 'deleted',
DESTROYED = 'destroyed',
}
export const subscribe = async (
z: ZObject,
bundle: Bundle,
operation: Operation,
) => {
try {
const data = {
targetUrl: bundle.targetUrl,
operations: [`${bundle.inputData.nameSingular}.${operation}`],
};
const result = await requestDb(
z,
bundle,
`mutation createWebhook {createWebhook(data:{${handleQueryParams(
data,
)}}) {id}}`,
);
return result.data.createWebhook;
} catch (e) {
// Remove that catch code when VERSION 0.32 is deployed
// probably removable after 01/11/2024
// (ie: when operations column exists in all active workspace schemas)
const data = {
targetUrl: bundle.targetUrl,
operation: `${bundle.inputData.nameSingular}.${operation}`,
};
const result = await requestDb(
z,
bundle,
`mutation createWebhook {createWebhook(data:{${handleQueryParams(
data,
)}}) {id}}`,
);
return result.data.createWebhook;
}
export const performSubscribe = async (z: ZObject, bundle: Bundle) => {
const data = {
targetUrl: bundle.targetUrl,
operations: [
`${bundle.inputData.nameSingular}.${bundle.inputData.operation}`,
],
};
const result = await requestDb(
z,
bundle,
`mutation createWebhook {createWebhook(data:{${handleQueryParams(
data,
)}}) {id}}`,
);
return result.data.createWebhook;
};
export const performUnsubscribe = async (z: ZObject, bundle: Bundle) => {
@ -62,20 +42,26 @@ export const performUnsubscribe = async (z: ZObject, bundle: Bundle) => {
};
export const perform = (z: ZObject, bundle: Bundle) => {
const record = bundle.cleanedRequest.record;
if (record.createdAt) {
record.createdAt = record.createdAt + 'Z';
const data = {
record: bundle.cleanedRequest.record,
...(bundle.cleanedRequest.updatedFields && {
updatedFields: bundle.cleanedRequest.updatedFields,
}),
};
if (data.record.createdAt) {
data.record.createdAt = data.record.createdAt + 'Z';
}
if (record.updatedAt) {
record.updatedAt = record.updatedAt + 'Z';
if (data.record.updatedAt) {
data.record.updatedAt = data.record.updatedAt + 'Z';
}
if (record.revokedAt) {
record.revokedAt = record.revokedAt + 'Z';
if (data.record.revokedAt) {
data.record.revokedAt = data.record.revokedAt + 'Z';
}
if (record.expiresAt) {
record.expiresAt = record.expiresAt + 'Z';
if (data.record.expiresAt) {
data.record.expiresAt = data.record.expiresAt + 'Z';
}
return [record];
return [data];
};
const getNamePluralFromNameSingular = async (
@ -92,10 +78,9 @@ const getNamePluralFromNameSingular = async (
throw new Error(`Unknown Object Name Singular ${nameSingular}`);
};
export const listSample = async (
export const performList = async (
z: ZObject,
bundle: Bundle,
onlyIds = false,
): Promise<ObjectData[]> => {
const nameSingular = bundle.inputData.nameSingular;
const namePlural = await getNamePluralFromNameSingular(
@ -103,19 +88,13 @@ export const listSample = async (
bundle,
nameSingular,
);
const result: { [key: string]: string }[] = await requestDbViaRestApi(
z,
bundle,
namePlural,
);
if (onlyIds) {
return result.map((res) => {
return {
id: res.id,
};
});
}
return result;
const results = await requestDbViaRestApi(z, bundle, namePlural);
return results.map((result) => ({
record: result,
...(bundle.inputData.operation === DatabaseEventAction.UPDATED && {
updatedFields: Object.keys(result).filter((key) => key !== 'id')?.[0] || [
'updatedField',
],
}),
}));
};