importTicketSales refactor + tests
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2021-03-29 15:42:29 +02:00
parent c1f457c263
commit 8797444553
2 changed files with 53 additions and 31 deletions

View File

@ -17,24 +17,45 @@ module.exports = Self => {
} }
}); });
Self.importTicketSales = async(ctx, params) => { Self.importTicketSales = async(ctx, params, options) => {
let models = Self.app.models; let models = Self.app.models;
let userId = ctx.req.accessToken.userId; let userId = ctx.req.accessToken.userId;
let worker = await models.Worker.findOne({where: {userFk: userId}});
let ticketSales = await models.Sale.find({ let tx;
where: {ticketFk: params.ticketFk} let myOptions = {};
});
let claimEnds = []; if (typeof options == 'object')
ticketSales.forEach(sale => { Object.assign(myOptions, options);
claimEnds.push({
saleFk: sale.id, if (!myOptions.transaction) {
claimFk: params.claimFk, tx = await Self.beginTransaction({});
workerFk: worker.id myOptions.transaction = tx;
}
try {
const worker = await models.Worker.findOne({where: {userFk: userId}}, myOptions);
let ticketSales = await models.Sale.find({
where: {ticketFk: params.ticketFk}
}, myOptions);
let claimEnds = [];
ticketSales.forEach(sale => {
claimEnds.push({
saleFk: sale.id,
claimFk: params.claimFk,
workerFk: worker.id
});
}); });
});
return await Self.create(claimEnds); const createdClaimEnds = await Self.create(claimEnds, myOptions);
if (tx) await tx.commit();
return createdClaimEnds;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
}; };

View File

@ -1,25 +1,26 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
describe('Claim importTicketSales()', () => { describe('Claim importTicketSales()', () => {
let claimEnds;
afterAll(async done => {
claimEnds.forEach(async line => {
await line.destroy();
});
done();
});
it('should import sales to a claim actions from an specific ticket', async() => { it('should import sales to a claim actions from an specific ticket', async() => {
let ctx = {req: {accessToken: {userId: 5}}}; const ctx = {req: {accessToken: {userId: 5}}};
claimEnds = await app.models.ClaimEnd.importTicketSales(ctx, {
claimFk: 1,
ticketFk: 1
});
expect(claimEnds.length).toEqual(4); const tx = await app.models.Entry.beginTransaction({});
expect(claimEnds[0].saleFk).toEqual(1); try {
expect(claimEnds[2].saleFk).toEqual(3); const options = {transaction: tx};
const claimEnds = await app.models.ClaimEnd.importTicketSales(ctx, {
claimFk: 1,
ticketFk: 1
}, options);
expect(claimEnds.length).toEqual(4);
expect(claimEnds[0].saleFk).toEqual(1);
expect(claimEnds[2].saleFk).toEqual(3);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
}); });