erp-mecanicas-diesel-backen.../src/modules/assets/entities/asset.entity.ts
Adrian Flores Cortes 89948663e9 feat(MMD-013): Implement Asset Management module
- 6 entities: Asset, AssetCategory, AssetAssignment, AssetAudit, AssetAuditItem, AssetMaintenance
- 4 services with CRUD, assignment, audit, and maintenance operations
- 4 controllers with REST endpoints
- Integration in main.ts with routes registration
- Multi-tenant support with RLS

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

164 lines
4.0 KiB
TypeScript

/**
* Asset Entity
* Mecánicas Diesel - ERP Suite
*
* Represents trackable assets and tools.
* Module: MMD-013 Asset Management
*/
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
ManyToOne,
OneToMany,
JoinColumn,
Index,
} from 'typeorm';
import { AssetCategory } from './asset-category.entity';
import { AssetAssignment } from './asset-assignment.entity';
import { AssetMaintenance } from './asset-maintenance.entity';
export enum AssetStatus {
AVAILABLE = 'available',
ASSIGNED = 'assigned',
IN_MAINTENANCE = 'in_maintenance',
DAMAGED = 'damaged',
RETIRED = 'retired',
}
export enum AssetLocation {
WAREHOUSE = 'warehouse',
UNIT = 'unit',
TECHNICIAN = 'technician',
EXTERNAL = 'external',
}
export enum CriticalityLevel {
LOW = 'low',
MEDIUM = 'medium',
HIGH = 'high',
CRITICAL = 'critical',
}
export enum AssigneeType {
EMPLOYEE = 'employee',
UNIT = 'unit',
LOCATION = 'location',
}
@Entity({ name: 'assets', schema: 'assets' })
@Index('idx_assets_tenant', ['tenantId'])
@Index('idx_assets_category', ['categoryId'])
@Index('idx_assets_status', ['status'])
@Index('idx_assets_location', ['currentLocation'])
@Index('idx_assets_assignee', ['currentAssigneeId'])
export class Asset {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ name: 'tenant_id', type: 'uuid' })
tenantId: string;
@Column({ type: 'varchar', length: 50 })
code: string;
@Column({ type: 'varchar', length: 150 })
name: string;
@Column({ type: 'text', nullable: true })
description?: string;
@Column({ name: 'category_id', type: 'uuid' })
categoryId: string;
@Column({ name: 'serial_number', type: 'varchar', length: 100, nullable: true })
serialNumber?: string;
@Column({ name: 'qr_code', type: 'varchar', length: 100, nullable: true })
qrCode?: string;
@Column({ type: 'varchar', length: 100, nullable: true })
barcode?: string;
@Column({ type: 'varchar', length: 100, nullable: true })
manufacturer?: string;
@Column({ type: 'varchar', length: 100, nullable: true })
model?: string;
@Column({ name: 'purchase_date', type: 'date', nullable: true })
purchaseDate?: Date;
@Column({ name: 'purchase_cost', type: 'decimal', precision: 12, scale: 2, nullable: true })
purchaseCost?: number;
@Column({ name: 'warranty_expiry', type: 'date', nullable: true })
warrantyExpiry?: Date;
@Column({
type: 'varchar',
length: 20,
default: AssetStatus.AVAILABLE,
})
status: AssetStatus;
@Column({
name: 'current_location',
type: 'varchar',
length: 20,
default: AssetLocation.WAREHOUSE,
})
currentLocation: AssetLocation;
@Column({ name: 'current_assignee_id', type: 'uuid', nullable: true })
currentAssigneeId?: string;
@Column({ name: 'current_assignee_type', type: 'varchar', length: 20, nullable: true })
currentAssigneeType?: AssigneeType;
@Column({
type: 'varchar',
length: 20,
default: CriticalityLevel.MEDIUM,
})
criticality: CriticalityLevel;
@Column({ name: 'requires_calibration', type: 'boolean', default: false })
requiresCalibration: boolean;
@Column({ name: 'last_calibration_date', type: 'date', nullable: true })
lastCalibrationDate?: Date;
@Column({ name: 'next_calibration_date', type: 'date', nullable: true })
nextCalibrationDate?: Date;
@Column({ name: 'photo_url', type: 'text', nullable: true })
photoUrl?: string;
@Column({ type: 'jsonb', default: {} })
metadata: Record<string, any>;
@CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
createdAt: Date;
@UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
updatedAt: Date;
@Column({ name: 'created_by', type: 'uuid', nullable: true })
createdBy?: string;
// Relations
@ManyToOne(() => AssetCategory)
@JoinColumn({ name: 'category_id' })
category: AssetCategory;
@OneToMany(() => AssetAssignment, assignment => assignment.asset)
assignments: AssetAssignment[];
@OneToMany(() => AssetMaintenance, maintenance => maintenance.asset)
maintenanceRecords: AssetMaintenance[];
}