salix/services/client/common/models/client-sample.js

21 lines
601 B
JavaScript

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)
throw new Error('Choose a company');
let filter = {where: {userFk: ctx.options.accessToken.userId}};
let worker = await Self.app.models.Worker.findOne(filter);
data.workerFk = worker.id;
});
};