All files / src/platform/reports/schemas report.schema.ts

0% Statements 0/51
100% Branches 0/0
100% Functions 0/0
0% Lines 0/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                                                                                                                                                                                                                                         
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema } from 'mongoose';
 
@Schema({ timestamps: true, collection: 'report_data_sources' })
export class ReportDataSource extends Document {
  @Prop({ required: true, unique: true, index: true })
  key: string; // e.g. 'users', 'invoices', 'deals'
 
  @Prop({ required: true })
  name: string;
 
  @Prop({ default: null })
  moduleKey: string;
 
  @Prop({ required: true })
  collectionName: string;
 
  @Prop({ type: [MongooseSchema.Types.Mixed], default: [] })
  fields: any[]; // Array of { fieldKey, label, type, filterable, sortable, aggregatable }
 
  @Prop({ default: true })
  isActive: boolean;
}
export const ReportDataSourceSchema =
  SchemaFactory.createForClass(ReportDataSource);
 
@Schema({ timestamps: true, collection: 'report_definitions' })
export class ReportDefinition extends Document {
  @Prop({ required: true, index: true })
  tenantId: string;
 
  @Prop({ required: true })
  name: string;
 
  @Prop({ default: null })
  description: string;
 
  @Prop({ required: true })
  dataSourceKey: string;
 
  @Prop({ default: null })
  moduleKey: string;
 
  @Prop({ type: MongooseSchema.Types.Mixed, default: {} })
  queryConfig: any; // { filters: [], sort: [], groupBy: [], aggregations: [], calculatedFields: [] }
 
  @Prop({ type: MongooseSchema.Types.Mixed, default: null })
  chartConfig: any;
 
  @Prop({ type: MongooseSchema.Types.Mixed, default: null })
  pivotConfig: any;
 
  @Prop({ default: false })
  isShared: boolean;
 
  @Prop({ default: null })
  createdBy: string;
 
  @Prop({ default: 1000 })
  maxResultCount: number;
 
  @Prop({ default: 30000 })
  maxQueryDurationMs: number;
}
export const ReportDefinitionSchema =
  SchemaFactory.createForClass(ReportDefinition);
ReportDefinitionSchema.index({ tenantId: 1, createdBy: 1 });
 
@Schema({ timestamps: true, collection: 'report_schedules' })
export class ReportSchedule extends Document {
  @Prop({ required: true, index: true })
  reportId: string;
 
  @Prop({ required: true })
  cron: string;
 
  @Prop({ type: [String], default: [] })
  recipients: string[]; // User IDs
 
  @Prop({ required: true, default: 'CSV' })
  fileFormat: string; // 'CSV' | 'PDF' | 'EXCEL'
 
  @Prop({ default: true })
  isActive: boolean;
}
export const ReportScheduleSchema =
  SchemaFactory.createForClass(ReportSchedule);
 
@Schema({ timestamps: true, collection: 'report_runs' })
export class ReportRun extends Document {
  @Prop({ required: true, index: true })
  reportId: string;
 
  @Prop({ required: true, index: true })
  tenantId: string;
 
  @Prop({ required: true, default: 'PENDING' })
  status: string; // 'PENDING' | 'RUNNING' | 'COMPLETED' | 'FAILED'
 
  @Prop({ default: null })
  generatedFileUrl: string;
 
  @Prop({ default: 0 })
  rowCount: number;
 
  @Prop({ default: 0 })
  executionTimeMs: number;
 
  @Prop({ default: null })
  error: string;
 
  @Prop({ default: null })
  triggeredBy: string;
}
export const ReportRunSchema = SchemaFactory.createForClass(ReportRun);
ReportRunSchema.index({ tenantId: 1, status: 1 });