Skip to content

Instantly share code, notes, and snippets.

@josedaniel josedaniel/Quote.js
Last active Jul 16, 2018

Embed
What would you like to do?
Regular Vulcano CRUD model
/* global mongoose, Quote, VSError */
/**
* Quote.js
*/
module.exports = {
attributes: {
active: {
type: Boolean,
required: true,
default: true
},
createdBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
clienteNit: {
type: String,
required: true
},
clienteRazonSocial: {
type: String,
required: true
},
clienteContacto: {
type: String,
required: true
},
clienteTelefono: {
type: String,
required: false
},
clienteEmail: {
type: String,
required: true
},
obra: {
type: String,
required: true
},
duracion: {
type: String,
required: true,
default: 0
},
precio: {
type: String,
required: true
},
transporte: {
type: Number,
default: 0,
required: true
},
observaciones: {
type: String,
required: true
},
modulos: {
type: mongoose.Schema.Types.Mixed
},
createdAt: {
type: Date,
default: Date.now
},
updatedAt: {
type: Date,
default: Date.now
}
},
beforeSave: function beforeSaveCallback (next) {
/* Modify this as needed, if aplicable */
// const data = this;
// this.price = (Number(data.price) || 0).toFixed(2);
next();
},
beforeFindOneAndUpdate: function beforeFindOneAndUpdateCallback (next) {
/* Modify this as needed, if aplicable */
// const data = this._update;
// if (data.price !== undefined) {
// this._update.price = (Number(data.price) || 0).toFixed(2);
// }
this._update.updatedAt = new Date();
next();
},
getQuote: (id) => {
return Promise.resolve().then( () => {
if (!(/^[a-fA-F0-9]{24}$/).test(id)) {
console.log(id);
return VSError.notFound();
}
return Quote.findOne({ _id: id }).then( (r) => {
if (!r) {
return VSError.notFound();
}
return r;
});
});
},
save: (data) => {
const obj = new Quote(data);
return obj.save();
},
update: (id, data) => {
return Quote.getQuote(id).then( (i) => {
const quote = i.toObject({ transform: true });
const merge = Object.assign({}, quote, data);
return Quote.findOneAndUpdate({ _id: id }, merge, { new: true });
});
},
delete: (id) => {
return Quote.update(id, { active: false }).then( (r) => {
if (!r) {
return VSError.notFound();
}
return {};
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.