importTicketSales refactor + tests
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
c1f457c263
commit
8797444553
|
@ -17,14 +17,27 @@ 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 tx;
|
||||||
|
let myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
if (!myOptions.transaction) {
|
||||||
|
tx = await Self.beginTransaction({});
|
||||||
|
myOptions.transaction = tx;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const worker = await models.Worker.findOne({where: {userFk: userId}}, myOptions);
|
||||||
|
|
||||||
let ticketSales = await models.Sale.find({
|
let ticketSales = await models.Sale.find({
|
||||||
where: {ticketFk: params.ticketFk}
|
where: {ticketFk: params.ticketFk}
|
||||||
});
|
}, myOptions);
|
||||||
|
|
||||||
let claimEnds = [];
|
let claimEnds = [];
|
||||||
ticketSales.forEach(sale => {
|
ticketSales.forEach(sale => {
|
||||||
|
@ -35,6 +48,14 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -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, {
|
|
||||||
|
const tx = await app.models.Entry.beginTransaction({});
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
|
||||||
|
const claimEnds = await app.models.ClaimEnd.importTicketSales(ctx, {
|
||||||
claimFk: 1,
|
claimFk: 1,
|
||||||
ticketFk: 1
|
ticketFk: 1
|
||||||
});
|
}, options);
|
||||||
|
|
||||||
expect(claimEnds.length).toEqual(4);
|
expect(claimEnds.length).toEqual(4);
|
||||||
expect(claimEnds[0].saleFk).toEqual(1);
|
expect(claimEnds[0].saleFk).toEqual(1);
|
||||||
expect(claimEnds[2].saleFk).toEqual(3);
|
expect(claimEnds[2].saleFk).toEqual(3);
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue