add auditing to webhook parser

This commit is contained in:
shamsmosowi
2021-12-14 16:12:19 +07:00
parent ac4dd82b92
commit dec4633084
6 changed files with 65 additions and 18 deletions

View File

@@ -20,4 +20,13 @@ declare namespace rowy {
* Gets the secret defined in Google Cloud Secret
*/
async function getSecret(name: string, v?: string): Promise<string | null>;
async function getServiceAccountUser(): Promise<{
email: string;
emailVerified: boolean;
displayName: string;
photoURL: string;
uid: string;
timestamp: number;
}>;
}

View File

@@ -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;
}`,

View File

@@ -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;
}`,

View File

@@ -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;
}`,

View File

@@ -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,
};
}

View File

@@ -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",