more module transactions
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2021-08-13 16:46:44 +02:00
parent cd334bf967
commit 7a7bbe2ba8
8 changed files with 257 additions and 228 deletions

View File

@ -77,12 +77,13 @@ module.exports = Self => {
}, myOptions);
if (!ticketFk) {
ticketFk = await createTicket(ctx, {
ctx.args = {
clientId: address.clientFk,
warehouseId: sale.ticket().warehouseFk,
companyId: sale.ticket().companyFk,
addressId: addressId
}, myOptions);
};
ticketFk = await createTicket(ctx, myOptions);
}
await models.Sale.create({
@ -153,22 +154,13 @@ module.exports = Self => {
return ticket && ticket.id;
}
async function createTicket(ctx, params, options) {
params.shipped = new Date();
params.landed = new Date();
params.agencyModeId = null;
params.routeId = null;
async function createTicket(ctx, options) {
ctx.args.shipped = new Date();
ctx.args.landed = new Date();
ctx.args.agencyModeId = null;
ctx.args.routeId = null;
const ticket = await Self.app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyId,
params.addressId,
params.agencyModeId,
params.routeId,
options);
const ticket = await Self.app.models.Ticket.new(ctx, options);
return ticket.id;
}

View File

@ -1,6 +1,6 @@
const app = require('vn-loopback/server/server');
describe('regularizeClaim()', () => {
describe('claim regularizeClaim()', () => {
const ctx = {
req: {
accessToken: {userId: 18},

View File

@ -65,16 +65,17 @@ module.exports = Self => {
}, myOptions);
if (!ticketId) {
ticketId = await createTicket(ctx, {
ctx.args = {
clientId: itemDestination.address().clientFk,
warehouseId: warehouseFk,
addressId: itemDestination.addressFk
}, myOptions);
};
ticketId = await createTicket(ctx, myOptions);
}
res = await models.Item.getVisibleAvailable(itemFk, warehouseFk);
const res = await models.Item.getVisibleAvailable(itemFk, warehouseFk, null, myOptions);
let newQuantity = res.visible - quantity;
const newQuantity = res.visible - quantity;
await models.Sale.create({
ticketFk: ticketId,
@ -92,23 +93,14 @@ module.exports = Self => {
throw e;
}
async function createTicket(ctx, params, options) {
params.shipped = new Date();
params.landed = new Date();
params.companyId = null;
params.agencyModeId = null;
params.routeId = null;
async function createTicket(ctx, options) {
ctx.args.shipped = new Date();
ctx.args.landed = new Date();
ctx.args.companyId = null;
ctx.args.agencyModeId = null;
ctx.args.routeId = null;
const ticket = await Self.app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyId,
params.addressId,
params.agencyModeId,
params.routeId,
options);
const ticket = await Self.app.models.Ticket.new(ctx, options);
return ticket.id;
}

View File

@ -57,77 +57,78 @@ module.exports = Self => {
}
});
Self.new = async(ctx, clientId, shipped, landed, warehouseId,
companyId, addressId, agencyModeId, routeId, options) => {
Self.new = async(ctx, options) => {
const args = ctx.args;
const myUserId = ctx.req.accessToken.userId;
const models = Self.app.models;
const address = await models.Address.findOne({
where: {id: addressId},
fields: ['id', 'clientFk'],
include: {
relation: 'client',
scope: {
include: {
relation: 'type'
}
}
}
});
if (!address)
throw new UserError(`This address doesn't exist`);
let agencyMode;
if (agencyModeId)
agencyMode = await models.AgencyMode.findById(agencyModeId);
if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) {
const canCreateTicket = await models.Client.canCreateTicket(clientId);
if (!canCreateTicket)
throw new UserError(`You can't create a ticket for a inactive client`);
}
const myOptions = {};
let tx;
if ((typeof options) != 'object')
options = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!options.transaction) {
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
options.transaction = tx;
myOptions.transaction = tx;
}
try {
if (!shipped && landed) {
const shippedResult = await models.Agency.getShipped(landed,
address.id, agencyModeId, warehouseId);
shipped = (shippedResult && shippedResult.shipped) || landed;
const address = await models.Address.findOne({
where: {id: args.addressId},
fields: ['id', 'clientFk'],
include: {
relation: 'client',
scope: {
include: {
relation: 'type'
}
}
}
}, myOptions);
if (!address)
throw new UserError(`This address doesn't exist`);
let agencyMode;
if (args.agencyModeId)
agencyMode = await models.AgencyMode.findById(args.agencyModeId, null, myOptions);
if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) {
const canCreateTicket = await models.Client.canCreateTicket(args.clientId, myOptions);
if (!canCreateTicket)
throw new UserError(`You can't create a ticket for a inactive client`);
}
if (shipped && !landed) {
const landedResult = await models.Agency.getLanded(shipped,
address.id, agencyModeId, warehouseId, false);
landed = landedResult && landedResult.landed;
if (!args.shipped && args.landed) {
const shippedResult = await models.Agency.getShipped(args.landed,
address.id, args.agencyModeId, args.warehouseId, myOptions);
args.shipped = (shippedResult && shippedResult.shipped) || args.landed;
}
if (args.shipped && !args.landed) {
const landedResult = await models.Agency.getLanded(args.shipped,
address.id, args.agencyModeId, args.warehouseId, false, myOptions);
args.landed = landedResult && landedResult.landed;
}
query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result);
SELECT @result newTicketId;`;
let result = await Self.rawSql(query, [
clientId,
shipped,
warehouseId,
companyId || 442,
addressId,
agencyModeId || null,
routeId || null,
landed,
const result = await Self.rawSql(query, [
args.clientId,
args.shipped,
args.warehouseId,
args.companyId || 442,
args.addressId,
args.agencyModeId || null,
args.routeId || null,
args.landed,
myUserId
], options);
], myOptions);
let ticket = await models.Ticket.findById(result[1][0].newTicketId, null, options);
let cleanInstance = JSON.parse(JSON.stringify(ticket));
const ticket = await models.Ticket.findById(result[1][0].newTicketId, null, myOptions);
const cleanInstance = JSON.parse(JSON.stringify(ticket));
let logRecord = {
const logRecord = {
originFk: cleanInstance.id,
userFk: myUserId,
action: 'insert',
@ -137,9 +138,10 @@ module.exports = Self => {
newInstance: cleanInstance
};
await models.TicketLog.create(logRecord, options);
await models.TicketLog.create(logRecord, myOptions);
if (tx) await tx.commit();
return await ticket;
} catch (e) {
if (tx) await tx.rollback();

View File

@ -6,43 +6,43 @@ module.exports = Self => {
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'Number',
type: 'number',
required: true,
description: 'The ticket id',
http: {source: 'path'}
},
{
arg: 'landed',
type: 'Date',
type: 'date',
description: 'The landing date',
required: true
},
{
arg: 'addressId',
type: 'Number',
type: 'number',
description: 'The address id',
required: true
},
{
arg: 'agencyModeId',
type: 'Number',
type: 'number',
description: 'The agencyMode id',
required: true
},
{
arg: 'zoneId',
type: 'Number',
type: 'number',
description: 'The zone id',
required: true
},
{
arg: 'warehouseId',
type: 'Number',
type: 'number',
description: 'The warehouse id',
required: true
}],
returns: {
type: ['Object'],
type: ['object'],
root: true
},
http: {
@ -51,38 +51,57 @@ module.exports = Self => {
}
});
Self.priceDifference = async(ctx, id, landed, addressId, agencyModeId, zoneId, warehouseId) => {
Self.priceDifference = async(ctx, options) => {
const args = ctx.args;
const models = Self.app.models;
const isEditable = await Self.isEditable(ctx, id);
const userId = ctx.req.accessToken.userId;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
const isEditable = await Self.isEditable(ctx, args.id, myOptions);
if (!isEditable)
throw new UserError(`The sales of this ticket can't be modified`);
const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss');
const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss', myOptions);
if (!isProductionBoss) {
const zoneShipped = await models.Agency.getShipped(landed, addressId, agencyModeId, warehouseId);
const zoneShipped = await models.Agency.getShipped(
args.landed,
args.addressId,
args.agencyModeId,
args.warehouseId,
myOptions);
if (!zoneShipped || zoneShipped.zoneFk != zoneId)
if (!zoneShipped || zoneShipped.zoneFk != args.zoneId)
throw new UserError(`You don't have privileges to change the zone`);
}
let salesObj = {
items: await models.Sale.find({
where: {
ticketFk: id
},
order: 'concept ASC',
include: 'item'
}),
const items = await models.Sale.find({
where: {
ticketFk: args.id
},
order: 'concept ASC',
include: 'item'
}, myOptions);
const salesObj = {
items: items,
totalUnitPrice: 0.00,
totalNewPrice: 0.00,
totalDifference: 0.00,
};
const query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`;
const args = [id, landed, addressId, zoneId, warehouseId];
const [difComponents] = await Self.rawSql(query, args);
const params = [args.id, args.landed, args.addressId, args.zoneId, args.warehouseId];
const [difComponents] = await Self.rawSql(query, params, myOptions);
const map = new Map();

View File

@ -1,118 +1,119 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
let UserError = require('vn-loopback/util/user-error');
describe('ticket new()', () => {
let ticketIdsToDelete = [];
let today = new Date();
let ctx = {req: {accessToken: {userId: 1}}};
afterAll(async done => {
for (id of ticketIdsToDelete)
await app.models.Ticket.destroyById(id);
done();
});
const today = new Date();
const ctx = {req: {accessToken: {userId: 1}}};
it('should throw an error if the client isnt frozen and isnt active', async() => {
let error;
let params = {
clientId: 1106,
shipped: today,
landed: null,
warehouseId: 1,
companyId: 442,
addressId: 6
};
const tx = await models.Ticket.beginTransaction({});
await app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyId,
params.addressId
).catch(e => {
let error;
try {
const options = {transaction: tx};
ctx.args = {
clientId: 1106,
shipped: today,
landed: null,
warehouseId: 1,
companyId: 442,
addressId: 6
};
await models.Ticket.new(ctx, options);
await tx.rollback();
} catch (e) {
error = e;
});
await tx.rollback();
}
expect(error).toEqual(new UserError(`You can't create a ticket for a inactive client`));
});
it('should throw an error if the address doesnt exist', async() => {
const tx = await models.Ticket.beginTransaction({});
let error;
let params = {
clientId: 1104,
shipped: today,
landed: null,
warehouseId: 1,
companyId: 442,
addressId: 'invalid address'
};
await app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyId,
params.addressId
).catch(response => {
expect(response.message).toEqual(`This address doesn't exist`);
error = response;
});
try {
const options = {transaction: tx};
expect(error).toBeDefined();
ctx.args = {
clientId: 1104,
shipped: today,
landed: null,
warehouseId: 1,
companyId: 442,
addressId: 'invalid address'
};
await models.Ticket.new(ctx, options);
await tx.rollback();
} catch (e) {
error = e;
await tx.rollback();
}
expect(error.message).toEqual(`This address doesn't exist`);
});
it('should return the id of the created ticket', async() => {
let params = {
clientId: 1104,
shipped: today,
landed: today,
warehouseId: 2,
companyId: 442,
addressId: 4,
agencyModeId: 1
};
const tx = await models.Ticket.beginTransaction({});
const ticket = await app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyId,
params.addressId,
params.agencyModeId);
try {
const options = {transaction: tx};
let newestTicketIdInFixtures = 21;
ctx.args = {
clientId: 1104,
shipped: today,
landed: today,
warehouseId: 2,
companyId: 442,
addressId: 4,
agencyModeId: 1
};
ticketIdsToDelete.push(ticket.id);
const ticket = await models.Ticket.new(ctx, options);
expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures);
const newestTicketIdInFixtures = 21;
expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return the set a shipped when the agency is not especified', async() => {
let params = {
clientId: 1104,
landed: today,
shipped: null,
warehouseId: 2,
companyId: 442,
addressId: 4,
agencyModeId: null
};
const tx = await models.Ticket.beginTransaction({});
const ticket = await app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyId,
params.addressId,
params.agencyModeId);
try {
const options = {transaction: tx};
ticketIdsToDelete.push(ticket.id);
ctx.args = {
clientId: 1104,
landed: today,
shipped: null,
warehouseId: 2,
companyId: 442,
addressId: 4,
agencyModeId: null
};
expect(ticket.shipped).toEqual(jasmine.any(Date));
const ticket = await models.Ticket.new(ctx, options);
expect(ticket.shipped).toEqual(jasmine.any(Date));
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -1,38 +1,61 @@
const app = require('vn-loopback/server/server');
let UserError = require('vn-loopback/util/user-error');
const models = require('vn-loopback/server/server').models;
const UserError = require('vn-loopback/util/user-error');
describe('sale priceDifference()', () => {
it('should return ticket price differences', async() => {
let tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
const tx = await models.Ticket.beginTransaction({});
const ticketId = 16;
const landed = tomorrow;
const addressId = 126;
const agencyModeId = 7;
const zoneId = 3;
const warehouseId = 1;
try {
const options = {transaction: tx};
const httpCtx = {req: {accessToken: {userId: 1106}}};
let result = await app.models.Ticket.priceDifference(httpCtx, ticketId, landed,
addressId, agencyModeId, zoneId, warehouseId);
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
expect(result.totalUnitPrice).toEqual(result.totalNewPrice);
expect(result.totalDifference).toEqual(0);
const ctx = {req: {accessToken: {userId: 1106}}};
ctx.args = {
id: 16,
landed: tomorrow,
addressId: 126,
agencyModeId: 7,
zoneId: 3,
warehouseId: 1
};
const result = await models.Ticket.priceDifference(ctx, options);
expect(result.totalUnitPrice).toEqual(result.totalNewPrice);
expect(result.totalDifference).toEqual(0);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return an error if the ticket is not editable', async() => {
const ticketId = 1;
const landed = new Date();
const addressId = 121;
const zoneId = 3;
const warehouseId = 1;
const tx = await models.Ticket.beginTransaction({});
let error;
const httpCtx = {req: {accessToken: {userId: 1106}}};
await app.models.Ticket.priceDifference(httpCtx, ticketId, landed, addressId, zoneId, warehouseId)
.catch(e => {
error = e;
});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: 1106}}};
ctx.args = {
id: 1,
landed: new Date(),
addressId: 121,
zoneId: 3,
warehouseId: 1
};
await models.Ticket.priceDifference(ctx, options);
await tx.rollback();
} catch (e) {
error = e;
await tx.rollback();
}
expect(error).toEqual(new UserError(`The sales of this ticket can't be modified`));
});

View File

@ -35,7 +35,7 @@ module.exports = Self => {
});
Self.getShipped = async(landed, addressFk, agencyModeFk, warehouseFk, options) => {
let myOptions = {};
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);