2018-03-15 13:36:09 +00:00
|
|
|
let UserError = require('../helpers').UserError;
|
2018-03-13 13:46:39 +00:00
|
|
|
|
2018-05-04 09:46:03 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
require('../methods/item/filter')(Self);
|
|
|
|
require('../methods/item/clone')(Self);
|
|
|
|
require('../methods/item/updateTaxes')(Self);
|
2018-01-10 11:49:52 +00:00
|
|
|
|
|
|
|
Self.validatesPresenceOf('name', {message: 'Cannot be blank'});
|
|
|
|
Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'});
|
2018-03-13 13:46:39 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-12-19 11:29:35 +00:00
|
|
|
};
|