diff --git a/modules/worker/back/methods/worker/chooseLocker.js b/modules/worker/back/methods/worker/chooseLocker.js new file mode 100644 index 000000000..b3b7678ca --- /dev/null +++ b/modules/worker/back/methods/worker/chooseLocker.js @@ -0,0 +1,32 @@ + +module.exports = Self => { + Self.remoteMethod('chooseLocker', { + description: 'Returns an unoccupied locker for the employee', + accessType: 'WRITE', + accepts: [{ + arg: 'filter', + type: 'Object', + description: 'Filter defining where and paginated data', + required: true + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/chooseLocker`, + verb: 'GET' + } + }); + + Self.chooseLocker = async filter => { + const query = + `SELECT l.code AS locker_code + FROM worker w + JOIN locker l ON w.sex = l.gender + WHERE w.id = ? AND l.workerFk IS NULL; + `[this.worker.id]; + + return Self.chooseLocker(query, filter); + }; +}; diff --git a/modules/worker/back/models/locker.js b/modules/worker/back/models/locker.js new file mode 100644 index 000000000..25b33f517 --- /dev/null +++ b/modules/worker/back/models/locker.js @@ -0,0 +1,7 @@ +module.exports = Self => { + require('../methods/worker/chooseLocker')(Self); + + Self.validatesUniquenessOf('locker', { + message: 'This locker has already been assigned' + }); +};