All files / src/platform/integrations/schemas integration.schema.ts

0% Statements 0/88
0% Branches 0/75
100% Functions 0/0
0% Lines 0/74

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                                                                                                                                                                                                                                                                                                                               
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema } from 'mongoose';
 
export type IntegrationStatus =
  | 'draft'
  | 'configured'
  | 'connected'
  | 'disconnected'
  | 'degraded'
  | 'failed'
  | 'suspended'
  | 'expired';
 
export type IntegrationCategory =
  | 'email'
  | 'sms'
  | 'whatsapp'
  | 'push'
  | 'payment'
  | 'social-login'
  | 'calendar'
  | 'video'
  | 'storage'
  | 'analytics'
  | 'maps'
  | 'biometric'
  | 'accounting'
  | 'crm-connector'
  | 'erp-connector'
  | 'webhook'
  | 'custom';
 
@Schema({ collection: 'integration_definitions', timestamps: true })
export class IntegrationDefinition extends Document {
  @Prop({ required: true, unique: true }) key!: string;
  @Prop({ required: true }) name!: string;
  @Prop() description?: string;
  @Prop({ required: true }) category!: IntegrationCategory;
  @Prop({ required: true }) providerClass!: string;
  @Prop({ default: true }) isSystemProvider!: boolean;
  @Prop({ default: true }) isActive!: boolean;
  @Prop({ default: false }) requiresOAuth!: boolean;
  @Prop({ type: [String], default: [] }) requiredScopes!: string[];
  @Prop({ type: [String], default: [] }) configFields!: string[];
  @Prop({ type: MongooseSchema.Types.Mixed, default: {} }) metadata!: Record<
    string,
    unknown
  >;
}
export const IntegrationDefinitionSchema = SchemaFactory.createForClass(
  IntegrationDefinition,
);
 
@Schema({ collection: 'tenant_integrations', timestamps: true })
export class TenantIntegration extends Document {
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true, index: true })
  tenantId!: MongooseSchema.Types.ObjectId;
  @Prop({ required: true }) integrationKey!: string;
  @Prop({ required: true }) status!: IntegrationStatus;
  @Prop({ type: MongooseSchema.Types.Mixed, default: {} }) settings!: Record<
    string,
    unknown
  >;
  @Prop() connectedAt?: Date;
  @Prop() disconnectedAt?: Date;
  @Prop() lastHealthCheckAt?: Date;
  @Prop() lastHealthCheckResult?: 'ok' | 'degraded' | 'failed';
  @Prop({ type: MongooseSchema.Types.ObjectId })
  connectedBy?: MongooseSchema.Types.ObjectId;
}
export const TenantIntegrationSchema =
  SchemaFactory.createForClass(TenantIntegration);
TenantIntegrationSchema.index(
  { tenantId: 1, integrationKey: 1 },
  { unique: true },
);
 
@Schema({ collection: 'integration_credentials', timestamps: true })
export class IntegrationCredential extends Document {
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true, index: true })
  tenantId!: MongooseSchema.Types.ObjectId;
  @Prop({ required: true }) integrationKey!: string;
  @Prop({ required: true }) encryptedCredentials!: string; // AES-256-GCM encrypted blob
  @Prop() iv?: string;
  @Prop() expiresAt?: Date;
  @Prop() refreshToken?: string; // encrypted separately
}
export const IntegrationCredentialSchema = SchemaFactory.createForClass(
  IntegrationCredential,
);
IntegrationCredentialSchema.index(
  { tenantId: 1, integrationKey: 1 },
  { unique: true },
);
 
@Schema({ collection: 'integration_webhooks', timestamps: true })
export class IntegrationWebhook extends Document {
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true, index: true })
  tenantId!: MongooseSchema.Types.ObjectId;
  @Prop({ required: true }) integrationKey!: string;
  @Prop({ required: true }) event!: string;
  @Prop({ required: true }) webhookUrl!: string;
  @Prop() signingSecret?: string;
  @Prop({ default: true }) isActive!: boolean;
  @Prop({ default: 0 }) totalDeliveries!: number;
  @Prop({ default: 0 }) failedDeliveries!: number;
}
export const IntegrationWebhookSchema =
  SchemaFactory.createForClass(IntegrationWebhook);
 
@Schema({ collection: 'integration_logs', timestamps: true })
export class IntegrationLog extends Document {
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true, index: true })
  tenantId!: MongooseSchema.Types.ObjectId;
  @Prop({ required: true }) integrationKey!: string;
  @Prop({ required: true }) action!: string;
  @Prop({ required: true, enum: ['success', 'failure', 'warning'] })
  level!: string;
  @Prop() message?: string;
  @Prop({ type: MongooseSchema.Types.Mixed }) metadata?: Record<
    string,
    unknown
  >;
  @Prop({ default: 0 }) durationMs!: number;
}
export const IntegrationLogSchema =
  SchemaFactory.createForClass(IntegrationLog);
 
@Schema({ collection: 'integration_health_checks', timestamps: true })
export class IntegrationHealthCheck extends Document {
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true, index: true })
  tenantId!: MongooseSchema.Types.ObjectId;
  @Prop({ required: true }) integrationKey!: string;
  @Prop({ required: true, enum: ['ok', 'degraded', 'failed'] }) status!: string;
  @Prop() message?: string;
  @Prop({ default: 0 }) latencyMs!: number;
  @Prop({ required: true }) checkedAt!: Date;
}
export const IntegrationHealthCheckSchema = SchemaFactory.createForClass(
  IntegrationHealthCheck,
);
 
@Schema({ collection: 'integration_sync_jobs', timestamps: true })
export class IntegrationSyncJob extends Document {
  @Prop({ type: MongooseSchema.Types.ObjectId, required: true, index: true })
  tenantId!: MongooseSchema.Types.ObjectId;
  @Prop({ required: true }) integrationKey!: string;
  @Prop({ required: true, enum: ['pending', 'running', 'completed', 'failed'] })
  status!: string;
  @Prop() lastSyncAt?: Date;
  @Prop() nextSyncAt?: Date;
  @Prop() lastCursor?: string;
  @Prop({ default: 0 }) totalSynced!: number;
  @Prop({ default: 0 }) totalFailed!: number;
  @Prop({ type: [MongooseSchema.Types.Mixed], default: [] })
  validationErrors!: Record<string, unknown>[];
}
export const IntegrationSyncJobSchema =
  SchemaFactory.createForClass(IntegrationSyncJob);