Skip to content

Commit fa5f37d

Browse files
committed
Refactored to pods and core structure
1 parent 1f226e9 commit fa5f37d

5 files changed

Lines changed: 30 additions & 41 deletions

File tree

‎back/.vscode/launch.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"runtimeExecutable": null,
1111
"runtimeArgs": ["--nolazy"],
1212
"env": {
13-
"NODE_ENV": "development"
13+
"NODE_ENV": "development",
14+
"MOCK_REPOSITORY": "true"
1415
},
1516
"console": "internalConsole",
1617
"sourceMaps": true,
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { RoomInfo } from 'dals';
22

33
export interface RoomRepositoryContract {
4-
saveRoomInfo: (newRoomInfo: RoomInfo, action: string) => Promise<void>;
4+
saveRoomInfo: (newRoomInfo: RoomInfo, setFullText: boolean) => Promise<void>;
55
isRoomAvailable: (room: string) => Promise<boolean>;
66
getRoomContent:(room: string) => Promise<string>;
77
}
8-
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import produce from 'immer';
2-
import { InputMessageTypes } from 'pods/messages';
32
import { RoomInfo, UserSession } from 'dals'
43

54
let userCollectionSession: UserSession[] = [];
@@ -8,13 +7,13 @@ let roomCollectionSession: RoomInfo[] = [];
87
export const isRoomAvailable = async (room: string): Promise<boolean> =>
98
!userCollectionSession.find((session) => session.room === room);
109

11-
export const saveRoomInfo = async (newRoomInfo: RoomInfo, action: string): Promise<void> => {
10+
export const saveRoomInfo = async (newRoomInfo: RoomInfo, setFullText: boolean): Promise<void> => {
1211
const roomIndex: number = await getRoomIndexByName(newRoomInfo);
1312
const content: string = newRoomInfo.content;
14-
roomCollectionSession =
13+
roomCollectionSession =
1514
roomIndex === -1
1615
? await insertRoom(newRoomInfo)
17-
: await updateRoomContent(roomIndex, content, action);
16+
: await updateRoomContent(roomIndex, content, setFullText);
1817
};
1918

2019
export const getRoomContent = async (room: string): Promise<string> => {
@@ -31,21 +30,23 @@ const getRoomIndexByName = async (newRoomInfo: RoomInfo): Promise<number> => {
3130
);
3231
};
3332

34-
const updateRoomContent = async (index: number, content: string, action:string): Promise<RoomInfo[]> => {
35-
return produce(roomCollectionSession, (draftState: RoomInfo[]) => {
36-
switch (action) {
37-
case InputMessageTypes.TRAINER_APPEND_TEXT:
38-
draftState[index].content += '\n' + content;
39-
break;
40-
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
41-
draftState[index].content = content;
42-
break;
43-
};
33+
const updateRoomContent = async (index: number, content: string, setFullText:boolean): Promise<RoomInfo[]> => {
34+
if (!Boolean(content))
35+
return roomCollectionSession;
36+
37+
const newRoomCollectionSession = produce(roomCollectionSession, (draftState: RoomInfo[]) => {
38+
const someCurrentContent: boolean = Boolean(draftState[index].content);
39+
if (someCurrentContent && !setFullText) {
40+
draftState[index].content += '\n';
41+
}
42+
draftState[index].content = setFullText ? content : (draftState[index].content + content);
4443
});
44+
45+
return newRoomCollectionSession;
4546
};
4647

4748
const insertRoom = async (newRoomInfo: RoomInfo): Promise<RoomInfo[]> => {
4849
return produce(roomCollectionSession, (draftState: RoomInfo[]) => {
4950
draftState.push(newRoomInfo);
5051
});
51-
};
52+
};
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { RoomInfo, RoomContext } from 'dals'
2-
import { InputMessageTypes } from 'pods/messages';
32

43
export const isRoomAvailable = async (room: string): Promise<boolean> => {
54
const roomAvailable = !(await RoomContext.exists({ room }));
65
return roomAvailable;
76
}
87

9-
export const saveRoomInfo = async (newRoomInfo: RoomInfo, action: string): Promise<void> => {
10-
await isRoomAvailable(newRoomInfo.room) ? await insertRoom(newRoomInfo) : await updateRoomContent(newRoomInfo, action);
8+
export const saveRoomInfo = async (newRoomInfo: RoomInfo, setFullText: boolean): Promise<void> => {
9+
await isRoomAvailable(newRoomInfo.room) ? await insertRoom(newRoomInfo) : await updateRoomContent(newRoomInfo, setFullText);
1110
};
1211

1312
export const getRoomContent = async (room: string): Promise<string> => {
@@ -19,29 +18,16 @@ const getRoomByName = async (room: string): Promise<RoomInfo> => {
1918
return (await RoomContext.findOne({room: room}).lean());
2019
};
2120

22-
const updateRoomContent = async (newRoomInfo: RoomInfo, action:string): Promise<void> => {
21+
const updateRoomContent = async (newRoomInfo: RoomInfo, setFullText:boolean): Promise<void> => {
2322
const room: RoomInfo = await getRoomByName(newRoomInfo.room);
24-
2523
let newContent = "";
26-
27-
if(room) {
28-
newContent = room.content;
24+
if(room && Boolean(room.content) && !setFullText) {
25+
newContent = room.content + '\n';
2926
}
30-
31-
switch (action) {
32-
case InputMessageTypes.TRAINER_APPEND_TEXT:
33-
newContent += '\n' + newRoomInfo.content;
34-
break;
35-
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
36-
newContent = newRoomInfo.content;
37-
break;
38-
};
39-
27+
newContent = setFullText ? newRoomInfo.content : (newContent + newRoomInfo.content);
4028
await RoomContext.updateOne({room: newRoomInfo.room}, {content: newContent});
41-
4229
};
4330

4431
const insertRoom = async (newRoomInfo: RoomInfo): Promise<void> => {
4532
await RoomContext.create(newRoomInfo);
4633
};
47-

‎back/src/pods/messages/processors/input-processor.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ const handleTrainerSendText = async (socketInfo: SocketInfo, text: string, actio
6363
}
6464
const room = await getRoomFromConnectionId(socketInfo.connectionId);
6565
const roomInfo = { room, content: text };
66-
saveRoomInfo(roomInfo, action);
66+
6767
switch(action){
6868
case InputMessageTypes.TRAINER_APPEND_TEXT:
69+
saveRoomInfo(roomInfo, false);
6970
return [{ type: OutputMessageTypes.APPEND_TEXT, payload: text }];
7071
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
72+
saveRoomInfo(roomInfo, true);
7173
return [{ type: OutputMessageTypes.UPDATE_FULL_CONTENT}];
7274
default:
7375
return [];
@@ -98,12 +100,12 @@ const handleEstablishConnectionTrainer = async (socketInfo: SocketInfo, room: st
98100
const roomAvailable: boolean = await isRoomAvailable(room);
99101

100102
if(roomAvailable) {
101-
saveRoomInfo({room, content:""}, "");
103+
saveRoomInfo({room, content:""}, false);
102104
}
103105

104106
if (roomAvailable || !(await isExistingConnection(socketInfo.connectionId))) {
105107
await addNewUser({ connectionId: socketInfo.connectionId, room, trainerToken, isTrainer: !!trainerToken });
106108
socketInfo.socket.join(room);
107109
}
108110
return [{ type: OutputMessageTypes.CONNECTION_ESTABLISHED_TRAINER }];
109-
};
111+
};

0 commit comments

Comments
 (0)