diff --git a/.eslintrc b/.eslintrc index 5d21a014..16fb3b5c 100644 --- a/.eslintrc +++ b/.eslintrc @@ -10,6 +10,9 @@ "jest": true, "mocha": true }, + "parserOptions": { + "sourceType": "module" + }, "globals": { "SELECT": true, "INSERT": true, diff --git a/bookshop/db/init.js b/bookshop/db/init.js index 1dec13c3..fb7c3f9f 100644 --- a/bookshop/db/init.js +++ b/bookshop/db/init.js @@ -6,7 +6,7 @@ // NOTE: We use cds.on('served') to delay the UPSERTs after the db init // to run after all INSERTs from .csv files happened. -module.exports = cds.on('served', ()=> cds.run( +cds.on('served', ()=> cds.run( UPSERT.into ('sap.common.Currencies') .columns ( [ 'code', 'symbol', 'name' ] ) .rows ( diff --git a/bookshop/index.js b/bookshop/index.js index 7bffbe36..6c6f39b9 100644 --- a/bookshop/index.js +++ b/bookshop/index.js @@ -1,2 +1 @@ -const { CatalogService } = require('./srv/cat-service') -module.exports = { CatalogService } +export { CatalogService } from './srv/cat-service' diff --git a/bookshop/jsconfig.json b/bookshop/jsconfig.json new file mode 100644 index 00000000..b7374dc7 --- /dev/null +++ b/bookshop/jsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "module": "NodeNext", + "moduleResolution": "nodenext", + "paths": { + "#cds/*": [ + "./@cds/*" + ] + } + } +} diff --git a/bookshop/package.json b/bookshop/package.json index 6c24d667..632f3ecc 100644 --- a/bookshop/package.json +++ b/bookshop/package.json @@ -2,6 +2,7 @@ "name": "@capire/bookshop", "version": "1.0.0", "description": "A simple self-contained bookshop service.", + "type": "module", "files": [ "app", "srv", @@ -10,7 +11,8 @@ "index.js" ], "devDependencies": { - "@cap-js/sqlite": "*" + "@cap-js/sqlite": "*", + "@cap-js/cds-typer": ">=0.1" }, "dependencies": { "@sap/cds": "^7", @@ -21,5 +23,8 @@ "genres": "cds serve test/genres.cds", "start": "cds-serve", "watch": "cds watch" + }, + "imports": { + "#cds/*": "./@cds/*/index.js" } } diff --git a/bookshop/srv/admin-service.js b/bookshop/srv/admin-service.js index d737a65a..d6059488 100644 --- a/bookshop/srv/admin-service.js +++ b/bookshop/srv/admin-service.js @@ -1,6 +1,6 @@ -const cds = require('@sap/cds/lib') +import cds from '@sap/cds' -module.exports = class AdminService extends cds.ApplicationService { init(){ +export class AdminService extends cds.ApplicationService { init(){ this.before ('NEW','Authors', genid) this.before ('NEW','Books', genid) return super.init() diff --git a/bookshop/srv/cat-service.js b/bookshop/srv/cat-service.js index 3c557078..2be1cec4 100644 --- a/bookshop/srv/cat-service.js +++ b/bookshop/srv/cat-service.js @@ -1,29 +1,36 @@ -const cds = require('@sap/cds') +import cds from '@sap/cds' -class CatalogService extends cds.ApplicationService { init(){ +export class CatalogService extends cds.ApplicationService { init() { - const { Books } = cds.entities ('sap.capire.bookshop') + const { Books } = cds.entities('sap.capire.bookshop') const { ListOfBooks } = this.entities - // Reduce stock of ordered books if available stock suffices - this.on ('submitOrder', async req => { - const {book,quantity} = req.data - if (quantity < 1) return req.reject (400,`quantity has to be 1 or more`) - let b = await SELECT `stock` .from (Books,book) - if (!b) return req.error (404,`Book #${book} doesn't exist`) - let {stock} = b - if (quantity > stock) return req.reject (409,`${quantity} exceeds stock for book #${book}`) - await UPDATE (Books,book) .with ({ stock: stock -= quantity }) - await this.emit ('OrderedBook', { book, quantity, buyer:req.user.id }) - return { stock } - }) - // Add some discount for overstocked books - this.after ('READ', ListOfBooks, each => { + this.after('READ', ListOfBooks, each => { if (each.stock > 111) each.title += ` -- 11% discount!` }) + // Reduce stock of ordered books if available stock suffices + this.on('submitOrder', async req => { + let { book:id, quantity } = req.data + let book = await SELECT.from (Books, id, b => b.stock) + + // Validate input data + if (!book) return req.error (404, `Book #${id} doesn't exist`) + if (quantity < 1) return req.error (400, `quantity has to be 1 or more`) + if (quantity > book.stock) return req.error (409, `${quantity} exceeds stock for book #${id}`) + + // Reduce stock in database and return updated stock value + await UPDATE (Books, id) .with ({ stock: book.stock -= quantity }) + return book + }) + + // Emit event when an order has been submitted + this.after('submitOrder', async (_,req) => { + let { book, quantity } = req.data + await this.emit('OrderedBook', { book, quantity, buyer: req.user.id }) + }) + + // Delegate requests to the underlying generic service return super.init() }} - -module.exports = { CatalogService } diff --git a/bookshop/srv/user-service.js b/bookshop/srv/user-service.js index 2ecb68d9..446dbf23 100644 --- a/bookshop/srv/user-service.js +++ b/bookshop/srv/user-service.js @@ -1,5 +1,5 @@ -const cds = require('@sap/cds') -module.exports = class UserService extends cds.Service { init(){ +import cds from '@sap/cds' +export class UserService extends cds.Service { init(){ this.on('READ', 'me', ({ tenant, user, locale }) => ({ id: user.id, locale, tenant })) this.on('login', (req) => { if (req.user._is_anonymous)