11import { BaseError } from "../errors/app.error.js" ;
22import { PrismaClient } from "@prisma/client" ;
33const prisma = new PrismaClient ( ) ;
4+
45import {
56 countDuplicateCategoryName ,
67 createUserCategory ,
@@ -11,8 +12,7 @@ import {
1112 listUserCategories ,
1213} from "./category.repository.js" ;
1314
14- // 카테고리 생성
15-
15+ /// 카테고리 생성
1616export const createCategoryService = async ( {
1717 userId,
1818 name,
@@ -27,19 +27,11 @@ export const createCategoryService = async ({
2727 if ( dup > 0 )
2828 throw new BaseError ( "이미 존재하는 카테고리명입니다" , 409 , "CAT_DUP" ) ;
2929
30- const lastCategory = await prisma . userCategory . findFirst ( {
31- where : { userId } ,
32- orderBy : { displayOrder : "desc" } ,
33- } ) ;
34-
35- const nextOrder = lastCategory ? lastCategory . displayOrder + 1 : 0 ;
36-
3730 const created = await createUserCategory ( {
3831 userId,
3932 categoryName : name ,
4033 iconKey,
4134 iconUrl,
42- displayOrder : nextOrder ,
4335 } ) ;
4436
4537 const wordCount = await countUserWordsInCategory ( {
@@ -51,20 +43,16 @@ export const createCategoryService = async ({
5143} ;
5244
5345// 카테고리 목록 조회
54- // - 기본 + 사용자 모두 userCategory에서 조회
55- // - isDefault 여부만 다름
56-
5746export const getCategoryListService = async ( { userId } ) => {
58- // 현재 유저 카테고리 조회
5947 let categories = await listUserCategories ( { userId } ) ;
6048
61- // 기본 카테고리가 하나도 없다면 자동 생성
6249 const hasDefault = categories . some ( ( c ) => c . isDefault ) ;
6350
6451 if ( ! hasDefault ) {
6552 const DEFAULT_ICON_MAP = {
6653 최근사용 : "ICON_RECENT" ,
6754 즐겨찾기 : "ICON_FAVORITE" ,
55+ 어미 : "ICON_ENDING" ,
6856 기본 : "ICON_BASIC" ,
6957 사람 : "ICON_PERSON" ,
7058 행동 : "ICON_ACTION" ,
@@ -86,11 +74,9 @@ export const getCategoryListService = async ({ userId }) => {
8674 } ) ) ,
8775 } ) ;
8876
89- // 다시 조회
9077 categories = await listUserCategories ( { userId } ) ;
9178 }
9279
93- // wordCount 계산
9480 return Promise . all (
9581 categories . map ( async ( c ) => {
9682 let wordCount = 0 ;
@@ -120,8 +106,6 @@ export const getCategoryListService = async ({ userId }) => {
120106} ;
121107
122108// 카테고리 수정
123- // - 기본 카테고리는 이름 수정 금지
124-
125109export const patchCategoryService = async ( {
126110 userId,
127111 id,
@@ -136,20 +120,17 @@ export const patchCategoryService = async ({
136120
137121 const LOCKED_DEFAULT_NAMES = [ "최근사용" , "즐겨찾기" , "어미" ] ;
138122
139- // 일부 기본 카테고리만 수정 금지
140123 if (
141124 existing . isDefault &&
142- name !== undefined &&
143125 LOCKED_DEFAULT_NAMES . includes ( existing . categoryName )
144126 ) {
145127 throw new BaseError (
146- "해당 기본 카테고리는 이름을 수정할 수 없습니다" ,
128+ "해당 기본 카테고리는 수정할 수 없습니다" ,
147129 400 ,
148130 "CAT_DEFAULT_EDIT" ,
149131 ) ;
150132 }
151133
152- // 이름 중복 검사
153134 if ( name !== undefined ) {
154135 const dup = await countDuplicateCategoryName ( {
155136 userId,
@@ -170,7 +151,12 @@ export const patchCategoryService = async ({
170151 const updated = await updateUserCategory ( { userId, id, data } ) ;
171152
172153 const wordCount = existing . isDefault
173- ? 0
154+ ? await prisma . word . count ( {
155+ where : {
156+ category : { categoryName : existing . categoryName } ,
157+ isDefault : true ,
158+ } ,
159+ } )
174160 : await countUserWordsInCategory ( {
175161 userId,
176162 userCategoryId : id ,
@@ -180,17 +166,20 @@ export const patchCategoryService = async ({
180166} ;
181167
182168// 카테고리 삭제
183- // - 기본 카테고리 삭제 금지
184-
185169export const removeCategoryService = async ( { userId, id } ) => {
186170 const existing = await findUserCategoryById ( { userId, id } ) ;
187171
188172 if ( ! existing )
189173 throw new BaseError ( "카테고리를 찾을 수 없습니다" , 404 , "CAT404" ) ;
190174
191- if ( existing . isDefault ) {
175+ const LOCKED_DEFAULT_NAMES = [ "최근사용" , "즐겨찾기" , "어미" ] ;
176+
177+ if (
178+ existing . isDefault &&
179+ LOCKED_DEFAULT_NAMES . includes ( existing . categoryName )
180+ ) {
192181 throw new BaseError (
193- "기본 카테고리는 삭제할 수 없습니다" ,
182+ "해당 기본 카테고리는 삭제할 수 없습니다" ,
194183 400 ,
195184 "CAT_DEFAULT_DELETE" ,
196185 ) ;
0 commit comments