diff --git a/src/components/CodeEditor/rowy.d.ts b/src/components/CodeEditor/rowy.d.ts index 35511fb4..d3282d8a 100644 --- a/src/components/CodeEditor/rowy.d.ts +++ b/src/components/CodeEditor/rowy.d.ts @@ -20,4 +20,13 @@ declare namespace rowy { * Gets the secret defined in Google Cloud Secret */ async function getSecret(name: string, v?: string): Promise; + + async function getServiceAccountUser(): Promise<{ + email: string; + emailVerified: boolean; + displayName: string; + photoURL: string; + uid: string; + timestamp: number; + }>; } diff --git a/src/components/TableHeader/Webhooks/Schemas/basic.tsx b/src/components/TableHeader/Webhooks/Schemas/basic.tsx index 86527018..0ea81a15 100644 --- a/src/components/TableHeader/Webhooks/Schemas/basic.tsx +++ b/src/components/TableHeader/Webhooks/Schemas/basic.tsx @@ -44,20 +44,36 @@ export const webhookBasic = { parser: { additionalVariables, extraLibs: parserExtraLibs, - template: `const basicParser: Parser = async({req, db,ref}) => { + template: (table) => `const basicParser: Parser = async({req, db,ref}) => { // request is the request object from the webhook // db is the database object // ref is the reference to collection of the table // the returned object will be added as a new row to the table // eg: adding the webhook body as row const {body} = req; + ${ + table.audit !== false + ? ` + // auditField + const ${ + table.auditFieldCreatedBy ?? "_createdBy" + } = await rowy.getServiceAccountUser() + return { + ...body, + ${table.auditFieldCreatedBy ?? "_createdBy"} + } + ` + : ` return body; + ` + } + }`, }, condition: { additionalVariables, extraLibs: conditionExtraLibs, - template: `const condition: Condition = async({ref,req,db}) => { + template: (table) => `const condition: Condition = async({ref,req,db}) => { // feel free to add your own code logic here return true; }`, diff --git a/src/components/TableHeader/Webhooks/Schemas/sendgrid.tsx b/src/components/TableHeader/Webhooks/Schemas/sendgrid.tsx index e91e804c..72050133 100644 --- a/src/components/TableHeader/Webhooks/Schemas/sendgrid.tsx +++ b/src/components/TableHeader/Webhooks/Schemas/sendgrid.tsx @@ -6,8 +6,13 @@ export const webhookSendgrid = { parser: { additionalVariables: null, extraLibs: null, - template: `const sendgridParser: Parser = async({req, db,ref}) =>{ - + template: ( + table + ) => `const sendgridParser: Parser = async({req, db,ref}) =>{ + // sendgrid webhook events docs : https://docs.sendgrid.com/for-developers/tracking-events/event#event-objects + + // sengrid sends request body with an array of events + // eg: // { // "email": "example@test.com", // "timestamp": 1513299569, @@ -24,7 +29,7 @@ export const webhookSendgrid = { condition: { additionalVariables: null, extraLibs: null, - template: `const condition: Condition = async({ref,req,db}) => { + template: (table) => `const condition: Condition = async({ref,req,db}) => { // feel free to add your own code logic here return true; }`, diff --git a/src/components/TableHeader/Webhooks/Schemas/typeform.tsx b/src/components/TableHeader/Webhooks/Schemas/typeform.tsx index 9d97d2c8..92e124a1 100644 --- a/src/components/TableHeader/Webhooks/Schemas/typeform.tsx +++ b/src/components/TableHeader/Webhooks/Schemas/typeform.tsx @@ -6,15 +6,16 @@ export const webhookTypeform = { parser: { additionalVariables: null, extraLibs: null, - - template: `const typeformParser: Parser = async({req, db,ref}) =>{ + template: ( + table + ) => `const typeformParser: Parser = async({req, db,ref}) =>{ // this reduces the form submission into a single object of key value pairs // eg: {name: "John", age: 20} // ⚠️ ensure that you have assigned ref values of the fields // set the ref value to field key you would like to sync to // docs: https://help.typeform.com/hc/en-us/articles/360050447552-Block-reference-format-restrictions const {submitted_at,hidden,answers} = req.body.form_response - return ({ + const submission = ({ _createdAt: submitted_at, ...hidden, ...answers.reduce((accRow, currAnswer) => { @@ -42,12 +43,30 @@ export const webhookTypeform = { }; } }, {}), - })};`, + }) + + ${ + table.audit !== false + ? ` + // auditField + const ${ + table.auditFieldCreatedBy ?? "_createdBy" + } = await rowy.getServiceAccountUser() + return { + ...submission, + ${table.auditFieldCreatedBy ?? "_createdBy"} + } + ` + : ` + return submission + ` + } + };`, }, condition: { additionalVariables: null, extraLibs: null, - template: `const condition: Condition = async({ref,req,db}) => { + template: (table) => `const condition: Condition = async({ref,req,db}) => { // feel free to add your own code logic here return true; }`, diff --git a/src/components/TableHeader/Webhooks/utils.tsx b/src/components/TableHeader/Webhooks/utils.tsx index 5701f875..7803a548 100644 --- a/src/components/TableHeader/Webhooks/utils.tsx +++ b/src/components/TableHeader/Webhooks/utils.tsx @@ -76,19 +76,16 @@ export const webhookSchemas = { export function emptyWebhookObject( type: WebhookType, - user: IWebhookEditor + user: IWebhookEditor, + table ): IWebhook { return { - name: "Untitled webhook", + name: `${type} webhook`, active: false, endpoint: generateRandomId(), type, - parser: - webhookSchemas[type].parser?.template ?? - webhookSchemas["basic"].parser.template, - conditions: - webhookSchemas[type].condition?.template ?? - webhookSchemas["basic"].condition.template, + parser: webhookSchemas[type].parser?.template(table), + conditions: webhookSchemas[type].condition?.template(table), lastEditor: user, }; } diff --git a/src/constants/externalLinks.ts b/src/constants/externalLinks.ts index 842124bf..8e069285 100644 --- a/src/constants/externalLinks.ts +++ b/src/constants/externalLinks.ts @@ -36,6 +36,7 @@ const WIKI_PATHS = { fieldTypesSupportedFields: "/field-types/supported-fields", fieldTypesDerivative: "/field-types/derivative", fieldTypesConnectTable: "/field-types/connect-table", + fieldTypesConnectService: "/field-types/connect-service", fieldTypesAction: "/field-types/action", fieldTypesAdd: "/field-types/add",