Skip to content

Commit

Permalink
Merge pull request #46 from andela/ft-view-most-travelled-destination…
Browse files Browse the repository at this point in the history
…s-168781713

#168781713 - Users can view travelled Destinations
  • Loading branch information
nakiwuge committed Nov 7, 2019
2 parents 541b60b + 68d8e75 commit 5b9455b
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 158 deletions.
132 changes: 66 additions & 66 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "caret-bn-backend",
"version": "1.0.0",
"description": "A platform to make company global travel and accommodation easy and convenient for the strong workforce of savvy members of staff, by leveraging the modern web.",
"description":
"A platform to make company global travel and accommodation easy and convenient for the strong workforce of savvy members of staff, by leveraging the modern web.",
"main": "index.js",
"scripts": {
"dev": "NODE_ENV=development babel-watch src/index.js",
Expand All @@ -14,9 +15,11 @@
"rollback": "NODE_ENV=test sequelize db:migrate:undo:all",
"seed": "NODE_ENV=test sequelize db:seed:all",
"test": "npm run rollback && npm run migrate && npm run seed && npm run test-suite",
"test:requests": "npm run rollback && npm run migrate && npm run seed && npm run request-suite",
"request-suite": "NODE_ENV=test nyc --reporter=html --reporter=text mocha src/tests/requestTests/index.spec.js --require @babel/register --timeout 20000 --exit",
"test-suite": "NODE_ENV=test nyc --reporter=html --reporter=text mocha src/tests/index.spec.js --require @babel/register --timeout 20000 --exit",
"test:destinations": "npm run rollback && npm run migrate && npm run seed && npm run destination-suite",
"destination-suite":
"NODE_ENV=test nyc --reporter=html --reporter=text mocha src/tests/destinationTests.spec.js --require @babel/register --timeout 20000 --exit",
"test-suite":
"NODE_ENV=test nyc --reporter=html --reporter=text mocha src/tests/index.spec.js --require @babel/register --timeout 20000 --exit",
"coveralls": "nyc report --reporter=text-lcov| coveralls",
"localbuild": "babel ./src --out-dir dist --source-maps --watch"
},
Expand Down
43 changes: 40 additions & 3 deletions src/controllers/destinationController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/* eslint-disable implicit-arrow-linebreak */
/* eslint-disable max-len */
/* eslint-disable function-paren-newline */
/* eslint-disable require-jsdoc */
import destinationServices from '../services/requestServices/destinationServices';
import requestServices from '../services/requestServices/requestServices';
import { findOne, allRequests } from '../services/requestServices/requestServices';
import { allDestinations } from '../utils/db/queries/destinationQueries';
import Utilities from '../utils/index';
import notifSender from '../helpers/notifSender';

import arrayMapper from '../helpers/arrayMapper';

export default class destinationController {
static async storeDestination(req, res, body, user, request) {
Expand All @@ -15,7 +20,7 @@ export default class destinationController {

const query = Utilities.requestQueries.singleRequest(request.id, user.payload.id);

const response = await requestServices.findOne(query);
const response = await findOne(query);

const { lineManager } = user.payload;

Expand All @@ -27,6 +32,38 @@ export default class destinationController {
response,
201
);
}

static async viewTopDestinations(req, res) {
const approvedTravelRequests = await allRequests(allDestinations);
const destinations = await Promise.all(approvedTravelRequests.map(async request => {
const destination = await request.destinations;
return destination;
}));

const locations = await Promise.all(destinations.map(async destination => destination.map(dest => ({
locationId: dest.locationId,
name: dest.location.name
}))));

const spread = [];
locations.forEach(location => spread.push(...location));

const filteredLocationArray = spread.map(local => ({
id: local.locationId,
name: local.name,
numberOfVisits: spread.filter(
location => (location.locationId === local.locationId)
).length
}));

const singleLocations = arrayMapper(filteredLocationArray);

return Utilities.responseHelper(
res,
'Top 5 locations',
singleLocations.sort((first, second) => second.numberOfVisits - first.numberOfVisits).slice(0, 5),
200
);
}
}
31 changes: 16 additions & 15 deletions src/controllers/requestController.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/* eslint-disable no-irregular-whitespace */
import Sequelize from 'sequelize';
/* eslint-disable require-jsdoc */
import Sequelize from 'sequelize';
import models from '../database/models';
import strings from '../utils/stringsUtil';
import text from '../utils/strings';
import responseHelper from '../utils/responseHelper';
import responseUtil from '../utils/responseUtil';
import userServices from '../services/userServices';
import requestServices from '../services/requestServices/requestServices';
import { createRequest, findOne } from '../services/requestServices/requestServices';
import destinationController from './destinationController';
import searchRequestsServices from '../services/searchRequestsServices';
import Utilities from '../utils/index';
import findOneRequest from '../helpers/findOneRequest';
import findUser from '../helpers/findUser';
import notifSender from '../helpers/notifSender';

const { Op } = Sequelize;
const { Op } = Sequelize;
const { SUCCESSFULLY_RETRIEVED_REQUESTS } = strings.requests;
const { NO_REQUESTS, ASSIGNED_REQUESTS } = text.user.requests;

Expand Down Expand Up @@ -86,23 +87,23 @@ export default class requestController {
return responseHelper(res, strings.requests.NOT_FOUND, null, 404);
}

static async updateRequest(req, res) {
static async updateRequest(req, res) {
const { id } = req.request;
try {
await models.requests.update(req.body, {
where: { id }, returning: true, raw: true,
try {
await models.requests.update(req.body, {
where: { id }, returning: true, raw: true,
});
const destination = req.body.destinations;
destination.forEach(async element => {
await models.destinations.update(element, {
where: {
[Op.and]: [{ requestId: id }, { id: element.id }]
const destination = req.body.destinations;
destination.forEach(async element => {
await models.destinations.update(element, {
where: {
[Op.and]: [{ requestId: id }, { id: element.id }]
},
});
});
const request = await allSearch({ id });
return responseUtil(res, 200, strings.request.success.SUCCESS_UPDATE_REQUEST, request);
} catch (error) {
} catch (error) {
return res.status(500).json({ error: 'Something wrong' });
}
}
Expand Down Expand Up @@ -131,7 +132,7 @@ export default class requestController {
const { id } = req.params;
const { user } = req;
const query = Utilities.requestQueries.singleRequest(id, user.payload.id);
const request = await requestServices.findOne(query);
const request = await findOne(query);
return Utilities.responseHelper(
res,
Utilities.stringsHelper.user.requests.SUCCESSFULLY_RETRIEVED_REQUESTS,
Expand All @@ -142,7 +143,7 @@ export default class requestController {

static async storeRequest(req, res) {
const { body, user } = req;
const request = await requestServices.createRequest(body, user.payload.id);
const request = await createRequest(body, user.payload.id);
return destinationController.storeDestination(req, res, body, user, request);
}
}
162 changes: 101 additions & 61 deletions src/database/seeders/20191016143026-requestTableSeeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,67 +43,107 @@ module.exports = {
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 5,
locationId: 3,
statusId: 1,
departureDate: '10-25-2019',
returnDate: '11-25-2019',
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 3,
locationId: 2,
statusId: 1,
departureDate: '10-25-2019',
returnDate: '11-25-2019',
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 6,
locationId: 2,
statusId: 1,
departureDate: '10-25-2019',
returnDate: null,
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 6,
locationId: 2,
statusId: 1,
departureDate: '10-25-2019',
returnDate: '11-25-2019',
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 6,
locationId: 2,
statusId: 1,
departureDate: '10-25-2019',
returnDate: null,
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 6,
locationId: 2,
statusId: 1,
departureDate: '10-25-2019',
returnDate: null,
createdAt: new Date(),
updatedAt: new Date()
}
])
{
typeId: 3,
userId: 5,
locationId: 3,
statusId: 1,
departureDate: '10-25-2019',
returnDate: '11-25-2019',
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 3,
locationId: 2,
statusId: 1,
departureDate: '10-25-2019',
returnDate: '11-25-2019',
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 6,
locationId: 2,
statusId: 1,
departureDate: '10-25-2019',
returnDate: null,
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 6,
locationId: 2,
statusId: 1,
departureDate: '10-25-2019',
returnDate: '11-25-2019',
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 6,
locationId: 2,
statusId: 1,
departureDate: '10-25-2019',
returnDate: null,
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 6,
locationId: 2,
statusId: 1,
departureDate: '10-25-2019',
returnDate: null,
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 1,
locationId: 2,
statusId: 3,
departureDate: '03-01-2019',
returnDate: null,
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 3,
userId: 3,
locationId: 2,
statusId: 3,
departureDate: '01-10-2019',
returnDate: null,
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 2,
userId: 3,
locationId: 2,
statusId: 3,
departureDate: '04-10-2019',
returnDate: '04-20-2019',
createdAt: new Date(),
updatedAt: new Date()
},
{
typeId: 2,
userId: 3,
locationId: 2,
statusId: 3,
departureDate: '05-10-2019',
returnDate: '05-20-2019',
createdAt: new Date(),
updatedAt: new Date()
}
])
]),

down: (queryInterface, Sequelize) => Promise.all([
Expand Down
Loading

0 comments on commit 5b9455b

Please sign in to comment.