salix/modules/travel/back/methods/thermograph/createThermograph.js

73 lines
2.0 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
2023-06-01 06:32:06 +00:00
Self.remoteMethodCtx('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'
}
});
2023-06-01 06:32:06 +00:00
Self.createThermograph = async(ctx, thermographId, model, temperatureFk, warehouseId, options) => {
const models = Self.app.models;
let tx;
2023-06-01 06:32:06 +00:00
const myOptions = {userId: ctx.req.accessToken.userId};
2023-01-16 14:18:24 +00:00
const date = Date.vnNew();
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)
2022-06-09 11:33:01 +00:00
VALUES (?, ?, ?, ?)
`, [thermograph.id, warehouseId, temperatureFk, date], myOptions);
if (tx) await tx.commit();
return thermograph;
} catch (err) {
if (tx) await tx.rollback();
throw err;
}
};
};