Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Fix hapi issues and auth issues #636

Merged
merged 1 commit into from
Aug 31, 2020
Merged
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
26 changes: 25 additions & 1 deletion app-bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
/**
* App bootstrap
* add logger and joi to services
*/

const fs = require('fs')
const path = require('path')
const logger = require('./src/common/logger')

global.Promise = require('bluebird')
const Joi = require('joi')

Joi.id = () => Joi.string().uuid().required()

/**
* add logger and joi schema to service
* @param dir
*/
function buildServices (dir) {
const files = fs.readdirSync(dir)
files.forEach((file) => {
const curPath = path.join(dir, file)
const stats = fs.statSync(curPath)
if (stats.isDirectory()) {
buildServices(curPath)
} else if (path.extname(file) === '.js') {
logger.buildService(require(curPath)); // eslint-disable-line
}
})
}

buildServices(path.join(__dirname, 'src', 'services'))
28 changes: 0 additions & 28 deletions app-constants.js

This file was deleted.

1 change: 0 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

require('./app-bootstrap')

const path = require('path')
const config = require('config')
const express = require('express')
const interceptor = require('express-interceptor')
Expand Down
71 changes: 37 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"url": ""
},
"dependencies": {
"@hapi/joi": "^16.1.8",
"aws-sdk": "^2.627.0",
"axios": "^0.19.2",
"bluebird": "^3.5.1",
Expand All @@ -33,6 +32,7 @@
"file-type": "^14.6.2",
"get-parameter-names": "^0.3.0",
"http-status-codes": "^1.3.0",
"joi": "^17.2.1",
"js-yaml": "^3.14.0",
"lodash": "^4.17.19",
"multer": "^1.4.2",
Expand Down
29 changes: 0 additions & 29 deletions src/bootstrap.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const _ = require('lodash')
const config = require('config')
const AWS = require('aws-sdk')
const path = require('path')
const axios = require('axios')
const querystring = require('querystring')
const NodeCache = require('node-cache')
const models = require('../models')
const errors = require('./errors')
const logger = require('./logger')
Expand Down
2 changes: 1 addition & 1 deletion src/common/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ logger.decorateWithValidators = function (service) {
service[name] = async function () {
const args = Array.prototype.slice.call(arguments)
const value = _combineObject(params, args)
const normalized = Joi.attempt(value, method.schema)
const normalized = Joi.attempt(value, Joi.object(method.schema))

const newArgs = []
// Joi will normalize values
Expand Down
16 changes: 14 additions & 2 deletions src/consts.js → src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ const AllAuthenticatedUsers = [
*/
const AdminUser = [UserRoles.admin, UserRoles.administrator]

const Scopes = {
CreateUpload: 'create:upload',
GetUpload: 'read:upload',
UpdateUpload: 'update:upload',
AllUpload: 'all:upload',
CreateTemplate: 'create:template',
GetTemplate: 'read:template',
AllTemplate: 'all:template',
GetSkill: 'read:skill',
AllSkill: 'all:skill'
}

module.exports = {
UserRoles,
AllAuthenticatedUsers,
AdminUser
AdminUser,
Scopes
}
2 changes: 1 addition & 1 deletion src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const multer = require('multer')
const config = require('config')
const _ = require('lodash')
const constants = require('../app-constants')
const constants = require('./constants')
const fileUpload = multer({ storage: multer.memoryStorage() })

// config template upload properties
Expand Down
8 changes: 4 additions & 4 deletions src/services/UploadService.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const logger = require('../common/logger')
* Checks the type of uploaded file and ensures it's allowed.
* @param {Object} upload The uploaded file
*/
async function ensureFileTypeIsValid(upload) {
async function ensureFileTypeIsValid (upload) {
const allowedExtensions = ['xls', 'xlsx', 'csv']
const allowedMimeTypes = [
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'text/csv',
'text/csv'
]
const fileType = await FileType.fromBuffer(upload.buffer)
const fileExt = upload.originalname.split('.').pop().toLowerCase()
Expand All @@ -30,7 +30,7 @@ async function ensureFileTypeIsValid(upload) {
if (isAllowed === false) {
throw new errors.ForbiddenError(`You are allowed to upload only ${_.join(allowedExtensions, ',')} types.`)
}
}
}

/**
* Get upload entity by id.
Expand Down Expand Up @@ -117,7 +117,7 @@ partiallyUpdate.schema = {
id: Joi.id(),
authUser: Joi.object().required(),
data: Joi.object().keys({
status: Joi.string().valid(['pending', 'completed', 'failed']).required(),
status: Joi.string().valid('pending', 'completed', 'failed').required(),
info: Joi.string()
}).required()
}
Expand Down