fix: refs #7917 tback
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Carlos Satorres 2024-11-26 10:23:42 +01:00
parent 3a6df984aa
commit d6637a32ce
6 changed files with 132 additions and 164 deletions

View File

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

View File

@ -1,88 +1,62 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
fdescribe('AgencyTerm filter()', () => {
const authUserId = 9;
const today = Date.vnNew();
today.setHours(2, 0, 0, 0);
let tx;
let ctx = beforeAll.getCtx();
console.log('ctx', ctx);
describe('AgencyTerm filter()', () => {
const ctx = beforeAll.getCtx();
it('should return all results matching the filter', async() => {
tx = await models.AgencyTerm.beginTransaction({});
ctx.args = {};
const agencyTerms = await models.AgencyTerm.filter(ctx);
const firstAgencyTerm = agencyTerms[0];
try {
const options = {transaction: tx};
const filter = {};
ctx = {req: {accessToken: {userId: authUserId}}};
const agencyTerms = await models.AgencyTerm.filter(ctx, filter, options);
const firstAgencyTerm = agencyTerms[0];
expect(firstAgencyTerm.routeFk).toEqual(1);
expect(agencyTerms.length).toEqual(5);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
expect(firstAgencyTerm.routeFk).toEqual(1);
expect(agencyTerms.length).toEqual(5);
});
it('should return results matching "search" searching by integer', async() => {
const ctx = {req: {args: {search: 1}}};
const search = 1;
ctx.args = {search};
let result = await app.models.AgencyTerm.filter(ctx);
const [result] = await models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(1);
expect(result.invoiceInFk).toEqual(search);
});
it('should return results matching "search" searching by string', async() => {
const ctx = {req: {args: {search: 'Plants SL'}}};
let result = await app.models.AgencyTerm.filter(ctx);
ctx.args = {search: 'Plants SL'};
let result = await models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(2);
});
it('should return results matching "from" and "to"', async() => {
tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
const from = Date.vnNew();
from.setHours(0, 0, 0, 0);
try {
const from = Date.vnNew();
from.setHours(0, 0, 0, 0);
const to = Date.vnNew();
to.setHours(23, 59, 59, 999);
const to = Date.vnNew();
to.setHours(23, 59, 59, 999);
ctx.args = {from, to};
const ctx = {req: {args: {from: from, to: to}}};
const results = await models.AgencyTerm.filter(ctx);
const results = await models.AgencyTerm.filter(ctx, options);
expect(results.length).toBe(5);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
expect(results.length).toBe(5);
});
it('should return results matching "agencyModeFk"', async() => {
const ctx = {req: {args: {agencyModeFk: 1}}};
const agencyModeFk = 1;
ctx.args = {agencyModeFk};
let result = await app.models.AgencyTerm.filter(ctx);
const [result] = await models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(1);
expect(result.agencyModeFk).toEqual(agencyModeFk);
});
it('should return results matching "agencyFk"', async() => {
const ctx = {req: {args: {agencyFk: 1}}};
let result = await app.models.AgencyTerm.filter(ctx);
const agencyFk = 1;
ctx.args = {agencyFk};
const [result] = await models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(2);
expect(result.agencyFk).toEqual(agencyFk);
});
});

View File

@ -1,109 +1,109 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
// const app = require('vn-loopback/server/server');
// const models = require('vn-loopback/server/server').models;
describe('AgencyTerm filter()', () => {
const authUserId = 9;
const today = Date.vnNew();
today.setHours(2, 0, 0, 0);
// describe('Clone ', () => {
// const authUserId = 9;
// const today = Date.vnNew();
// today.setHours(2, 0, 0, 0);
it('should return all results matching the filter', async() => {
const tx = await models.AgencyTerm.beginTransaction({});
// it('should return all results matching the filter', async() => {
// const tx = await models.AgencyTerm.beginTransaction({});
try {
const options = {transaction: tx};
const filter = {};
const ctx = {req: {accessToken: {userId: authUserId}}};
// try {
// const options = {transaction: tx};
// const filter = {};
// const ctx = {req: {accessToken: {userId: authUserId}}};
const agencyTerms = await models.AgencyTerm.filter(ctx, filter, options);
const firstAgencyTerm = agencyTerms[0];
// const agencyTerms = await models.AgencyTerm.filter(ctx, filter, options);
// const firstAgencyTerm = agencyTerms[0];
expect(firstAgencyTerm.routeFk).toEqual(1);
expect(agencyTerms.length).toEqual(5);
// expect(firstAgencyTerm.routeFk).toEqual(1);
// expect(agencyTerms.length).toEqual(5);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
// await tx.rollback();
// } catch (e) {
// await tx.rollback();
// throw e;
// }
// });
it('should return results matching "search" searching by integer', async() => {
let ctx = {
args: {
search: 1,
}
};
// it('should return results matching "search" searching by integer', async() => {
// let ctx = {
// args: {
// search: 1,
// }
// };
let result = await app.models.AgencyTerm.filter(ctx);
// let result = await app.models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(1);
});
// expect(result.length).toEqual(1);
// expect(result[0].routeFk).toEqual(1);
// });
it('should return results matching "search" searching by string', async() => {
let ctx = {
args: {
search: 'Plants SL',
}
};
// it('should return results matching "search" searching by string', async() => {
// let ctx = {
// args: {
// search: 'Plants SL',
// }
// };
let result = await app.models.AgencyTerm.filter(ctx);
// let result = await app.models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(2);
});
// expect(result.length).toEqual(2);
// });
it('should return results matching "from" and "to"', async() => {
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
// it('should return results matching "from" and "to"', async() => {
// const tx = await models.Buy.beginTransaction({});
// const options = {transaction: tx};
try {
const from = Date.vnNew();
from.setHours(0, 0, 0, 0);
// try {
// const from = Date.vnNew();
// from.setHours(0, 0, 0, 0);
const to = Date.vnNew();
to.setHours(23, 59, 59, 999);
// const to = Date.vnNew();
// to.setHours(23, 59, 59, 999);
const ctx = {
args: {
from: from,
to: to
}
};
// const ctx = {
// 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);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
// await tx.rollback();
// } catch (e) {
// await tx.rollback();
// throw e;
// }
// });
it('should return results matching "agencyModeFk"', async() => {
let ctx = {
args: {
agencyModeFk: 1,
}
};
// it('should return results matching "agencyModeFk"', async() => {
// let ctx = {
// args: {
// agencyModeFk: 1,
// }
// };
let result = await app.models.AgencyTerm.filter(ctx);
// let result = await app.models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(1);
});
// expect(result.length).toEqual(1);
// expect(result[0].routeFk).toEqual(1);
// });
it('should return results matching "agencyFk"', async() => {
let ctx = {
args: {
agencyFk: 2,
}
};
// it('should return results matching "agencyFk"', async() => {
// let ctx = {
// args: {
// agencyFk: 2,
// }
// };
let result = await app.models.AgencyTerm.filter(ctx);
// let result = await app.models.AgencyTerm.filter(ctx);
expect(result.length).toEqual(1);
expect(result[0].routeFk).toEqual(2);
});
});
// expect(result.length).toEqual(1);
// expect(result[0].routeFk).toEqual(2);
// });
// });

View File

@ -1,6 +1,8 @@
const app = require('vn-loopback/server/server');
describe('route getTickets()', () => {
const ctx = beforeAll.getCtx();
it('should return the tickets for a given route', async() => {
const filter = {id: 2};
let result = await app.models.Route.getTickets(ctx, filter);

View File

@ -1,51 +1,38 @@
const app = require('vn-loopback/server/server');
describe('route summary()', () => {
let ctx = beforeAll.getCtx();
beforeAll(async() => {
ctx = {
req: {
accessToken: {},
headers: {origin: 'http://localhost'},
}
};
});
const id = 1;
const ctx = beforeAll.getCtx();
it('should return a summary object containing data from one route', async() => {
const filter = {
id: 1
};
const result = await app.models.Route.summary(ctx, filter);
const result = await app.models.Route.summary(ctx, id);
expect(result.route.id).toEqual(1);
});
it(`should return a summary object containing it's agency`, async() => {
const filter = {
id: 1
};
const result = await app.models.Route.summary(ctx, filter);
const result = await app.models.Route.summary(ctx, id);
const agency = result.route.agencyMode();
expect(agency.name).toEqual('inhouse pickup');
});
it(`should return a summary object containing it's vehicle`, async() => {
const result = await app.models.Route.summary(ctx, filter);
const result = await app.models.Route.summary(ctx, id);
const vehicle = result.route.vehicle();
expect(vehicle.numberPlate).toEqual('3333-BAT');
});
it(`should return a summary object containing it's worker`, async() => {
const result = await app.models.Route.summary(ctx, filter);
const result = await app.models.Route.summary(ctx, id);
const worker = result.route.worker().user();
expect(worker.name).toEqual('delivery');
});
it(`should return a summary object containing data from the tickets`, async() => {
const result = await app.models.Route.summary(ctx, filter);
const result = await app.models.Route.summary(ctx, 2);
expect(result.tickets.length).toEqual(1);
});

View File

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