import { FastifySchema , Response , ResponseError } from "../../../../types/Server" ; import { Status } from "../../../../constants/Project" ; import { ErrorCode } from "../../../../ErrorCode" ; import { roomIsIdle , roomIsRunning } from "../utils/RoomStatus" ; import { Controller } from "../../../../decorator/Controller" ; import { AbstractController , ControllerClassParams } from "../../../../abstract/controller" ; import { ServiceRoom , ServiceRoomUser } from "../../../service" ; import { FlatError } from "../../../../error/FlatError" ; import { ControllerError } from "../../../../error/ControllerError" ; import { RoomModel } from "../../../../model/room/Room" ; import { whiteboardBanRoom } from "../../../utils/request/whiteboard/WhiteboardRequest" ; import { parseError } from "../../../../logger" ; import { getConnection } from "typeorm" ; @ Controller < RequestType , ResponseType > ( { method : "post" , path : "room/cancel/ordinary" , auth : true , } ) export class CancelOrdinary extends AbstractController < RequestType , ResponseType > { public static readonly schema : FastifySchema < RequestType > = { body : { type : "object" , required : [ "roomUUID" ] , properties : { roomUUID : { type : "string" , format : "uuid-v4" , } , } , } , } ; private readonly svc : { room : ServiceRoom ; roomUser : ServiceRoomUser ; } ; public constructor ( params ) { super ( params ) ; this . svc = { room : new ServiceRoom ( this . body . roomUUID , this . userUUID ) , roomUser : new ServiceRoomUser ( this . body . roomUUID , this . userUUID ) , } ; } public async execute ( ) { const roomInfo = await this . svc . room . assertInfo ( [ "room_status" , "owner_uuid" , "periodic_uuid" , "whiteboard_room_uuid" , "region" , ] ) ; const { owner_uuid , room_status , region , whiteboard_room_uuid } = roomInfo ; this . assertRoomValid ( roomInfo ) ; await getConnection ( ) . transaction ( async ( t ) => { const commands = [ ] ; commands . push ( this . svc . roomUser . removeSelf ( t ) ) ; if ( this . userIsRoomOwner ( owner_uuid ) && roomIsIdle ( room_status ) ) { commands . push ( this . svc . room . remove ( t ) ) ; await Promise . all ( commands ) ; whiteboardBanRoom ( region , whiteboard_room_uuid ) . catch ( err => { this . logger . warn ( "ban room failed" , parseError ( err ) ) ; } ) ; return ; } else { await Promise . all ( commands ) ; } } ) ; return { status : Status . Success , data : { } , } ; } public errorHandler ( error ) { return this . autoHandlerError ( error ) ; } private assertRoomValid ( roomInfo ) { if ( roomInfo . periodic_uuid !== "" ) { throw new ControllerError ( ErrorCode . NotPermission ) ; } if ( this . userIsRoomOwner ( roomInfo . owner_uuid ) && roomIsRunning ( roomInfo . room_status ) ) { throw new ControllerError ( ErrorCode . RoomIsRunning ) ; } } private userIsRoomOwner ( owner_uuid ) { return owner_uuid === this . userUUID ; } } interface RequestType { body : { roomUUID : string ; } ; } interface ResponseType { }