import { InitAction } from "../action/InitAction"; import { Context } from "../context/Context"; import { ERuleTypes } from "../rules/enum/ERuleTypes"; import { InitRules } from "../rules/InitRules"; import { IManager } from "./interface/IManager"; import { v4 as uuidv4 } from "uuid"; import { EActionStatus } from "../action/enum/EActionStatus"; import { CreateClientOperation } from "../operation/CreateClientOperation"; import { CreateListenersOperation } from "../operation/CreateListenersOperation"; import { EMessageType } from "../logger/enum/EMessageType"; import { EOperationStatus } from "../operation/enum/EOperationStatus"; import { InitActionParams } from "../action/type/InitActionParams"; import { BaseManager } from "./base/BaseManager"; import { CustomError } from "../exception/CustomError"; const parseError = require('parse-error'); export class InitActionManager extends BaseManager implements IManager { constructor(action : InitAction, context: Context){ super(action, context); } /*@overwrite*/ public async performAction(): Promise { return new Promise( async (resolve, reject)=>{ let params : InitActionParams = Object.assign(this.getAction().getParams()); let validationRules = new InitRules(params); validationRules.validate([ERuleTypes.IS_NOT_INITIATED, ERuleTypes.INIT_PARAM_IS_TRUE], this.context); if(validationRules.passes){ try { let createClientOperation = new CreateClientOperation(this.context); await createClientOperation.handler(params); let createListenersOperation = new CreateListenersOperation(this.context); await createListenersOperation.handler(params); if(createClientOperation.status == EOperationStatus.PERFORMED && createListenersOperation.status == EOperationStatus.PERFORMED){ this.action.status = EActionStatus.PERFORMED; this.logger.log({ id: uuidv4(), title: this.action.type, body: "Action "+this.action.type + " performaced", action: this.action, errors: validationRules.errors, type:EMessageType.INFO }); }else{ let errors : Array = []; if(createClientOperation.errors) errors = [...errors, ...createClientOperation.errors]; if(createListenersOperation.errors) errors = [...errors, ...createListenersOperation.errors]; this.action.status = EActionStatus.FAILED; this.logger.log({ id: uuidv4(), title: this.action.type, body: "Something operations not performaced in action "+this.action.type, action: this.action, errors: errors, type: EMessageType.ERROR }); } } catch (error) { let errors = parseError(error); this.action.status = EActionStatus.FAILED; this.logger.log({ id: uuidv4(), title: this.action.type, body: "Something operations not performaced in action "+this.action.type, action: this.action, errors: errors, type: EMessageType.ERROR }); } }else{ this.action.status = EActionStatus.FAILED; this.logger.log({ id: uuidv4(), title: this.action.type, body: "Action "+this.action.type + " not performaced", action: this.action, errors: validationRules.errors, type: EMessageType.ERROR }); } resolve(this.action.status); }); } }