refactor: refs #7937 streamline importToNewRefundTicket function and add comprehensive tests
gitea/salix/pipeline/pr-dev There was a failure building this commit
Details
gitea/salix/pipeline/pr-dev There was a failure building this commit
Details
This commit is contained in:
parent
d65ab26bcf
commit
b97eeea2d2
|
@ -74,10 +74,6 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const worker = await models.Worker.findOne({
|
|
||||||
where: {id: userId}
|
|
||||||
}, myOptions);
|
|
||||||
|
|
||||||
const observationSalesPerson = await models.ObservationType.findOne({
|
const observationSalesPerson = await models.ObservationType.findOne({
|
||||||
where: {code: 'salesPerson'}
|
where: {code: 'salesPerson'}
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
|
@ -89,6 +85,7 @@ module.exports = Self => {
|
||||||
let nickname;
|
let nickname;
|
||||||
let state;
|
let state;
|
||||||
let discountValue = null;
|
let discountValue = null;
|
||||||
|
let packages = 0;
|
||||||
|
|
||||||
if (claim.pickup === null) {
|
if (claim.pickup === null) {
|
||||||
state = await models.State.findOne({
|
state = await models.State.findOne({
|
||||||
|
@ -105,6 +102,7 @@ module.exports = Self => {
|
||||||
const claimConfig = await models.ClaimConfig.findOne();
|
const claimConfig = await models.ClaimConfig.findOne();
|
||||||
|
|
||||||
discountValue = 100;
|
discountValue = 100;
|
||||||
|
packages = 1;
|
||||||
state = await models.State.findOne({
|
state = await models.State.findOne({
|
||||||
where: {code: 'WAITING_FOR_PICKUP'}
|
where: {code: 'WAITING_FOR_PICKUP'}
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
|
@ -125,7 +123,8 @@ module.exports = Self => {
|
||||||
companyFk: claim.ticket().companyFk,
|
companyFk: claim.ticket().companyFk,
|
||||||
addressFk: claim.ticket().addressFk,
|
addressFk: claim.ticket().addressFk,
|
||||||
agencyModeFk,
|
agencyModeFk,
|
||||||
zoneFk: null
|
zoneFk: claim.ticket().zoneFk,
|
||||||
|
packages
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
|
|
||||||
if (claim.pickup == 'pickup') {
|
if (claim.pickup == 'pickup') {
|
||||||
|
@ -149,20 +148,16 @@ module.exports = Self => {
|
||||||
observationTypeFk: observationSalesPerson.id
|
observationTypeFk: observationSalesPerson.id
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
|
|
||||||
|
const salesToRefund = await models.ClaimBeginning.find(salesFilter, myOptions);
|
||||||
|
const createdSales = await addSalesToTicket(salesToRefund, newRefundTicket.id, discountValue, myOptions);
|
||||||
|
await insertIntoClaimEnd(createdSales, id, userId, myOptions);
|
||||||
|
|
||||||
await models.Ticket.state(ctx, {
|
await models.Ticket.state(ctx, {
|
||||||
ticketFk: newRefundTicket.id,
|
ticketFk: newRefundTicket.id,
|
||||||
stateFk: state.id,
|
stateFk: state.id,
|
||||||
userFk: worker.id
|
userFk: userId
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
|
|
||||||
const salesToRefund = await models.ClaimBeginning.find(salesFilter, myOptions);
|
|
||||||
const createdSales = await addSalesToTicket(salesToRefund, newRefundTicket.id, discountValue, myOptions);
|
|
||||||
await insertIntoClaimEnd(createdSales, id, worker.id, myOptions);
|
|
||||||
|
|
||||||
await Self.rawSql('CALL vn.ticketCalculateClon(?, ?)', [
|
|
||||||
newRefundTicket.id, claim.ticketFk
|
|
||||||
], myOptions);
|
|
||||||
|
|
||||||
if (tx) await tx.commit();
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
return newRefundTicket;
|
return newRefundTicket;
|
||||||
|
@ -172,23 +167,36 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function addSalesToTicket(salesToRefund, ticketId, discountValue, options) {
|
async function addSalesToTicket(salesToRefund, newTicketId, discountValue, options) {
|
||||||
let formatedSales = [];
|
const createdSales = [];
|
||||||
salesToRefund.forEach(sale => {
|
const models = Self.app.models;
|
||||||
let formatedSale = {
|
for (const saleToRefund of salesToRefund) {
|
||||||
itemFk: sale.sale().itemFk,
|
const oldSale = saleToRefund.sale();
|
||||||
ticketFk: ticketId,
|
const newSaleData = {
|
||||||
concept: sale.sale().concept,
|
itemFk: oldSale.itemFk,
|
||||||
quantity: -Math.abs(sale.quantity),
|
ticketFk: newTicketId,
|
||||||
price: sale.sale().price,
|
concept: oldSale.concept,
|
||||||
discount: discountValue ?? sale.sale().discount,
|
quantity: -Math.abs(saleToRefund.quantity),
|
||||||
reserved: sale.sale().reserved,
|
price: oldSale.price,
|
||||||
isPicked: sale.sale().isPicked,
|
discount: discountValue ?? oldSale.discount,
|
||||||
created: sale.sale().created
|
reserved: oldSale.reserved,
|
||||||
|
isPicked: oldSale.isPicked,
|
||||||
|
created: oldSale.created
|
||||||
};
|
};
|
||||||
formatedSales.push(formatedSale);
|
const newSale = await models.Sale.create(newSaleData, options);
|
||||||
});
|
const oldSaleComponents = await models.SaleComponent.find({
|
||||||
return await Self.app.models.Sale.create(formatedSales, options);
|
where: {saleFk: oldSale.id}
|
||||||
|
}, options);
|
||||||
|
const newComponents = oldSaleComponents.map(component => {
|
||||||
|
const data = component.toJSON ? component.toJSON() : {...component};
|
||||||
|
delete data.id;
|
||||||
|
data.saleFk = newSale.id;
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
await models.SaleComponent.create(newComponents, options);
|
||||||
|
createdSales.push(newSale);
|
||||||
|
}
|
||||||
|
return createdSales;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function insertIntoClaimEnd(createdSales, claimId, workerId, options) {
|
async function insertIntoClaimEnd(createdSales, claimId, workerId, options) {
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
const app = require('vn-loopback/server/server');
|
|
||||||
const LoopBackContext = require('loopback-context');
|
|
||||||
const models = app.models;
|
|
||||||
|
|
||||||
describe('claimBeginning', () => {
|
|
||||||
const claimManagerId = 72;
|
|
||||||
const activeCtx = {
|
|
||||||
accessToken: {userId: claimManagerId},
|
|
||||||
__: value => value
|
|
||||||
};
|
|
||||||
const ctx = {req: activeCtx};
|
|
||||||
|
|
||||||
describe('importToNewRefundTicket()', () => {
|
|
||||||
it('should create a new ticket with negative sales and insert the negative sales into claimEnd', async() => {
|
|
||||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
||||||
active: activeCtx
|
|
||||||
});
|
|
||||||
let claimId = 1;
|
|
||||||
|
|
||||||
const tx = await models.Entry.beginTransaction({});
|
|
||||||
try {
|
|
||||||
const options = {transaction: tx};
|
|
||||||
|
|
||||||
const ticket = await models.ClaimBeginning.importToNewRefundTicket(ctx, claimId, options);
|
|
||||||
|
|
||||||
const refundTicketSales = await models.Sale.find({
|
|
||||||
where: {ticketFk: ticket.id}
|
|
||||||
}, options);
|
|
||||||
const salesInsertedInClaimEnd = await models.ClaimEnd.find({
|
|
||||||
where: {claimFk: claimId}
|
|
||||||
}, options);
|
|
||||||
|
|
||||||
expect(refundTicketSales.length).toEqual(1);
|
|
||||||
expect(refundTicketSales[0].quantity).toEqual(-5);
|
|
||||||
expect(salesInsertedInClaimEnd[0].saleFk).toEqual(refundTicketSales[0].id);
|
|
||||||
|
|
||||||
await tx.rollback();
|
|
||||||
} catch (e) {
|
|
||||||
await tx.rollback();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -0,0 +1,97 @@
|
||||||
|
const app = require('vn-loopback/server/server');
|
||||||
|
const LoopBackContext = require('loopback-context');
|
||||||
|
const models = app.models;
|
||||||
|
|
||||||
|
describe('importToNewRefundTicket()', () => {
|
||||||
|
let tx;
|
||||||
|
const claimManagerId = 72;
|
||||||
|
const activeCtx = {
|
||||||
|
accessToken: {userId: claimManagerId},
|
||||||
|
};
|
||||||
|
let ctx = {req: activeCtx};
|
||||||
|
let options;
|
||||||
|
const claimId = 1;
|
||||||
|
|
||||||
|
beforeEach(async() => {
|
||||||
|
LoopBackContext.getCurrentContext = () => ({
|
||||||
|
active: activeCtx,
|
||||||
|
});
|
||||||
|
tx = await models.Entry.beginTransaction({});
|
||||||
|
options = {transaction: tx};
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async() => {
|
||||||
|
await tx.rollback();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create a new ticket with negative sales and insert the negative sales into claimEnd', async() => {
|
||||||
|
const ticket = await models.ClaimBeginning.importToNewRefundTicket(ctx, claimId, options);
|
||||||
|
|
||||||
|
const refundTicketSales = await models.Sale.find({
|
||||||
|
where: {ticketFk: ticket.id}
|
||||||
|
}, options);
|
||||||
|
const salesInsertedInClaimEnd = await models.ClaimEnd.find({
|
||||||
|
where: {claimFk: claimId}
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
expect(refundTicketSales.length).toEqual(1);
|
||||||
|
expect(refundTicketSales[0].quantity).toEqual(-5);
|
||||||
|
expect(salesInsertedInClaimEnd[0].saleFk).toEqual(refundTicketSales[0].id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('debe establecer estado DELIVERED y modo de agencia refund', async() => {
|
||||||
|
const state = await models.State.findOne({
|
||||||
|
where: {code: 'DELIVERED'}
|
||||||
|
}, options);
|
||||||
|
const ticket = await models.ClaimBeginning.importToNewRefundTicket(ctx, claimId, options);
|
||||||
|
const ticketTracking = await models.TicketTracking.findOne({
|
||||||
|
where: {ticketFk: ticket.id},
|
||||||
|
order: 'id DESC'
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
const newSales = await models.Sale.find({
|
||||||
|
where: {ticketFk: ticket.id}
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
newSales.forEach(sale => {
|
||||||
|
expect(sale.discount).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(ticketTracking.stateFk).toEqual(state.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('debe establecer estado WAITING_FOR_PICKUP para las recogidas por reparto', async() => {
|
||||||
|
const state = await models.State.findOne({
|
||||||
|
where: {code: 'WAITING_FOR_PICKUP'}
|
||||||
|
}, options);
|
||||||
|
await models.Claim.updateAll({id: claimId}, {pickup: 'delivery'}, options);
|
||||||
|
const ticket = await models.ClaimBeginning.importToNewRefundTicket(ctx, claimId, options);
|
||||||
|
const ticketTracking = await models.TicketTracking.findOne({
|
||||||
|
where: {ticketFk: ticket.id},
|
||||||
|
order: 'id DESC'
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
expect(ticketTracking.stateFk).toEqual(state.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('debe establecer estado WAITING_FOR_PICKUP para las recogidas por agencia', async() => {
|
||||||
|
const state = await models.State.findOne({
|
||||||
|
where: {code: 'WAITING_FOR_PICKUP'}
|
||||||
|
}, options);
|
||||||
|
await models.Claim.updateAll({id: claimId}, {pickup: 'agency'}, options);
|
||||||
|
const ticket = await models.ClaimBeginning.importToNewRefundTicket(ctx, claimId, options);
|
||||||
|
const ticketTracking = await models.TicketTracking.findOne({
|
||||||
|
where: {ticketFk: ticket.id},
|
||||||
|
order: 'id DESC'
|
||||||
|
}, options);
|
||||||
|
const newSales = await models.Sale.find({
|
||||||
|
where: {ticketFk: ticket.id}
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
newSales.forEach(sale => {
|
||||||
|
expect(sale.discount).toEqual(100);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(ticketTracking.stateFk).toEqual(state.id);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue