Skip to content

Commit

Permalink
Merge 2306a95 into 7a958b0
Browse files Browse the repository at this point in the history
  • Loading branch information
gadishimwe committed Jan 7, 2020
2 parents 7a958b0 + 2306a95 commit 4799183
Show file tree
Hide file tree
Showing 26 changed files with 824 additions and 186 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,4 @@
"allowImportExportEverywhere": true,
"ecmaVersion": 11
}
}

}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"migrate:drop": "sequelize db:migrate:undo:all",
"dev": "nodemon --exec babel-node src/server.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"pretest": "npm run migrate:drop && npm run migrate:up",
"pretest": "npm run migrate:drop && npm run migrate:up && npm run seed:up",
"test": "export NODE_ENV=test && nyc --reporter=html --reporter=text mocha --require @babel/register src/tests/**/*.test.js --timeout 100000 --exit",
"migrate:up": "npx sequelize-cli db:migrate"
"migrate:up": "npx sequelize-cli db:migrate",
"seed:up": "npx sequelize-cli db:seed:all"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -83,4 +84,4 @@
"swagger-ui-express": "^4.1.2",
"test-exclude": "^5.2.3"
}
}
}
52 changes: 47 additions & 5 deletions src/controllers/trip.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import TripService from '../services/trip.service';
import ResponseService from '../services/response.service';
import JwtService from '../services/jwt.service';
import RequestService from '../services/request.service';
import UserService from '../services/user.service';

/**
*
Expand All @@ -17,12 +19,11 @@ class TripController {
* @returns {response} @memberof Trips
*/
static async requestOneWayTrip(req, res) {
const newTrip = await TripService.createTrip({
...req.body, userId: req.userData.id, tripType: 'one-way', status: 'pending'
const { dataValues } = await TripService.createTrip({
...req.body, tripId: req.tripId, userId: req.userData.id, tripType: 'one-way', status: 'pending'
});
delete newTrip.dataValues.updatedAt;
delete newTrip.dataValues.createdAt;
ResponseService.setSuccess(201, 'Trip is successfully created', newTrip.dataValues);
const { updatedAt, createdAt, ...newTrip } = dataValues;
ResponseService.setSuccess(201, 'Trip is successfully created', newTrip);
return ResponseService.send(res);
}

Expand All @@ -40,6 +41,7 @@ class TripController {
} = req.body;

const returnTrip = {
tripId: req.tripId,
userId: signInUser.id,
tripType: 'return-trip',
departure,
Expand All @@ -66,6 +68,46 @@ class TripController {
ResponseService.setSuccess(200, 'List of requested trips', trips);
return ResponseService.send(res);
}

/**
* @static
* @param {req} req
* @param {res} res
* @returns {response} @memberof Trips
*/
static async requestMultiCityTrip(req, res) {
const newMultiCityTrip = req.body.map((trip) => ({ ...trip, tripId: req.tripId, userId: req.userData.id, tripType: 'multi-city' }));
const newTrips = await TripService.createMultiCityTrip(newMultiCityTrip);
const newTripArray = newTrips.map((trip) => {
const { dataValues } = trip;
const { updatedAt, createdAt, ...newTrip } = dataValues;
return newTrip;
});
const { lineManagerId } = await UserService.findUserByProperty(req.userData.id);
const { dataValues } = await RequestService.createRequest({ requesterId: newMultiCityTrip[0].userId, tripId: req.tripId, status: 'pending', lineManagerId });
const { updatedAt, createdAt, ...newRequest } = dataValues;
ResponseService.setSuccess(201, 'Trip request is successfully created', { newTrip: newTripArray, newRequest });
return ResponseService.send(res);
}

/**
*
*
* @static
* @param {req} req
* @param {res} res
* @returns {response} @memberof Trips
*/
static async viewAvailableLocations(req, res) {
const locations = await TripService.findAllLocations();
const availableLocations = locations.map(loc => {
const { dataValues } = loc;
const { updatedAt, createdAt, ...location } = dataValues;
return location;
});
ResponseService.setSuccess(200, 'List of all available locations', availableLocations);
return ResponseService.send(res);
}
}

export default TripController;
3 changes: 3 additions & 0 deletions src/migrations/20191218075931-create-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export function up(queryInterface, Sequelize) {
type: Sequelize.BOOLEAN,
defaultValue: false
},
lineManagerId: {
type: Sequelize.INTEGER
},
token: {
allowNull: true,
type: Sequelize.STRING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ export const up = (queryInterface, Sequelize) => queryInterface.createTable('Tri
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER
},
tripType: {
type: Sequelize.STRING
},
departure: {
type: Sequelize.STRING
tripId: {
type: Sequelize.BIGINT
},
destination: {
type: Sequelize.STRING
userId: {
type: Sequelize.INTEGER
},
travelDate: {
originId: {
type: Sequelize.INTEGER
},
destinationId: {
type: Sequelize.INTEGER
},
departureDate: {
type: Sequelize.DATE
},
returnDate: {
Expand All @@ -26,11 +29,8 @@ export const up = (queryInterface, Sequelize) => queryInterface.createTable('Tri
travelReasons: {
type: Sequelize.STRING
},
accommodation: {
type: Sequelize.STRING
},
status: {
type: Sequelize.STRING
accommodationId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
Expand Down
34 changes: 34 additions & 0 deletions src/migrations/20200101101824-create-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export const up = (queryInterface, Sequelize) => queryInterface.createTable('Requests', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
requesterId: {
type: Sequelize.INTEGER
},
tripId: {
type: Sequelize.BIGINT
},
status: {
type: Sequelize.STRING
},
lineManagerId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
/**
* @exports
* @class
* @param {object} queryInterface
*/
export function down(queryInterface) { return queryInterface.dropTable('Requests'); }
28 changes: 28 additions & 0 deletions src/migrations/20200102145246-create-location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const up = (queryInterface, Sequelize) => queryInterface.createTable('Locations', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
country: {
type: Sequelize.STRING
},
city: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
/**
* @exports
* @class
* @param {object} queryInterface
*/
export function down(queryInterface) { return queryInterface.dropTable('Locations'); }
7 changes: 7 additions & 0 deletions src/models/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default (sequelize, DataTypes) => {
const Location = sequelize.define('Location', {
country: DataTypes.STRING,
city: DataTypes.STRING
}, {});
return Location;
};
13 changes: 13 additions & 0 deletions src/models/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default (sequelize, DataTypes) => {
const Request = sequelize.define('Request', {
requesterId: DataTypes.INTEGER,
tripId: DataTypes.BIGINT,
status: DataTypes.STRING,
lineManagerId: DataTypes.INTEGER
}, {});
Request.associate = (models) => {
Request.belongsTo(models.Users, { foreignKey: 'requesterId', targetKey: 'id' });
Request.belongsTo(models.Trip, { foreignKey: 'tripId', targetKey: 'tripId' });
};
return Request;
};
17 changes: 9 additions & 8 deletions src/models/trip.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
export default (sequelize, DataTypes) => {
const Trip = sequelize.define('Trip', {
userId: DataTypes.INTEGER,
tripType: DataTypes.STRING,
departure: DataTypes.STRING,
destination: DataTypes.STRING,
travelDate: DataTypes.DATE,
tripId: DataTypes.BIGINT,
userId: DataTypes.INTEGER,
originId: DataTypes.INTEGER,
destinationId: DataTypes.INTEGER,
departureDate: DataTypes.DATE,
returnDate: DataTypes.DATE,
travelReasons: DataTypes.STRING,
accommodation: DataTypes.STRING,
status: DataTypes.STRING
accommodationId: DataTypes.INTEGER
}, {});
Trip.associate = (models) => {
// associations can be defined here
Trip.belongsTo(models.Users, { foreignKey: 'userId', as: 'user', targetKey: 'id' });
Trip.belongsTo(models.Users, { foreignKey: 'userId', targetKey: 'id' });
Trip.belongsTo(models.Location, { foreignKey: 'originId', targetKey: 'id' });
Trip.belongsTo(models.Location, { foreignKey: 'destinationId', targetKey: 'id' });
};
return Trip;
};
3 changes: 2 additions & 1 deletion src/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default (sequelize, DataTypes) => {
lineManager: DataTypes.STRING,
role: DataTypes.ENUM('super_admin', 'travel_admin', 'travel_team_member', 'manager', 'requester'),
token: DataTypes.STRING,
isVerified: DataTypes.BOOLEAN
isVerified: DataTypes.BOOLEAN,
lineManagerId: DataTypes.INTEGER
}, {});
Users.associate = (models) => {
// associations can be defined here
Expand Down
3 changes: 3 additions & 0 deletions src/routes/trip.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import oneWayTripValidation from '../validations/one-way-trip.validation';
import authMiddleware from '../middlewares/auth.middleware';
import TripValidation from '../validations/trip.validation';
import requestValidation from '../validations/request.validation';
import multiCityTripValidation from '../validations/multi-city-trip.validation';

const router = express.Router();

router.post('/one-way-trips', authMiddleware.checkUserLoggedIn, oneWayTripValidation, TripController.requestOneWayTrip); // One way trip route
router.post('/return-trip', authMiddleware.checkUserLoggedIn, TripValidation, TripController.requestReturnTrip); // Return trip route
router.get('/requests/:userId', authMiddleware.checkUserLoggedIn, requestValidation, TripController.userTripRequestList); // user request list route
router.post('/multi-city-trips', authMiddleware.checkUserLoggedIn, multiCityTripValidation, TripController.requestMultiCityTrip);
router.get('/locations', authMiddleware.checkUserLoggedIn, TripController.viewAvailableLocations);

export default router;
65 changes: 65 additions & 0 deletions src/seeders/20200102151920-Location-seeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export const up = (queryInterface) => queryInterface.bulkInsert('Locations', [
{
country: 'Rwanda',
city: 'Kigali',
createdAt: new Date(),
updatedAt: new Date()
},
{
country: 'Rwanda',
city: 'Gisenyi',
createdAt: new Date(),
updatedAt: new Date()
},
{
country: 'Uganda',
city: 'Kampala',
createdAt: new Date(),
updatedAt: new Date()
},
{
country: 'Uganda',
city: 'Entebee',
createdAt: new Date(),
updatedAt: new Date()
},
{
country: 'Kenya',
city: 'Nairobi',
createdAt: new Date(),
updatedAt: new Date()
},
{
country: 'Kenya',
city: 'Mombasa',
createdAt: new Date(),
updatedAt: new Date()
},
{
country: 'Nigeria',
city: 'Lagos',
createdAt: new Date(),
updatedAt: new Date()
},
{
country: 'Tanzania',
city: 'Dodoma',
createdAt: new Date(),
updatedAt: new Date()
},
{
country: 'Tanzania',
city: 'Dar es salaam',
createdAt: new Date(),
updatedAt: new Date()
}
], {});
export const down = () => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkDelete('People', null, {});
*/
};
26 changes: 26 additions & 0 deletions src/services/location.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import models from '../models';

const { Location } = models;

/**
*
*
* @class LocationService
*/
class LocationService {
/**
* find Location
* @static
* @param {object} property
* @memberof LocationService
* @returns {object} data
*/
static findLocationByProperty(property) {
return Location.findOne({
where: { ...property }
});
}
}


export default LocationService;
Loading

0 comments on commit 4799183

Please sign in to comment.