transtaction commits on several endpoints
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2021-12-16 10:56:10 +01:00
parent 2bfc6a5987
commit 5a448234d6
5 changed files with 106 additions and 70 deletions

View File

@ -33,23 +33,30 @@ module.exports = Self => {
myOptions.transaction = tx; myOptions.transaction = tx;
} }
const isEditable = await Self.app.models.Order.isEditable(params.orderFk, myOptions); try {
const isEditable = await Self.app.models.Order.isEditable(params.orderFk, myOptions);
if (!isEditable) if (!isEditable)
throw new UserError('This order is not editable'); throw new UserError('This order is not editable');
const promises = []; const promises = [];
for (const item of params.items) { for (const item of params.items) {
promises.push( promises.push(
Self.rawSql( Self.rawSql(
`CALL hedera.order_addItem(?, ?, ?, ?)`, `CALL hedera.order_addItem(?, ?, ?, ?)`,
[params.orderFk, item.warehouseFk, item.itemFk, item.quantity], [params.orderFk, item.warehouseFk, item.itemFk, item.quantity],
myOptions myOptions
) )
); );
}
await Promise.all(promises);
if (tx) await tx.commit();
return true;
} catch (e) {
if (tx) await tx.rollback();
throw e;
} }
await Promise.all(promises);
return true;
}; };
}; };

View File

@ -33,18 +33,27 @@ module.exports = Self => {
myOptions.transaction = tx; myOptions.transaction = tx;
} }
if (!params.rows || !params.rows.length) try {
throw new UserError('There is nothing to delete'); if (!params.rows || !params.rows.length)
throw new UserError('There is nothing to delete');
const isEditable = await Self.app.models.Order.isEditable(params.actualOrderId, myOptions); const isEditable = await Self.app.models.Order.isEditable(params.actualOrderId, myOptions);
if (!isEditable) if (!isEditable)
throw new UserError('This order is not editable'); throw new UserError('This order is not editable');
const promises = []; const promises = [];
for (let i = 0; i < params.rows.length; i++) for (let i = 0; i < params.rows.length; i++)
promises.push(Self.app.models.OrderRow.destroyById(params.rows[i], myOptions)); promises.push(Self.app.models.OrderRow.destroyById(params.rows[i], myOptions));
return Promise.all(promises); const deletions = await Promise.all(promises);
if (tx) await tx.commit();
return deletions;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
}; };

View File

@ -44,33 +44,40 @@ module.exports = Self => {
myOptions.transaction = tx; myOptions.transaction = tx;
} }
const address = await Self.app.models.Address.findOne({ try {
where: {id: addressId}, const address = await Self.app.models.Address.findOne({
fields: ['clientFk'], where: {id: addressId},
include: [ fields: ['clientFk'],
{relation: 'client', include: [
scope: { {relation: 'client',
include: { scope: {
relation: 'type' include: {
relation: 'type'
}
} }
} }
} ]
] }, myOptions);
}, myOptions);
if (address.client().type().code === 'normal') { if (address.client().type().code === 'normal') {
if (!address.client().isActive) if (!address.client().isActive)
throw new UserError(`You can't create an order for an inactive client`); throw new UserError(`You can't create an order for an inactive client`);
}
query = `CALL vn.orderListCreate(?, ?, ?, ?);`;
[result] = await Self.rawSql(query, [
landed,
agencyModeId,
addressId,
'SALIX'
], myOptions);
if (tx) await tx.commit();
return result[0].vOrderId;
} catch (e) {
if (tx) await tx.rollback();
throw e;
} }
query = `CALL vn.orderListCreate(?, ?, ?, ?);`;
[result] = await Self.rawSql(query, [
landed,
agencyModeId,
addressId,
'SALIX'
], myOptions);
return result[0].vOrderId;
}; };
}; };

View File

@ -30,16 +30,23 @@ module.exports = Self => {
myOptions.transaction = tx; myOptions.transaction = tx;
} }
const ticket = await Self.app.models.Ticket.findOne({ try {
where: {id: ticketFk} const ticket = await Self.app.models.Ticket.findOne({
}, myOptions); where: {id: ticketFk}
}, myOptions);
const landed = ticket.landed; const landed = ticket.landed;
const addressFk = ticket.addressFk; const addressFk = ticket.addressFk;
const agencyModeFk = ticket.agencyModeFk; const agencyModeFk = ticket.agencyModeFk;
const orderID = await Self.app.models.Order.new(landed, addressFk, agencyModeFk, myOptions); const orderID = await Self.app.models.Order.new(landed, addressFk, agencyModeFk, myOptions);
return orderID; if (tx) await tx.commit();
return orderID;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
}; };

View File

@ -43,23 +43,29 @@ module.exports = Self => {
tx = await Self.beginTransaction({}); tx = await Self.beginTransaction({});
myOptions.transaction = tx; myOptions.transaction = tx;
} }
try {
const order = await models.Order.findById(id, null, myOptions);
const orderRows = await models.OrderRow.find({where: {orderFk: id}}, myOptions);
const order = await models.Order.findById(id, null, myOptions); if (order.isConfirmed || orderRows.length != 0)
const orderRows = await models.OrderRow.find({where: {orderFk: id}}, myOptions); throw new UserError(`You can't make changes on the basic data of an confirmed order or with rows`);
if (order.isConfirmed || orderRows.length != 0) const updateParams = pick(params, [
throw new UserError(`You can't make changes on the basic data of an confirmed order or with rows`); 'clientFk',
'addressFk',
'landed',
'agencyModeFk',
'note',
]);
if (Object.keys(updateParams).length)
await order.updateAttributes(updateParams, myOptions);
const updateParams = pick(params, [ if (tx) await tx.commit();
'clientFk',
'addressFk',
'landed',
'agencyModeFk',
'note',
]);
if (Object.keys(updateParams).length)
await order.updateAttributes(updateParams, myOptions);
return order; return order;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
}; };