All files / services/config-service/src/models Workflow.js

75.14% Statements 127/169
100% Branches 1/1
0% Functions 0/3
75.14% Lines 127/169

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 161 162 163 164 165 166 167 168 169 1701x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x 1x 1x           1x 1x 1x 1x                                                               1x 1x 1x  
const mongoose = require('mongoose');
 
/**
 * Schéma pour les champs d'un JSON Schema (mode 'data')
 * Supporte la récursion pour les objets imbriqués
 */
const schemaFieldSchema = new mongoose.Schema({
  type: {
    type: String,
    enum: ['string', 'number', 'integer', 'boolean', 'array', 'object'],
    required: true
  },
  description: {
    type: String,
    default: ''
  },
  required: {
    type: Boolean,
    default: false
  },
  // Pour les arrays: type des items
  items: {
    type: mongoose.Schema.Types.Mixed,
    default: null
  },
  // Pour les objets: propriétés imbriquées
  properties: {
    type: mongoose.Schema.Types.Mixed,
    default: null
  }
}, { _id: false });
 
/**
 * Schéma pour la spécification selon le mode
 */
const specSchema = new mongoose.Schema({
  // Mode FACT: instruction textuelle
  instruction: {
    type: String,
    default: ''
  },
  maxFacts: {
    type: Number,
    default: 100,
    min: 1,
    max: 1000
  },
  
  // Mode DATA: JSON Schema avec descriptions
  schema: {
    type: mongoose.Schema.Types.Mixed,
    default: null
  }
}, { _id: false });
 
/**
 * Modèle Workflow unifié
 * Représente une config métier de synthèse (visible utilisateur)
 * Supporte deux modes: 'fact' et 'data'
 */
const workflowSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true,
    index: true
  },
  name: {
    type: String,
    required: true,
    trim: true
  },
  description: {
    type: String,
    default: ''
  },
  
  // Mode de synthèse: 'fact' ou 'data'
  mode: {
    type: String,
    enum: ['fact', 'data'],
    required: true,
    index: true
  },
  
  // Spécification selon le mode
  spec: {
    type: specSchema,
    required: true
  },
  
  // Router optionnel pour le routage des résultats
  routerId: {
    type: String,
    default: ''
  },
  
  // Bases de connaissances cibles
  targetKnowledgeBases: [{
    type: String
  }],
  
  // Statut actif
  isActive: {
    type: Boolean,
    default: true
  }
}, {
  timestamps: true
});
 
// Index composé pour les requêtes courantes
workflowSchema.index({ userId: 1, isActive: 1, mode: 1 });
 
// Méthode d'instance pour obtenir le schéma formaté (mode data)
workflowSchema.methods.getFormattedSchema = function() {
  if (this.mode !== 'data' || !this.spec || !this.spec.schema) {
    return null;
  }
  
  // Retourne le schéma avec les descriptions pour le prompt LLM
  return this.spec.schema;
};
 
// Méthode d'instance pour obtenir l'instruction (mode fact)
workflowSchema.methods.getInstruction = function() {
  if (this.mode !== 'fact' || !this.spec) {
    return '';
  }
  
  return this.spec.instruction || '';
};
 
// Méthode statique pour valider la cohérence mode/spec
workflowSchema.statics.validateSpec = function(mode, spec) {
  const errors = [];
  
  if (!spec) {
    errors.push('La spécification (spec) est requise');
    return errors;
  }
  
  if (mode === 'fact') {
    // Mode FACT: instruction requise
    if (!spec.instruction || spec.instruction.trim() === '') {
      errors.push('Mode FACT: l\'instruction est requise');
    }
    if (!spec.maxFacts || spec.maxFacts < 1 || spec.maxFacts > 1000) {
      errors.push('Mode FACT: maxFacts doit être entre 1 et 1000');
    }
  } else if (mode === 'data') {
    // Mode DATA: schema requis et valide
    if (!spec.schema) {
      errors.push('Mode DATA: le schéma JSON est requis');
    } else {
      // Validation basique du JSON Schema
      if (!spec.schema.type) {
        errors.push('Mode DATA: le schéma doit avoir un type');
      }
      if (spec.schema.type !== 'object' && spec.schema.type !== 'array') {
        errors.push('Mode DATA: le type racine doit être "object" ou "array"');
      }
    }
  }
  
  return errors;
};
 
module.exports = mongoose.model('Workflow', workflowSchema);