From 72444e9fc94b124c581cd7b89e4f6c46107f77df Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 20 Jun 2023 21:47:32 +0100 Subject: [PATCH] v2 user routes complete Signed-off-by: Fred Boniface --- src/configs/errorCodes.configs.js | 3 ++ src/controllers/registration.controllers.js | 7 ++- src/routes/user2.routes.js | 1 + src/services/registration.services.js | 57 +++++++++++++-------- 4 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/configs/errorCodes.configs.js b/src/configs/errorCodes.configs.js index 38c39bd..49fe495 100644 --- a/src/configs/errorCodes.configs.js +++ b/src/configs/errorCodes.configs.js @@ -1,4 +1,5 @@ const statusCodes = { + 400: 'data not found', 700: 'no authentication attempt', 701: 'invalid credentials', 702: 'domain not whitelisted', @@ -6,7 +7,9 @@ const statusCodes = { 800: 'location code not found', 801: 'unable to fetch location data', 900: 'invalid request format', + 901: 'email not provided', 950: 'upstream server error', + 951: 'unknown server error' }; module.exports = statusCodes; \ No newline at end of file diff --git a/src/controllers/registration.controllers.js b/src/controllers/registration.controllers.js index 706755f..37b4d20 100644 --- a/src/controllers/registration.controllers.js +++ b/src/controllers/registration.controllers.js @@ -23,7 +23,12 @@ async function request(req, res, next) { async function getUser(req, res, next) { try { let uuid = req.params.uuid; - res.json(await reg.getUser(uuid)); + let data = await reg.getUser(uuid); + if (data.status) { + res.status(data.status).json(data); + } else { + res.json(data); + } } catch (err) { console.error(err); next(err); diff --git a/src/routes/user2.routes.js b/src/routes/user2.routes.js index c455f69..0b29e00 100644 --- a/src/routes/user2.routes.js +++ b/src/routes/user2.routes.js @@ -4,6 +4,7 @@ const regCtr = require('../controllers/registration.controllers'); // User router.get('/:uuid', regCtr.getUser); +// Not Implemented router.delete('/:uuid', regCtr.deleteUser); router.post('/request', regCtr.request); router.post('/register', regCtr.register); diff --git a/src/services/registration.services.js b/src/services/registration.services.js index 24837f4..8e4666d 100644 --- a/src/services/registration.services.js +++ b/src/services/registration.services.js @@ -8,25 +8,29 @@ const errors = require('../configs/errorCodes.configs'); async function createRegKey(body) { log.out('registerServices.createRegKey: Incoming request', 'INFO'); - const domain = await clean.getDomainFromEmail(body.email); - log.out(`registerServices: Registration request from: ${domain}`, 'info'); - if (domains.includes(domain)) { - log.out(`registerServices.createRegKey: Key from valid domain: ${domain}`); - const uuid = await auth.generateKey(); - db.addRegReq(uuid, domain); - const message = await auth.generateConfirmationEmail(body.email, uuid); - if (!message) { - const err = new Error('Message generation error'); - log.out('registerServices.createRegKey: Error generating email', 'err'); - log.out(err, 'err'); - return 500; + if (body.email) { + const domain = await clean.getDomainFromEmail(body.email); + log.out(`registerServices: Registration request from: ${domain}`, 'info'); + if (domains.includes(domain)) { + log.out(`registerServices.createRegKey: Key from valid: ${domain}`,'info'); + const uuid = await auth.generateKey(); + db.addRegReq(uuid, domain); + const message = await auth.generateConfirmationEmail(body.email, uuid); + if (!message) { + const err = new Error('Message generation error'); + log.out('registerServices.createRegKey: Error generating email', 'err'); + log.out(err, 'err'); + return 500; + } + if (await mail.send(message) == true) { + return {status: 201, message: 'email sent'}; + } + return {status: 500, errorCode: 950, errorMsg: errors[950]}; } - if (await mail.send(message) == true) { - return {status: 201, message: 'email sent'}; - } - return {status: 500, errorCode: 950, errorMsg: errors[950]}; + return {status: 403, errorCode: 702, errorMsg: errors[702]}; + } else { + return {status: 400, errorCode: 901, errorMsg: errors[902]}; } - return {status: 403, errorCode: 702, errorMsg: errors[702]}; } async function regUser(req) { // Add input validation @@ -47,11 +51,20 @@ async function regUser(req) { // Add input validation async function getUser(uuid) { log.out('registrationServices: Finding user for given UUID', 'dbug'); - const filter = { - uuid: uuid - }; - const res = await db.query('users', filter, false); - return res; + try { + const filter = { + uuid: uuid + }; + const res = await db.query('users', filter, false); + if (res.length) { + return res; + } else { + return {status: 404, errorCode:400, errorMsg: errors[400]}; + } + } catch (err) { + console.log(err); + return {status: 500, errorCode: 951, errorMsg: errors[951]}; + } } module.exports = {