Skip to content

Commit

Permalink
Merge pull request #215 from boostcampwm2023/feat/211-purchase-design…
Browse files Browse the repository at this point in the history
…-list-api

[Feat] 유료 디자인 구매 목록 API 구현
  • Loading branch information
JoonSoo-Kim authored Dec 5, 2023
2 parents 1cb80a3 + cee1a7a commit 24da4cd
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 4 deletions.
4 changes: 4 additions & 0 deletions BE/src/purchase/dto/purchase.design.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export class PurchaseDesignDto {
@IsNotEmpty({ message: "디자인은 비어있지 않아야 합니다." })
design: string;
}

export class PurchaseListDto {
[domain: string]: { design: string[] };
}
17 changes: 15 additions & 2 deletions BE/src/purchase/purchase.controller.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -18,4 +25,10 @@ export class PurchaseController {
): Promise<void> {
await this.purchaseService.purchaseDesign(user, purchaseDesignDto);
}

@Get("/design")
@HttpCode(200)
async getDesignPurchaseList(@GetUser() user: User): Promise<PurchaseListDto> {
return await this.purchaseService.getDesignPurchaseList(user);
}
}
4 changes: 4 additions & 0 deletions BE/src/purchase/purchase.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ export class PurchaseRepository {

purchase.save();
}

async getDesignPurchaseList(user: User) {
return Purchase.find({ where: { user: { userId: user.userId } } });
}
}
25 changes: 23 additions & 2 deletions BE/src/purchase/purchase.service.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -23,7 +23,7 @@ export class PurchaseService {
}

if (
Purchase.findOne({
await Purchase.findOne({
where: {
user: { userId: user.userId },
domain: domain,
Expand All @@ -39,4 +39,25 @@ export class PurchaseService {

await this.purchaseRepository.purchaseDesign(user, domain, design);
}

async getDesignPurchaseList(user: User): Promise<PurchaseListDto> {
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;
}
}
98 changes: 98 additions & 0 deletions BE/test/e2e/purchase.design-list.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -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>(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);
});
});
32 changes: 32 additions & 0 deletions BE/test/int/purchase.repository.int-spec.ts
Original file line number Diff line number Diff line change
@@ -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>(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);
});
});
});
33 changes: 33 additions & 0 deletions BE/test/int/purchase.service.int-spec.ts
Original file line number Diff line number Diff line change
@@ -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>(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: [],
});
});
});
});

0 comments on commit 24da4cd

Please sign in to comment.