All files / src/domains/crm/schemas pipeline.schema.ts

0% Statements 0/55
0% Branches 0/44
100% Functions 0/0
0% Lines 0/47

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                                                                                                                                                                           
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema } from 'mongoose';
 
// ════════════════════════════════════════════════════════════════
// SALES PIPELINES & STAGES
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'sales_pipelines', timestamps: true })
export class SalesPipeline extends Document {
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true, index: true })
  tenantId!: MongooseSchema.Types.ObjectId;
 
  @Prop({ required: true }) pipelineName!: string;
  @Prop() description?: string;
  @Prop({ default: false }) isDefault!: boolean;
  @Prop({ default: true }) active!: boolean;
  @Prop({ default: 1 }) currentVersion!: number;
}
export const SalesPipelineSchema = SchemaFactory.createForClass(SalesPipeline);
SalesPipelineSchema.index({ tenantId: 1, pipelineName: 1 }, { unique: true });
 
@Schema({ collection: 'sales_pipeline_versions', timestamps: true })
export class SalesPipelineVersion extends Document {
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true, index: true })
  tenantId!: MongooseSchema.Types.ObjectId;
 
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true })
  pipelineId!: MongooseSchema.Types.ObjectId;
  @Prop({ required: true }) versionNumber!: number;
  @Prop({ default: 'draft', enum: ['draft', 'published', 'archived'] })
  status!: string;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  publishedBy?: MongooseSchema.Types.ObjectId;
  @Prop() publishedAt?: Date;
}
export const SalesPipelineVersionSchema =
  SchemaFactory.createForClass(SalesPipelineVersion);
SalesPipelineVersionSchema.index(
  { tenantId: 1, pipelineId: 1, versionNumber: 1 },
  { unique: true },
);
 
@Schema({ collection: 'sales_stages', timestamps: true })
export class SalesStage extends Document {
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true, index: true })
  tenantId!: MongooseSchema.Types.ObjectId;
 
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true })
  pipelineId!: MongooseSchema.Types.ObjectId;
  @Prop({ required: true }) pipelineVersionId!: string;
  @Prop({ required: true }) stageCode!: string;
  @Prop({ required: true }) stageName!: string;
  @Prop({ required: true }) sequence!: number;
  @Prop({ default: 0 }) probability!: number;
  @Prop({
    default: 'pipeline',
    enum: ['pipeline', 'best_case', 'commit', 'closed', 'omitted'],
  })
  forecastCategory!: string;
  @Prop({ default: 14 }) expectedDurationDays!: number;
  @Prop({ type: [String], default: [] }) requiredFields!: string[];
  @Prop({ type: [String], default: [] }) requiredActivities!: string[];
  @Prop({ default: false }) approvalRequired!: boolean;
  @Prop({ default: false }) winStage!: boolean;
  @Prop({ default: false }) lossStage!: boolean;
  @Prop({ default: true }) active!: boolean;
}
export const SalesStageSchema = SchemaFactory.createForClass(SalesStage);
SalesStageSchema.index({ tenantId: 1, pipelineId: 1, stageCode: 1 });
 
@Schema({ collection: 'sales_stage_transitions', timestamps: true })
export class SalesStageTransition extends Document {
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true, index: true })
  tenantId!: MongooseSchema.Types.ObjectId;
 
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true })
  pipelineId!: MongooseSchema.Types.ObjectId;
  @Prop({ required: true }) fromStageCode!: string;
  @Prop({ required: true }) toStageCode!: string;
  @Prop({ default: true }) allowed!: boolean;
  @Prop({ default: false }) requiresApproval!: boolean;
  @Prop() automationAction?: string;
}
export const SalesStageTransitionSchema =
  SchemaFactory.createForClass(SalesStageTransition);