fix: refs #7917 error filter
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Carlos Satorres 2024-11-25 06:16:07 +01:00
parent 9e0d4b7c97
commit bdac9e2167
3 changed files with 21 additions and 53 deletions

View File

@ -72,7 +72,7 @@ module.exports = Self => {
} }
}); });
filter = mergeFilters(filter, {where}); filter = mergeFilters(ctx.args.filter, {where});
const supplier = await Self.app.models.Supplier.isSupplier(ctx, myOptions); const supplier = await Self.app.models.Supplier.isSupplier(ctx, myOptions);
if (supplier) { if (supplier) {

View File

@ -1,25 +1,20 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('AgencyTerm filter()', () => { fdescribe('AgencyTerm filter()', () => {
const authUserId = 9;
const today = Date.vnNew(); const today = Date.vnNew();
today.setHours(2, 0, 0, 0); today.setHours(2, 0, 0, 0);
let tx;
let ctx = beforeAll.getCtx(); let ctx = beforeAll.getCtx();
beforeAll(async() => { console.log('ctx', ctx);
ctx = {
req: {
accessToken: {},
headers: {origin: 'http://localhost'},
}
};
});
it('should return all results matching the filter', async() => { it('should return all results matching the filter', async() => {
const tx = await models.AgencyTerm.beginTransaction({}); tx = await models.AgencyTerm.beginTransaction({});
try { try {
const options = {transaction: tx}; const options = {transaction: tx};
const filter = {}; const filter = {};
ctx = {req: {accessToken: {userId: authUserId}}};
const agencyTerms = await models.AgencyTerm.filter(ctx, filter, options); const agencyTerms = await models.AgencyTerm.filter(ctx, filter, options);
const firstAgencyTerm = agencyTerms[0]; const firstAgencyTerm = agencyTerms[0];
@ -35,39 +30,23 @@ describe('AgencyTerm filter()', () => {
}); });
it('should return results matching "search" searching by integer', async() => { it('should return results matching "search" searching by integer', async() => {
// ctx = { const ctx = {req: {args: {search: 1}}};
// args: {
// search: 1,
// }
// };
const filter = {
order: 'isActive ASC, name',
search: 1
};
let result = await app.models.AgencyTerm.filter(ctx, filter); let result = await app.models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1); expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(1); expect(result[0].routeFk).toEqual(1);
}); });
it('should return results matching "search" searching by string', async() => { it('should return results matching "search" searching by string', async() => {
// ctx = { const ctx = {req: {args: {search: 'Plants SL'}}};
// args: { let result = await app.models.AgencyTerm.filter(ctx);
// search: 'Plants SL',
// }
// };
const filter = {
search: 'Plants SL'
};
let result = await app.models.AgencyTerm.filter(ctx, filter);
expect(result.length).toEqual(2); expect(result.length).toEqual(2);
}); });
it('should return results matching "from" and "to"', async() => { it('should return results matching "from" and "to"', async() => {
const tx = await models.Buy.beginTransaction({}); tx = await models.Buy.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
try { try {
@ -77,6 +56,8 @@ describe('AgencyTerm filter()', () => {
const to = Date.vnNew(); const to = Date.vnNew();
to.setHours(23, 59, 59, 999); to.setHours(23, 59, 59, 999);
const ctx = {req: {args: {from: from, to: to}}};
const results = await models.AgencyTerm.filter(ctx, options); const results = await models.AgencyTerm.filter(ctx, options);
expect(results.length).toBe(5); expect(results.length).toBe(5);
@ -89,31 +70,17 @@ describe('AgencyTerm filter()', () => {
}); });
it('should return results matching "agencyModeFk"', async() => { it('should return results matching "agencyModeFk"', async() => {
// ctx = { const ctx = {req: {args: {agencyModeFk: 1}}};
// args: {
// agencyModeFk: 1, let result = await app.models.AgencyTerm.filter(ctx);
// }
// };
const filter = {
agencyModeFk: 1,
};
let result = await app.models.AgencyTerm.filter(ctx, filter);
expect(result.length).toEqual(1); expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(1); expect(result[0].routeFk).toEqual(1);
}); });
it('should return results matching "agencyFk"', async() => { it('should return results matching "agencyFk"', async() => {
// ctx = { const ctx = {req: {args: {agencyFk: 1}}};
// args: { let result = await app.models.AgencyTerm.filter(ctx);
// agencyFk: 2,
// }
// };
const filter = {
agencyFk: 2
};
let result = await app.models.AgencyTerm.filter(ctx, filter);
expect(result.length).toEqual(1); expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(2); expect(result[0].routeFk).toEqual(2);

View File

@ -164,7 +164,8 @@ module.exports = Self => {
throw new UserError('The social name has an invalid format'); throw new UserError('The social name has an invalid format');
}); });
Self.isSupplier = async(userId, options = {}) => { Self.isSupplier = async(ctx, options = {}) => {
const userId = ctx.req.accessToken.userId;
const client = await Self.app.models.Client.findById(userId, options); const client = await Self.app.models.Client.findById(userId, options);
const supplier = await Self.app.models.Supplier.findOne({where: {nif: client.fi}}, options); const supplier = await Self.app.models.Supplier.findOne({where: {nif: client.fi}}, options);
return supplier; return supplier;