update delete behavior when have thread and beautify code
This commit is contained in:
46
back/mails/room/Room.ts
Normal file
46
back/mails/room/Room.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { deleteRoom, getRoomNbMessageAndThread, getRoomOnMessageId } from "../../db/Room-db";
|
||||
|
||||
export default class Room {
|
||||
private _roomId: number;
|
||||
constructor() {
|
||||
this._roomId;
|
||||
}
|
||||
|
||||
setRoomId(roomId: number): Room {
|
||||
this._roomId = roomId;
|
||||
return this;
|
||||
}
|
||||
|
||||
get roomId(): number {
|
||||
return this._roomId;
|
||||
}
|
||||
|
||||
async setRoomIdOnMessageId(messageId: number): Promise<Room> {
|
||||
const res = await getRoomOnMessageId(messageId);
|
||||
if (res.length == 0) {
|
||||
throw "Message has no room";
|
||||
}
|
||||
this._roomId = res[0].room_id;
|
||||
return this;
|
||||
}
|
||||
|
||||
async shouldDelete(): Promise<boolean> {
|
||||
if (!this._roomId) {
|
||||
throw "shouldDelete needs to have a roomId set.";
|
||||
}
|
||||
const res = await getRoomNbMessageAndThread(this._roomId);
|
||||
if (res.length === 0) return true;
|
||||
if (res[0].nbMessage === 0 && res[0].nbThread === 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async delete(): Promise<Room> {
|
||||
if (!this._roomId) {
|
||||
throw "shouldDelete needs to have a roomId set.";
|
||||
}
|
||||
await deleteRoom(this._roomId);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user