From 5c7efbb838ff4ac2b103b67ca95208b4df6a14f0 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 11 Nov 2021 10:33:21 +0100 Subject: [PATCH] feat(item): back filter buyer in search panel ans test --- .../10370-pickles/00-item_getBalance.sql | 4 +- db/dump/fixtures.sql | 2 +- modules/item/back/methods/item/active.js | 42 +++++++++++++++++++ .../item/back/methods/item/activeWithBuyer.js | 31 ++++++++++++++ .../item/specs/activeWithBuyer.spec.js | 14 +++++++ modules/item/back/models/item.js | 2 + modules/item/front/search-panel/index.html | 4 +- 7 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 modules/item/back/methods/item/active.js create mode 100644 modules/item/back/methods/item/activeWithBuyer.js create mode 100644 modules/item/back/methods/item/specs/activeWithBuyer.spec.js diff --git a/db/changes/10370-pickles/00-item_getBalance.sql b/db/changes/10370-pickles/00-item_getBalance.sql index 91e5c3681..90e3ee2bb 100644 --- a/db/changes/10370-pickles/00-item_getBalance.sql +++ b/db/changes/10370-pickles/00-item_getBalance.sql @@ -1,9 +1,9 @@ -DROP PROCEDURE IF EXISTS vn.item_getBalance; +DROP PROCEDURE IF EXISTS `vn`.`item_getBalance`; DELIMITER $$ $$ CREATE - definer = root@`%` procedure vn.item_getBalance(IN vItemId int, IN vWarehouse int) + definer = root@`%` procedure `vn`.`item_getBalance`(IN vItemId int, IN vWarehouse int) BEGIN DECLARE vDateInventory DATETIME; DECLARE vCurdate DATE DEFAULT CURDATE(); diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 49a78ec98..28b8e9181 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -739,7 +739,7 @@ INSERT INTO `vn`.`itemType`(`id`, `code`, `name`, `categoryFk`, `warehouseFk`, ` (3, 'WPN', 'Paniculata', 2, 1, 31, 35, 0), (4, 'PRT', 'Delivery ports', 3, 1, NULL, 35, 1), (5, 'CON', 'Container', 3, 1, NULL, 35, 1), - (6, 'ALS', 'Alstroemeria', 1, 1, 31, 35, 0); + (6, 'ALS', 'Alstroemeria', 1, 1, 31, 16, 0); INSERT INTO `vn`.`ink`(`id`, `name`, `picture`, `showOrder`, `hex`) VALUES diff --git a/modules/item/back/methods/item/active.js b/modules/item/back/methods/item/active.js new file mode 100644 index 000000000..4537ab994 --- /dev/null +++ b/modules/item/back/methods/item/active.js @@ -0,0 +1,42 @@ +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +const buildFilter = require('vn-loopback/util/filter').buildFilter; +const mergeFilters = require('vn-loopback/util/filter').mergeFilters; + +module.exports = Self => { + Self.activeWorkers = async(query, filter) => { + let conn = Self.dataSource.connector; + if (filter.where && filter.where.and && Array.isArray(filter.where.and)) { + let where = {}; + filter.where.and.forEach(element => { + where[Object.keys(element)[0]] = Object.values(element)[0]; + }); + filter.where = where; + } + let clientFilter = Object.assign({}, filter); + clientFilter.where = buildFilter(filter.where, (param, value) => { + switch (param) { + case 'role': + return {'r.name': value}; + case 'firstName': + return {or: [ + {'w.firstName': {like: `%${value}%`}}, + {'w.lastName': {like: `%${value}%`}}, + {'u.name': {like: `%${value}%`}}, + {'u.nickname': {like: `%${value}%`}} + ]}; + case 'id': + return {'w.id': value}; + } + }); + + let myFilter = { + where: {'u.active': true} + }; + + myFilter = mergeFilters(myFilter, clientFilter); + + let stmt = new ParameterizedSQL(query); + stmt.merge(conn.makeSuffix(myFilter)); + return conn.executeStmt(stmt); + }; +}; diff --git a/modules/item/back/methods/item/activeWithBuyer.js b/modules/item/back/methods/item/activeWithBuyer.js new file mode 100644 index 000000000..ca2c9329d --- /dev/null +++ b/modules/item/back/methods/item/activeWithBuyer.js @@ -0,0 +1,31 @@ + +module.exports = Self => { + Self.remoteMethod('activeWithBuyer', { + description: 'Returns active workers in itemType', + accessType: 'READ', + accepts: [{ + arg: 'filter', + type: 'Object', + description: 'Filter defining where and paginated data', + required: false + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/activeWithBuyer`, + verb: 'GET' + } + }); + + Self.activeWithBuyer = async filter => { + const query = + `SELECT DISTINCT w.id, w.firstName, w.lastName, u.name, u.nickname + FROM worker w + JOIN itemType i ON i.workerFk = w.id + JOIN account.user u ON u.id = w.id`; + + return Self.activeWorkers(query, filter); + }; +}; diff --git a/modules/item/back/methods/item/specs/activeWithBuyer.spec.js b/modules/item/back/methods/item/specs/activeWithBuyer.spec.js new file mode 100644 index 000000000..f894e431a --- /dev/null +++ b/modules/item/back/methods/item/specs/activeWithBuyer.spec.js @@ -0,0 +1,14 @@ +const models = require('vn-loopback/server/server').models; + +describe('Worker activeWithBuyer', () => { + it('should return the buyers in itemType as result', async() => { + const filter = {}; + const result = await models.Item.activeWithBuyer(filter); + const firstWorker = result[0]; + const secondWorker = result[1]; + + expect(result.length).toEqual(2); + expect(firstWorker.nickname).toEqual('logisticBossNick'); + expect(secondWorker.nickname).toEqual('buyerNick'); + }); +}); diff --git a/modules/item/back/models/item.js b/modules/item/back/models/item.js index 6cf5ba625..887aa0a11 100644 --- a/modules/item/back/models/item.js +++ b/modules/item/back/models/item.js @@ -14,6 +14,8 @@ module.exports = Self => { require('../methods/item/getWasteByWorker')(Self); require('../methods/item/getWasteByItem')(Self); require('../methods/item/createIntrastat')(Self); + require('../methods/item/active')(Self); + require('../methods/item/activeWithBuyer')(Self); Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'}); diff --git a/modules/item/front/search-panel/index.html b/modules/item/front/search-panel/index.html index 518062eba..425df3d4f 100644 --- a/modules/item/front/search-panel/index.html +++ b/modules/item/front/search-panel/index.html @@ -41,12 +41,10 @@