Skip to content

Actions Reactions

Benjicatch edited this page Jan 8, 2024 · 3 revisions

Actions

Add service

Inside the Action folder in the server, check if a folder of your service who is gonna contain the action already exist. If not, create a module and a service in the folder action. :warning: :warning: import your module in the AppModule, not the ActionModule

Add a action

  1. Create a dto folder in your action folder.
  2. In the dto folder, create a class containing the structure of the parameters needed for your action.
  3. In parameters of the constructor of your service, create at least a EventEmitter2 variable and a ActionService variable.
  4. Put the logic of your action in the service file. The parameters MUST BE the structure of your dto and the id of the action.
  • For example: async ActionExecuteTime(structInfo: TimeDto, actionId: number)
  1. If you have data to save for the logic of your action, please implement this code, this will update your variable you need to save:
const action = await this.actionService.getAction(actionId);
let saveParams = action.saveParams as unknown as // your type of structure for save data;
...
// your logic of the action
...
await this.actionService.updateAction(actionId, saveParams);
  1. If your action is completed and need to lauch the reaction, call the function executeReaction from the ActionService variable. It should contain the name of the action and the Dto structure.
  2. Add a decorator @OnEvent() with the name of your action inside. The decorator should be in front of the reaction function.
  • For example:
@OnEvent('ExecuteTime')
async ActionExecuteTime(structInfo: TimeDto, actionId: number) {
  1. Inside the constructor, add this line: this.eventEmitter.on("{ name of your action }.struct", (struct: { name of your DtoParameters }) => {
  • Inside this function, fill your dto stuct with random value, for example:
this.eventEmitter.on("ExecuteTime.struct", (struct: TimeDto) => {
   struct.MinutesTime = 0;
})

⚠️ ⚠️ If the name of your action is not inside the about.json, add it on the good service in the action list

Reactions

Add service

Inside the reaction folder in the server, check if a folder of your service who is gonna contain the reaction already exist. If not, create a module and a service in the folder reaction.

Add a reaction

  1. Create a dto folder in your reaction folder.
  2. In the dto folder, create a class containing the structure of the parameters needed for your reaction.
  3. Put the logic of your reaction in the service file. The parameters MUST BE the structure of your dto and the randomToken of the user.
  • For example: async sendMail(structInfo: SendMailDto, randomToken: string) {
  1. Add a decorator @OnEvent() with the name of your reaction inside. The decorator should be in front of the reaction function.
  • For example:
@OnEvent('sendEmail')
async sendMail(structInfo: SendMailDto, randomToken: string) {
  1. In parameters of the constructor of your service, create at least a EventEmitter2 variable if it don't exist.
  2. Inside the constructor, add this line: this.eventEmitter.on("{ name of your reaction }.struct", (struct: { name of your DtoParameters }) => {
  • Inside this function, fill your dto stuct with random value, for example:
this.eventEmitter.on("sendEmail.struct", (struct: SendMailDto) => {
      struct.to = 'string';
      struct.from = 'string';
      struct.subject = 'string';
      struct.template = 'string';
      struct.code = 'string';
})

⚠️ ⚠️ If the name of your reaction is not inside the about.json, add it on the good service in the reaction list

Clone this wiki locally