template-saas-backend-v2/src/modules/mlm/entities/commission.entity.ts
Adrian Flores Cortes c683ab0353 [SAAS-021] feat: Implement MLM module backend
- 6 entities: structure, rank, node, commission, bonus, rank_history
- 4 DTOs with validation
- 4 services with tree operations (LTREE path queries)
- 4 controllers with full CRUD
- Commission calculation and rank evaluation logic
- My network endpoints for user dashboard

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 06:48:44 -06:00

92 lines
2.2 KiB
TypeScript

import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
ManyToOne,
JoinColumn,
} from 'typeorm';
import { NodeEntity } from './node.entity';
export enum CommissionType {
LEVEL = 'level',
MATCHING = 'matching',
INFINITY = 'infinity',
LEADERSHIP = 'leadership',
POOL = 'pool',
}
export enum CommissionStatus {
PENDING = 'pending',
APPROVED = 'approved',
PAID = 'paid',
CANCELLED = 'cancelled',
}
@Entity({ schema: 'mlm', name: 'commissions' })
export class CommissionEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ name: 'tenant_id', type: 'uuid' })
tenantId: string;
@Column({ name: 'node_id', type: 'uuid' })
nodeId: string;
@Column({ name: 'source_node_id', type: 'uuid' })
sourceNodeId: string;
@Column({
type: 'enum',
enum: CommissionType,
})
type: CommissionType;
@Column({ type: 'integer' })
level: number;
@Column({ name: 'source_amount', type: 'decimal', precision: 15, scale: 2 })
sourceAmount: number;
@Column({ name: 'rate_applied', type: 'decimal', precision: 10, scale: 4 })
rateApplied: number;
@Column({ name: 'commission_amount', type: 'decimal', precision: 15, scale: 2 })
commissionAmount: number;
@Column({ type: 'varchar', length: 3, default: 'USD' })
currency: string;
@Column({ name: 'period_id', type: 'uuid', nullable: true })
periodId: string | null;
@Column({ name: 'source_reference', type: 'varchar', length: 200, nullable: true })
sourceReference: string | null;
@Column({
type: 'enum',
enum: CommissionStatus,
default: CommissionStatus.PENDING,
})
status: CommissionStatus;
@Column({ name: 'paid_at', type: 'timestamptz', nullable: true })
paidAt: Date | null;
@Column({ type: 'text', nullable: true })
notes: string | null;
@CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
createdAt: Date;
// Relations
@ManyToOne(() => NodeEntity, (node) => node.commissionsReceived, { onDelete: 'CASCADE' })
@JoinColumn({ name: 'node_id' })
node: NodeEntity;
@ManyToOne(() => NodeEntity, (node) => node.commissionsGenerated, { onDelete: 'CASCADE' })
@JoinColumn({ name: 'source_node_id' })
sourceNode: NodeEntity;
}