add getUser method

This commit is contained in:
Dzmitry_Tamashevich@epam.com
2020-10-19 23:59:47 +03:00
committed by Daniel Hutzel
parent 0690762207
commit 30d5c789bc
6 changed files with 65 additions and 37 deletions

View File

@@ -1,5 +1,7 @@
const cds = require("@sap/cds");
const USER_LEVELS = { customer: 1, employee: 2 };
module.exports = async function () {
const db = await cds.connect.to("db"); // connect to database service
const { Employees, Customers } = db.entities;
@@ -15,6 +17,25 @@ module.exports = async function () {
);
});
this.on("getUser", async (req) => {
return await db.run(
SELECT.one(Customers)
.columns(
"lastName",
"firstName",
"city",
"state",
"address",
"country",
"postalCode",
"phone",
"fax",
"email"
)
.where({ email: req.user.id })
);
});
this.on("mockLogin", async (req) => {
const { email, password } = req.data;
@@ -30,7 +51,7 @@ module.exports = async function () {
return {
mockedToken: Buffer.from(`${email}:${password}`).toString("base64"),
level: role === "customer" ? 1 : 2,
level: USER_LEVELS[role],
email: userFromDb.email,
ID: userFromDb.ID,
roles: [role],