Skip to content

Commit bcf8f02

Browse files
committed
Storage contract and mocked storage created
1 parent 0396e69 commit bcf8f02

14 files changed

Lines changed: 112 additions & 240 deletions

File tree

‎back/.babelrc‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"alias": {
2222
"core": "./src/core",
2323
"dals": "./src/dals",
24-
"pods": "./src/pods"
24+
"pods": "./src/pods",
25+
"messages": "./src/messages",
26+
"business": "./src/business"
2527
}
2628
}
2729
]

‎back/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@types/jest": "^26.0.15",
4444
"@types/mongoose": "^5.7.37",
4545
"@types/socket.io": "^2.1.10",
46+
"babel-plugin-module-resolver": "^4.0.0",
4647
"cross-env": "^7.0.2",
4748
"jest": "^26.6.1",
4849
"nodemon": "^2.0.4",

‎back/src/app.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ app.listen(envConstants.PORT, () => {
4343

4444
// whenever a user connects on port 3000 via
4545
// a websocket, log that a user has connected
46-
io.on('connection', function (socket: Socket) {
46+
io.on('connection', async function (socket: Socket) {
4747
// WATCH OUT !! Reconnect is not implemented
4848
// In the connection input processing, we should
4949
// check if connectionId matches with userId and RoomId
@@ -63,15 +63,15 @@ io.on('connection', function (socket: Socket) {
6363

6464
// TODO encapuslate this to processInputMessage
6565
if (trainertoken) {
66-
outputMessageCollection = processInputMessage(socketInfo, {
66+
outputMessageCollection = await processInputMessage(socketInfo, {
6767
type: InputMessageTypes.ESTABLISH_CONNECTION_TRAINER,
6868
payload: {
6969
room,
7070
trainertoken,
7171
},
7272
});
7373
} else {
74-
outputMessageCollection = processInputMessage(socketInfo, {
74+
outputMessageCollection = await processInputMessage(socketInfo, {
7575
type: InputMessageTypes.ESTABLISH_CONNECTION_STUDENT,
7676
payload: {
7777
room,
@@ -81,15 +81,15 @@ io.on('connection', function (socket: Socket) {
8181

8282
processOutputMessageCollection(socketInfo, outputMessageCollection);
8383

84-
socket.on('message', function (message: any) {
84+
socket.on('message', async function (message: any) {
8585
console.log(message);
8686
if (message && message.type) {
87-
const outputMessageCollection: Action[] = processInputMessage(
87+
const outputMessageCollection: Action[] = await processInputMessage(
8888
socketInfo,
8989
message
9090
);
9191

92-
processOutputMessageCollection(socketInfo, outputMessageCollection);
92+
await processOutputMessageCollection(socketInfo, outputMessageCollection);
9393
}
9494
});
9595
});

‎back/src/dals/session/repository/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import * as mockRepository from './session.mock';
44
import * as repository from './session.mock';
55
import { SessionRepository } from './session.contract';
66

7-
export const userRepository: SessionRepository = envConstants.isMockRepository
7+
export const sessionRepository: SessionRepository = envConstants.isMockRepository
88
? mockRepository
99
: repository;
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { UserSession, ConnectSessionInfo } from 'dals/session';
1+
import { RoomInfo } from 'dals/room';
2+
import { ConnectSessionInfo } from 'dals/session';
23

34
export interface SessionRepository {
4-
isRoomAvailable: (room: string) => Promise<boolean>;
5-
addNewUser: (connectionId: string, connectSession: ConnectSessionInfo) => Promise<boolean>;
6-
saveRoomInfo:
7-
getRoomContent:
85
isTrainerUser: (connectionId: string) => Promise<boolean>;
9-
isExistingConnection:
6+
saveRoomInfo: (newRoomInfo: RoomInfo, action: string) => Promise<void>;
7+
isExistingConnection: (connectionId: string) => Promise<boolean>;
8+
isRoomAvailable: (room: string) => Promise<boolean>;
9+
addNewUser: (connectionId: string, connectSession: ConnectSessionInfo) => Promise<void>;
1010
getRoomFromConnectionId: (connectionId: string) => Promise<string>;
11-
getRoomIndexByName:
12-
updateRoomContent:
13-
insertRoom:
11+
getRoomContent:(room: string) => Promise<string>;
1412
}
1513

‎back/src/dals/session/repository/session.mock.ts‎

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import produce from 'immer';
2-
import { InputMessageTypes } from '../../../messages';
3-
import { ConnectSessionInfo, RoomInfo, UserSession} from 'dals'
2+
import { InputMessageTypes } from 'messages';
3+
4+
interface ConnectSessionInfo {
5+
room: string;
6+
trainerToken: string;
7+
isTrainer: boolean;
8+
}
9+
10+
interface RoomInfo {
11+
room: string;
12+
content: string;
13+
}
14+
15+
interface UserSession extends ConnectSessionInfo {
16+
connectionId: string;
17+
}
418

519
let userCollectionSession: UserSession[] = [];
620
let roomCollectionSession: RoomInfo[] = [];
721

8-
export const isRoomAvailable = (room: string): boolean =>
22+
export const isRoomAvailable = async (room: string): Promise<boolean> =>
923
!userCollectionSession.find((session) => session.room === room);
1024

11-
export const addNewUser = (
12-
connectionId: string,
13-
{ room, trainerToken, isTrainer }: ConnectSessionInfo
14-
) => {
15-
const currentUsers = userCollectionSession.length;
25+
export const addNewUser = async (connectionId: string, { room, trainerToken, isTrainer }: ConnectSessionInfo): Promise<void> => {
1626
userCollectionSession = [
1727
...userCollectionSession,
1828
{
@@ -22,49 +32,51 @@ export const addNewUser = (
2232
isTrainer,
2333
},
2434
];
25-
return userCollectionSession.length > currentUsers;
2635
};
2736

28-
export const saveRoomInfo = (newRoomInfo: RoomInfo, action: string) => {
29-
const roomIndex: number = getRoomIndexByName(newRoomInfo);
37+
export const saveRoomInfo = async (newRoomInfo: RoomInfo, action: string): Promise<void> => {
38+
const roomIndex: number = await getRoomIndexByName(newRoomInfo);
3039
const content: string = newRoomInfo.content;
3140
roomCollectionSession =
3241
roomIndex === -1
33-
? insertRoom(newRoomInfo)
34-
: updateRoomContent(roomIndex, content, action);
42+
? await insertRoom(newRoomInfo)
43+
: await updateRoomContent(roomIndex, content, action);
3544
};
3645

37-
export const getRoomContent = (room: string) => {
46+
export const getRoomContent = async (room: string): Promise<string> => {
3847
const roomInfo = roomCollectionSession.find(
3948
(roomInfo: RoomInfo) => roomInfo.room === room
4049
);
4150

4251
return roomInfo ? roomInfo.content : '';
4352
};
4453

45-
export const isTrainerUser = (connectionId: string) => {
46-
!userCollectionSession.find((session) => session.connectionId === connectionId && session.isTrainer);
54+
export const isTrainerUser = async (connectionId: string): Promise<boolean> => {
55+
const session = userCollectionSession.find(
56+
(session) => session.connectionId === connectionId && session.isTrainer
57+
);
58+
return session ? true : false;
4759
};
4860

49-
export const isExistingConnection = (connectionId: string) =>
61+
export const isExistingConnection = async (connectionId: string): Promise<boolean> =>
5062
userCollectionSession.findIndex(
5163
(session) => session.connectionId === connectionId
5264
) !== -1;
5365

54-
export const getRoomFromConnectionId = (connectionId: string) => {
66+
export const getRoomFromConnectionId = async (connectionId: string): Promise<string> => {
5567
const session = userCollectionSession.find(
5668
(session) => session.connectionId === connectionId
5769
);
5870
return session ? session.room : '';
5971
};
6072

61-
export const getRoomIndexByName = (newRoomInfo: RoomInfo): number => {
73+
const getRoomIndexByName = async (newRoomInfo: RoomInfo): Promise<number> => {
6274
return roomCollectionSession.findIndex(
6375
(roomInfo: RoomInfo) => roomInfo.room === newRoomInfo.room
6476
);
6577
};
6678

67-
export const updateRoomContent = (index: number, content: string, action:string) => {
79+
const updateRoomContent = async (index: number, content: string, action:string): Promise<RoomInfo[]> => {
6880
return produce(roomCollectionSession, (draftState: RoomInfo[]) => {
6981
switch (action) {
7082
case InputMessageTypes.TRAINER_APPEND_TEXT:
@@ -77,7 +89,7 @@ export const updateRoomContent = (index: number, content: string, action:string)
7789
});
7890
};
7991

80-
export const insertRoom = (newRoomInfo: RoomInfo) => {
92+
const insertRoom = async (newRoomInfo: RoomInfo): Promise<RoomInfo[]> => {
8193
return produce(roomCollectionSession, (draftState: RoomInfo[]) => {
8294
draftState.push(newRoomInfo);
8395
});

‎back/src/messages/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from './consts';
1+
export * from './messages.consts';
22
export * from './input-processor';
33
export * from './output-processor';
4-
export * from './model';
4+
export * from './messages.model';

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

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,45 @@
1-
import { InputMessageTypes, OutputMessageTypes } from './consts';
2-
import {
3-
Action,
4-
InputEstablishConnectionMaster,
5-
InputEstablishConnectionPlayer,
6-
SocketInfo,
7-
} from './model';
8-
import {
9-
isTrainerUser,
10-
saveRoomInfo,
11-
isExistingConnection,
12-
isRoomAvailable,
13-
addNewUser,
14-
getRoomFromConnectionId,
15-
} from '../storage';
16-
import { processOuputMessage } from './output-processor';
1+
import { InputMessageTypes, OutputMessageTypes } from './messages.consts';
2+
import { Action, InputEstablishConnectionTrainer, SocketInfo } from './messages.model';
3+
import {sessionRepository } from 'dals';
4+
const {addNewUser, getRoomFromConnectionId, isExistingConnection, isRoomAvailable, isTrainerUser, saveRoomInfo} = sessionRepository;
175

18-
export const processInputMessage = (socketInfo: SocketInfo, action: Action): Action[] => {
6+
export const processInputMessage = async (socketInfo: SocketInfo, action: Action): Promise<Action[]> => {
197
let outputActionCollection: Action[] = [];
208
switch (action.type) {
219
case InputMessageTypes.ESTABLISH_CONNECTION_TRAINER:
22-
const payloadECT: InputEstablishConnectionMaster = action.payload;
23-
outputActionCollection = handleEstablishConnectionTrainer(
10+
const payloadECT: InputEstablishConnectionTrainer = action.payload;
11+
outputActionCollection = await handleEstablishConnectionTrainer(
2412
socketInfo,
2513
payloadECT.room,
2614
payloadECT.trainertoken
2715
);
2816
break;
2917
case InputMessageTypes.ESTABLISH_CONNECTION_STUDENT:
30-
const payloadECS: InputEstablishConnectionMaster = action.payload;
31-
outputActionCollection = handleEstablishConnectionStudent(
18+
const payloadECS: InputEstablishConnectionTrainer = action.payload;
19+
outputActionCollection = await handleEstablishConnectionStudent(
3220
socketInfo,
3321
payloadECS.room
3422
);
3523
break;
3624
case InputMessageTypes.TRAINER_APPEND_TEXT:
37-
outputActionCollection = handleTrainerSendText(
25+
outputActionCollection = await handleTrainerSendText(
3826
socketInfo,
3927
action.payload,
4028
InputMessageTypes.TRAINER_APPEND_TEXT
4129
);
4230
break;
4331
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
44-
outputActionCollection = handleTrainerSendText(
32+
outputActionCollection = await handleTrainerSendText(
4533
socketInfo,
4634
action.payload,
4735
InputMessageTypes.TRAINER_SET_FULL_TEXT
4836
);
4937
break;
5038
case InputMessageTypes.STUDENT_REQUEST_FULL_CONTENT:
51-
outputActionCollection = handleRequestGetStudentContent(socketInfo);
39+
outputActionCollection = await handleRequestGetStudentContent(socketInfo);
5240
break;
5341
case InputMessageTypes.TRAINER_REQUEST_FULL_CONTENT:
54-
outputActionCollection = handleRequestGetTrainerContent(socketInfo);
42+
outputActionCollection = await handleRequestGetTrainerContent(socketInfo);
5543
break;
5644
default:
5745
break;
@@ -60,19 +48,19 @@ export const processInputMessage = (socketInfo: SocketInfo, action: Action): Act
6048
return outputActionCollection;
6149
};
6250

63-
const handleRequestGetStudentContent = (socketInfo: SocketInfo) => {
51+
const handleRequestGetStudentContent = async (socketInfo: SocketInfo): Promise<Action[]> => {
6452
return [{ type: OutputMessageTypes.STUDENT_SEND_FULL_CONTENT }];
6553
};
6654

67-
const handleRequestGetTrainerContent = (socketInfo: SocketInfo) => {
55+
const handleRequestGetTrainerContent = async (socketInfo: SocketInfo): Promise<Action[]> => {
6856
return [{ type: OutputMessageTypes.TRAINER_SEND_FULL_CONTENT }];
6957
};
7058

71-
const handleTrainerSendText = (socketInfo: SocketInfo, text: string, action: string) => {
59+
const handleTrainerSendText = async (socketInfo: SocketInfo, text: string, action: string): Promise<Action[]> => {
7260
if (!isTrainerUser(socketInfo.connectionId)) {
7361
return [];
7462
}
75-
const room = getRoomFromConnectionId(socketInfo.connectionId);
63+
const room = await getRoomFromConnectionId(socketInfo.connectionId);
7664
const roomInfo = { room, content: text };
7765
saveRoomInfo(roomInfo, action);
7866
switch(action){
@@ -85,17 +73,14 @@ const handleTrainerSendText = (socketInfo: SocketInfo, text: string, action: str
8573
};
8674
};
8775

88-
const handleEstablishConnectionStudent = (
89-
socketInfo: SocketInfo,
90-
room: string
91-
) => {
76+
const handleEstablishConnectionStudent = async (socketInfo: SocketInfo, room: string): Promise<Action[]> => {
9277
if (!room) {
9378
// Ignore
9479
return [];
9580
}
9681

97-
if (isRoomAvailable(room) || !isExistingConnection(socketInfo.connectionId)) {
98-
addNewUser(socketInfo.connectionId, {
82+
if (await isRoomAvailable(room) || !(await isExistingConnection(socketInfo.connectionId))) {
83+
await addNewUser(socketInfo.connectionId, {
9984
room,
10085
trainerToken: '',
10186
isTrainer: false,
@@ -105,18 +90,14 @@ const handleEstablishConnectionStudent = (
10590
return [{ type: OutputMessageTypes.CONNECTION_ESTABLISHED_STUDENT }];
10691
};
10792

108-
const handleEstablishConnectionTrainer = (
109-
socketInfo: SocketInfo,
110-
room: string,
111-
trainerToken: string
112-
): Action[] => {
93+
const handleEstablishConnectionTrainer = async (socketInfo: SocketInfo, room: string, trainerToken: string): Promise<Action[]> => {
11394
if (!trainerToken || !room) {
11495
// Ignore
11596
return [];
11697
}
11798

118-
if (isRoomAvailable(room) || !isExistingConnection(socketInfo.connectionId)) {
119-
addNewUser(socketInfo.connectionId, {
99+
if (await isRoomAvailable(room) || !(await isExistingConnection(socketInfo.connectionId))) {
100+
await addNewUser(socketInfo.connectionId, {
120101
room,
121102
trainerToken,
122103
isTrainer: !!trainerToken,
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ export interface Action {
1414
messageType?: MessageType;
1515
}
1616

17-
export interface InputEstablishConnectionMaster {
17+
export interface InputEstablishConnectionTrainer {
1818
room: string;
1919
trainertoken: string;
2020
}
21-
22-
export interface InputEstablishConnectionPlayer {
23-
nickname: string;
24-
room: string;
25-
}

0 commit comments

Comments
 (0)