update delete behavior when have thread and beautify code

This commit is contained in:
grimhilt
2023-05-07 23:27:08 +02:00
parent 843659b495
commit f7c95b3a36
8 changed files with 239 additions and 72 deletions

46
back/mails/room/Room.ts Normal file
View 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;
}
}