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 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({
|
||||
where: {ticketFk: params.ticketFk}
|
||||
});
|
||||
}, myOptions);
|
||||
|
||||
let claimEnds = [];
|
||||
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');
|
||||
|
||||
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() => {
|
||||
let ctx = {req: {accessToken: {userId: 5}}};
|
||||
claimEnds = await app.models.ClaimEnd.importTicketSales(ctx, {
|
||||
const ctx = {req: {accessToken: {userId: 5}}};
|
||||
|
||||
const tx = await app.models.Entry.beginTransaction({});
|
||||
try {
|
||||
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;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue