createFromSales refactor + tests
This commit is contained in:
parent
eddd769af0
commit
ee79f23185
|
@ -24,15 +24,22 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.createFromSales = async(ctx, ticketId, sales) => {
|
||||
Self.createFromSales = async(ctx, ticketId, sales, options) => {
|
||||
let tx;
|
||||
let myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
const models = Self.app.models;
|
||||
const userId = ctx.req.accessToken.userId;
|
||||
const tx = await Self.beginTransaction({});
|
||||
|
||||
try {
|
||||
let options = {transaction: tx};
|
||||
|
||||
const ticket = await models.Ticket.findById(ticketId, null, options);
|
||||
const ticket = await models.Ticket.findById(ticketId, null, myOptions);
|
||||
if (ticket.isDeleted)
|
||||
throw new UserError(`You can't create a claim for a removed ticket`);
|
||||
|
||||
|
@ -41,7 +48,7 @@ module.exports = Self => {
|
|||
clientFk: ticket.clientFk,
|
||||
ticketCreated: ticket.shipped,
|
||||
workerFk: userId
|
||||
}, options);
|
||||
}, myOptions);
|
||||
const promises = [];
|
||||
|
||||
for (const sale of sales) {
|
||||
|
@ -49,17 +56,18 @@ module.exports = Self => {
|
|||
saleFk: sale.id,
|
||||
claimFk: newClaim.id,
|
||||
quantity: sale.quantity
|
||||
}, options);
|
||||
}, myOptions);
|
||||
|
||||
promises.push(newClaimBeginning);
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
await tx.commit();
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return newClaim;
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -10,36 +10,45 @@ describe('Claim createFromSales()', () => {
|
|||
const ctx = {req: {accessToken: {userId: 1}}};
|
||||
|
||||
it('should create a new claim', async() => {
|
||||
let claim = await app.models.Claim.createFromSales(ctx, ticketId, newSale);
|
||||
const tx = await app.models.Claim.beginTransaction({});
|
||||
|
||||
expect(claim.ticketFk).toEqual(ticketId);
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
let claimBeginning = await app.models.ClaimBeginning.findOne({where: {claimFk: claim.id}});
|
||||
const claim = await app.models.Claim.createFromSales(ctx, ticketId, newSale, options);
|
||||
|
||||
expect(claimBeginning.saleFk).toEqual(newSale[0].id);
|
||||
expect(claimBeginning.quantity).toEqual(newSale[0].quantity);
|
||||
expect(claim.ticketFk).toEqual(ticketId);
|
||||
|
||||
const createdClaimId = claim.id;
|
||||
let claimBeginning = await app.models.ClaimBeginning.findOne({where: {claimFk: claim.id}}, options);
|
||||
|
||||
// restores
|
||||
await app.models.Claim.destroyById(createdClaimId);
|
||||
expect(claimBeginning.saleFk).toEqual(newSale[0].id);
|
||||
expect(claimBeginning.quantity).toEqual(newSale[0].quantity);
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
it('should not be able to create a claim if exists that sale', async() => {
|
||||
let claim = await app.models.Claim.createFromSales(ctx, ticketId, newSale);
|
||||
const createdClaimId = claim.id;
|
||||
const tx = await app.models.Claim.beginTransaction({});
|
||||
|
||||
let error;
|
||||
|
||||
await app.models.Claim.createFromSales(ctx, ticketId, newSale)
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
.catch(e => {
|
||||
error = e;
|
||||
});
|
||||
await app.models.Claim.createFromSales(ctx, ticketId, newSale, options);
|
||||
|
||||
await app.models.Claim.createFromSales(ctx, ticketId, newSale, options);
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
error = e;
|
||||
await tx.rollback();
|
||||
}
|
||||
|
||||
expect(error.toString()).toContain(`A claim with that sale already exists`);
|
||||
|
||||
// restores
|
||||
await app.models.Claim.destroyById(createdClaimId);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue