72 lines
1.9 KiB
JavaScript
72 lines
1.9 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethod('createThermograph', {
|
|
description: 'Creates a new thermograph',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'thermographId',
|
|
type: 'string',
|
|
description: 'The thermograph id',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'model',
|
|
type: 'string',
|
|
description: 'The thermograph model',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'temperatureFk',
|
|
type: 'string',
|
|
description: 'The thermograph temperature',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'warehouseId',
|
|
type: 'number',
|
|
description: 'The warehouse id',
|
|
required: true
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/createThermograph`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.createThermograph = async(thermographId, model, temperatureFk, warehouseId, options) => {
|
|
const models = Self.app.models;
|
|
let tx;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const thermograph = await models.Thermograph.create({
|
|
id: thermographId,
|
|
model: model
|
|
}, myOptions);
|
|
|
|
await Self.rawSql(`
|
|
INSERT INTO travelThermograph(thermographFk, warehouseFk, temperatureFk, created)
|
|
VALUES (?, ?, ?, NOW())
|
|
`, [thermograph.id, warehouseId, temperatureFk], myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return thermograph;
|
|
} catch (err) {
|
|
if (tx) await tx.rollback();
|
|
throw err;
|
|
}
|
|
};
|
|
};
|