prettier
This commit is contained in:
3
packages/bookshop/.prettierrc
Normal file
3
packages/bookshop/.prettierrc
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"printWidth": 120
|
||||||
|
}
|
||||||
@@ -27,9 +27,7 @@ module.exports = cds.service.impl(async () => {
|
|||||||
const local = db.transaction(req)
|
const local = db.transaction(req)
|
||||||
const [replica] = await local.read(Addresses).where(assigned)
|
const [replica] = await local.read(Addresses).where(assigned)
|
||||||
if (replica) return //> already replicated
|
if (replica) return //> already replicated
|
||||||
const [address] = await bupa
|
const [address] = await bupa.tx(req).run(SELECT.from(externalAddresses).where(assigned))
|
||||||
.tx(req)
|
|
||||||
.run(SELECT.from(externalAddresses).where(assigned))
|
|
||||||
if (address) return local.create(Addresses).entries(address)
|
if (address) return local.create(Addresses).entries(address)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -68,13 +66,8 @@ module.exports = cds.service.impl(async () => {
|
|||||||
const { Items } = req.data
|
const { Items } = req.data
|
||||||
|
|
||||||
// validate input...
|
// validate input...
|
||||||
if (!Items || Items.length === 0)
|
if (!Items || Items.length === 0) return req.reject('Please order at least one item.')
|
||||||
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 (!req.data.shippingAddress_ID)
|
|
||||||
return req.reject(
|
|
||||||
'Please enter a valid shipping address.',
|
|
||||||
'shippingAddress_ID'
|
|
||||||
)
|
|
||||||
|
|
||||||
// reduce stock on ordered books...
|
// reduce stock on ordered books...
|
||||||
const all = await db.tx(req).run(
|
const all = await db.tx(req).run(
|
||||||
@@ -85,14 +78,7 @@ module.exports = cds.service.impl(async () => {
|
|||||||
.set('stock -=', each.amount)
|
.set('stock -=', each.amount)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
all.forEach(
|
all.forEach((affectedRows, i) => affectedRows > 0 || req.error(409, `${Items[i].amount} exceeds stock for book #${Items[i].book_ID}`))
|
||||||
(affectedRows, i) =>
|
|
||||||
affectedRows > 0 ||
|
|
||||||
req.error(
|
|
||||||
409,
|
|
||||||
`${Items[i].amount} exceeds stock for book #${Items[i].book_ID}`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
require('./utils')
|
require('./utils')
|
||||||
|
|||||||
@@ -3,11 +3,7 @@ const { Books } = cds.entities
|
|||||||
|
|
||||||
/** Service implementation for CatalogService */
|
/** Service implementation for CatalogService */
|
||||||
module.exports = cds.service.impl(function () {
|
module.exports = cds.service.impl(function () {
|
||||||
this.after(
|
this.after('READ', 'Books', each => each.stock > 111 && _addDiscount2(each, 11))
|
||||||
'READ',
|
|
||||||
'Books',
|
|
||||||
each => each.stock > 111 && _addDiscount2(each, 11)
|
|
||||||
)
|
|
||||||
this.before('CREATE', 'Orders', _reduceStock)
|
this.before('CREATE', 'Orders', _reduceStock)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -28,12 +24,6 @@ async function _reduceStock (req) {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
all.forEach((affectedRows, i) => {
|
all.forEach((affectedRows, i) => {
|
||||||
if (affectedRows === 0)
|
if (affectedRows === 0) req.error(409, `${OrderItems[i].amount} exceeds stock for book #${OrderItems[i].book_ID}`)
|
||||||
req.error(
|
|
||||||
409,
|
|
||||||
`${OrderItems[i].amount} exceeds stock for book #${
|
|
||||||
OrderItems[i].book_ID
|
|
||||||
}`
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,33 +4,18 @@ process.env['https_proxy'] = ''
|
|||||||
process.env['HTTP_PROXY'] = ''
|
process.env['HTTP_PROXY'] = ''
|
||||||
process.env['HTTPS_PROXY'] = ''
|
process.env['HTTPS_PROXY'] = ''
|
||||||
|
|
||||||
const diff = (obj1, obj2) =>
|
const diff = (obj1, obj2) => Object.keys(obj1).reduce((res, curr) => (obj1[curr] === obj2[curr] ? res : (res[curr] = obj2[curr]) && res), {})
|
||||||
Object.keys(obj1).reduce(
|
|
||||||
(res, curr) =>
|
|
||||||
obj1[curr] === obj2[curr] ? res : (res[curr] = obj2[curr]) && res,
|
|
||||||
{}
|
|
||||||
)
|
|
||||||
|
|
||||||
const queriesToUpdateDifferences = (entity, ownEntries, otherEntries) =>
|
const queriesToUpdateDifferences = (entity, ownEntries, otherEntries) =>
|
||||||
ownEntries
|
ownEntries
|
||||||
.map(ownEntry => {
|
.map(ownEntry => {
|
||||||
const otherEntry = otherEntries.find(otherEntry =>
|
const otherEntry = otherEntries.find(otherEntry => Object.keys(entity.keys).reduce((res, curr) => res && otherEntry[curr] === ownEntry[curr], true))
|
||||||
Object.keys(entity.keys).reduce(
|
|
||||||
(res, curr) => res && otherEntry[curr] === ownEntry[curr],
|
|
||||||
true
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if (otherEntry) {
|
if (otherEntry) {
|
||||||
const differences = diff(ownEntry, otherEntry)
|
const differences = diff(ownEntry, otherEntry)
|
||||||
if (Object.keys(differences).length) {
|
if (Object.keys(differences).length) {
|
||||||
return UPDATE(entity)
|
return UPDATE(entity)
|
||||||
.set(differences)
|
.set(differences)
|
||||||
.where(
|
.where(Object.keys(entity.keys).reduce((res, curr) => (res[curr] = ownEntry[curr]) && res, {}))
|
||||||
Object.keys(entity.keys).reduce(
|
|
||||||
(res, curr) => (res[curr] = ownEntry[curr]) && res,
|
|
||||||
{}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user