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;
}
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)
throw new UserError('This order is not editable');
if (!isEditable)
throw new UserError('This order is not editable');
const promises = [];
for (const item of params.items) {
promises.push(
Self.rawSql(
`CALL hedera.order_addItem(?, ?, ?, ?)`,
[params.orderFk, item.warehouseFk, item.itemFk, item.quantity],
myOptions
)
);
const promises = [];
for (const item of params.items) {
promises.push(
Self.rawSql(
`CALL hedera.order_addItem(?, ?, ?, ?)`,
[params.orderFk, item.warehouseFk, item.itemFk, item.quantity],
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;
}
if (!params.rows || !params.rows.length)
throw new UserError('There is nothing to delete');
try {
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)
throw new UserError('This order is not editable');
if (!isEditable)
throw new UserError('This order is not editable');
const promises = [];
for (let i = 0; i < params.rows.length; i++)
promises.push(Self.app.models.OrderRow.destroyById(params.rows[i], myOptions));
const promises = [];
for (let i = 0; i < params.rows.length; i++)
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;
}
const address = await Self.app.models.Address.findOne({
where: {id: addressId},
fields: ['clientFk'],
include: [
{relation: 'client',
scope: {
include: {
relation: 'type'
try {
const address = await Self.app.models.Address.findOne({
where: {id: addressId},
fields: ['clientFk'],
include: [
{relation: 'client',
scope: {
include: {
relation: 'type'
}
}
}
}
]
}, myOptions);
]
}, myOptions);
if (address.client().type().code === 'normal') {
if (!address.client().isActive)
throw new UserError(`You can't create an order for an inactive client`);
if (address.client().type().code === 'normal') {
if (!address.client().isActive)
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;
}
const ticket = await Self.app.models.Ticket.findOne({
where: {id: ticketFk}
}, myOptions);
try {
const ticket = await Self.app.models.Ticket.findOne({
where: {id: ticketFk}
}, myOptions);
const landed = ticket.landed;
const addressFk = ticket.addressFk;
const agencyModeFk = ticket.agencyModeFk;
const landed = ticket.landed;
const addressFk = ticket.addressFk;
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({});
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);
const orderRows = await models.OrderRow.find({where: {orderFk: id}}, myOptions);
if (order.isConfirmed || orderRows.length != 0)
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)
throw new UserError(`You can't make changes on the basic data of an confirmed order or with rows`);
const updateParams = pick(params, [
'clientFk',
'addressFk',
'landed',
'agencyModeFk',
'note',
]);
if (Object.keys(updateParams).length)
await order.updateAttributes(updateParams, myOptions);
const updateParams = pick(params, [
'clientFk',
'addressFk',
'landed',
'agencyModeFk',
'note',
]);
if (Object.keys(updateParams).length)
await order.updateAttributes(updateParams, myOptions);
if (tx) await tx.commit();
return order;
return order;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};