Skip to content

Commit

Permalink
Feat: Coupon 모델에 type 필드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
suhye0n committed Nov 12, 2024
1 parent cadd20a commit 8ac6f9f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/dtos/coupons.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IsString, IsEmail, IsNotEmpty, MinLength, MaxLength, IsInt, IsOptional } from 'class-validator';
import { IsString, IsInt, IsNotEmpty, IsOptional, IsEnum } from 'class-validator';
import { CouponType } from '@interfaces/coupons.interface';

export class CreateCouponDto {
@IsNotEmpty()
Expand All @@ -20,6 +21,10 @@ export class CreateCouponDto {
@IsOptional()
@IsInt()
public tagId?: number;

@IsNotEmpty()
@IsEnum(CouponType)
public type: CouponType;
}

export class UpdateCouponDto {
Expand All @@ -38,4 +43,8 @@ export class UpdateCouponDto {
@IsOptional()
@IsInt()
public tagId?: number;

@IsOptional()
@IsEnum(CouponType)
public type?: CouponType;
}
6 changes: 6 additions & 0 deletions src/interfaces/coupons.interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
export enum CouponType {
COUPON = 'COUPON',
CARD = 'CARD',
}

export interface Coupon {
id?: number;
userId: number;
name: string;
barcode: string;
memo?: string;
tagId?: number;
type: CouponType;
}
10 changes: 8 additions & 2 deletions src/models/coupons.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Sequelize, DataTypes, Model, Optional } from 'sequelize';
import { Coupon } from '@/interfaces/coupons.interface';
import { Coupon, CouponType } from '@interfaces/coupons.interface';

export type CouponCreationAttributes = Optional<Coupon, 'id' | 'barcode' | 'memo' | 'userId'>;
export type CouponCreationAttributes = Optional<Coupon, 'id' | 'barcode' | 'memo' | 'userId' | 'type'>;

export class CouponModel extends Model<Coupon, CouponCreationAttributes> implements Coupon {
public id: number;
Expand All @@ -10,6 +10,7 @@ export class CouponModel extends Model<Coupon, CouponCreationAttributes> impleme
public barcode: string;
public memo?: string;
public tagId?: number;
public type: CouponType;

public readonly createdAt!: Date;
public readonly updatedAt!: Date;
Expand Down Expand Up @@ -55,6 +56,11 @@ export default function (sequelize: Sequelize): typeof CouponModel {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
},
type: {
type: DataTypes.ENUM(...Object.values(CouponType)),
allowNull: false,
defaultValue: CouponType.COUPON,
},
},
{
tableName: 'coupons',
Expand Down
2 changes: 1 addition & 1 deletion src/services/coupons.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class CouponService {

public async createCoupon(couponData: CreateCouponDto): Promise<Coupon> {
const existingCoupon: Coupon = await DB.Coupons.findOne({
where: { barcode: couponData.barcode, userId: couponData.userId },
where: { barcode: couponData.barcode, userId: couponData.userId, type: couponData.type },
});
if (existingCoupon) throw new HttpException(409, `This barcode ${couponData.barcode} already exists`);

Expand Down

0 comments on commit 8ac6f9f

Please sign in to comment.