Skip to content

After Change Hook

Create a new file in stack9/workflow-functions/custom called send-email-after-customer-saved.js

@prop context - object - context

module.exports = {
  exec: async (context) => {
    const { entity, oldEntity } = context;
    const isNewCustomer = oldEntity.id != entity.id;

    if (isNewCustomer) {
      sendMail(entity.to, 'welcome ...');
    }
  },
  timeout: 5000,
};

or in TypeScript

import { CustomFunction } from "@april9/stack9-sdk"

// stack9/server/workflow-functions/custom/send-email-after-customer-saved.ts
class ExampleValidateAndTransform extends CustomFunction {
  constructor(private context: CustomFunctionContext<User>) {
    super();
  }

  async exec(): Promise<void> {
    const { entity, oldEntity } = this.context;
    const isNewCustomer = oldEntity.id != entity.id;

    if (isNewCustomer) {
      sendMail(entity.to, 'welcome ...');
    }
  }
}

export default withCustomFunction(ExampleValidateAndTransform, 5000);

Registration

Register in the entity definition file your want the hook to be executed. eg: stack9/entities/custom/customer.json

  "hooks": [
    {
      "type": "function",
      "hookType": "AfterChange",
      "functionName": "custom/send-email-after-customer-saved"
    },
  ]