Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/services/postWellItem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
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 } from '@frolog/frolog-api';
import { Well, WellItem } from '@frolog/models';
import getUserWellItemCount from './getUserWellItemCount.js';

const grantInitialStoreItem = new GrantInitialStoreItem({
accessToken: SSC_TOKEN,
});

/**
* 우물 아이템 작성.
Expand Down Expand Up @@ -36,6 +41,7 @@ export default async function postWellItem(reqDto, user) {
};
}

// (3) 기존 우물 아이템 조회 또는 새로 생성
let existing = true;
let wellItem = await WellItem.findOne({
where: {
Expand All @@ -46,17 +52,15 @@ 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']],
}).catch(handleSqlError);
let newOrder = 0;
if (lastItem) newOrder = lastItem.order + 1;

// (3-2) 새로운 우물 아이템 생성
wellItem = await WellItem.create({
well_id: wellId,
book_isbn: reqDto.isbn,
Expand All @@ -65,9 +69,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({ id: user.id }, user).catch(
handleSqlError,
);

// (6) 책 2권 이상이면 초기 개구리 지급
if (total >= 2) {
await grantInitialStoreItem

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await grantInitialStoreItem(user.id, total)
이런식으로 그냥 함수 쓰듯이 사용하면 될 것 같아요

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다!!~

.fetch({
id: user.id,
count: total,
})
.catch(handleSqlError);
}

return {
result: true,
id: itemHashId,
Expand Down