import { InitAction } from "../action/InitAction"; import { Context } from "../context/Context"; 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 { RegionChangeRules } from "../rules/RegionChangeRules"; import { RegionChangeActionParams } from "../action/type/RegionChangeActionParams"; import { LeaveChannelOperation } from "../operation/LeaveChannelOperation"; import { JoinChannelOperation } from "../operation/JoinChannelOperation"; import { ActiveProxyOperation } from "../operation/ActiveProxyOperation"; import { PublishTracksOperation } from "../operation/PublishTracksOperation"; import { SetupRoleOperation } from "../operation/SetupRoleOperation"; import { CustomError } from "../exception/CustomError"; import { MuteAudioOperation } from "../operation/MuteAudioOperation"; import { RegionChangeAction } from "../action/RegionChangeAction"; const parseError = require('parse-error'); export class RegionChangeActionManager extends BaseManager implements IManager { constructor(action : RegionChangeAction, context: Context){ super(action, context); } /*@overwrite*/ public async performAction(): Promise { return new Promise( async (resolve, reject)=>{ if(this.checkKeepOut()){ resolve(this.action.status); return true; } let params : RegionChangeActionParams = Object.assign(this.getAction().getParams()); let validationRules = new RegionChangeRules(params); validationRules.validate([ERuleTypes.IS_CLIENT_INIT, ERuleTypes.IS_VALID_CHANNEL], this.context); if(validationRules.passes){ try { let leaveChannelOperation = new LeaveChannelOperation(this.context); await leaveChannelOperation.handler(); let activeProxyOperation = new ActiveProxyOperation(this.context); await activeProxyOperation.handler({type:3, active: this.context.getDeterminants().getHasProxy()}); let setupRoleOperation = new SetupRoleOperation(this.context); await setupRoleOperation.handler(params); let joinChannelOperation = new JoinChannelOperation(this.context); await joinChannelOperation.handler(params); if( leaveChannelOperation.status == EOperationStatus.PERFORMED && joinChannelOperation.status == EOperationStatus.PERFORMED && activeProxyOperation.status == EOperationStatus.PERFORMED && setupRoleOperation.status == EOperationStatus.PERFORMED ){ validationRules.reset(); validationRules.validate([ERuleTypes.IS_HOST, ERuleTypes.IS_CONNECTED], this.context); if(validationRules.passes){ let publishTracksOperation = new PublishTracksOperation(this.context); await publishTracksOperation.handler(params); let muteAudioOperation = new MuteAudioOperation(this.context); await muteAudioOperation.handler({action:this.context.getDeterminants().getMuted()}); if(publishTracksOperation.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: [], type:EMessageType.INFO }); }else{ let errors : Array = []; if(publishTracksOperation.errors) errors = [...errors, ...publishTracksOperation.errors]; this.action.status = EActionStatus.INCOMPLETE; this.logger.log({ id: uuidv4(), title: this.action.type, body: "Something operations not performaced in action "+this.action.type, action: this.action, errors: publishTracksOperation.errors, type: EMessageType.WARNING }); } }else{ this.action.status = EActionStatus.PERFORMED; this.logger.log({ id: uuidv4(), title: this.action.type, body: "Action "+this.action.type + " performaced", action: this.action, errors: [], type:EMessageType.INFO }); } }else{ let errors : Array = []; if(leaveChannelOperation.errors) errors = [...errors, ...leaveChannelOperation.errors]; if(activeProxyOperation.errors) errors = [...errors, ...activeProxyOperation.errors]; if(joinChannelOperation.errors) errors = [...errors, ...joinChannelOperation.errors]; if(setupRoleOperation.errors) errors = [...errors, ...setupRoleOperation.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); }); } }