-
Notifications
You must be signed in to change notification settings - Fork 0
Actions Reactions
Benjicatch edited this page Jan 8, 2024
·
3 revisions
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
- Create a dto folder in your action folder.
- In the dto folder, create a class containing the structure of the parameters needed for your action.
- In parameters of the constructor of your service, create at least a EventEmitter2 variable and a ActionService variable.
- 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)
- 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);
- 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.
- 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) {
- 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;
})
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.
- Create a dto folder in your reaction folder.
- In the dto folder, create a class containing the structure of the parameters needed for your reaction.
- 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) {
- 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) {
- In parameters of the constructor of your service, create at least a EventEmitter2 variable if it don't exist.
- 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';
})