import { Column, CreateDateColumn, Entity, JoinColumn, ManyToOne, OneToMany, OneToOne, PrimaryGeneratedColumn, UpdateDateColumn, } from 'typeorm'; import { Document } from '~/document/entities'; import { Guardian } from '~/guardian/entities/guradian.entity'; import { Junior } from '~/junior/entities'; import { GiftColor, GiftStatus } from '../enums'; import { GiftRedemption } from './gift-redemption.entity'; import { GiftReply } from './gift-reply.entity'; @Entity('gift') export class Gift { @PrimaryGeneratedColumn('uuid') id!: string; @Column({ type: 'varchar', length: 255, name: 'name' }) name!: string; @Column({ type: 'text', name: 'description' }) description!: string; @Column({ type: 'varchar', name: 'color' }) color!: GiftColor; @Column({ type: 'decimal', name: 'amount', precision: 10, scale: 3, transformer: { to: (value: number) => value, from: (value: string) => parseFloat(value), }, }) amount!: number; @Column({ type: 'varchar', name: 'status', default: GiftStatus.AVAILABLE }) status!: GiftStatus; @Column({ type: 'timestamp with time zone', name: 'redeemed_at', nullable: true }) redeemedAt!: Date | null; @Column({ type: 'uuid', name: 'giver_id' }) giverId!: string; @Column({ type: 'uuid', name: 'image_id' }) imageId!: string; @Column({ type: 'uuid', name: 'recipient_id' }) recipientId!: string; @ManyToOne(() => Guardian, (guardian) => guardian.gifts) @JoinColumn({ name: 'giver_id' }) giver!: Guardian; @ManyToOne(() => Junior, (junior) => junior.gifts) @JoinColumn({ name: 'recipient_id' }) recipient!: Junior; @ManyToOne(() => Document, (document) => document.gifts) @JoinColumn({ name: 'image_id' }) image!: Document; @OneToMany(() => GiftRedemption, (giftRedemption) => giftRedemption.gift, { cascade: true }) redemptions!: GiftRedemption[]; @OneToOne(() => GiftReply, (giftReply) => giftReply.gift, { cascade: true }) reply?: GiftReply; @CreateDateColumn({ name: 'created_at', type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP' }) createdAt!: Date; @UpdateDateColumn({ name: 'updated_at', type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP' }) updatedAt!: Date; }