Skip to content

Commit

Permalink
bg(ensure that the email is sent to the line manager): added email no…
Browse files Browse the repository at this point in the history
…tification functionality

added fetch single trip route
[#Starts #170258130]
  • Loading branch information
hezzie committed Dec 12, 2019
1 parent 38d1d98 commit b3bbd44
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/controllers/TripController.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ class TripController {
}
}

/**
* fetch a single trip
* @static
* @param {object} req request object
* @param {object} res response object
* @memberof TripController
* @returns {object} data
*/
static async getSingleTrip(req, res) {
try {
const { tripId } = req.params;
const result = await TripService.getSingleTrip(tripId);
return Response.successMessage(req, res, 'succesfully fetched one trip', result, 200);
} catch (err) {
return Response.errorMessage(req, res, err.message, 500);
}
}

/**
* Manager should be able to accept a trip request
* @static
Expand Down
14 changes: 11 additions & 3 deletions src/helpers/TripHelper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Response from './Response';
import emailHelper from './EmailHelper';
import CommonQueries from '../services/CommonQueries';
import { tripRequests, trips, cities } from '../database/models';
import {
tripRequests, trips, cities, userProfile, users
} from '../database/models';
import NotificationService from '../services/NotificationService';


Expand Down Expand Up @@ -39,10 +41,16 @@ class TripHelper {
returnDate: item.returnDate
});
});

req.result = newTrip;
await NotificationService.newTripRequestNotification(req);
emailHelper.approveEmailHelper(req, process.env.MANAGER_EMAIL);
const { id } = req.user;
const userInfo = await CommonQueries.findOne(userProfile,
{ where: { userId: id }, raw: true });
const { managerId } = userInfo;
const managerInfo = await CommonQueries.findOne(users,
{ where: { id: managerId }, raw: true });
const { email: managerEmail } = managerInfo;
emailHelper.approveEmailHelper(req, managerEmail);
return Response.successMessage(req, res, 'Trip requested successfully', newTrip, 201);
} catch (err) {
return Response.errorMessage(req, res, err.message, 500);
Expand Down
11 changes: 10 additions & 1 deletion src/middlewares/Validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,16 @@ class Validate {
check('tripRequestId', 'ID should be an integer').isInt(),
];
}

/**
* Validate input
* @static
* @returns {object} errors
*/
static isTripIDInteger() {
return [
check('tripId', 'ID should be an integer').isInt(),
];
}
/**
* Validate accommodation params
* @static
Expand Down
5 changes: 3 additions & 2 deletions src/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
socket.on('trip_request_client', (name) => {
console.log(JSON.parse(name));
});

</script>
</html>

</html>
33 changes: 33 additions & 0 deletions src/routes/api/tripRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,39 @@ tripRouter.get(
TripController.getUserRequests
);

/**
* @swagger
*
* /trips/:id:
* get:
* summary: Available trip requests
* /trips/requests:
* get:
* summary: Available requests to manager for approval
* tags: [Trip]
* parameters:
* - name: token
* in: header
* description: enter token
* required: true
* schema:
* type: string
* responses:
* "200":
* description: Trip requests fetched successfuly
* "401":
* description: No token provided
* "404":
* description: Trip requests are not found
*/
tripRouter.get(
'/:tripId',
verifyToken,
isUserVerified,
Validate.isTripIDInteger(),
checkInputDataError,
TripController.getSingleTrip
);

/**
* @swagger
Expand Down
16 changes: 16 additions & 0 deletions src/services/TripService.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ class TripService {
});
return Promise.all(tripsWithCityName);
}

/**
* fetch a single trip
* @static
* @param {object} tripId request object
* @memberof class TripService
* @returns {object} data
*/
static async getSingleTrip(tripId) {
const singleTripObject = {
where: { id: tripId },
raw: true
};
const result = await CommonQueries.findOne(trips, singleTripObject);
return result;
}
}

export default TripService;
18 changes: 18 additions & 0 deletions src/tests/060-tripTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,24 @@ describe('Get requests', () => {
done();
});
});
it('A user should be able to get a single trip', (done) => {
chai.request(app).get('/api/v1/trips/1')
.set('token', token)
.end((err, res) => {
res.should.have.status(200);
expect(res.body.message).eql('succesfully fetched one trip');
done();
});
});
it('A user should receive 404 error when the trip id is not found in the database', (done) => {
chai.request(app).get('/api/v1/trips/nbfddbb')
.set('token', token)
.end((err, res) => {
res.should.have.status(400);
expect(res.body.message).eql(['ID should be an integer']);
done();
});
});
it('A user should be not able to get his/her trip requests if not verified', (done) => {
chai.request(app).get('/api/v1/trips/')
.set('token', unverifiedUserToken)
Expand Down

0 comments on commit b3bbd44

Please sign in to comment.