File size: 3,587 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
var mongoose = require('mongoose'); //.set('debug', true);
var Schema = mongoose.Schema;

var CustomFieldSchema = new Schema(
  {
    fieldType: String,
    label: String,
    display: String,
    displaySub: { type: String, default: '' },
    position: Number,
    size: {
      type: Number,
      enum: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      default: 12,
    },
    offset: {
      type: Number,
      enum: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      default: 0,
    },
    required: { type: Boolean, default: false },
    description: { type: String, default: '' },
    text: [{ _id: false, locale: String, value: Schema.Types.Mixed }],
    options: [{ _id: false, locale: String, value: String }],
  },
  { timestamps: true },
);

CustomFieldSchema.index(
  { label: 1, display: 1, displaySub: 1 },
  {
    name: 'unique_label_display',
    unique: true,
    partialFilterExpression: { label: { $exists: true, $gt: '' } },
  },
);

/*
 *** Statics ***
 */

// Get all Fields
CustomFieldSchema.statics.getAll = () => {
  return new Promise((resolve, reject) => {
    var query = CustomField.find().sort('position');
    query.select(
      'fieldType label display displaySub size offset required description text options',
    );
    query
      .exec()
      .then(rows => {
        resolve(rows);
      })
      .catch(err => {
        reject(err);
      });
  });
};

// Create Field
CustomFieldSchema.statics.create = field => {
  return new Promise((resolve, reject) => {
    var query = new CustomField(field);
    query
      .save()
      .then(row => {
        resolve(row);
      })
      .catch(err => {
        if (err.code === 11000)
          reject({
            fn: 'BadParameters',
            message: 'Custom Field already exists',
          });
        else reject(err);
      });
  });
};

// Update Fields
CustomFieldSchema.statics.updateAll = fields => {
  return new Promise((resolve, reject) => {
    var promises = fields.map(field => {
      return CustomField.findByIdAndUpdate(field._id, field).exec();
    });
    return Promise.all(promises)
      .then(row => {
        resolve('Fields updated successfully');
      })
      .catch(err => {
        reject(err);
      });
  });
};

// Delete Field
CustomFieldSchema.statics.delete = fieldId => {
  return new Promise((resolve, reject) => {
    var pullCount = 0;
    var Vulnerability = mongoose.model('Vulnerability');
    var query = Vulnerability.find();
    query
      .exec()
      .then(rows => {
        var promises = [];
        promises.push(CustomField.findByIdAndDelete(fieldId).exec());
        rows.map(row => {
          row.details.map(detail => {
            if (
              detail.customFields.some(
                field => `${field.customField}` === fieldId,
              )
            )
              pullCount++;

            detail.customFields.pull({ customField: fieldId });
          });
          promises.push(row.save());
        });
        return Promise.all(promises);
      })
      .then(row => {
        if (row && row[0])
          resolve({
            msg: `Custom Field deleted successfully`,
            vulnCount: pullCount,
          });
        else
          reject({
            fn: 'NotFound',
            message: { msg: 'Custom Field not found', vulnCount: pullCount },
          });
      })
      .catch(err => {
        console.log(err);
        reject(err);
      });
  });
};

/*
 *** Methods ***
 */

var CustomField = mongoose.model('CustomField', CustomFieldSchema);
CustomField.syncIndexes();
module.exports = CustomField;