Skip to content

Commit

Permalink
[feature #167727322] Users should be able to comment on requests
Browse files Browse the repository at this point in the history
- Write create and get comments route
- Validate data
- Create model, migration and seeder file
- Save trip comment
- Get comments to owner trip request
- Write swagger documentation
- Write tests for feature

[finishes #167727322]
  • Loading branch information
steelze committed Sep 11, 2019
1 parent d382d18 commit f346d57
Show file tree
Hide file tree
Showing 23 changed files with 550 additions and 9,086 deletions.
9,067 changes: 0 additions & 9,067 deletions package-lock.json

This file was deleted.

72 changes: 71 additions & 1 deletion src/controllers/tripController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {
postTrip, updateTripStatus, getRequesterEmail
postTrip, updateTripStatus, getRequesterEmail, findTripById,
findUserTrip
} from '../services/tripServices';
import { createTripsComment, getTripComments } from '../services/tripCommentServices';
import { respondWithSuccess, respondWithWarning } from '../helpers/responseHandler';
import statusCode from '../helpers/statusCode';
import { approvedEmitter } from '../helpers/notificationHandler';


/**
* make trip request
* @param {object} req
Expand All @@ -23,6 +26,73 @@ export const oneWayTripRequest = async (req, res) => {
}
};

/**
* comment on a trip request
* @param {object} req
* @param {object} res
* @returns {object} json response
*/
export const createTripComment = async (req, res) => {
try {
const { id: userId } = req.auth;
const { comment } = req.body;
const { tripId } = req.params;
const trip = await findTripById(tripId);
if (!trip) {
return respondWithWarning(res, statusCode.resourceNotFound, 'Trip not found');
}
const data = await createTripsComment({ userId, tripId, comment });
return respondWithSuccess(res, statusCode.created, 'Comment saved', data.dataValues);
} catch (error) {
return respondWithWarning(res, statusCode.internalServerError, error.message);
}
};

/**
* get a user trip comments
* @param {object} req
* @param {object} res
* @param {Function} next
* @returns {object} json response
*/
export const getUserTripComments = async (req, res, next) => {
try {
const { tripId } = req.params;
const { id, roleId } = req.auth;
if (roleId === 6) {
const trip = await findUserTrip(Number(tripId), id);
if (!trip) {
return respondWithWarning(res, statusCode.resourceNotFound, 'Trip not found');
}
const data = await getTripComments(trip);
return respondWithSuccess(res, statusCode.success, 'Data retrieved successfully', data);
}
return next();
} catch (error) {
return respondWithWarning(res, statusCode.internalServerError, error.message);
}
};

/**
* get a trip comments
* @param {object} req
* @param {object} res
* @returns {object} json response
*/
export const getAllTripComments = async (req, res) => {
try {
const { tripId } = req.params;
const trip = await findTripById(Number(tripId));
if (!trip) {
return respondWithWarning(res, statusCode.resourceNotFound, 'Trip not found');
}
const data = await getTripComments(trip);
return respondWithSuccess(res, statusCode.success, 'Data retrieved successfully', data);
} catch (error) {
return respondWithWarning(res, statusCode.internalServerError, error.message);
}
};

export const approveTripRequest = async (req, res) => {
const status = 'approved';
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default {
// values should be ['approved', 'pending', 'rejected']
},
userId: {
type: Sequelize.STRING,
type: Sequelize.INTEGER,
allowNull: false,
},
createdAt: {
Expand Down
4 changes: 1 addition & 3 deletions src/database/migrations/20190831213947-create-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,5 @@ export default {
type: Sequelize.DATE
}
}),
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Profiles');
}
down: (queryInterface, Sequelize) => queryInterface.dropTable('Profiles')
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('TripComments', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
},
tripId: {
type: Sequelize.INTEGER,
allowNull: false,
},
comment: {
type: Sequelize.STRING,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
},
deletedAt: {
type: Sequelize.DATE,
}
}),
down: (queryInterface) => queryInterface.dropTable('TripComments')
};
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ export default {
{
actionName: 'VIEW_BOOK_ACCOMODATION_FOR_TRIP',
},
{
actionName: 'VIEW_TRIP_COMMENT',
},
], {}),

down: (queryInterface, Sequelize) => queryInterface.bulkDelete('Permissions', null, {})
Expand Down
16 changes: 16 additions & 0 deletions src/database/seeders/20190825190012-create-users-seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ export default {
password: await passwordHash('123456'),
roleId: 4
},
{
firstName: 'Travel',
lastName: 'Admin',
username: 'traveladmin',
email: 'traveladmin@nomad.com',
password: await passwordHash('123456'),
roleId: 2
},
{
firstName: 'Dale',
lastName: 'Mark',
username: 'marck',
email: 'mark@nomad.com',
password: await passwordHash('123456'),
roleId: 6
}
], {}),

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ export default {
roleId: 6,
permissionId: 16
},
{
roleId: 6,
permissionId: 17
},
{
roleId: 2,
permissionId: 17
Expand Down Expand Up @@ -464,6 +460,26 @@ export default {
{
roleId: 6,
permissionId: 41
},
{
roleId: 1,
permissionId: 42
},
{
roleId: 2,
permissionId: 42
},
{
roleId: 3,
permissionId: 42
},
{
roleId: 4,
permissionId: 42
},
{
roleId: 6,
permissionId: 42
}
], {}),

Expand Down
13 changes: 11 additions & 2 deletions src/database/seeders/20190906230800-create-trip-request-seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export default {
origin: 'Lagos',
destination: 'Nairobi',
reason: 'Trade Fair',
departureDate: '9/9/2019',
returnDate: '10/10/2019',
departureDate: '2019-09-09T00:09:31.812Z',
returnDate: '2019-19-09T00:09:31.812Z',
type: 'one-way',
userId: 3
},
Expand All @@ -28,6 +28,15 @@ export default {
departureDate: '2019-19-09T00:09:31.812Z',
status: 'pending',
userId: 2
},
{
origin: 'Lagos',
destination: 'New York',
reason: 'DockerCon',
departureDate: '2019-09-09T00:09:31.812Z',
returnDate: '2019-19-09T00:09:31.812Z',
type: 'one-way',
userId: 6
}
], {}),

Expand Down
16 changes: 16 additions & 0 deletions src/database/seeders/20190907230800-create-trip-comment-seeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
up: async (queryInterface) => queryInterface.bulkInsert('TripComments', [
{
tripId: 1,
userId: 5,
comment: 'Avoid staying out late',
},
{
tripId: 2,
userId: 5,
comment: 'Avoid the subways.',
}
], {}),

down: queryInterface => queryInterface.bulkDelete('TripRequests', null, {})
};
9 changes: 9 additions & 0 deletions src/docs/swagger/definitions/trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ export const createTrip = {
}
}
};

export const createTripComment = {
type: 'object',
properties: {
comment: {
type: 'string',
},
}
};
1 change: 0 additions & 1 deletion src/docs/swagger/paths/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,3 @@ const ProfilePath = {
};

export default ProfilePath;

Loading

0 comments on commit f346d57

Please sign in to comment.