Spaces:
Runtime error
Runtime error
File size: 3,092 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 |
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var VulnerabilityCategorySchema = new Schema(
{
name: { type: String, unique: true },
sortValue: { type: String, default: 'cvssScore' },
sortOrder: { type: String, enum: ['desc', 'asc'], default: 'desc' },
sortAuto: { type: Boolean, default: true },
},
{ timestamps: true },
);
/*
*** Statics ***
*/
// Get all vulnerabilityCategorys
VulnerabilityCategorySchema.statics.getAll = () => {
return new Promise((resolve, reject) => {
var query = VulnerabilityCategory.find();
query.select('name sortValue sortOrder sortAuto');
query
.exec()
.then(rows => {
resolve(rows);
})
.catch(err => {
reject(err);
});
});
};
// Create vulnerabilityCategory
VulnerabilityCategorySchema.statics.create = vulnerabilityCategory => {
return new Promise((resolve, reject) => {
var query = new VulnerabilityCategory(vulnerabilityCategory);
query
.save()
.then(row => {
resolve(row);
})
.catch(err => {
if (err.code === 11000)
reject({
fn: 'BadParameters',
message: 'Vulnerability Category name already exists',
});
else reject(err);
});
});
};
// Update vulnerabilityCategory
VulnerabilityCategorySchema.statics.update = (name, vulnerabilityCategory) => {
return new Promise((resolve, reject) => {
var query = VulnerabilityCategory.findOneAndUpdate(
{ name: name },
vulnerabilityCategory,
);
query
.exec()
.then(row => {
if (row) resolve(row);
else
reject({
fn: 'NotFound',
message: 'Vulnerability category not found',
});
})
.catch(err => {
if (err.code === 11000)
reject({
fn: 'BadParameters',
message: 'Vulnerability Category already exists',
});
else reject(err);
});
});
};
// Update vulnerability Categories
VulnerabilityCategorySchema.statics.updateAll = vulnCategories => {
return new Promise((resolve, reject) => {
VulnerabilityCategory.deleteMany()
.then(row => {
VulnerabilityCategory.insertMany(vulnCategories);
})
.then(row => {
resolve('Vulnerability Categories updated successfully');
})
.catch(err => {
reject(err);
});
});
};
// Delete vulnerabilityCategory
VulnerabilityCategorySchema.statics.delete = name => {
return new Promise((resolve, reject) => {
VulnerabilityCategory.deleteOne({ name: name })
.then(res => {
if (res.deletedCount === 1) resolve('Vulnerability Category deleted');
else
reject({
fn: 'NotFound',
message: 'Vulnerability Category not found',
});
})
.catch(err => {
reject(err);
});
});
};
/*
*** Methods ***
*/
var VulnerabilityCategory = mongoose.model(
'VulnerabilityCategory',
VulnerabilityCategorySchema,
);
module.exports = VulnerabilityCategory;
|