47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { deleteRoom, getRoomNbMessageAndThread, getRoomOnMessageId } from "../../db/Room-db";
|
|
|
|
export default class Room {
|
|
private _roomId: number;
|
|
|
|
constructor() {}
|
|
|
|
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;
|
|
}
|
|
|
|
// check if the room have threads or messages
|
|
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;
|
|
}
|
|
}
|