Files
dkala-api/src/modules/coupons/entities/coupon.entity.ts
T
2026-02-08 15:06:52 +03:30

72 lines
2.2 KiB
TypeScript

import { Entity, Index, ManyToOne, Property, Enum, Unique } from '@mikro-orm/core';
import { BaseEntity } from '../../../../common/entities/base.entity';
import { Shop } from ../../../ shops / entities / shop.entity';
import { normalizePhone } from '../../../utils/phone.util';
import { CouponType } from '../interface/coupon';
@Entity({ tableName: 'coupons' })
@Unique({ properties: ['code', 'shop'] })
@Index({ properties: ['shop', 'code', 'isActive'] })
@Index({ properties: ['shop', 'isActive'] })
@Index({ properties: ['code'] })
export class Coupon extends BaseEntity {
@ManyToOne(() => Shop)
shop!: Shop;
@Property()
code!: string;
@Property()
name!: string;
@Property({ type: 'text', nullable: true })
description?: string;
@Enum(() => CouponType)
type!: CouponType;
@Property({ type: 'decimal', precision: 10, scale: 0 })
value!: number; // Discount amount or percentage
@Property({ type: 'decimal', precision: 10, scale: 0, nullable: true })
maxDiscount?: number; // Maximum discount for percentage coupons
@Property({ type: 'decimal', precision: 10, scale: 0, nullable: true })
minOrderAmount?: number; // Minimum order amount to use coupon
@Property({ type: 'int', nullable: true })
maxUses?: number; // Maximum number of times coupon can be used
@Property({ type: 'int', default: 0 })
usedCount: number = 0; // Number of times coupon has been used
@Property({ type: 'int', nullable: true })
maxUsesPerUser?: number; // Maximum uses per user
@Property({ type: 'timestamptz', nullable: true })
startDate?: Date; // Coupon validity start date
@Property({ type: 'timestamptz', nullable: true })
endDate?: Date; // Coupon validity end date
@Property({ type: 'boolean', default: true })
isActive: boolean = true;
@Property({ type: 'json', nullable: true })
foodCategories?: string[]; // Array of category IDs
@Property({ type: 'json', nullable: true })
products?: string[]; // Array of product IDs
private _userPhone?: string;
@Property({ nullable: true })
get userPhone(): string | undefined {
return this._userPhone;
}
set userPhone(value: string | undefined) {
this._userPhone = value ? normalizePhone(value) : undefined;
}
}