2018-12-27 11:54:16 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2018-07-31 09:08:22 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.validatesPresenceOf('typeFk', {
|
|
|
|
message: 'Sample type cannot be blank'
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.observe('before save', async function(ctx) {
|
|
|
|
let models = Self.app.models;
|
|
|
|
let data = ctx.instance;
|
|
|
|
|
|
|
|
let sample = await models.Sample.findById(data.typeFk);
|
|
|
|
|
|
|
|
if (sample.hasCompany && !data.companyFk)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError('Choose a company');
|
2018-07-31 09:08:22 +00:00
|
|
|
|
|
|
|
let filter = {where: {userFk: ctx.options.accessToken.userId}};
|
|
|
|
let worker = await Self.app.models.Worker.findOne(filter);
|
|
|
|
|
|
|
|
data.workerFk = worker.id;
|
|
|
|
});
|
|
|
|
};
|