Skip to content

Commit fea143f

Browse files
authored
Merge pull request #129 from Lemoncode/fix-upsert-room-content
upsert fix
2 parents 5789d50 + a52cc06 commit fea143f

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { RoomInfo, RoomContext } from 'dals'
1+
import { RoomInfo, RoomContext } from 'dals';
22

33
export const isRoomAvailable = async (room: string): Promise<boolean> => {
44
const roomAvailable = !(await RoomContext.exists({ room }));
55
return roomAvailable;
6-
}
7-
8-
export const saveRoomInfo = async (newRoomInfo: RoomInfo, setFullText: boolean): Promise<void> => {
9-
await isRoomAvailable(newRoomInfo.room) ? await insertRoom(newRoomInfo) : await updateRoomContent(newRoomInfo, setFullText);
6+
};
7+
8+
export const saveRoomInfo = async (
9+
newRoomInfo: RoomInfo,
10+
setFullText: boolean
11+
): Promise<void> => {
12+
(await isRoomAvailable(newRoomInfo.room))
13+
? await insertRoom(newRoomInfo)
14+
: await updateRoomContent(newRoomInfo, setFullText);
1015
};
1116

1217
export const getRoomContent = async (room: string): Promise<string> => {
@@ -15,19 +20,35 @@ export const getRoomContent = async (room: string): Promise<string> => {
1520
};
1621

1722
const getRoomByName = async (room: string): Promise<RoomInfo> => {
18-
return (await RoomContext.findOne({room: room}).lean());
23+
return await RoomContext.findOne({ room: room }).lean();
1924
};
2025

21-
const updateRoomContent = async (newRoomInfo: RoomInfo, setFullText:boolean): Promise<void> => {
26+
const appendRoomContent = async (newRoomInfo: RoomInfo): Promise<void> => {
2227
const room: RoomInfo = await getRoomByName(newRoomInfo.room);
23-
let newContent = "";
24-
if(room && Boolean(room.content) && !setFullText) {
25-
newContent = room.content + '\n';
28+
29+
if (room) {
30+
const newContent = `${room.content}\n${newRoomInfo.content}`;
31+
await RoomContext.updateOne(
32+
{ room: newRoomInfo.room },
33+
{ content: newContent }
34+
);
2635
}
27-
newContent = setFullText ? newRoomInfo.content : (newContent + newRoomInfo.content);
28-
await RoomContext.updateOne({room: newRoomInfo.room}, {content: newContent});
2936
};
30-
37+
38+
const replaceRoomContent = async (newRoomInfo: RoomInfo): Promise<void> =>
39+
await RoomContext.updateOne(
40+
{ room: newRoomInfo.room },
41+
{ content: newRoomInfo.content }
42+
);
43+
44+
const updateRoomContent = async (
45+
newRoomInfo: RoomInfo,
46+
setFullText: boolean
47+
): Promise<void> =>
48+
setFullText
49+
? replaceRoomContent(newRoomInfo)
50+
: appendRoomContent(newRoomInfo);
51+
3152
const insertRoom = async (newRoomInfo: RoomInfo): Promise<void> => {
3253
await RoomContext.create(newRoomInfo);
3354
};

0 commit comments

Comments
 (0)