fix: refs #7524 backSuppliersPackaging #3074

Merged
sergiodt merged 3 commits from 7524-WhereWithoutLimit into master 2024-10-07 10:47:15 +00:00
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);
Review

ací en els que hagen tingut entries en el últim any hi ha suficient, si no tindrem sempre proveedors obsolets

ací en els que hagen tingut entries en el últim any hi ha suficient, si no tindrem sempre proveedors obsolets
Review

Modificat

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