Skip to content

Commit 56cb8dc

Browse files
committed
feat: 투표 블럭 서비스 로직 DB와 연동
1 parent 6b23eb6 commit 56cb8dc

File tree

5 files changed

+115
-93
lines changed

5 files changed

+115
-93
lines changed

server/apis/mom/block/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import mongoose from '@db';
22
import { Schema } from 'mongoose';
33
import { BlockType } from '@wabinar/api-types/block';
44

5-
import { Vote } from '../vote/service';
5+
import { Vote } from './vote/service';
66
import { Question } from './question/service';
77

88
interface Block {

server/apis/mom/block/service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import LinkedList from '@wabinar/crdt/linked-list';
22
import { BlockType } from '@wabinar/api-types/block';
33
import blockModel from './model';
44
import { Question } from './question/service';
5+
import { Vote } from './vote/service';
56

67
export const getBlock = async (id: string) => {
78
const block = await blockModel.findOne({ id });
@@ -27,7 +28,7 @@ export const putBlockType = async (id: string, type: BlockType) => {
2728
export const putBlock = async (
2829
id: string,
2930
type: BlockType,
30-
data: LinkedList | Question[],
31+
data: LinkedList | Question[] | Vote,
3132
) => {
3233
switch (type) {
3334
case BlockType.H1:
@@ -37,6 +38,7 @@ export const putBlock = async (
3738
await putTextBlock(id, data as LinkedList);
3839
break;
3940
case BlockType.VOTE:
41+
await putVoteBlock(id, data as Vote);
4042
break;
4143
case BlockType.QUESTION:
4244
await putQuestionBlock(id, data as Question[]);
@@ -53,6 +55,10 @@ const putTextBlock = async (id: string, data: LinkedList) => {
5355
);
5456
};
5557

58+
const putVoteBlock = async (id: string, vote: Vote) => {
59+
await blockModel.updateOne({ id }, { voteProperties: vote });
60+
};
61+
5662
const putQuestionBlock = async (id: string, questions: Question[]) => {
5763
await blockModel.updateOne({ id }, { questionProperties: questions });
5864
};
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { BlockType } from '@wabinar/api-types/block';
2+
import { getBlock, putBlock } from '../service';
3+
4+
export interface Option {
5+
id: number;
6+
text: string;
7+
count: number;
8+
}
9+
10+
interface Options {
11+
[index: number]: Omit<Option, 'id'>;
12+
}
13+
14+
export interface Vote {
15+
title: string;
16+
options: Options;
17+
isDoing: boolean;
18+
participants: { [index: number]: number };
19+
}
20+
21+
export const createVote = async (blockId: string, options: Option[]) => {
22+
const processedOptions = options.reduce((obj, { id, text }) => {
23+
obj[id] = { text, count: 0 };
24+
return obj;
25+
}, {} as Options);
26+
27+
await putBlock(blockId, BlockType.VOTE, {
28+
title: '투표',
29+
options: processedOptions,
30+
isDoing: true,
31+
participants: {},
32+
});
33+
};
34+
35+
export const updateVote = async (
36+
blockId: string,
37+
optionIdString: string,
38+
userId: number,
39+
) => {
40+
const voteBlock = await getBlock(blockId);
41+
42+
const { voteProperties: vote } = voteBlock;
43+
44+
if (!vote) return;
45+
46+
const optionId = Number(optionIdString);
47+
const option = vote.options[optionId];
48+
49+
if (!option) return;
50+
51+
const count = option.count;
52+
53+
if (!vote.participants[userId]) {
54+
vote.options[optionId] = { ...option, count: count + 1 };
55+
}
56+
57+
vote.participants[userId] = optionId;
58+
59+
await putBlock(blockId, BlockType.VOTE, vote);
60+
61+
const participantCount = Object.keys(vote.participants).length;
62+
63+
return participantCount;
64+
};
65+
66+
export const endVote = async (blockId: string) => {
67+
const voteBlock = await getBlock(blockId);
68+
69+
const { voteProperties: vote } = voteBlock;
70+
71+
vote.isDoing = false;
72+
73+
const participantCount = Object.keys(vote.participants).length;
74+
75+
await putBlock(blockId, BlockType.VOTE, vote);
76+
77+
const result = {
78+
options: Object.entries(vote.options).map(([id, rest]) => ({
79+
id,
80+
...rest,
81+
})),
82+
participantCount,
83+
};
84+
85+
return result;
86+
};

server/apis/mom/vote/service.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import * as Vote from '@apis/mom/vote/service';
1+
import {
2+
createVote,
3+
endVote,
4+
Option,
5+
updateVote,
6+
} from '@apis/mom/block/vote/service';
27
import { BLOCK_EVENT } from '@wabinar/constants/socket-message';
38
import { Server, Socket } from 'socket.io';
49

@@ -7,23 +12,29 @@ export default function handleVoteBlock(
712
namespace: string,
813
socket: Socket,
914
) {
10-
socket.on(BLOCK_EVENT.CREATE_VOTE, (options: Vote.Option[]) => {
15+
socket.on(BLOCK_EVENT.CREATE_VOTE, async (blockId, options: Option[]) => {
1116
const momId = socket.data.momId;
12-
const newVote = Vote.createVote(momId, options);
1317

14-
socket.to(momId).emit(BLOCK_EVENT.CREATE_VOTE, newVote);
18+
await createVote(blockId, options);
19+
20+
socket.to(momId).emit(`${BLOCK_EVENT.CREATE_VOTE}-${blockId}`, options);
1521
});
1622

17-
socket.on(BLOCK_EVENT.UPDATE_VOTE, (optionId, userId) => {
23+
socket.on(BLOCK_EVENT.UPDATE_VOTE, async (blockId, optionId, userId) => {
1824
const momId = socket.data.momId;
19-
const participantCount = Vote.updateVote(momId, Number(optionId), userId);
2025

21-
io.of(namespace).to(momId).emit(BLOCK_EVENT.UPDATE_VOTE, participantCount);
26+
const participantCount = await updateVote(blockId, optionId, userId);
27+
28+
io.of(namespace)
29+
.to(momId)
30+
.emit(`${BLOCK_EVENT.UPDATE_VOTE}-${blockId}`, participantCount);
2231
});
2332

24-
socket.on(BLOCK_EVENT.END_VOTE, () => {
33+
socket.on(BLOCK_EVENT.END_VOTE, async (blockId) => {
2534
const momId = socket.data.momId;
26-
const res = Vote.endVote(momId);
27-
io.of(namespace).to(momId).emit(BLOCK_EVENT.END_VOTE, res);
35+
36+
const res = await endVote(blockId);
37+
38+
io.of(namespace).to(momId).emit(`${BLOCK_EVENT.END_VOTE}-${blockId}`, res);
2839
});
2940
}

0 commit comments

Comments
 (0)