From 52e8bac168830e72f9df7e5be41cbaf9893df6ea Mon Sep 17 00:00:00 2001 From: sergiodt Date: Fri, 4 Oct 2024 13:34:04 +0200 Subject: [PATCH] fix: refs #7524 backSuppliersPackaging --- .../back/methods/supplier/getWithPackaging.js | 40 +++++++++++++++++++ .../supplier/specs/getWithPackaging.spec.js | 32 +++++++++++++++ modules/supplier/back/models/supplier.js | 1 + 3 files changed, 73 insertions(+) create mode 100644 modules/supplier/back/methods/supplier/getWithPackaging.js create mode 100644 modules/supplier/back/methods/supplier/specs/getWithPackaging.spec.js diff --git a/modules/supplier/back/methods/supplier/getWithPackaging.js b/modules/supplier/back/methods/supplier/getWithPackaging.js new file mode 100644 index 000000000..658b0696c --- /dev/null +++ b/modules/supplier/back/methods/supplier/getWithPackaging.js @@ -0,0 +1,40 @@ +module.exports = Self => { + Self.remoteMethod('getWithPackaging', { + description: 'Returns the list of suppliers with an entry of type packaging', + accessType: 'READ', + returns: { + type: 'object', + root: true + }, + http: { + path: `/getWithPackaging`, + verb: 'GET' + }, + nolimit: true + }); + Self.getWithPackaging = async options => { + const models = Self.app.models; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const entries = await models.Entry.find({ + where: {typeFk: 'packaging'}, + include: { + relation: 'supplier', + scope: { + fields: ['id', 'name'] + } + }, + fields: {supplierFk: true} + }, myOptions); + + const result = entries.map(item => ({ + id: item.supplier().id, + name: item.supplier().name + })); + return Array.from(new Map(result.map(entry => [entry.id, entry])).values()); + }; +}; + diff --git a/modules/supplier/back/methods/supplier/specs/getWithPackaging.spec.js b/modules/supplier/back/methods/supplier/specs/getWithPackaging.spec.js new file mode 100644 index 000000000..f73d20f84 --- /dev/null +++ b/modules/supplier/back/methods/supplier/specs/getWithPackaging.spec.js @@ -0,0 +1,32 @@ +const {models} = require('vn-loopback/server/server'); + +describe('Supplier getWithPackaging()', () => { + it('should return a list of suppliers with an entry of type packaging', async() => { + const typeFk = 'packaging'; + + const tx = await models.Supplier.beginTransaction({}); + const myOptions = {transaction: tx}; + + try { + const entry = await models.Entry.findOne( + { + where: { + id: 1 + }, + myOptions + }); + + await entry.updateAttributes({ + typeFk: typeFk, + }); + + const result = await models.Supplier.getWithPackaging(myOptions); + + expect(result.length).toEqual(1); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/supplier/back/models/supplier.js b/modules/supplier/back/models/supplier.js index 2d3ffef3e..7e6908d57 100644 --- a/modules/supplier/back/models/supplier.js +++ b/modules/supplier/back/models/supplier.js @@ -12,6 +12,7 @@ module.exports = Self => { require('../methods/supplier/campaignMetricsEmail')(Self); require('../methods/supplier/newSupplier')(Self); require('../methods/supplier/getItemsPackaging')(Self); + require('../methods/supplier/getWithPackaging')(Self); Self.validatesPresenceOf('name', { message: 'The social name cannot be empty'