auditforge / backend /src /models /vulnerability-category.js
Kaballas's picture
initialize project structure with essential configurations and components
56b6519
raw
history blame
3.09 kB
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;