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