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,22 +40,31 @@ const _qlsToUpdateDifferences = (ownAddresses, remoteAddresses) =>
bupaSrv.on('sap/messaging/ccf/BO/BusinessPartner/Changed', async msg => {
console.log('>> Message:', msg.data)
const BusinessPartner = msg.data.KEY[0].BUSINESSPARTNER
const tx = cds.transaction()
const selectQl = SELECT.from(ShippingAddresses).where({ BusinessPartner })
const ownAddresses = await tx.run(selectQl)
await tx.commit()
console.log('own:', ownAddresses)
if (ownAddresses && ownAddresses.length > 0) {
console.log('found')
const txExt = bupaSrv.transaction()
try {
const remoteAddresses = await txExt.run(selectQl)
await _qlsToUpdateDifferences(ownAddresses, remoteAddresses).map(async ql =>
await tx.run(ql)
)
const qlsToUpdateDifferences = _qlsToUpdateDifferences(ownAddresses, remoteAddresses)
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 () {
@@ -73,21 +82,24 @@ module.exports = cds.service.impl(function () {
ql.where(req.query.SELECT.where)
}
try {
const result = await txExt.run(ql)
return result
} catch (e) {
console.error(e)
}
}
async function _fillAddress(req) {
if (req.data.shippingAddress_AddressID) {
const BusinessPartner = req.user.id
const txExt = bupaSrv.transaction(req)
try {
const response = await txExt.run(
SELECT.from(ShippingAddresses).where({
AddressID: req.data.shippingAddress_AddressID,
BusinessPartner
})
)
}))
if (response && response.length > 0) {
const tx = cds.transaction(req)
try {
@@ -99,6 +111,9 @@ module.exports = cds.service.impl(function () {
} else {
req.error('Shipping address not found.')
}
} catch (e) {
console.error(e)
}
}
}