Skip to content

Commit

Permalink
Merge pull request #49 from HangHae-plus-2-9/feature-common
Browse files Browse the repository at this point in the history
Feat: convert integer ID into string UUID
  • Loading branch information
HC-kang authored Oct 23, 2023
2 parents da455b0 + 6ef506d commit e87243f
Show file tree
Hide file tree
Showing 49 changed files with 437 additions and 384 deletions.
6 changes: 4 additions & 2 deletions src/common/entities/common-columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import {
BaseEntity,
CreateDateColumn,
DeleteDateColumn,
Generated,
PrimaryColumn,
UpdateDateColumn,
} from 'typeorm';

export class CommonColumns extends BaseEntity {
@ApiProperty({ description: 'id', example: 1 })
@PrimaryColumn()
public id!: number;
@PrimaryColumn('uuid')
@Generated('uuid')
public id!: string;

@CreateDateColumn()
public readonly created_at!: Date | string;
Expand Down
9 changes: 7 additions & 2 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { v4 as uuidv4 } from 'uuid';

export const toSeconds = (input: string) => {
const units = {
s: 1, // seconds
Expand Down Expand Up @@ -45,8 +47,11 @@ export const getEndOfDay = (date: Date) => {
);
};

export const createNumericId = () => {
return Math.floor(Math.random() * 2000000000);
export const generateUUIDs = (keys) => {
return keys.reduce((acc, key) => {
acc[key] = uuidv4();
return acc;
}, {});
};

export const formattedString = (str: string) => JSON.stringify(str, null, 2);
77 changes: 0 additions & 77 deletions src/database/migrations/1698047231384-Migration.ts

This file was deleted.

77 changes: 77 additions & 0 deletions src/database/migrations/1698055877986-Migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class Migration1698055877986 implements MigrationInterface {
name = 'Migration1698055877986';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "favorites" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, "user_id" integer, "product_id" integer, CONSTRAINT "PK_890818d27523748dd36a4d1bdc8" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "carts" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, "customer_id" integer NOT NULL, CONSTRAINT "PK_b5f695a59f5ebb50af3c8160816" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "cart_items" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, "cart_id" integer NOT NULL, "product" character varying NOT NULL, "quantity" integer NOT NULL, "cartIdId" uuid, CONSTRAINT "PK_6fccf5ec03c172d27a28a82928b" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "order_items" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, "order_id" uuid NOT NULL, "product_id" character varying NOT NULL, "quantity" integer NOT NULL, "price" integer NOT NULL, CONSTRAINT "PK_005269d8574e6fac0493715c308" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "orders" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, "customer_id" character varying NOT NULL, "payment_method" integer NOT NULL, "payment_amount" integer NOT NULL, "paid_at" TIMESTAMP, "canceled_at" TIMESTAMP, "courier_name" integer, "invoice_number" character varying, "shipping_address" character varying NOT NULL, "shipping_receiver" character varying NOT NULL, "shipping_receiver_phone" character varying NOT NULL, "departed_at" TIMESTAMP, "arrived_at" TIMESTAMP, CONSTRAINT "PK_710e2d4957aa5878dfe94e4ac2f" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TYPE "public"."payments_payment_method_enum_enum" AS ENUM('CREDIT_CARD', 'VIRTUAL_ACCOUNT')`,
);
await queryRunner.query(
`CREATE TYPE "public"."payments_payment_status_enum_enum" AS ENUM('PENDING', 'COMPLETED', 'CANCELED')`,
);
await queryRunner.query(
`CREATE TABLE "payments" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, "payment_method_enum" "public"."payments_payment_method_enum_enum" NOT NULL DEFAULT 'CREDIT_CARD', "amount" integer NOT NULL, "canceled_amount" integer, "canceled_at" TIMESTAMP, "payment_status_enum" "public"."payments_payment_status_enum_enum" NOT NULL DEFAULT 'PENDING', "orderId" uuid, CONSTRAINT "REL_af929a5f2a400fdb6913b4967e" UNIQUE ("orderId"), CONSTRAINT "PK_197ab7af18c93fbb0c9b28b4a59" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "products" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, "seller_id" character varying NOT NULL, "name" character varying NOT NULL, "cat_name" character varying NOT NULL, "desc" text NOT NULL, "price" integer NOT NULL, "stock" integer NOT NULL, "status" integer NOT NULL, CONSTRAINT "PK_0806c755e0aca124e67c0cf6d7d" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "users" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, "name" character varying NOT NULL, "email" character varying NOT NULL, "password" character varying NOT NULL, "email_verified_at" TIMESTAMP, "phone" character varying, "status" integer NOT NULL DEFAULT '1', CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "sellers" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, "user_id" integer NOT NULL, "business_num" character varying NOT NULL, "store_name" character varying NOT NULL, "account_num" character varying NOT NULL, CONSTRAINT "PK_97337ccbf692c58e6c7682de8a2" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`ALTER TABLE "cart_items" ADD CONSTRAINT "FK_033b32986b1524e256cbfa9f711" FOREIGN KEY ("cartIdId") REFERENCES "carts"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "order_items" ADD CONSTRAINT "FK_145532db85752b29c57d2b7b1f1" FOREIGN KEY ("order_id") REFERENCES "orders"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "payments" ADD CONSTRAINT "FK_af929a5f2a400fdb6913b4967e1" FOREIGN KEY ("orderId") REFERENCES "orders"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "payments" DROP CONSTRAINT "FK_af929a5f2a400fdb6913b4967e1"`,
);
await queryRunner.query(
`ALTER TABLE "order_items" DROP CONSTRAINT "FK_145532db85752b29c57d2b7b1f1"`,
);
await queryRunner.query(
`ALTER TABLE "cart_items" DROP CONSTRAINT "FK_033b32986b1524e256cbfa9f711"`,
);
await queryRunner.query(`DROP TABLE "sellers"`);
await queryRunner.query(`DROP TABLE "users"`);
await queryRunner.query(`DROP TABLE "products"`);
await queryRunner.query(`DROP TABLE "payments"`);
await queryRunner.query(
`DROP TYPE "public"."payments_payment_status_enum_enum"`,
);
await queryRunner.query(
`DROP TYPE "public"."payments_payment_method_enum_enum"`,
);
await queryRunner.query(`DROP TABLE "orders"`);
await queryRunner.query(`DROP TABLE "order_items"`);
await queryRunner.query(`DROP TABLE "cart_items"`);
await queryRunner.query(`DROP TABLE "carts"`);
await queryRunner.query(`DROP TABLE "favorites"`);
}
}
8 changes: 4 additions & 4 deletions src/database/seeds/orders.seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default class OrdersSeeder implements Seeder {
const repository = dataSource.getRepository(OrderEntity);
await repository.insert([
{
id: 1,
customer_id: 1,
id: '93fd5c73-6992-4973-bdf2-15872518057a',
customer_id: 'bc4806f0-8668-4998-b610-27a046b9c4cd',
payment_method: PAYMENT_METHOD.CREDIT_CARD,
payment_amount: 10000,
paid_at: new Date(),
Expand All @@ -26,8 +26,8 @@ export default class OrdersSeeder implements Seeder {
deleted_at: null,
} as OrderEntity,
{
id: 2,
customer_id: 1,
id: 'ec930a41-392a-4f75-83fc-9bc6cadd05a0',
customer_id: 'bc4806f0-8668-4998-b610-27a046b9c4cd',
payment_method: PAYMENT_METHOD.CREDIT_CARD,
payment_amount: 10000,
paid_at: new Date(),
Expand Down
12 changes: 6 additions & 6 deletions src/database/seeds/products.seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default class ProductsSeeder implements Seeder {
const repository = dataSource.getRepository(ProductEntity);
await repository.insert([
{
id: 100001,
seller_id: 1,
id: 'e58ebbc1-6d85-46c2-8c1b-183ded15f9bc',
seller_id: '0fbfde54-4d15-490f-8653-899236631d9e',
name: 'iPhone 15',
cat_name: 'Smartphone',
desc: 'The latest iPhone',
Expand All @@ -21,8 +21,8 @@ export default class ProductsSeeder implements Seeder {
deleted_at: null,
} as ProductEntity,
{
id: 100002,
seller_id: 1,
id: 'dc620d83-f58b-4109-ac34-9467a71c4117',
seller_id: '0fbfde54-4d15-490f-8653-899236631d9e',
name: 'iPhone 14',
cat_name: 'Smartphone',
desc: 'The latest iPhone',
Expand All @@ -34,8 +34,8 @@ export default class ProductsSeeder implements Seeder {
deleted_at: null,
} as ProductEntity,
{
id: 100003,
seller_id: 2,
id: '90bfe00d-832d-4459-9537-ebc47418965a',
seller_id: 'd699cb8f-cb9e-4301-ad12-d7d859cc6f3f',
name: 'Surface Pro 8',
cat_name: 'Laptop',
desc: 'The latest Surface Pro',
Expand Down
2 changes: 1 addition & 1 deletion src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class AuthService {

async createAccessToken(data: {
role: ROLE_TYPE;
userId: number;
userId: string;
}): Promise<TokenPayloadDto> {
return new TokenPayloadDto({
expiresIn: toSeconds(required('JWT_EXPIRES_IN') as string),
Expand Down
2 changes: 1 addition & 1 deletion src/modules/auth/dto/access-token-payload.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ApiProperty } from '@nestjs/swagger';

export class AccessTokenPayload {
@ApiProperty()
userId: number;
userId: string;

@ApiProperty()
type: TOKEN_TYPE;
Expand Down
6 changes: 3 additions & 3 deletions src/modules/carts/cart-items.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ export class CartItemsRepository {
return await this.model.save(cartItemEntity);
}

async findAllCartData(cartId: number): Promise<CartItemEntity[]> {
async findAllCartData(cartId: string): Promise<CartItemEntity[]> {
if (!cartId) return null;
return await this.model.find({
where: { cart_id: cartId },
});
}

async findCartItemByCartId(
cartId: number,
productId: number,
cartId: string,
productId: string,
): Promise<CartItemEntity> {
if (!cartId) return null;
return await this.model.findOne({
Expand Down
5 changes: 2 additions & 3 deletions src/modules/carts/carts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Patch,
Param,
Delete,
ParseIntPipe,
} from '@nestjs/common';
import { CartsService } from './carts.service';
import { CreateCartItemsDto } from './dto/create-cart.dto';
Expand Down Expand Up @@ -43,7 +42,7 @@ export class CartsController {
@Patch(':id')
@Auth([ROLE_TYPE.ADMIN])
async updateQuantity(
@Param('id', ParseIntPipe) productId: number,
@Param('id') productId: string,
@AuthUser() tokenPayload: AccessTokenPayload,
@Body() UpdateCartItemsDto: UpdateCartItemsDto,
) {
Expand All @@ -57,7 +56,7 @@ export class CartsController {
@Delete(':id')
@Auth([ROLE_TYPE.ADMIN])
async removeCart(
@Param('id', ParseIntPipe) productId: number,
@Param('id') productId: string,
@AuthUser() tokenPayload: AccessTokenPayload,
) {
return this.cartsService.remove(tokenPayload.userId, productId);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/carts/carts.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class CartsRepository {
return await this.model.save(cartEntity);
}

async findCart(userId: number): Promise<CartEntity> {
async findCart(userId: string): Promise<CartEntity> {
if (!userId) return null;
return await this.model.findOne({
where: { customer_id: userId },
Expand Down
18 changes: 9 additions & 9 deletions src/modules/carts/carts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export class CartsService {
private readonly cartItemRepo: CartItemsRepository,
) {}

async create(userId: number, createCartItemsDto: CreateCartItemsDto) {
async create(userId: string, createCartItemsDto: CreateCartItemsDto) {
try {
let cartId: number | null;
let cartId: string | null;
const cart = await this.repo.findCart(userId);
if (!cart) {
const newCart = await this.createCart(userId);
Expand All @@ -40,7 +40,7 @@ export class CartsService {
}
}

async getCartData(id: number) {
async getCartData(id: string) {
try {
const cartData = await this.repo.findCart(id);
const cartId = cartData.id;
Expand All @@ -58,8 +58,8 @@ export class CartsService {
}

async update(
userId: number,
productId: number,
userId: string,
productId: string,
updateCartItemsDto: UpdateCartItemsDto,
) {
try {
Expand All @@ -85,7 +85,7 @@ export class CartsService {
}
}

async remove(userId: number, productId: number) {
async remove(userId: string, productId: string) {
try {
const cartData = await this.repo.findCart(userId);
const cartId = cartData.id;
Expand All @@ -104,7 +104,7 @@ export class CartsService {
}
}

private async createCart(id: number) {
private async createCart(id: string) {
try {
const cart = CartEntity.create({
customer_id: id,
Expand All @@ -118,8 +118,8 @@ export class CartsService {
}

private async createCartItem(
cart_id: number,
product_id: number,
cart_id: string,
product_id: string,
quantity: number,
) {
try {
Expand Down
Loading

0 comments on commit e87243f

Please sign in to comment.