Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/features/store/constants/store-wishlist-error-messages.ts
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 src/features/store/repositories/store-wishlist.repository.ts
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);
}
}
9 changes: 9 additions & 0 deletions src/features/store/repositories/store.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ export class StoreRepository {
});
}

/** 활성 매장 존재 검증(찜 등). */
async existsActiveStore(storeId: bigint): Promise<boolean> {
const found = await this.prisma.store.findFirst({
where: { id: storeId, is_active: true, deleted_at: null },
select: { id: true },
});
return Boolean(found);
}

/** 매장별 활성 찜 수. */
async aggregateWishlistCounts(
storeIds: bigint[],
Expand Down
12 changes: 9 additions & 3 deletions src/features/store/resolvers/store-query.resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PrismaClient } from '@prisma/client';

import { StoreWishlistRepository } from '@/features/store/repositories/store-wishlist.repository';
import { StoreRepository } from '@/features/store/repositories/store.repository';
import { StoreQueryResolver } from '@/features/store/resolvers/store-query.resolver';
import { StoreListingService } from '@/features/store/services/store-listing.service';
Expand All @@ -18,7 +19,12 @@ describe('Store Query Resolver (real DB)', () => {

beforeAll(async () => {
const { module, prisma: p } = await createTestingModuleWithRealDb({
providers: [StoreQueryResolver, StoreListingService, StoreRepository],
providers: [
StoreQueryResolver,
StoreListingService,
StoreRepository,
StoreWishlistRepository,
],
});
resolver = module.get(StoreQueryResolver);
prisma = p;
Expand All @@ -36,7 +42,7 @@ describe('Store Query Resolver (real DB)', () => {
it('popularStores: 서비스에 위임해 커넥션을 반환한다', async () => {
await createStore(prisma, { store_name: '리졸버매장' });

const result = await resolver.popularStores();
const result = await resolver.popularStores(undefined);

expect(result.totalCount).toBe(1);
expect(result.items[0].storeName).toBe('리졸버매장');
Expand All @@ -48,7 +54,7 @@ describe('Store Query Resolver (real DB)', () => {
const target = await createStore(prisma, { region_id: region.id });
await createStore(prisma); // region 없는 매장

const result = await resolver.popularStores({
const result = await resolver.popularStores(undefined, {
regionIds: [region.id.toString()],
});

Expand Down
13 changes: 12 additions & 1 deletion src/features/store/resolvers/store-query.resolver.ts
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Register the optional auth guard

When popularStores is queried with a bearer token, this guard is the only code path that authenticates the request and populates req.user, but OptionalJwtAuthGuard is not registered in the Nest provider graph (checked AuthGlobalModule and the new StoreModule providers). Route-scoped guards supplied as classes are resolved from providers, so this guard is skipped/absent and @CurrentUser() stays undefined, causing isWishlisted to remain false for logged-in users until the guard is added to providers/exports (or this module's providers).

Useful? React with 👍 / 👎.

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);
}
}
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 src/features/store/resolvers/store-wishlist-mutation.resolver.ts
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,
);
}
}
28 changes: 27 additions & 1 deletion src/features/store/services/store-listing.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { PrismaClient } from '@prisma/client';

import { StoreWishlistRepository } from '@/features/store/repositories/store-wishlist.repository';
import { StoreRepository } from '@/features/store/repositories/store.repository';
import { StoreListingService } from '@/features/store/services/store-listing.service';
import { disconnectTestPrismaClient } from '@/test/db/prisma-test-client';
import { closeTruncateConnection, truncateAll } from '@/test/db/truncate';
import {
createAccount,
createOrder,
createOrderItem,
createProduct,
Expand All @@ -23,7 +25,11 @@ describe('StoreListingService (real DB)', () => {

beforeAll(async () => {
const { module, prisma: p } = await createTestingModuleWithRealDb({
providers: [StoreListingService, StoreRepository],
providers: [
StoreListingService,
StoreRepository,
StoreWishlistRepository,
],
});
service = module.get(StoreListingService);
prisma = p;
Expand Down Expand Up @@ -206,5 +212,25 @@ describe('StoreListingService (real DB)', () => {

expect(result.totalCount).toBe(0);
});

it('로그인 사용자의 찜 매장은 isWishlisted=true, 비로그인/미찜은 false', async () => {
const account = await createAccount(prisma, { account_type: 'USER' });
const wished = await createStore(prisma, { store_name: '찜한매장' });
await createStore(prisma, { store_name: '안찜매장' });
await createStoreWishlist(prisma, {
account_id: account.id,
store_id: wished.id,
});

const loggedIn = await service.popularStores(undefined, account.id);
const byName = new Map(
loggedIn.items.map((s) => [s.storeName, s.isWishlisted]),
);
expect(byName.get('찜한매장')).toBe(true);
expect(byName.get('안찜매장')).toBe(false);

const anonymous = await service.popularStores();
expect(anonymous.items.every((s) => s.isWishlisted === false)).toBe(true);
});
});
});
Loading
Loading