diff --git a/src/repositories/shareRepository.js b/src/repositories/shareRepository.js new file mode 100644 index 0000000000000000000000000000000000000000..b6c9cbe94a73d1829b447ab5c2c0fb2b2164704a --- /dev/null +++ b/src/repositories/shareRepository.js @@ -0,0 +1,30 @@ +import pool from '../db.js'; + +const ShareRepository = { + async createCombinationUuid(combinationId) { + const resp = await pool.query( + `INSERT INTO shares (combination_id) + VALUES ($1) + RETURNING uuid;`, + [combinationId] + ); + const [uuid] = resp.rows; + return uuid; + }, + async getCombinationByUuid(uniqueId) { + const resp = await pool.query( + `SELECT + shares.combination_id, + array(SELECT relations.part_id FROM relations WHERE relations.combination_id = shares.combination_id) as parts, + shares.created_at, + (SELECT created_at FROM combinations WHERE combinations.id = shares.combination_id) as verified_at + FROM shares + WHERE uuid=$1;`, + [uniqueId] + ); + const [row] = resp.rows; + return row; + }, +}; + +export default ShareRepository; diff --git a/src/routes/my.js b/src/routes/my.js index f95edeb975ab5778db977e64c5eb84da44969078..ef5e99e2798d31b240d1440e3a4c627934f821a9 100644 --- a/src/routes/my.js +++ b/src/routes/my.js @@ -43,6 +43,18 @@ myRouter.patch( }) ); +myRouter.post( + '/pc/:combinationId/uuid', + authMiddleware, + wrapAsync(async (req, res) => { + const { combinationId } = req.params; + const userId = req.user?.userId; + + const uuid = await myService.createCombinationUuid(userId, combinationId); + res.sendResponse('', 200, { uuid }); + }) +); + myRouter.get( '/registration-code', authMiddleware, diff --git a/src/routes/parts.js b/src/routes/parts.js index f8d6d9ac1f616986ac636b76b9abab7a8ddd5c2e..e456151903b682ac6a6881cee59dc518739aac6e 100644 --- a/src/routes/parts.js +++ b/src/routes/parts.js @@ -52,4 +52,13 @@ partRouter.get( }) ); +partRouter.get( + '/combination/by-uuid/:uuid', + wrapAsync(async (req, res) => { + const { uuid } = req.params; + const info = await PartService.getCombinationByUuid(uuid); + return res.sendResponse('', 200, { ...info }); + }) +); + export default partRouter; diff --git a/src/services/myService.js b/src/services/myService.js index 9bfcc07b5da973069235922d7b4aa66a04d4050c..c8bee06aa3a72722395314d5bdb40ce08c62ee70 100644 --- a/src/services/myService.js +++ b/src/services/myService.js @@ -1,6 +1,7 @@ import { Redis } from '../redis.js'; import { ReportableError } from '../errors.js'; import myRepository from '../repositories/myRepository.js'; +import ShareRepository from '../repositories/shareRepository.js'; import crypto from 'crypto'; const myService = { @@ -163,6 +164,20 @@ const myService = { message: '조합 이름이 성공적으로 변경되었습니다.', }; }, + + async createCombinationUuid(userId, combinationId) { + if (!userId || !combinationId) + throw new ReportableError(400, '올바르지 않은 요청입니다.'); + + const combination = await myRepository.getCombinationById(combinationId); + if (!combination) + throw new ReportableError(404, '조합을 찾을 수 없습니다.'); + + if (combination.userId !== userId) + throw new ReportableError(403, '해당 조합에 대한 권한이 없습니다.'); + + return await ShareRepository.createCombinationUuid(combinationId); + }, }; export default myService; diff --git a/src/services/partService.js b/src/services/partService.js index 2a9c553afef474cb422491aa174a3b0c932b75db..642ff21fb34a7044e5cb0c728f977d77ce605c89 100644 --- a/src/services/partService.js +++ b/src/services/partService.js @@ -1,6 +1,7 @@ import { columnMapping } from '../constants/columnMapping.js'; import { ReportableError } from '../errors.js'; import PartRepository from '../repositories/partRepository.js'; +import ShareRepository from '../repositories/shareRepository.js'; const PartService = { cleanEntity(entity) { @@ -108,6 +109,14 @@ const PartService = { partIds: combination.partids, })); }, + + async getCombinationByUuid(uuid) { + if (!uuid) throw new ReportableError(400, '올바르지 않은 요청입니다.'); + + const { combination_id, parts, created_at, updated_at } = + await ShareRepository.getCombinationByUuid(uuid); + return { combination_id, parts, created_at, updated_at }; + }, }; export default PartService;