31 lines
960 B
JavaScript
31 lines
960 B
JavaScript
let UserError = require('../helpers').UserError;
|
|
|
|
module.exports = function(Self) {
|
|
require('../methods/item/filter.js')(Self);
|
|
require('../methods/item/clone.js')(Self);
|
|
require('../methods/item/updateTaxes.js')(Self);
|
|
|
|
Self.validatesPresenceOf('name', {message: 'Cannot be blank'});
|
|
Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'});
|
|
|
|
Self.observe('before save', async function(ctx) {
|
|
await Self.availableId(ctx);
|
|
});
|
|
|
|
Self.availableId = async function(ctx) {
|
|
if (ctx.isNewInstance) {
|
|
try {
|
|
let query = `SELECT i1.id + 1 as id FROM vn.item i1
|
|
LEFT JOIN vn.item i2 ON i1.id + 1 = i2.id
|
|
WHERE i2.id IS NULL ORDER BY i1.id LIMIT 1`;
|
|
|
|
let newId = await Self.rawSql(query);
|
|
|
|
ctx.instance.id = newId[0].id;
|
|
} catch (e) {
|
|
throw new UserError(e);
|
|
}
|
|
}
|
|
};
|
|
};
|