needed to use several transactions

This commit is contained in:
D065023
2019-12-11 13:19:50 +01:00
parent 7a760cfaf8
commit 1aa9237d20

View File

@@ -40,26 +40,35 @@ const _qlsToUpdateDifferences = (ownAddresses, remoteAddresses) =>
bupaSrv.on('sap/messaging/ccf/BO/BusinessPartner/Changed', async msg => { bupaSrv.on('sap/messaging/ccf/BO/BusinessPartner/Changed', async msg => {
console.log('>> Message:', msg.data) console.log('>> Message:', msg.data)
const BusinessPartner = msg.data.KEY[0].BUSINESSPARTNER const BusinessPartner = msg.data.KEY[0].BUSINESSPARTNER
const tx = cds.transaction() const tx = cds.transaction()
const selectQl = SELECT.from(ShippingAddresses).where({ BusinessPartner }) const selectQl = SELECT.from(ShippingAddresses).where({ BusinessPartner })
const ownAddresses = await tx.run(selectQl) const ownAddresses = await tx.run(selectQl)
await tx.commit()
console.log('own:', ownAddresses) console.log('own:', ownAddresses)
if (ownAddresses && ownAddresses.length > 0) { if (ownAddresses && ownAddresses.length > 0) {
console.log('found') console.log('found')
const txExt = bupaSrv.transaction() const txExt = bupaSrv.transaction()
const remoteAddresses = await txExt.run(selectQl) try {
const remoteAddresses = await txExt.run(selectQl)
await _qlsToUpdateDifferences(ownAddresses, remoteAddresses).map(async ql => const qlsToUpdateDifferences = _qlsToUpdateDifferences(ownAddresses, remoteAddresses)
await tx.run(ql) const tx2 = cds.transaction()
) if (qlsToUpdateDifferences.length) {
await Promise.all(qlsToUpdateDifferences.map(ql =>
tx2.run(ql)
))
tx2.commit()
}
} catch (e) {
console.error(e)
}
} }
await tx.commit()
}) })
module.exports = cds.service.impl(function () { module.exports = cds.service.impl(function () {
async function _readAddresses (req) { async function _readAddresses(req) {
console.log('Addresses', ShippingAddresses) console.log('Addresses', ShippingAddresses)
const BusinessPartner = req.user.id const BusinessPartner = req.user.id
const txExt = bupaSrv.transaction(req) const txExt = bupaSrv.transaction(req)
@@ -73,36 +82,42 @@ module.exports = cds.service.impl(function () {
ql.where(req.query.SELECT.where) ql.where(req.query.SELECT.where)
} }
const result = await txExt.run(ql) try {
const result = await txExt.run(ql)
return result return result
} catch (e) {
console.error(e)
}
} }
async function _fillAddress (req) { async function _fillAddress(req) {
if (req.data.shippingAddress_AddressID) { if (req.data.shippingAddress_AddressID) {
const BusinessPartner = req.user.id const BusinessPartner = req.user.id
const txExt = bupaSrv.transaction(req) const txExt = bupaSrv.transaction(req)
const response = await txExt.run( try {
SELECT.from(ShippingAddresses).where({ const response = await txExt.run(
AddressID: req.data.shippingAddress_AddressID, SELECT.from(ShippingAddresses).where({
BusinessPartner AddressID: req.data.shippingAddress_AddressID,
}) BusinessPartner
) }))
if (response && response.length > 0) { if (response && response.length > 0) {
const tx = cds.transaction(req) const tx = cds.transaction(req)
try { try {
const qlStatement = INSERT.into(ShippingAddresses).entries(response) const qlStatement = INSERT.into(ShippingAddresses).entries(response)
await tx.run(qlStatement) await tx.run(qlStatement)
} catch (e) { } catch (e) {
// already in there // already in there
}
} else {
req.error('Shipping address not found.')
} }
} else { } catch (e) {
req.error('Shipping address not found.') console.error(e)
} }
} }
} }
async function _reduceStock (req) { async function _reduceStock(req) {
const { Items: OrderItems } = req.data const { Items: OrderItems } = req.data
if (OrderItems && OrderItems.length > 0) { if (OrderItems && OrderItems.length > 0) {
const all = await cds.transaction(req).run(() => const all = await cds.transaction(req).run(() =>
@@ -118,7 +133,7 @@ module.exports = cds.service.impl(function () {
req.error( req.error(
409, 409,
`${OrderItems[i].amount} exceeds stock for book #${ `${OrderItems[i].amount} exceeds stock for book #${
OrderItems[i].book_ID OrderItems[i].book_ID
}` }`
) )
}) })