Skip to content

Commit 11a0c4c

Browse files
authored
server pushcode 로직 수정 (#82)
* feat(core/server): pushCode에 로직 추가 * feat(server): pushCode시에 anonymous인지 검사하는 로직 추가
1 parent 202a287 commit 11a0c4c

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

core/server/src/pushCode.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { pushCodeRequestValidate, PushCodeResponse } from '@codepocket/schema';
2-
import { CodeInfo, PocketToken, PushCodeParams } from 'types';
3-
1+
import { pushCodeRequestValidate, PushCodeResponse } from '@pocket/schema';
2+
import { CodeInfo, CodeName, PocketToken, PushCodeParams } from 'types';
43
import { postMessageToSlack, SlackConfig, uploadCodeToSlack } from './slack';
54

65
interface PushCodeType<Response> {
76
validateErrorFunc: () => Response;
87
successResponseFunc: (body: PushCodeResponse) => Response;
98
slackConfig?: SlackConfig;
109
getAuthorName: ({ pocketToken }: PocketToken) => Promise<string>;
10+
checkAnonymousCode: (params: CodeName) => Promise<void>;
1111
isExistCode: ({ codeName, codeAuthor }: CodeInfo) => Promise<boolean>;
1212
pushCode: (obj: PushCodeParams) => Promise<void>;
1313
}
@@ -18,6 +18,8 @@ export default async <T, Response>(request: T, modules: PushCodeType<Response>)
1818

1919
const codeAuthor = await modules.getAuthorName({ pocketToken });
2020

21+
if (isAnonymous) await modules.checkAnonymousCode({ codeName });
22+
2123
const isAlreadyPushedCode = await modules.isExistCode({ codeName, codeAuthor });
2224

2325
const slackInfo = modules.slackConfig

server/src/dbModule/code.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ export const getCodeCode =
9393
return code.code;
9494
};
9595

96+
export const checkAnonymousCode =
97+
(server: FastifyInstance) =>
98+
async ({ codeName }: Types.CodeName) => {
99+
const [checkAnonymousCodeError, code] = await to(
100+
(async () => await server.store.Code.findOne({ codeName, isAnonymous: true }))(),
101+
);
102+
if (!checkAnonymousCodeError) throw new CustomResponse({ customStatus: 5000 });
103+
if (code) throw new CustomResponse({ customStatus: 4009 });
104+
};
105+
96106
export const pushCode =
97107
(server: FastifyInstance) =>
98108
async ({

server/src/router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export default fp(async (server: FastifyInstance, _: FastifyPluginOptions) => {
103103
CHAPTER_FRONTED_CHANNEL_ID: env.CHAPTER_FRONTED_CHANNEL_ID,
104104
CODEPOCKET_CHANNEL_ID: env.CODEPOCKET_CHANNEL_ID,
105105
},
106+
checkAnonymousCode: CodeModule.checkAnonymousCode(server),
106107
getAuthorName: UserModule.getAuthorName(server),
107108
isExistCode: CodeModule.isExistCode(server),
108109
pushCode: CodeModule.pushCode(server),

server/src/utils/responseHandler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const CustomResponseStatuses = {
2020
4006: { message: '삭제할 코드가 없어요', status: 404 },
2121
4007: { message: '없는 코드를 지정하셨어요', status: 404 },
2222
4008: { message: '존재하지 않는 코드예요', status: 404 },
23+
4009: {
24+
message: '이미 같은 이름의 익명 코드가 존재해요. 이름을 바꾸시거나 실명으로 올려주세요',
25+
status: 400,
26+
},
2327

2428
5000: { message: '데이터베이스에 문제가 생겼어요', status: 500 },
2529
5001: { message: '슬랙에 문제가 생겼어요', status: 500 },

0 commit comments

Comments
 (0)