Skip to content

Utilities #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _helpers/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mongoose.Promise = global.Promise;
module.exports = {
Account: require('accounts/account.model'),
RefreshToken: require('accounts/refresh-token.model'),
Utilities: require('utilities/utilities.model'),
isValidId
};

Expand Down
12 changes: 12 additions & 0 deletions accounts/account.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const crypto = require("crypto");
const sendEmail = require('_helpers/send-email');
const db = require('_helpers/db');
const Role = require('_helpers/role');
const utilitiesService = require('utilities/utilities.service');

module.exports = {
authenticate,
Expand Down Expand Up @@ -77,6 +78,17 @@ async function revokeToken({ token, ipAddress }) {
}

async function register(params, origin) {
const utility = await db.Utilities.findOne({name: 'Registration'});

if(!utility) {
utilitiesService.create({
name: 'Registration',
status: true
});
} else if (utility && !utility.isActive) {
throw 'We are not able to process your request. Please try again later.';
}

// validate
if (await db.Account.findOne({ email: params.email })) {
// send already registered error in email to prevent account enumeration
Expand Down
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ app.use(cors({ origin: (origin, callback) => callback(null, true), credentials:

// api routes
app.use('/accounts', require('./accounts/accounts.controller'));
app.use('/utilities', require('./utilities/utilities.contorller'));

// swagger docs route
app.use('/api-docs', require('_helpers/swagger'));
Expand Down
263 changes: 263 additions & 0 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,269 @@ paths:
"401":
$ref: "#/components/responses/UnauthorizedError"

/utilities/enable/{id}:
parameters:
- in: path
name: id
description: Utility id
required: true
example: "5f47bf56f9893d3eb89e698a"
schema:
type: string
post:
summary: Enable a utility
description: Restricted to admin users.
operationId: enableUtility
security:
- bearerAuth: []
responses:
"200":
description: Utility enabled
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "Utility enabled"

"404":
description: Utility not found
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "Utility not found"
/utilities/disable/{id}:
parameters:
- in: path
name: id
description: Utility id
required: true
example: "5f47bf56f9893d3eb89e698a"
schema:
type: string
post:
summary: Disable a utility
description: Restricted to admin users.
operationId: disableUtility
security:
- bearerAuth: []
responses:
"200":
description: Utility disabled
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "Utility disabled"

"404":
description: Utility not found
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "Utility not found"
/utilities:
get:
summary: Get a list of all utilities
description: Restricted to admin users.
operationId: getAllUtilities
security:
- bearerAuth: []
responses:
"200":
description: An array of all utilities
content:
application/json:
schema:
type: array
items:
type: object
properties:
id:
type: string
example: "5f47bf56f9893d3eb89e698a"
name:
type: string
example: "Example"
status:
type: boolean
example: true
modified:
type: string
example: "2020-08-27T14:12:38.094Z"
"401":
$ref: "#/components/responses/UnauthorizedError"
post:
summary: Create a new utility
description: Restricted to admin users.
operationId: createUtility
security:
- bearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
name:
type: string
example: "Example"
status:
type: boolean
example: true
required:
- name
- status
responses:
"200":
description: Utility created successfully. The details of the new utility is returned.
content:
application/json:
schema:
type: object
properties:
id:
type: string
example: "5f47bf56f9893d3eb89e698a"
name:
type: string
example: "Example"
status:
type: boolean
example: true
modified:
type: string
example: "2020-08-27T14:12:38.094Z"
"400":
description: Utility is already exists
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "Utility 'Example' is already exists"
"401":
$ref: "#/components/responses/UnauthorizedError"
/utilities/{id}:
parameters:
- in: path
name: id
description: Utility id
required: true
example: "5f47bf56f9893d3eb89e698a"
schema:
type: string
get:
summary: Get a single utility by id
description: Restricted to admin users.
operationId: getUtilityById
security:
- bearerAuth: []
responses:
"200":
description: Details of the specified utility
content:
application/json:
schema:
type: object
properties:
id:
type: string
example: "5f47bf56f9893d3eb89e698a"
name:
type: string
example: "Example"
status:
type: boolean
example: true
modified:
type: string
example: "2020-08-27T14:12:38.094Z"
"404":
$ref: "#/components/responses/NotFoundError"
"401":
$ref: "#/components/responses/UnauthorizedError"
put:
summary: Update a utility
description: Restricted to admin users.
operationId: updateUtility
security:
- bearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
name:
type: string
example: "Example"
status:
type: boolean
example: true
responses:
"200":
description: Utility updated successfully. The details of the updated utility is returned.
content:
application/json:
schema:
type: object
properties:
id:
type: string
example: "5f47bf56f9893d3eb89e698a"
name:
type: string
example: "Example"
status:
type: boolean
example: true
modified:
type: string
example: "2020-08-27T14:12:38.094Z"
"404":
$ref: "#/components/responses/NotFoundError"
"401":
$ref: "#/components/responses/UnauthorizedError"
delete:
summary: Delete a utility
description: Restricted to admin users.
operationId: deleteUtility
security:
- bearerAuth: []
responses:
"200":
description: Utility deleted successfully
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "Utility deleted successfully"
"404":
$ref: "#/components/responses/NotFoundError"
"401":
$ref: "#/components/responses/UnauthorizedError"

components:
securitySchemes:
bearerAuth:
Expand Down
78 changes: 78 additions & 0 deletions utilities/utilities.contorller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const express = require('express');
const router = express.Router();
const Joi = require('joi');
const authorize = require('_middleware/authorize')
const Role = require('_helpers/role');
const utilitiesService = require('./utilities.service');
const validateRequest = require('_middleware/validate-request');

module.exports = router;

// routes
router.post('/enable/:id', authorize(Role.Admin), enable);
router.post('/disable/:id', authorize(Role.Admin), disable);
router.get('/', authorize(Role.Admin), getAll);
router.get('/:id', authorize(Role.Admin), getById);
router.post('/', authorize(Role.Admin), createSchema, create);
router.put('/:id', authorize(Role.Admin), updateSchema, update);
router.delete('/:id', authorize(Role.Admin), _delete);

function enable(req, res, next) {
utilitiesService.enable(req.params.id)
.then(() => res.json({ message: 'Utility enabled' }))
.catch(next);
}

function disable(req, res, next) {
utilitiesService.disable(req.params.id)
.then(() => res.json({ message: 'Utility disabled' }))
.catch(next);
}

function getAll(req, res, next) {
utilitiesService.getAll()
.then(utilities => res.json(utilities))
.catch(next);
}

function getById(req, res, next) {
utilitiesService.getById(req.params.id)
.then(utility => utility ? res.json(utility) : res.sendStatus(404))
.catch(next);
}

function createSchema(req, res, next) {
const schema = Joi.object({
name: Joi.string().required(),
status: Joi.boolean().required()
});
validateRequest(req, next, schema);
}

function create(req, res, next) {
utilitiesService.create(req.body)
.then(utility => res.json(utility))
.catch(next);
}

function updateSchema(req, res, next) {
const schemaRules = {
name: Joi.string().empty(''),
status: Joi.boolean().empty('')
};

const schema = Joi.object(schemaRules);
validateRequest(req, next, schema);
}

function update(req, res, next) {
utilitiesService.update(req.params.id, req.body)
.then(utility => res.json(utility))
.catch(next);
}

function _delete(req, res, next) {
utilitiesService.delete(req.params.id)
.then(() => res.json({ message: 'Utility deleted successfully' }))
.catch(next);
}
Loading