diff --git a/BE/src/purchase/dto/purchase.design.dto.ts b/BE/src/purchase/dto/purchase.design.dto.ts index 3fa9909..98411d7 100644 --- a/BE/src/purchase/dto/purchase.design.dto.ts +++ b/BE/src/purchase/dto/purchase.design.dto.ts @@ -7,3 +7,7 @@ export class PurchaseDesignDto { @IsNotEmpty({ message: "디자인은 비어있지 않아야 합니다." }) design: string; } + +export class PurchaseListDto { + [domain: string]: { design: string[] }; +} diff --git a/BE/src/purchase/purchase.controller.ts b/BE/src/purchase/purchase.controller.ts index ddeaab7..a7726e8 100644 --- a/BE/src/purchase/purchase.controller.ts +++ b/BE/src/purchase/purchase.controller.ts @@ -1,7 +1,14 @@ -import { Controller, Post, UseGuards, Body, HttpCode } from "@nestjs/common"; +import { + Controller, + Get, + Post, + UseGuards, + Body, + HttpCode, +} from "@nestjs/common"; import { JwtAuthGuard } from "src/auth/guard/auth.jwt-guard"; import { PurchaseService } from "./purchase.service"; -import { PurchaseDesignDto } from "./dto/purchase.design.dto"; +import { PurchaseDesignDto, PurchaseListDto } from "./dto/purchase.design.dto"; import { GetUser } from "src/auth/get-user.decorator"; import { User } from "src/auth/users.entity"; @@ -18,4 +25,10 @@ export class PurchaseController { ): Promise { await this.purchaseService.purchaseDesign(user, purchaseDesignDto); } + + @Get("/design") + @HttpCode(200) + async getDesignPurchaseList(@GetUser() user: User): Promise { + return await this.purchaseService.getDesignPurchaseList(user); + } } diff --git a/BE/src/purchase/purchase.repository.ts b/BE/src/purchase/purchase.repository.ts index ef6aae4..3fbdba6 100644 --- a/BE/src/purchase/purchase.repository.ts +++ b/BE/src/purchase/purchase.repository.ts @@ -17,4 +17,8 @@ export class PurchaseRepository { purchase.save(); } + + async getDesignPurchaseList(user: User) { + return Purchase.find({ where: { user: { userId: user.userId } } }); + } } diff --git a/BE/src/purchase/purchase.service.ts b/BE/src/purchase/purchase.service.ts index 983b5ed..18b6658 100644 --- a/BE/src/purchase/purchase.service.ts +++ b/BE/src/purchase/purchase.service.ts @@ -1,5 +1,5 @@ import { Injectable, BadRequestException } from "@nestjs/common"; -import { PurchaseDesignDto } from "./dto/purchase.design.dto"; +import { PurchaseDesignDto, PurchaseListDto } from "./dto/purchase.design.dto"; import { User } from "src/auth/users.entity"; import { PurchaseRepository } from "./purchase.repository"; import { Purchase } from "./purchase.entity"; @@ -23,7 +23,7 @@ export class PurchaseService { } if ( - Purchase.findOne({ + await Purchase.findOne({ where: { user: { userId: user.userId }, domain: domain, @@ -39,4 +39,25 @@ export class PurchaseService { await this.purchaseRepository.purchaseDesign(user, domain, design); } + + async getDesignPurchaseList(user: User): Promise { + const purchaseList = + await this.purchaseRepository.getDesignPurchaseList(user); + + const groundPurchase = []; + const skyPurchase = []; + await purchaseList.forEach((purchase) => { + if (purchase.domain === "ground") { + groundPurchase.push(purchase.design); + } + if (purchase.domain === "sky") { + skyPurchase.push(purchase.design); + } + }); + + const result = {}; + result["ground"] = groundPurchase; + result["sky"] = skyPurchase; + return result; + } } diff --git a/BE/test/e2e/purchase.design-list.e2e-spec.ts b/BE/test/e2e/purchase.design-list.e2e-spec.ts new file mode 100644 index 0000000..d649535 --- /dev/null +++ b/BE/test/e2e/purchase.design-list.e2e-spec.ts @@ -0,0 +1,98 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { INestApplication } from "@nestjs/common"; +import * as request from "supertest"; +import { ValidationPipe } from "@nestjs/common"; +import { typeORMTestConfig } from "src/configs/typeorm.test.config"; +import { TypeOrmModule } from "@nestjs/typeorm"; +import { RedisModule } from "@liaoliaots/nestjs-redis"; +import { clearUserDb } from "src/utils/clearDb"; +import { UsersRepository } from "src/auth/users.repository"; +import { AuthModule } from "src/auth/auth.module"; +import { PurchaseModule } from "src/purchase/purchase.module"; +import { User } from "src/auth/users.entity"; + +describe("[디자인 구매 내역 조회] /purchase/design GET e2e 테스트", () => { + let app: INestApplication; + let accessToken: string; + + beforeAll(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [ + TypeOrmModule.forRoot(typeORMTestConfig), + RedisModule.forRoot({ + readyLog: true, + config: { + host: "223.130.129.145", + port: 6379, + }, + }), + PurchaseModule, + AuthModule, + ], + providers: [UsersRepository], + }).compile(); + + let usersRepository = moduleFixture.get(UsersRepository); + + await clearUserDb(moduleFixture, usersRepository); + + app = moduleFixture.createNestApplication(); + app.enableCors(); + app.useGlobalPipes(new ValidationPipe()); + + await app.init(); + + const signInPost = await request(app.getHttpServer()) + .post("/auth/signin") + .send({ + userId: "commonUser", + password: process.env.COMMON_USER_PASS, + }); + + accessToken = signInPost.body.accessToken; + + // 1000 별가루 충전 + const user = await User.findOne({ where: { userId: "commonUser" } }); + user.credit = 1000; + await user.save(); + + // 디자인 구매 + await request(app.getHttpServer()) + .post("/purchase/design") + .set("Authorization", `Bearer ${accessToken}`) + .send({ + domain: "GROUND", + design: "GROUND_GREEN", + }); + + await request(app.getHttpServer()) + .post("/purchase/design") + .set("Authorization", `Bearer ${accessToken}`) + .send({ + domain: "GROUND", + design: "GROUND_MOCHA", + }); + }); + + afterAll(async () => { + await app.close(); + }); + + it("정상 요청 시 200 OK 응답", async () => { + const getResponse = await request(app.getHttpServer()) + .get("/purchase/design") + .set("Authorization", `Bearer ${accessToken}`) + .expect(200); + + expect(getResponse.body).toStrictEqual({ + ground: ["#254117", "#493D26"], + sky: [], + }); + }); + + it("유효하지 않은 토큰 요청 시 401 Unauthorized 응답", async () => { + const getResponse = await request(app.getHttpServer()) + .get("/purchase/design") + .expect(401); + }); +}); diff --git a/BE/test/int/purchase.repository.int-spec.ts b/BE/test/int/purchase.repository.int-spec.ts new file mode 100644 index 0000000..f23ee55 --- /dev/null +++ b/BE/test/int/purchase.repository.int-spec.ts @@ -0,0 +1,32 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { TypeOrmModule } from "@nestjs/typeorm"; +import { User } from "src/auth/users.entity"; +import { typeORMTestConfig } from "src/configs/typeorm.test.config"; +import { Purchase } from "src/purchase/purchase.entity"; +import { PurchaseRepository } from "src/purchase/purchase.repository"; + +describe("PurchaseRepository 통합 테스트", () => { + let purchaseRepository: PurchaseRepository; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [TypeOrmModule.forRoot(typeORMTestConfig)], + providers: [PurchaseRepository], + }).compile(); + + purchaseRepository = module.get(PurchaseRepository); + }); + + describe("getDesignPurchaseList 메서드", () => { + it("메서드 정상 요청", async () => { + const user = await User.findOne({ where: { userId: "commonUser" } }); + + const purchaseList = await Purchase.find({ + where: { user: { userId: user.userId } }, + }); + const result = await purchaseRepository.getDesignPurchaseList(user); + + expect(result).toStrictEqual(purchaseList); + }); + }); +}); diff --git a/BE/test/int/purchase.service.int-spec.ts b/BE/test/int/purchase.service.int-spec.ts new file mode 100644 index 0000000..f4a27e8 --- /dev/null +++ b/BE/test/int/purchase.service.int-spec.ts @@ -0,0 +1,33 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { TypeOrmModule } from "@nestjs/typeorm"; +import { User } from "src/auth/users.entity"; +import { typeORMTestConfig } from "src/configs/typeorm.test.config"; +import { PurchaseRepository } from "src/purchase/purchase.repository"; +import { PurchaseService } from "src/purchase/purchase.service"; + +describe("PurchaseService 통합 테스트", () => { + let purchaseService: PurchaseService; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [TypeOrmModule.forRoot(typeORMTestConfig)], + providers: [PurchaseService, PurchaseRepository], + }).compile(); + + purchaseService = module.get(PurchaseService); + }); + + describe("getDesignPurchaseList 메서드", () => { + it("메서드 정상 요청", async () => { + const user = await User.findOne({ where: { userId: "commonUser" } }); + + // 테스트 DB 독립 시 수정 필요 + const result = await purchaseService.getDesignPurchaseList(user); + + expect(result).toStrictEqual({ + ground: ["#254117", "#493D26"], + sky: [], + }); + }); + }); +});