refactor(thermograph): added temperature model + updated thermographs models and endpoints

This commit is contained in:
Carlos Jimenez Ruiz 2021-09-06 16:38:27 +02:00
parent ca82d7e759
commit 1f0bcb63f1
8 changed files with 109 additions and 68 deletions

View File

@ -0,0 +1 @@
alter table `vn`.`travelThermograph` modify `temperature` enum('COOL', 'WARM') null;

View File

@ -4,27 +4,30 @@ module.exports = Self => {
accessType: 'WRITE',
accepts: [{
arg: 'thermographId',
type: 'String',
type: 'string',
description: 'The thermograph id',
required: true
}, {
},
{
arg: 'model',
type: 'String',
type: 'string',
description: 'The thermograph model',
required: true
}, {
arg: 'temperature',
type: 'String',
},
{
arg: 'temperatureFk',
type: 'string',
description: 'The thermograph temperature',
required: true
}, {
},
{
arg: 'warehouseId',
type: 'Number',
type: 'number',
description: 'The warehouse id',
required: true
}],
returns: {
type: 'Object',
type: 'object',
root: true
},
http: {
@ -33,27 +36,35 @@ module.exports = Self => {
}
});
Self.createThermograph = async(thermographId, model, temperature, warehouseId) => {
Self.createThermograph = async(thermographId, model, temperatureFk, warehouseId, options) => {
const models = Self.app.models;
const tx = await Self.beginTransaction({});
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const options = {transaction: tx};
const thermograph = await models.Thermograph.create({
id: thermographId,
model: model
}, options);
}, myOptions);
await Self.rawSql(`
INSERT INTO travelThermograph(thermographFk, warehouseFk, temperature, created)
VALUES (?, ?,?, NOW())
`, [thermograph.id, warehouseId, temperature], options);
INSERT INTO travelThermograph(thermographFk, warehouseFk, temperatureFk, created)
VALUES (?, ?, ?, NOW())
`, [thermograph.id, warehouseId, temperatureFk], myOptions);
await tx.commit();
if (tx) await tx.commit();
return thermograph;
} catch (err) {
await tx.rollback();
if (tx) await tx.rollback();
throw err;
}
};

View File

@ -1,18 +0,0 @@
module.exports = Self => {
Self.remoteMethod('getThermographTemperatures', {
description: 'Gets the thermograph temperatures',
accessType: 'READ',
returns: {
type: ['String'],
root: true
},
http: {
path: `/getThermographTemperatures`,
verb: 'GET'
}
});
Self.getThermographTemperatures = async() => {
return Self.getEnumValues('temperature');
};
};

View File

@ -6,44 +6,51 @@ module.exports = Self => {
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'Number',
type: 'number',
description: 'The travel id',
http: {source: 'path'}
}, {
},
{
arg: 'thermographId',
type: 'String',
type: 'string',
description: 'The thermograph id',
required: true
}, {
},
{
arg: 'state',
type: 'String',
type: 'string',
required: true
}, {
},
{
arg: 'warehouseId',
type: 'Number',
type: 'number',
description: 'The warehouse id',
required: true
}, {
},
{
arg: 'companyId',
type: 'Number',
type: 'number',
description: 'The company id',
required: true
}, {
},
{
arg: 'dmsTypeId',
type: 'Number',
type: 'number',
description: 'The dms type id',
required: true
}, {
},
{
arg: 'reference',
type: 'String',
type: 'string',
required: true
}, {
},
{
arg: 'description',
type: 'String',
type: 'string',
required: true
}],
returns: {
type: 'Object',
type: 'object',
root: true
},
http: {
@ -52,32 +59,40 @@ module.exports = Self => {
}
});
Self.createThermograph = async(ctx, id, thermographId, state) => {
Self.createThermograph = async(ctx, id, thermographId, state, options) => {
const models = Self.app.models;
const tx = await Self.beginTransaction({});
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const options = {transaction: tx};
const travelThermograph = await models.TravelThermograph.findOne({
where: {
thermographFk: thermographId,
travelFk: null
}
}, options);
}, myOptions);
if (!travelThermograph)
throw new UserError('No valid travel thermograph found');
const uploadedFiles = await models.Dms.uploadFile(ctx, options);
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
const firstDms = uploadedFiles[0];
await travelThermograph.updateAttributes({
dmsFk: firstDms.id,
travelFk: id,
result: state
}, options);
}, myOptions);
await tx.commit();
if (tx) await tx.commit();
return travelThermograph;
} catch (err) {

View File

@ -13,5 +13,8 @@
},
"TravelThermograph": {
"dataSource": "vn"
},
"Temperature": {
"dataSource": "vn"
}
}

View File

@ -0,0 +1,30 @@
{
"name": "Temperature",
"base": "VnModel",
"options": {
"mysql": {
"table": "temperature"
}
},
"properties": {
"code": {
"type": "string",
"id": true,
"description": "Identifier",
"required": true
},
"name": {
"type": "string",
"required": true
},
"description": {
"type": "string"
}
},
"acls": [{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}]
}

View File

@ -1,4 +0,0 @@
module.exports = Self => {
require('../methods/travel-thermograph/getThermographTemperatures')(Self);
};

View File

@ -13,22 +13,25 @@
},
"properties": {
"id": {
"type": "Number",
"type": "number",
"description": "Identifier",
"id": true
},
"created": {
"type": "Date"
"type": "date"
},
"temperature": {
"type": "String",
"type": "string"
},
"temperatureFk": {
"type": "string",
"required": true
},
"result": {
"type": "String"
"type": "string"
},
"warehouseFk": {
"type": "Number",
"type": "number",
"required": true
}
},