Skip to content

Commit

Permalink
Merge pull request #5 from ccem-dev/release-1.1.0
Browse files Browse the repository at this point in the history
[RELEASE] release-1.1.0 into master
  • Loading branch information
drferreira authored Jul 23, 2020
2 parents a93d645 + 7d90703 commit 472f049
Show file tree
Hide file tree
Showing 25 changed files with 635 additions and 149 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*

source/.npmrc
# Runtime data
pids
*.pid
Expand Down
18 changes: 3 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
FROM mongo:4.0.4 AS database
ARG INITDB_FILE=server/database/initdb.sh
ENV MONGO_INITDB_ROOT_USERNAME="admin"
ENV MONGO_INITDB_ROOT_PASSWORD="admin"
ENV MONGO_INITDB_DATABASE="admin"
ENV DATABASE_USER="user"
ENV DATABASE_PASSWORD="user"
USER root
COPY server/database/initdb /initdb
COPY ${INITDB_FILE} /docker-entrypoint-initdb.d/
EXPOSE 27017

FROM node:10.16.0-alpine AS api
COPY source/. src/
WORKDIR /src
ENV MEMORY 6144
ENV DATABASE_USER="user"
ENV DATABASE_PASSWORD="user"
ENV DATABASE_HOSTNAME="db-distribution-database"
ENV DATABASE_USER="distribution"
ENV DATABASE_PASSWORD="distribution"
ENV DATABASE_HOSTNAME="otus-database"
ENV DATABASE_PORT="27017"
EXPOSE 8080
RUN apk update && apk add --no-cache mongodb-tools
Expand Down
6 changes: 0 additions & 6 deletions server/database/initdb.sh

This file was deleted.

3 changes: 0 additions & 3 deletions server/database/initdb/db-roles.js

This file was deleted.

12 changes: 0 additions & 12 deletions server/database/initdb/db-user.js

This file was deleted.

1 change: 1 addition & 0 deletions source/app/models/StaticDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module.exports.correctCurrentVariables = () => {
});
});
};

module.exports.getVariables = async (identification, variables) => {
let getCurrentVariables = this.getCurrentVariables;
let getOldVariables = this.getOldVariables;
Expand Down
2 changes: 1 addition & 1 deletion source/app/models/VariableTypeCorrelation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ const mongoose = require('mongoose');
module.exports.updateVariableTypeCorrelation = (variable) => {
let db = mongoose.connection.db;
let variableTypeCorrelationCollection = db.collection('variableTypeCorrelation');
return variableTypeCorrelationCollection.updateOne({name:variable.name},{$setOnInsert:{name:variable.name,type:variable.type}},{upsert:true});
return variableTypeCorrelationCollection.updateOne({ name: variable.name }, { $setOnInsert: { name: variable.name, type: variable.type } }, { upsert: true });
};
36 changes: 18 additions & 18 deletions source/app/routes/StaticDatabaseRest.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
const multer = require('multer');
const multer = require('multer');
const { body, buildSanitizeFunction, validationResult } = require('express-validator');
const sanitizeBody = buildSanitizeFunction(['body']);
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
const upload = multer({ dest: 'uploads/' });

module.exports = function(application) {
module.exports = function (application) {
const StaticDatabaseController = application.app.controllers.StaticDatabaseController;
const { validateFileContainer } = application.app.utils.Utils;

let uploadedDatabaseFile = upload.fields([{ name: 'databaseJson' }]);
application.post('/api/upload/database', uploadedDatabaseFile ,async function(req, res) {
application.post('/api/upload/database', uploadedDatabaseFile, async function (req, res) {
res.setTimeout(0);
res.header('Content-Type', 'application/json');
try {
let databaseJson = await validateFileContainer(req.files.databaseJson, ".json");
let result = await StaticDatabaseController.uploadDatabase(databaseJson);
res.status(result.code).send(result.body);
} catch (e){
} catch (e) {
res.status(e.code).send(e.body);
}
});

let uploadedCorrelationFile = upload.fields([{ name: 'variableTypeCorrelationJson' }]);
application.post('/api/upload/variable-type-correlation', uploadedCorrelationFile ,async function(req, res) {
application.post('/api/upload/variable-type-correlation', uploadedCorrelationFile, async function (req, res) {
res.setTimeout(0);
res.header('Content-Type', 'application/json');
try {
let variableTypeCorrelationJson = await validateFileContainer(req.files.variableTypeCorrelationJson, ".json");
let result = await StaticDatabaseController.uploadVariableTypeCorrelation(variableTypeCorrelationJson);
res.status(result.code).send(result.body);
} catch (e){
} catch (e) {
res.status(e.code).send(e.body);
}
});

application.post('/api/findVariables', jsonParser ,[
application.post('/api/findVariables', jsonParser, [
body('variables').custom(value => {
let error = false;
value.forEach(variable=>{
if(!variable.name || !variable.sending){
value.forEach(variable => {
if (!variable.name || !variable.sending) {
error = true;
}
});
if (error){
if (error) {
return Promise.reject();
} else {
return Promise.resolve();
Expand All @@ -52,8 +52,8 @@ module.exports = function(application) {
sanitizeBody('variables').customSanitizer((value, { req }) => {
let correctedArray = [];
let resultErrors = validationResult(req);
if(resultErrors.errors.length === 0){
value.forEach(variable=>{
if (resultErrors.errors.length === 0) {
value.forEach(variable => {
correctedArray.push({
name: variable.name.toString(),
sending: variable.sending.toString(),
Expand All @@ -63,21 +63,21 @@ module.exports = function(application) {
return correctedArray;
}),
sanitizeBody('identification').customSanitizer((value, { req }) => {
if(value) {
if (value) {
return value.toString();
}
}),
body('identification').isString()
], async function(req, res) {
], async function (req, res) {
res.header('Content-Type', 'application/json');
let validation = validationResult(req);
if (validation.errors.length > 0){
res.status(406).send({data:"There are invalid fields in the request"});
if (validation.errors.length > 0) {
res.status(406).send({ data: "There are invalid fields in the request" });
} else {
try {
let result = await StaticDatabaseController.getVariables(req.body.identification,req.body.variables);
let result = await StaticDatabaseController.getVariables(req.body.identification, req.body.variables);
res.status(result.code).send(result.body);
} catch (e){
} catch (e) {
res.status(e.code).send(e.body);
}
}
Expand Down
11 changes: 5 additions & 6 deletions source/app/services/StaticDatabaseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,21 @@ module.exports = function (application) {
});
});

return endImport.then(()=>{
return endImport.then(() => {
return Response.success();
}).catch((e)=>{
}).catch((e) => {
console.log(e);
throw Response.internalServerError();
});
},
getVariables:async function (identification, variables) {
getVariables: async function (identification, variables) {
try {
let foundVariables = await StaticDatabase.getVariables(identification, variables);
return Response.success({variables:foundVariables} )
return Response.success({variables: foundVariables})
} catch (e) {
console.log(e);
throw Response.internalServerError();
}
}
};
};

};
40 changes: 20 additions & 20 deletions source/app/services/VariableTypeCorrelationService.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
const fs = require('fs');
module.exports = function (application) {
const Response = application.app.utils.Response;
const VariableTypeCorrelation = application.app.models.VariableTypeCorrelation;
const Response = application.app.utils.Response;
const VariableTypeCorrelation = application.app.models.VariableTypeCorrelation;

return {
uploadVariableTypeCorrelation: async function (variableTypeCorrelationJson) {
let promises = [];
let buffer = fs.readFileSync(variableTypeCorrelationJson.path);
let parsedJson = JSON.parse(buffer);
return {
uploadVariableTypeCorrelation: async function (variableTypeCorrelationJson) {
let promises = [];
let buffer = fs.readFileSync(variableTypeCorrelationJson.path);
let parsedJson = JSON.parse(buffer);

let correlationLength = parsedJson.length;
for (let i = 0; i < correlationLength; i++) {
promises.push(VariableTypeCorrelation.updateVariableTypeCorrelation(parsedJson[i]))
}
let correlationLength = parsedJson.length;
for (let i = 0; i < correlationLength; i++) {
promises.push(VariableTypeCorrelation.updateVariableTypeCorrelation(parsedJson[i]))
}

try {
fs.unlinkSync(variableTypeCorrelationJson.path);
await Promise.all(promises);
return Response.success();
} catch (e) {
throw Response.internalServerError();
}
}
};
try {
fs.unlinkSync(variableTypeCorrelationJson.path);
await Promise.all(promises);
return Response.success();
} catch (e) {
throw Response.internalServerError();
}
}
};
};

8 changes: 4 additions & 4 deletions source/app/utils/Response.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
module.exports.success = function (body) {
let response = {};
response.code = 200;
response.body = {data: body || true};
response.body = { data: body || true };
return response;
};

module.exports.notAcceptable = function (body) {
let err = {};
err.code = 406;
err.body = {data: body || {message: 'Value not acceptable'}};
err.body = { data: body || { message: 'Value not acceptable' } };
return err;
};

module.exports.internalServerError = function (body) {
let err = {};
err.code = 500;
err.body = {data: body || {message: 'There was an error. Please try again later.'}};
err.body = { data: body || { message: 'There was an error. Please try again later.' } };
return err;
};

module.exports.notFound = function (body) {
let err = {};
err.code = 404;
err.body = {data: body || {message: 'Data not found'}};
err.body = { data: body || { message: 'Data not found' } };
return err;
};

28 changes: 15 additions & 13 deletions source/app/utils/Utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const path = require('path');
const Response = require('../../app/utils/Response.js');

module.exports.validateFileContainer = (fileContainer, ext) => {
return new Promise(function (resolve, reject) {
let errorMessage;
if(!fileContainer){
errorMessage = 'The databaseJson field is required';
} else if(path.extname(fileContainer[0].originalname) !== ext){
errorMessage = 'Invalid File Type. Only JSON files are allowed';
}
if (errorMessage){
reject(Response.notAcceptable({data:'The databaseJson field is required'}));
} else {
resolve(fileContainer[0])
}
});
return new Promise(function (resolve, reject) {
let errorMessage;
if (!fileContainer) {
errorMessage = 'The databaseJson field is required';
} else if (path.extname(fileContainer[0].originalname) !== ext) {
errorMessage = 'Invalid File Type. Only JSON files are allowed';
}
if (errorMessage) {
reject(Response.notAcceptable({data: 'The databaseJson field is required'}));
} else {
resolve(fileContainer[0])
}
});
}
18 changes: 18 additions & 0 deletions source/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var gulp = require('gulp');
var browserSync = require('browser-sync').create();


gulp.task('browser-sync', function() {
browserSync.init({
proxy: "localhost:8080",
reloadDelay: 400,
files: [
"app/views/*/*.ejs",
"app/routes/*.js",
"app/models/*.js",
"app/controllers/*.js",
"config/*.js"
],
open: "external"
});
});
12 changes: 12 additions & 0 deletions source/jest-mongodb-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
mongodbMemoryServerOptions: {
instance: {
dbName: 'jest'
},
binary: {
version: '4.0.2', // Version of MongoDB
skipMD5: true
},
autoStart: false
}
};
21 changes: 21 additions & 0 deletions source/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
preset: '@shelf/jest-mongodb',
collectCoverage: true,
collectCoverageFrom: [
"**/app/**/*.js",
'**/tests/**/*-spec.js'
],
moduleFileExtensions: ['js', 'jsx', 'json', 'vue'],
testMatch: [
"**/tests/**/*-spec.js"
],
transformIgnorePatterns: ['<rootDir>/node_modules/'],
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.(js|jsx)?$': 'babel-jest'
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
}
};
Loading

0 comments on commit 472f049

Please sign in to comment.