import { InitAction } from "../action/InitAction"; import { Context } from "../context/Context"; import { Logger } from "../logger/Logger"; import { ERuleTypes } from "../rules/enum/ERuleTypes"; import { IManager } from "./interface/IManager"; import { v4 as uuidv4 } from "uuid"; import { EActionStatus } from "../action/enum/EActionStatus"; import { EMessageType } from "../logger/enum/EMessageType"; import { EOperationStatus } from "../operation/enum/EOperationStatus"; import { BaseManager } from "./base/BaseManager"; import { ActiveProxyActionParams } from "../action/type/ActiveProxyActionParams"; import { ActiveProxyOperation } from "../operation/ActiveProxyOperation"; import { ActiveProxyRules } from "../rules/ActiveProxyRules"; import { LeaveChannelOperation } from "../operation/LeaveChannelOperation"; import { CustomError } from "../exception/CustomError"; import { ActiveProxyAction } from "../action/ActiveProxyAction"; const parseError = require('parse-error'); export class ActiveProxyActionManager extends BaseManager implements IManager { constructor(action : ActiveProxyAction, context: Context){ super(action, context); } /*@overwrite*/ public async performAction(): Promise { return new Promise( async (resolve, reject)=>{ let params : ActiveProxyActionParams = Object.assign(this.getAction().getParams()); let validationRules = new ActiveProxyRules(params); validationRules.validate([ERuleTypes.PROXY_VALID_TYPE, ERuleTypes.IS_CLIENT_INIT], this.context); if(validationRules.passes){ try { let active = this.context.getDeterminants().getHasProxy(); params.active = !active; let leaveChannelOperation = new LeaveChannelOperation(this.context); await leaveChannelOperation.handler(); let activeProxyOperation = new ActiveProxyOperation(this.context); await activeProxyOperation.handler(params); if(activeProxyOperation.status == EOperationStatus.PERFORMED && leaveChannelOperation.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(leaveChannelOperation.errors) errors = [...errors, ...leaveChannelOperation.errors]; if(activeProxyOperation.errors) errors = [...errors, ...activeProxyOperation.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); }); } }