more module transactions
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
cd334bf967
commit
7a7bbe2ba8
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const app = require('vn-loopback/server/server');
|
||||
|
||||
describe('regularizeClaim()', () => {
|
||||
describe('claim regularizeClaim()', () => {
|
||||
const ctx = {
|
||||
req: {
|
||||
accessToken: {userId: 18},
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -57,12 +57,24 @@ 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 myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
try {
|
||||
const address = await models.Address.findOne({
|
||||
where: {id: addressId},
|
||||
where: {id: args.addressId},
|
||||
fields: ['id', 'clientFk'],
|
||||
include: {
|
||||
relation: 'client',
|
||||
|
@ -72,62 +84,51 @@ module.exports = Self => {
|
|||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}, myOptions);
|
||||
|
||||
if (!address)
|
||||
throw new UserError(`This address doesn't exist`);
|
||||
|
||||
let agencyMode;
|
||||
if (agencyModeId)
|
||||
agencyMode = await models.AgencyMode.findById(agencyModeId);
|
||||
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(clientId);
|
||||
const canCreateTicket = await models.Client.canCreateTicket(args.clientId, myOptions);
|
||||
if (!canCreateTicket)
|
||||
throw new UserError(`You can't create a ticket for a inactive client`);
|
||||
}
|
||||
|
||||
let tx;
|
||||
|
||||
if ((typeof options) != 'object')
|
||||
options = {};
|
||||
|
||||
if (!options.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
options.transaction = tx;
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
if (!shipped && landed) {
|
||||
const shippedResult = await models.Agency.getShipped(landed,
|
||||
address.id, agencyModeId, warehouseId);
|
||||
shipped = (shippedResult && shippedResult.shipped) || landed;
|
||||
}
|
||||
|
||||
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 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();
|
||||
|
|
|
@ -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({
|
||||
const items = await models.Sale.find({
|
||||
where: {
|
||||
ticketFk: id
|
||||
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();
|
||||
|
||||
|
|
|
@ -1,21 +1,19 @@
|
|||
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() => {
|
||||
const tx = await models.Ticket.beginTransaction({});
|
||||
|
||||
let error;
|
||||
let params = {
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
ctx.args = {
|
||||
clientId: 1106,
|
||||
shipped: today,
|
||||
landed: null,
|
||||
|
@ -24,23 +22,26 @@ describe('ticket new()', () => {
|
|||
addressId: 6
|
||||
};
|
||||
|
||||
await app.models.Ticket.new(ctx,
|
||||
params.clientId,
|
||||
params.shipped,
|
||||
params.landed,
|
||||
params.warehouseId,
|
||||
params.companyId,
|
||||
params.addressId
|
||||
).catch(e => {
|
||||
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 = {
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
ctx.args = {
|
||||
clientId: 1104,
|
||||
shipped: today,
|
||||
landed: null,
|
||||
|
@ -49,23 +50,24 @@ describe('ticket new()', () => {
|
|||
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;
|
||||
});
|
||||
await models.Ticket.new(ctx, options);
|
||||
|
||||
expect(error).toBeDefined();
|
||||
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 = {
|
||||
const tx = await models.Ticket.beginTransaction({});
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
ctx.args = {
|
||||
clientId: 1104,
|
||||
shipped: today,
|
||||
landed: today,
|
||||
|
@ -75,24 +77,26 @@ describe('ticket new()', () => {
|
|||
agencyModeId: 1
|
||||
};
|
||||
|
||||
const ticket = await app.models.Ticket.new(ctx,
|
||||
params.clientId,
|
||||
params.shipped,
|
||||
params.landed,
|
||||
params.warehouseId,
|
||||
params.companyId,
|
||||
params.addressId,
|
||||
params.agencyModeId);
|
||||
const ticket = await models.Ticket.new(ctx, options);
|
||||
|
||||
let newestTicketIdInFixtures = 21;
|
||||
|
||||
ticketIdsToDelete.push(ticket.id);
|
||||
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 = {
|
||||
const tx = await models.Ticket.beginTransaction({});
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
ctx.args = {
|
||||
clientId: 1104,
|
||||
landed: today,
|
||||
shipped: null,
|
||||
|
@ -102,17 +106,14 @@ describe('ticket new()', () => {
|
|||
agencyModeId: null
|
||||
};
|
||||
|
||||
const ticket = await app.models.Ticket.new(ctx,
|
||||
params.clientId,
|
||||
params.shipped,
|
||||
params.landed,
|
||||
params.warehouseId,
|
||||
params.companyId,
|
||||
params.addressId,
|
||||
params.agencyModeId);
|
||||
|
||||
ticketIdsToDelete.push(ticket.id);
|
||||
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;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
const tx = await models.Ticket.beginTransaction({});
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
const tomorrow = new Date();
|
||||
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||
|
||||
const ticketId = 16;
|
||||
const landed = tomorrow;
|
||||
const addressId = 126;
|
||||
const agencyModeId = 7;
|
||||
const zoneId = 3;
|
||||
const warehouseId = 1;
|
||||
const ctx = {req: {accessToken: {userId: 1106}}};
|
||||
ctx.args = {
|
||||
id: 16,
|
||||
landed: tomorrow,
|
||||
addressId: 126,
|
||||
agencyModeId: 7,
|
||||
zoneId: 3,
|
||||
warehouseId: 1
|
||||
};
|
||||
|
||||
const httpCtx = {req: {accessToken: {userId: 1106}}};
|
||||
let result = await app.models.Ticket.priceDifference(httpCtx, ticketId, landed,
|
||||
addressId, agencyModeId, zoneId, warehouseId);
|
||||
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 => {
|
||||
|
||||
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`));
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue