-
Notifications
You must be signed in to change notification settings - Fork 0
feat(store): 매장 찜 토글 + 인기 매장 isWishlisted (옵셔널 JWT 가드) #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
src/features/store/constants/store-wishlist-error-messages.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export const STORE_WISHLIST_ERRORS = { | ||
| STORE_NOT_FOUND: '존재하지 않는 매장입니다.', | ||
| USER_ONLY: '매장 찜은 일반 사용자만 이용할 수 있습니다.', | ||
| } as const; |
72 changes: 72 additions & 0 deletions
72
src/features/store/repositories/store-wishlist.repository.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
|
|
||
| import { PrismaService } from '@/prisma'; | ||
|
|
||
| @Injectable() | ||
| export class StoreWishlistRepository { | ||
| constructor(private readonly prisma: PrismaService) {} | ||
|
|
||
| /** 매장 찜 추가 (멱등). 없으면 생성, soft-delete된 경우 복원. */ | ||
| async upsertStoreWishlist(args: { | ||
| accountId: bigint; | ||
| storeId: bigint; | ||
| now: Date; | ||
| }): Promise<void> { | ||
| await this.prisma.storeWishlistItem.upsert({ | ||
| where: { | ||
| account_id_store_id: { | ||
| account_id: args.accountId, | ||
| store_id: args.storeId, | ||
| }, | ||
| }, | ||
| create: { account_id: args.accountId, store_id: args.storeId }, | ||
| update: { deleted_at: null, updated_at: args.now }, | ||
| }); | ||
| } | ||
|
|
||
| /** 매장 찜 해제 (멱등). active 항목만 soft-delete. */ | ||
| async softDeleteStoreWishlist(args: { | ||
| accountId: bigint; | ||
| storeId: bigint; | ||
| now: Date; | ||
| }): Promise<void> { | ||
| await this.prisma.storeWishlistItem.updateMany({ | ||
| where: { | ||
| account_id: args.accountId, | ||
| store_id: args.storeId, | ||
| deleted_at: null, | ||
| }, | ||
| data: { deleted_at: args.now }, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * 주어진 storeIds 중 사용자가 찜한 store_id 집합(string)을 단일 IN 쿼리로 반환. | ||
| * 비활성/soft-delete된 매장은 제외해 목록 가시성과 일관되게 한다(N+1 회피). | ||
| */ | ||
| async findWishlistedStoreIds(args: { | ||
| accountId: bigint; | ||
| storeIds: bigint[]; | ||
| }): Promise<Set<string>> { | ||
| if (args.storeIds.length === 0) return new Set(); | ||
| const rows = await this.prisma.storeWishlistItem.findMany({ | ||
| where: { | ||
| account_id: args.accountId, | ||
| store_id: { in: args.storeIds }, | ||
| deleted_at: null, | ||
| store: { is_active: true, deleted_at: null }, | ||
| }, | ||
| select: { store_id: true }, | ||
| }); | ||
| return new Set(rows.map((r) => r.store_id.toString())); | ||
| } | ||
|
|
||
| /** 활성 USER 계정 여부. 매장 찜은 구매자(USER)만 가능 → 인기 랭킹 무결성 보호. */ | ||
| async isActiveUserAccount(accountId: bigint): Promise<boolean> { | ||
| const account = await this.prisma.account.findFirst({ | ||
| where: { id: accountId, account_type: 'USER', deleted_at: null }, | ||
| select: { id: true }, | ||
| }); | ||
| return Boolean(account); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,31 @@ | ||
| import { UseGuards } from '@nestjs/common'; | ||
| import { Args, Query, Resolver } from '@nestjs/graphql'; | ||
|
|
||
| import { PopularStoresInput } from '@/features/store/dto/inputs/popular-stores.input'; | ||
| import { StoreListingService } from '@/features/store/services/store-listing.service'; | ||
| import type { PopularStoreConnection } from '@/features/store/types/store-output.type'; | ||
| import { | ||
| CurrentUser, | ||
| OptionalJwtAuthGuard, | ||
| parseAccountId, | ||
| type JwtUser, | ||
| } from '@/global/auth'; | ||
|
|
||
| /** | ||
| * 매장 조회 resolver. 인기 매장 리스트는 비로그인도 접근 가능한 public query. | ||
| * 옵셔널 인증으로 로그인 시에만 isWishlisted를 채운다. | ||
| */ | ||
| @Resolver('Query') | ||
| export class StoreQueryResolver { | ||
| constructor(private readonly storeListingService: StoreListingService) {} | ||
|
|
||
| @Query('popularStores') | ||
| @UseGuards(OptionalJwtAuthGuard) | ||
| popularStores( | ||
| @CurrentUser() user: JwtUser | undefined, | ||
| @Args('input', { nullable: true }) input?: PopularStoresInput, | ||
| ): Promise<PopularStoreConnection> { | ||
| return this.storeListingService.popularStores(input); | ||
| const accountId = user ? parseAccountId(user) : undefined; | ||
| return this.storeListingService.popularStores(input, accountId); | ||
| } | ||
| } | ||
93 changes: 93 additions & 0 deletions
93
src/features/store/resolvers/store-wishlist-mutation.resolver.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { NotFoundException } from '@nestjs/common'; | ||
| import type { PrismaClient } from '@prisma/client'; | ||
|
|
||
| import { StoreWishlistRepository } from '@/features/store/repositories/store-wishlist.repository'; | ||
| import { StoreRepository } from '@/features/store/repositories/store.repository'; | ||
| import { StoreWishlistMutationResolver } from '@/features/store/resolvers/store-wishlist-mutation.resolver'; | ||
| import { StoreWishlistService } from '@/features/store/services/store-wishlist.service'; | ||
| import { disconnectTestPrismaClient } from '@/test/db/prisma-test-client'; | ||
| import { closeTruncateConnection, truncateAll } from '@/test/db/truncate'; | ||
| import { | ||
| createAccount, | ||
| createStore, | ||
| createStoreWishlist, | ||
| } from '@/test/factories'; | ||
| import { createTestingModuleWithRealDb } from '@/test/modules/testing-module.builder'; | ||
|
|
||
| describe('Store Wishlist Mutation Resolver (real DB)', () => { | ||
| let resolver: StoreWishlistMutationResolver; | ||
| let prisma: PrismaClient; | ||
|
|
||
| beforeAll(async () => { | ||
| const { module, prisma: p } = await createTestingModuleWithRealDb({ | ||
| providers: [ | ||
| StoreWishlistMutationResolver, | ||
| StoreWishlistService, | ||
| StoreWishlistRepository, | ||
| StoreRepository, | ||
| ], | ||
| }); | ||
| resolver = module.get(StoreWishlistMutationResolver); | ||
| prisma = p; | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await closeTruncateConnection(); | ||
| await disconnectTestPrismaClient(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| await truncateAll(); | ||
| }); | ||
|
|
||
| it('addStoreToWishlist: accountId 변환 후 찜을 생성한다', async () => { | ||
| const account = await createAccount(prisma, { account_type: 'USER' }); | ||
| const store = await createStore(prisma); | ||
|
|
||
| const ok = await resolver.addStoreToWishlist( | ||
| { accountId: account.id.toString() }, | ||
| store.id.toString(), | ||
| ); | ||
|
|
||
| expect(ok).toBe(true); | ||
| const row = await prisma.storeWishlistItem.findUniqueOrThrow({ | ||
| where: { | ||
| account_id_store_id: { | ||
| account_id: account.id, | ||
| store_id: store.id, | ||
| }, | ||
| }, | ||
| }); | ||
| expect(row.deleted_at).toBeNull(); | ||
| }); | ||
|
|
||
| it('addStoreToWishlist: 없는 매장이면 NotFoundException 전파', async () => { | ||
| const account = await createAccount(prisma, { account_type: 'USER' }); | ||
| await expect( | ||
| resolver.addStoreToWishlist( | ||
| { accountId: account.id.toString() }, | ||
| '999999', | ||
| ), | ||
| ).rejects.toThrow(NotFoundException); | ||
| }); | ||
|
|
||
| it('removeStoreFromWishlist: 찜을 해제한다', async () => { | ||
| const account = await createAccount(prisma, { account_type: 'USER' }); | ||
| const store = await createStore(prisma); | ||
| await createStoreWishlist(prisma, { | ||
| account_id: account.id, | ||
| store_id: store.id, | ||
| }); | ||
|
|
||
| const ok = await resolver.removeStoreFromWishlist( | ||
| { accountId: account.id.toString() }, | ||
| store.id.toString(), | ||
| ); | ||
|
|
||
| expect(ok).toBe(true); | ||
| const remaining = await prisma.storeWishlistItem.count({ | ||
| where: { account_id: account.id, store_id: store.id, deleted_at: null }, | ||
| }); | ||
| expect(remaining).toBe(0); | ||
| }); | ||
| }); |
38 changes: 38 additions & 0 deletions
38
src/features/store/resolvers/store-wishlist-mutation.resolver.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { UseGuards } from '@nestjs/common'; | ||
| import { Args, Mutation, Resolver } from '@nestjs/graphql'; | ||
|
|
||
| import { StoreWishlistService } from '@/features/store/services/store-wishlist.service'; | ||
| import { | ||
| CurrentUser, | ||
| JwtAuthGuard, | ||
| parseAccountId, | ||
| type JwtUser, | ||
| } from '@/global/auth'; | ||
|
|
||
| @Resolver('Mutation') | ||
| @UseGuards(JwtAuthGuard) | ||
| export class StoreWishlistMutationResolver { | ||
| constructor(private readonly storeWishlistService: StoreWishlistService) {} | ||
|
|
||
| @Mutation('addStoreToWishlist') | ||
| addStoreToWishlist( | ||
| @CurrentUser() user: JwtUser, | ||
| @Args('storeId') storeId: string, | ||
| ): Promise<boolean> { | ||
| return this.storeWishlistService.addStoreToWishlist( | ||
| parseAccountId(user), | ||
| storeId, | ||
| ); | ||
| } | ||
|
|
||
| @Mutation('removeStoreFromWishlist') | ||
| removeStoreFromWishlist( | ||
| @CurrentUser() user: JwtUser, | ||
| @Args('storeId') storeId: string, | ||
| ): Promise<boolean> { | ||
| return this.storeWishlistService.removeStoreFromWishlist( | ||
| parseAccountId(user), | ||
| storeId, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
popularStoresis queried with a bearer token, this guard is the only code path that authenticates the request and populatesreq.user, butOptionalJwtAuthGuardis not registered in the Nest provider graph (checkedAuthGlobalModuleand the newStoreModuleproviders). Route-scoped guards supplied as classes are resolved from providers, so this guard is skipped/absent and@CurrentUser()stays undefined, causingisWishlistedto remain false for logged-in users until the guard is added to providers/exports (or this module's providers).Useful? React with 👍 / 👎.