Skip to content

Commit 2fb534a

Browse files
committed
refactor: endVote와 getVoteResult 불필요한 DB 접근 제거
1 parent b4191c2 commit 2fb534a

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

server/apis/mom/block/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { BlockType } from '@wabinar/api-types/block';
55
import { Vote } from './vote/service';
66
import { Question } from './question/service';
77

8-
interface Block {
8+
export interface Block {
99
id: string;
1010
type: BlockType;
1111
head: Object;

server/apis/mom/block/service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ const putVoteBlock = async (id: string, vote: Vote) => {
5959
await blockModel.updateOne({ id }, { voteProperties: vote });
6060
};
6161

62+
export const putVoteBlockStatus = async (id: string, isDoing: boolean) => {
63+
const block = await blockModel.findOneAndUpdate(
64+
{ id },
65+
{ $set: { 'voteProperties.isDoing': isDoing } },
66+
{ new: true },
67+
);
68+
69+
return block;
70+
};
71+
6272
const putQuestionBlock = async (id: string, questions: Question[]) => {
6373
await blockModel.updateOne({ id }, { questionProperties: questions });
6474
};

server/apis/mom/block/vote/service.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BlockType } from '@wabinar/api-types/block';
2-
import { getBlock, putBlock } from '../service';
3-
2+
import { Block } from '../model';
3+
import { getBlock, putBlock, putVoteBlockStatus } from '../service';
44
export interface Option {
55
id: number;
66
text: string;
@@ -70,18 +70,12 @@ export const updateVote = async (
7070
};
7171

7272
export const endVote = async (blockId: string) => {
73-
const voteBlock = await getBlock(blockId);
74-
75-
const { voteProperties: vote } = voteBlock;
76-
77-
vote.isDoing = false;
78-
79-
await putBlock(blockId, BlockType.VOTE, vote);
80-
81-
return vote;
73+
return await putVoteBlockStatus(blockId, false);
8274
};
8375

84-
export const getVoteResult = (vote: Vote) => {
76+
export const getVoteResult = async (block: Block) => {
77+
const { voteProperties: vote } = block;
78+
8579
const participantCount = Object.keys(vote.participants).length;
8680

8781
const options = Object.entries(vote.options).map(([id, rest]) => ({

server/socket/Mom/handleVoteBlock.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ export default function handleVoteBlock(
3434
socket.on(BLOCK_EVENT.END_VOTE, async (blockId) => {
3535
const momId = socket.data.momId;
3636

37-
const vote = await endVote(blockId);
37+
const block = await endVote(blockId);
3838

39-
const voteResult = getVoteResult(vote);
39+
const res = await getVoteResult(block);
4040

41-
io.of(namespace)
42-
.to(momId)
43-
.emit(`${BLOCK_EVENT.END_VOTE}-${blockId}`, voteResult);
41+
io.of(namespace).to(momId).emit(`${BLOCK_EVENT.END_VOTE}-${blockId}`, res);
4442
});
4543
}

0 commit comments

Comments
 (0)