Merge pull request 'fix: refs #7524 backSuppliersPackaging' (!3074) from 7524-WhereWithoutLimit into master
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #3074
Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
This commit is contained in:
Sergio De la torre 2024-10-07 10:47:14 +00:00
commit a96d6f7ed8
3 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,45 @@
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 = {};
const oneYearAgo = new Date();
oneYearAgo.setFullYear(oneYearAgo.getFullYear() - 1);
if (typeof options == 'object')
Object.assign(myOptions, options);
const entries = await models.Entry.find({
where: {
typeFk: 'packaging',
created: {gte: oneYearAgo}
},
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());
};
};

View File

@ -0,0 +1,33 @@
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,
created: new Date()
});
const result = await models.Supplier.getWithPackaging(myOptions);
expect(result.length).toEqual(1);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -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'