This commit is contained in:
D065023
2020-01-08 13:45:53 +01:00
parent 9ba5aae999
commit dc1ea91d9e
4 changed files with 12 additions and 48 deletions

View File

@@ -0,0 +1,3 @@
{
"printWidth": 120
}

View File

@@ -27,9 +27,7 @@ module.exports = cds.service.impl(async () => {
const local = db.transaction(req)
const [replica] = await local.read(Addresses).where(assigned)
if (replica) return //> already replicated
const [address] = await bupa
.tx(req)
.run(SELECT.from(externalAddresses).where(assigned))
const [address] = await bupa.tx(req).run(SELECT.from(externalAddresses).where(assigned))
if (address) return local.create(Addresses).entries(address)
})
@@ -68,13 +66,8 @@ module.exports = cds.service.impl(async () => {
const { Items } = req.data
// validate input...
if (!Items || Items.length === 0)
return req.reject('Please order at least one item.')
if (!req.data.shippingAddress_ID)
return req.reject(
'Please enter a valid shipping address.',
'shippingAddress_ID'
)
if (!Items || Items.length === 0) return req.reject('Please order at least one item.')
if (!req.data.shippingAddress_ID) return req.reject('Please enter a valid shipping address.', 'shippingAddress_ID')
// reduce stock on ordered books...
const all = await db.tx(req).run(
@@ -85,14 +78,7 @@ module.exports = cds.service.impl(async () => {
.set('stock -=', each.amount)
)
)
all.forEach(
(affectedRows, i) =>
affectedRows > 0 ||
req.error(
409,
`${Items[i].amount} exceeds stock for book #${Items[i].book_ID}`
)
)
all.forEach((affectedRows, i) => affectedRows > 0 || req.error(409, `${Items[i].amount} exceeds stock for book #${Items[i].book_ID}`))
})
})
require('./utils')

View File

@@ -3,11 +3,7 @@ const { Books } = cds.entities
/** Service implementation for CatalogService */
module.exports = cds.service.impl(function () {
this.after(
'READ',
'Books',
each => each.stock > 111 && _addDiscount2(each, 11)
)
this.after('READ', 'Books', each => each.stock > 111 && _addDiscount2(each, 11))
this.before('CREATE', 'Orders', _reduceStock)
})
@@ -28,12 +24,6 @@ async function _reduceStock (req) {
)
)
all.forEach((affectedRows, i) => {
if (affectedRows === 0)
req.error(
409,
`${OrderItems[i].amount} exceeds stock for book #${
OrderItems[i].book_ID
}`
)
if (affectedRows === 0) req.error(409, `${OrderItems[i].amount} exceeds stock for book #${OrderItems[i].book_ID}`)
})
}

View File

@@ -4,33 +4,18 @@ process.env['https_proxy'] = ''
process.env['HTTP_PROXY'] = ''
process.env['HTTPS_PROXY'] = ''
const diff = (obj1, obj2) =>
Object.keys(obj1).reduce(
(res, curr) =>
obj1[curr] === obj2[curr] ? res : (res[curr] = obj2[curr]) && res,
{}
)
const diff = (obj1, obj2) => Object.keys(obj1).reduce((res, curr) => (obj1[curr] === obj2[curr] ? res : (res[curr] = obj2[curr]) && res), {})
const queriesToUpdateDifferences = (entity, ownEntries, otherEntries) =>
ownEntries
.map(ownEntry => {
const otherEntry = otherEntries.find(otherEntry =>
Object.keys(entity.keys).reduce(
(res, curr) => res && otherEntry[curr] === ownEntry[curr],
true
)
)
const otherEntry = otherEntries.find(otherEntry => Object.keys(entity.keys).reduce((res, curr) => res && otherEntry[curr] === ownEntry[curr], true))
if (otherEntry) {
const differences = diff(ownEntry, otherEntry)
if (Object.keys(differences).length) {
return UPDATE(entity)
.set(differences)
.where(
Object.keys(entity.keys).reduce(
(res, curr) => (res[curr] = ownEntry[curr]) && res,
{}
)
)
.where(Object.keys(entity.keys).reduce((res, curr) => (res[curr] = ownEntry[curr]) && res, {}))
}
}
})