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 { ChangeDeviceActionParams } from "../action/type/ChangeDeviceActionParams"; import { ChangeDeviceOperation } from "../operation/ChangeDeviceOperation"; import { ChangeDeviceRules } from "../rules/ChangeDeviceRules"; import { ChangeDeviceAction } from "../action/ChangeDeviceAction"; const parseError = require('parse-error'); export class ChangeDeviceManager extends BaseManager implements IManager { constructor(action : ChangeDeviceAction, context: Context){ super(action, context); } /*@overwrite*/ public async performAction(): Promise { return new Promise( async (resolve, reject)=>{ let params : ChangeDeviceActionParams = Object.assign(this.getAction().getParams()); let validationRules = new ChangeDeviceRules(params); validationRules.validate([ERuleTypes.IS_VALID_DEVICE, ERuleTypes.IS_CLIENT_INIT], this.context); if(validationRules.passes){ try { let active = this.context.getDeterminants().getHasProxy(); let changeDeviceOperation = new ChangeDeviceOperation(this.context); await changeDeviceOperation.handler(params); if(changeDeviceOperation.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(changeDeviceOperation.errors) errors = [...errors, ...changeDeviceOperation.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); }); } }