diff --git a/db/changes/10360-september/01-travelThermograph.sql b/db/changes/10360-september/01-travelThermograph.sql new file mode 100644 index 000000000..dec86d102 --- /dev/null +++ b/db/changes/10360-september/01-travelThermograph.sql @@ -0,0 +1 @@ +alter table `vn`.`travelThermograph` modify `temperature` enum('COOL', 'WARM') null; \ No newline at end of file diff --git a/modules/travel/back/methods/thermograph/createThermograph.js b/modules/travel/back/methods/thermograph/createThermograph.js index bfca208fe..137f32ac4 100644 --- a/modules/travel/back/methods/thermograph/createThermograph.js +++ b/modules/travel/back/methods/thermograph/createThermograph.js @@ -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; } }; diff --git a/modules/travel/back/methods/travel-thermograph/getThermographTemperatures.js b/modules/travel/back/methods/travel-thermograph/getThermographTemperatures.js deleted file mode 100644 index 1d510b513..000000000 --- a/modules/travel/back/methods/travel-thermograph/getThermographTemperatures.js +++ /dev/null @@ -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'); - }; -}; diff --git a/modules/travel/back/methods/travel/createThermograph.js b/modules/travel/back/methods/travel/createThermograph.js index 974b67923..df10bbb78 100644 --- a/modules/travel/back/methods/travel/createThermograph.js +++ b/modules/travel/back/methods/travel/createThermograph.js @@ -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) { diff --git a/modules/travel/back/model-config.json b/modules/travel/back/model-config.json index b06d00f06..34321ba78 100644 --- a/modules/travel/back/model-config.json +++ b/modules/travel/back/model-config.json @@ -13,5 +13,8 @@ }, "TravelThermograph": { "dataSource": "vn" + }, + "Temperature": { + "dataSource": "vn" } } diff --git a/modules/travel/back/models/temperature.json b/modules/travel/back/models/temperature.json new file mode 100644 index 000000000..575b5f8be --- /dev/null +++ b/modules/travel/back/models/temperature.json @@ -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" + }] +} diff --git a/modules/travel/back/models/travel-thermograph.js b/modules/travel/back/models/travel-thermograph.js deleted file mode 100644 index 91aa4de69..000000000 --- a/modules/travel/back/models/travel-thermograph.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = Self => { - require('../methods/travel-thermograph/getThermographTemperatures')(Self); -}; - diff --git a/modules/travel/back/models/travel-thermograph.json b/modules/travel/back/models/travel-thermograph.json index 70ee0de07..1e6dd1905 100644 --- a/modules/travel/back/models/travel-thermograph.json +++ b/modules/travel/back/models/travel-thermograph.json @@ -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 } },