translations and cleanup
This commit is contained in:
@@ -9,6 +9,7 @@ Name = Name
|
|||||||
AuthorName = Author's Name
|
AuthorName = Author's Name
|
||||||
Authors = Authors
|
Authors = Authors
|
||||||
Order = Order
|
Order = Order
|
||||||
|
OrderItems = Order Items
|
||||||
Orders = Orders
|
Orders = Orders
|
||||||
Price = Price
|
Price = Price
|
||||||
shippingAddress = Shipping Address
|
shippingAddress = Shipping Address
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ annotate AdminService.Orders with @(UI : {
|
|||||||
Facets : [
|
Facets : [
|
||||||
{
|
{
|
||||||
$Type : 'UI.ReferenceFacet',
|
$Type : 'UI.ReferenceFacet',
|
||||||
Label : '{i18n>ShippingAddress}',
|
Label : '{i18n>shippingAddress}',
|
||||||
Target : '@UI.FieldGroup#ShippingAddress'
|
Target : '@UI.FieldGroup#ShippingAddress'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -46,11 +46,12 @@ bupaSrv.on('sap/S4HANAOD/c532/BO/BusinessPartner/Changed', async msg => {
|
|||||||
const txExt = bupaSrv.transaction(msg)
|
const txExt = bupaSrv.transaction(msg)
|
||||||
try {
|
try {
|
||||||
const remoteAddresses = await txExt.run(selectQl)
|
const remoteAddresses = await txExt.run(selectQl)
|
||||||
const qlsToUpdateDifferences = _qlsToUpdateDifferences(ownAddresses, remoteAddresses)
|
const qlsToUpdateDifferences = _qlsToUpdateDifferences(
|
||||||
|
ownAddresses,
|
||||||
|
remoteAddresses
|
||||||
|
)
|
||||||
if (qlsToUpdateDifferences.length) {
|
if (qlsToUpdateDifferences.length) {
|
||||||
await Promise.all(qlsToUpdateDifferences.map(ql =>
|
await Promise.all(qlsToUpdateDifferences.map(ql => tx.run(ql)))
|
||||||
tx.run(ql)
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
@@ -58,71 +59,64 @@ bupaSrv.on('sap/S4HANAOD/c532/BO/BusinessPartner/Changed', async msg => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
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 txExt = bupaSrv.transaction(req)
|
||||||
|
const ql = req.query.from(ShippingAddresses).where({ BusinessPartner })
|
||||||
|
|
||||||
|
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 BusinessPartner = req.user.id
|
||||||
const txExt = bupaSrv.transaction(req)
|
const txExt = bupaSrv.transaction(req)
|
||||||
const ql = req.query.from(ShippingAddresses).where({ BusinessPartner })
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await txExt.run(ql)
|
const response = await txExt.run(
|
||||||
return result
|
SELECT.from(ShippingAddresses).where({
|
||||||
} catch (e) {
|
AddressID: req.data.shippingAddress_AddressID,
|
||||||
console.error(e)
|
BusinessPartner
|
||||||
}
|
})
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
const qlStatement = INSERT.into(ShippingAddresses).entries(response)
|
|
||||||
await tx.run(qlStatement)
|
|
||||||
} catch (e) {
|
|
||||||
// already in there
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
req.error('Shipping address not found.')
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function _reduceStock(req) {
|
|
||||||
const { Items: OrderItems } = req.data
|
|
||||||
if (OrderItems && OrderItems.length > 0) {
|
|
||||||
const all = await cds.transaction(req).run(() =>
|
|
||||||
OrderItems.map(order =>
|
|
||||||
UPDATE(Books)
|
|
||||||
.set('stock -=', order.amount)
|
|
||||||
.where('ID =', order.book_ID)
|
|
||||||
.and('stock >=', order.amount)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
all.forEach((affectedRows, i) => {
|
if (response && response.length === 1) {
|
||||||
if (affectedRows === 0)
|
const tx = cds.transaction(req)
|
||||||
req.error(
|
const qlStatement = INSERT.into(ShippingAddresses).entries(response)
|
||||||
409,
|
await tx.run(qlStatement)
|
||||||
`${OrderItems[i].amount} exceeds stock for book #${
|
}
|
||||||
OrderItems[i].book_ID
|
} catch (e) {}
|
||||||
}`
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function _reduceStock (req) {
|
||||||
|
const { Items: OrderItems } = req.data
|
||||||
|
if (OrderItems && OrderItems.length > 0) {
|
||||||
|
const all = await cds.transaction(req).run(() =>
|
||||||
|
OrderItems.map(order =>
|
||||||
|
UPDATE(Books)
|
||||||
|
.set('stock -=', order.amount)
|
||||||
|
.where('ID =', order.book_ID)
|
||||||
|
.and('stock >=', order.amount)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
all.forEach((affectedRows, i) => {
|
||||||
|
if (affectedRows === 0)
|
||||||
|
req.error(
|
||||||
|
409,
|
||||||
|
`${OrderItems[i].amount} exceeds stock for book #${
|
||||||
|
OrderItems[i].book_ID
|
||||||
|
}`
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = cds.service.impl(function () {
|
||||||
this.before('CREATE', 'Orders', _reduceStock)
|
this.before('CREATE', 'Orders', _reduceStock)
|
||||||
this.before('PATCH', 'Orders', _fillAddress)
|
this.before('PATCH', 'Orders', _fillAddress)
|
||||||
this.on('READ', 'Addresses', _readAddresses)
|
this.on('READ', 'Addresses', _readAddresses)
|
||||||
|
|||||||
Reference in New Issue
Block a user