From f446ee0b3f81591abff49161439330d8b4f3040d Mon Sep 17 00:00:00 2001 From: lilloo04 Date: Tue, 22 Jul 2025 19:50:24 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20postWellItem=EC=97=90=20=EB=88=84?= =?UTF-8?q?=EC=A0=81=20=EC=B1=85=202=EA=B6=8C=20=EC=9D=B4=EC=83=81=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20=EC=8B=9C=20=EA=B0=9C=EA=B5=AC=EB=A6=AC=20?= =?UTF-8?q?=EC=95=84=EC=9D=B4=ED=85=9C=20=EC=A7=80=EA=B8=89=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/postWellItem.js | 37 ++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/services/postWellItem.js b/src/services/postWellItem.js index c9bc30b..8c226eb 100644 --- a/src/services/postWellItem.js +++ b/src/services/postWellItem.js @@ -1,7 +1,17 @@ -import { decodeHashId, encodeHashId } from '@frolog/common-utils'; +import { decodeHashId, encodeHashId, SSC_TOKEN } from '@frolog/common-utils'; import { handleSqlError } from '@frolog/express-api-server'; -import { FetchError } from '@frolog/frolog-api'; +import { + FetchError, + GrantInitialStoreItem, + GetUserWellItemCount, +} from '@frolog/frolog-api'; import { Well, WellItem } from '@frolog/models'; +const getUserWellItemCount = new GetUserWellItemCount({ + accessToken: SSC_TOKEN, +}); +const grantInitialStoreItem = new GrantInitialStoreItem({ + accessToken: SSC_TOKEN, +}); /** * 우물 아이템 작성. @@ -36,6 +46,7 @@ export default async function postWellItem(reqDto, user) { }; } + // (3) 기존 우물 아이템 조회 또는 새로 생성 let existing = true; let wellItem = await WellItem.findOne({ where: { @@ -46,10 +57,7 @@ export default async function postWellItem(reqDto, user) { if (!wellItem) { existing = false; - // 새로운 order 연산 - // Note: 여기서 count를 쓰지 않고 findOne을 쓰는 이유는, - // 비정상 동작으로 정합적이지 않은 order가 생기는 경우에도 - // 정상 동작할 수 있게 하기 위함임. + // (3-1) 마지막 order 기준으로 새 order 설정 const lastItem = await WellItem.findOne({ where: { well_id: wellId }, order: [['order', 'DESC']], @@ -57,6 +65,7 @@ export default async function postWellItem(reqDto, user) { let newOrder = 0; if (lastItem) newOrder = lastItem.order + 1; + // (3-2) 새로운 우물 아이템 생성 wellItem = await WellItem.create({ well_id: wellId, book_isbn: reqDto.isbn, @@ -65,9 +74,25 @@ export default async function postWellItem(reqDto, user) { }).catch(handleSqlError); } + // (4) 아이템 ID 인코딩 const itemId = wellItem.item_id; const itemHashId = encodeHashId(itemId); + // (5) 현재 유저의 등록된 책 개수 조회 + const { total } = await getUserWellItemCount + .fetch({ id: user.id }) + .catch(handleSqlError); + + // (6) 책 2권 이상이면 초기 개구리 지급 + if (total >= 2) { + await grantInitialStoreItem + .fetch({ + id: user.id, + count: total, + }) + .catch(handleSqlError); + } + return { result: true, id: itemHashId, From 5f235a4bb21db90d198f4dc426acb566a986a292 Mon Sep 17 00:00:00 2001 From: lilloo04 Date: Tue, 22 Jul 2025 21:01:44 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20getUserWellItemCount=20?= =?UTF-8?q?=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8A=94=20=EB=B0=A9=EC=8B=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/postWellItem.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/services/postWellItem.js b/src/services/postWellItem.js index 8c226eb..43deea4 100644 --- a/src/services/postWellItem.js +++ b/src/services/postWellItem.js @@ -1,14 +1,9 @@ import { decodeHashId, encodeHashId, SSC_TOKEN } from '@frolog/common-utils'; import { handleSqlError } from '@frolog/express-api-server'; -import { - FetchError, - GrantInitialStoreItem, - GetUserWellItemCount, -} from '@frolog/frolog-api'; +import { FetchError, GrantInitialStoreItem } from '@frolog/frolog-api'; import { Well, WellItem } from '@frolog/models'; -const getUserWellItemCount = new GetUserWellItemCount({ - accessToken: SSC_TOKEN, -}); +import getUserWellItemCount from './getUserWellItemCount.js'; + const grantInitialStoreItem = new GrantInitialStoreItem({ accessToken: SSC_TOKEN, }); @@ -79,9 +74,9 @@ export default async function postWellItem(reqDto, user) { const itemHashId = encodeHashId(itemId); // (5) 현재 유저의 등록된 책 개수 조회 - const { total } = await getUserWellItemCount - .fetch({ id: user.id }) - .catch(handleSqlError); + const { total } = await getUserWellItemCount({ id: user.id }, user).catch( + handleSqlError, + ); // (6) 책 2권 이상이면 초기 개구리 지급 if (total >= 2) {