diff --git a/db/versions/11010-blackChrysanthemum/00-firstScript.sql b/db/versions/11010-blackChrysanthemum/00-firstScript.sql index a0d10891c..c8dce54b2 100644 --- a/db/versions/11010-blackChrysanthemum/00-firstScript.sql +++ b/db/versions/11010-blackChrysanthemum/00-firstScript.sql @@ -15,3 +15,7 @@ ALTER TABLE vn.deviceProductionUser ADD IF NOT EXISTS simSerialNumber TEXT NULL; ALTER TABLE vn.deviceProductionConfig ADD IF NOT EXISTS maxDevicesPerUser INT UNSIGNED NULL; UPDATE vn.deviceProductionConfig SET maxDevicesPerUser=1 WHERE id=1; + +INSERT IGNORE INTO salix.ACL (model,property,accessType,permission,principalType,principalId) + VALUES ('Worker','getAvailablePda','READ','ALLOW','ROLE','hr'); + diff --git a/loopback/locale/en.json b/loopback/locale/en.json index ca76eae42..601a26f5b 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -223,7 +223,8 @@ "printerNotExists": "The printer does not exist", "There are not picking tickets": "There are not picking tickets", "ticketCommercial": "The ticket {{ ticket }} for the salesperson {{ salesMan }} is in preparation. (automatically generated message)", - "This password can only be changed by the user themselves": "This password can only be changed by the user themselves", - "They're not your subordinate": "They're not your subordinate", - "InvoiceIn is already booked": "InvoiceIn is already booked" -} + "This password can only be changed by the user themselves": "This password can only be changed by the user themselves", + "They're not your subordinate": "They're not your subordinate", + "InvoiceIn is already booked": "InvoiceIn is already booked", + "This workCenter is already assigned to this agency": "This workCenter is already assigned to this agency" +} \ No newline at end of file diff --git a/loopback/locale/es.json b/loopback/locale/es.json index f1c57455e..c67f7ecd6 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -355,6 +355,7 @@ "No results found": "No se han encontrado resultados", "InvoiceIn is already booked": "La factura recibida está contabilizada", "This workCenter is already assigned to this agency": "Este centro de trabajo ya está asignado a esta agencia", - "Select ticket or client": "Elija un ticket o un client", - "It was not able to create the invoice": "No se pudo crear la factura" -} + "Select ticket or client": "Elija un ticket o un client", + "It was not able to create the invoice": "No se pudo crear la factura", + "This PDA is already assigned to another user": "This PDA is already assigned to another user" +} \ No newline at end of file diff --git a/modules/worker/back/methods/worker/getAvailablePda.js b/modules/worker/back/methods/worker/getAvailablePda.js new file mode 100644 index 000000000..5c97e15e1 --- /dev/null +++ b/modules/worker/back/methods/worker/getAvailablePda.js @@ -0,0 +1,25 @@ +module.exports = Self => { + Self.remoteMethod('getAvailablePda', { + description: 'returns devices without user', + accessType: 'READ', + accepts: [], + returns: { + type: 'array', + root: true + }, + http: { + path: `/getAvailablePda`, + verb: 'GET' + } + }); + Self.getAvailablePda = async() => { + const models = Self.app.models; + + return models.DeviceProduction.rawSql( + `SELECT d.* + FROM deviceProduction d + LEFT JOIN deviceProductionUser du ON du.deviceProductionFk = d.id + WHERE du.deviceProductionFk IS NULL` + ); + }; +}; diff --git a/modules/worker/back/methods/worker/specs/getAvailablePda.spec.js b/modules/worker/back/methods/worker/specs/getAvailablePda.spec.js new file mode 100644 index 000000000..7649225f1 --- /dev/null +++ b/modules/worker/back/methods/worker/specs/getAvailablePda.spec.js @@ -0,0 +1,13 @@ +const models = require('vn-loopback/server/server').models; + +describe('worker getAvailablePda()', () => { + fit('should return a Pda that has no user assigned', async() => { + const [{id}] = await models.Worker.getAvailablePda(); + + const deviceProductionUser = await models.DeviceProductionUser.findOne({ + where: {deviceProductionFk: id} + }); + + expect(!deviceProductionUser).toBeTruthy(); + }); +}); diff --git a/modules/worker/back/models/device-production-user.js b/modules/worker/back/models/device-production-user.js new file mode 100644 index 000000000..81af484d3 --- /dev/null +++ b/modules/worker/back/models/device-production-user.js @@ -0,0 +1,8 @@ +const UserError = require('vn-loopback/util/user-error'); +module.exports = Self => { + Self.rewriteDbError(function(err) { + if (err.code === 'ER_DUP_ENTRY') + return new UserError(`This PDA is already assigned to another user`); + return err; + }); +}; diff --git a/modules/worker/back/models/device-production-user.json b/modules/worker/back/models/device-production-user.json index d63fe0148..37955ab76 100644 --- a/modules/worker/back/models/device-production-user.json +++ b/modules/worker/back/models/device-production-user.json @@ -34,9 +34,9 @@ }, "relations": { "deviceProduction": { - "type": "belongsTo", + "type": "hasOne", "model": "DeviceProduction", - "foreignKey": "deviceProductionFk" + "foreignKey": "id" }, "user": { "type": "belongsTo", diff --git a/modules/worker/back/models/device-production.json b/modules/worker/back/models/device-production.json index f6e5105ad..91b05eb85 100644 --- a/modules/worker/back/models/device-production.json +++ b/modules/worker/back/models/device-production.json @@ -55,6 +55,11 @@ "type": "belongsTo", "model": "DeviceProductionState", "foreignKey": "stateFk" + }, + "deviceProductionUser": { + "type": "hasMany", + "model": "DeviceProductionUser", + "foreignKey": "deviceProductionFk" } } } diff --git a/modules/worker/back/models/worker.js b/modules/worker/back/models/worker.js index b475bf26e..7128f973c 100644 --- a/modules/worker/back/models/worker.js +++ b/modules/worker/back/models/worker.js @@ -20,6 +20,7 @@ module.exports = Self => { require('../methods/worker/search')(Self); require('../methods/worker/isAuthorized')(Self); require('../methods/worker/setPassword')(Self); + require('../methods/worker/getAvailablePda')(Self); Self.validatesUniquenessOf('locker', { message: 'This locker has already been assigned'