Skip to content

Commit

Permalink
Merge pull request #47 from AndreySurzhan/develop
Browse files Browse the repository at this point in the history
Release 07/11/2020
  • Loading branch information
AndreySurzhan authored Jul 11, 2020
2 parents 0e7c765 + 87f5d53 commit d94433f
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 48 deletions.
44 changes: 19 additions & 25 deletions src/controllers/checklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ module.exports = class ChecklistController {
}

async addNewChecklist(req, res, next) {
let checklist = req.body;
let newChecklist;
let user = req.user;
let userData = {};
const checklist = req.body;
const user = req.user;

try {
checklist.users = [];
Expand All @@ -26,12 +24,11 @@ module.exports = class ChecklistController {
checklist.createdBy = user._id;
checklist.modifiedBy = user._id;

newChecklist = await this.checklistRepo.insert(checklist);
const newChecklist = await this.checklistRepo.insert(checklist);

userData.checklists = user.checklists;
userData.checklists.push(newChecklist._id);
user.checklists.push(newChecklist._id);

await this.userRepo.update(user._id, userData);
await this.userRepo.update(user._id, user);

await res.json(newChecklist);
} catch (error) {
Expand Down Expand Up @@ -64,22 +61,19 @@ module.exports = class ChecklistController {
}

async addItemToChecklist(req, res, next) {
let addedItem;
let checklist;
let id = req.params.id;
let item = req.body;
let user = req.user;
const id = req.params.id;
const item = req.body;
const user = req.user;

try {
checklist = await this.checklistRepo.findById(req.params.id);
user = await this.userRepo.findByUsername(user.username);
const checklist = await this.checklistRepo.findById(req.params.id);

item.checklist = id;
item.createdBy = user._id;
item.modifiedBy = user._id;
item.translations = await this.translationController.translateMany(item.text, user);

addedItem = await this.itemRepo.insert(item);
const addedItem = await this.itemRepo.insert(item);

checklist.items.push(addedItem._id);

Expand Down Expand Up @@ -161,25 +155,25 @@ module.exports = class ChecklistController {
}

async deleteChecklistById(req, res, next) {
let checklist;
let id = req.params.id;
let users;
const id = req.params.id;

try {
checklist = await this.checklistRepo.delete(id);
const checklist = await this.checklistRepo.delete(id);

await this.itemRepo.deleteManyById(checklist.items);

users = await this.userRepo.findAllByChecklistId(id);
const users = await this.userRepo.findAllByChecklistId(id);

for (let i = 0; i < users.length; i++) {
for (let k = 0; k < users[i].checklists.length; k++) {
let index = users[i].checklists.indexOf(id)
const user = users[i];

for (let k = 0; k < user.checklists.length; k++) {
const index = user.checklists.indexOf(id)

if (index > -1) {
users[i].checklists.splice(index, 1)
user.checklists.splice(index, 1)

await this.userRepo.update(users[i]);
await this.userRepo.update(user._id, user);
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/controllers/language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const LanguageRepo = require('../repositories/language');


module.exports = class LanguageController {
constructor() {
this.languageRepo = new LanguageRepo();
}

async findAllLanguages(req, res, next) {
try {
const languages = await this.languageRepo.findAll();

await res.json(languages);
} catch (error) {
next(error);
}
}
};
8 changes: 7 additions & 1 deletion src/controllers/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ module.exports = class Translate {
translate.translate(text, {
to: user.languages[i]
}, (error, response) => {
let translation = {};
const translation = {};

error = error
? error
: !response.text && response.code
? new Error(response.message)
: null;

if (error) {
logging.error(`Failed to translate "${text}"`)
Expand Down
2 changes: 1 addition & 1 deletion src/models/checklist.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Libs
const mongoose = require('mongoose');
const validations = require('../utils/validations');
const Validations = require('../utils/validations');
/// Models
const Item = require('./item');
const User = require('./user');
Expand Down
103 changes: 103 additions & 0 deletions src/models/language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/// Libs
const mongoose = require('mongoose');
/// Models
const User = require('./user');
/// Local variables
const Schema = mongoose.Schema;

let Language;

/**
* @swagger
*
* components:
* schemas:
* Language:
* allOf:
* - $ref: '#/components/schemas/MongoId'
* - type: object
* properties:
* code:
* type: string
* name:
* type: string
* - $ref: '#/components/schemas/Audit'
* required:
* - name
* - code
*/

/**
* Language mongoose schema.
* @class models/LanguageSchema
*/
const LanguageSchema = new Schema({
/**
* The item text.
*
* @type String
* @memberof models/LanguageSchema
*/
code: {
type: String,
required: true
},

/**
* The item text.
*
* @type String
* @memberof models/LanguageSchema
*/
name: {
type: String,
required: true
},

/**
* The creation date.
*
* @type Date
* @memberof models/LanguageSchema
*/
created: {
type: Date
},

/**
* The date of the last user modification.
*
* @type Date
* @memberof models/LanguageSchema
*/
modified: {
type: Date,
default: Date.now
},

/**
* @type ObjectId
* @memberof models/LanguageSchema
*/
createdBy: {
type: Schema.Types.ObjectId,
ref: 'User'
},

/**
* @type ObjectId
* @memberof models/LanguageSchema
*/
modifiedBy: {
type: Schema.Types.ObjectId,
ref: 'User'
}
});

if (mongoose.models.Language) {
Language = mongoose.model('Language');
} else {
Language = mongoose.model('Language', LanguageSchema);
}

module.exports = Language;
12 changes: 7 additions & 5 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const bcrypt = require('bcrypt-nodejs');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
const logging = require('../utils/logging');
const validations = require('../utils/validations');
const Validations = require('../utils/validations');
/// Models
const Checklist = require('./checklist');
/// Local variables
Expand Down Expand Up @@ -184,7 +184,7 @@ UserSchema.virtual('token').get(function() {
UserSchema.methods.generateHash = password => {
let hashedPassword = null;

if (!validations.isNotEmpty(password)) {
if (!Validations.isNotEmpty(password)) {
const validationError = new mongoose.Error.ValidationError();

validationError.message = 'Password should not be empty'
Expand Down Expand Up @@ -249,9 +249,11 @@ UserSchema.methods.generateJWT = function() {

UserSchema.set('toObject', { virtuals: true });

UserSchema.path('username').validate((userEmail) => {
return validations.isNotEmpty(userEmail) && validations.isEmailValid(userEmail);
}, 'Is not valid email address');
UserSchema
.path('username')
.validate(
userEmail => Validations.isNotEmpty(userEmail) && Validations.isEmailValid(userEmail),
'Is not valid email address');

if (mongoose.models.User) {
User = mongoose.model('User');
Expand Down
2 changes: 1 addition & 1 deletion src/repositories/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = class ItemRepository extends Repository {
let existedItems = null

try {
existedItems = ItemModel.find({
existedItems = await ItemModel.find({
checklist: checklistId
});
} catch (error) {
Expand Down
36 changes: 36 additions & 0 deletions src/repositories/language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const logging = require('../utils/logging');
const LanguageModel = require('../models/language');
const Repository = require('./repository');
const NotFoundError = require('../utils/errors').NotFoundError

module.exports = class LanguageRepository extends Repository {
constructor(){
super(LanguageModel);
}

/**
* Method that gets all language models
*
* @async
* @returns {Promise <Query>[]}
* @memberof LanguageRepository
*/
async findAll() {
let languages = null

try {
languages = await LanguageModel.find();
} catch (error) {
logging.error('Failed to find all languages from database');

throw error;
}

if (!languages) {
throw new NotFoundError('Languages don\'t exist')
}

logging.info('All languages have been successfully found');
return languages;
}
}
4 changes: 2 additions & 2 deletions src/repositories/repository.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const logging = require('../utils/logging');
const NotFoundError = require('../utils/errors').NotFoundError
const mongoose = require('mongoose');
const validations = require('../utils/validations');
const Validations = require('../utils/validations');

module.exports = class Repository {

Expand Down Expand Up @@ -90,7 +90,7 @@ module.exports = class Repository {
let doc;

try {
if (validations.isObjectEmpty(data)) {
if (Validations.isObjectEmpty(data)) {
const validationError = new mongoose.Error.ValidationError();

validationError.message = 'Body should not be empty'
Expand Down
Loading

0 comments on commit d94433f

Please sign in to comment.