[go: nahoru, domu]

Skip to content

Commit

Permalink
added upcoming bookings time filter
Browse files Browse the repository at this point in the history
  • Loading branch information
dynamic11 committed Jul 4, 2019
1 parent 3484fe8 commit ee06d5f
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
113 changes: 113 additions & 0 deletions app/Controllers/Http/BookingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,119 @@ class BookingController {
return view.render('userPages.manageBookings', { bookings, numberOfApprovedBookings, numberOfBookingsThisMonth, numberOfCancelled, bookingsType, canEdit });
}



/**
* Retrives all of the bookings that correspond to a specific room.
*
* @param {Object} Context The context object.
*/
async getUserBookingz ({ params, view, auth, response }) {


const userRole = await auth.user.getUserRole();
var canEdit = (auth.user.id === Number(params.id) || userRole === 'admin') ? 1 : 0;

var idType = (params.bookingType === 'user') ? 'user_id' : 'room_id';

let startTimeFilter,endTimeFilter,searchResults;

if(params.catFilter === "upcoming"){

startTimeFilter=moment().format('YYYY-MM-DDTHH:mm');

switch(String(params.timeFilter)) {
case "month":
endTimeFilter = moment().endOf('month').format('YYYY-MM-DD hh:mm');
break;
case "3-months":
endTimeFilter = moment().add(3, 'months').endOf('month').format('YYYY-MM-DD hh:mm');
break;
case "6-months":
endTimeFilter = moment().add(6, 'months').endOf('month').format('YYYY-MM-DD hh:mm');
break;
case "year":
endTimeFilter = moment().endOf('year').format('YYYY-MM-DD hh:mm');
break;
case "all":
endTimeFilter = moment().add(100, 'years').endOf('month').format('YYYY-MM-DD hh:mm');
break;
default:
response.route('home');
break;
}


console.log(idType);
searchResults = await Booking
.query()
.where(idType, params.id)
.whereBetween('from',[startTimeFilter, endTimeFilter])
.orderBy('from', 'asc')
.fetch();

// if(params.timeFilter==="month"){
// timeFilter= moment().endOf('month').format('YYYY-MM-DD hh:mm');
// }else if(params.timeFilter==="3-months"){
// timeFilter= moment().add(3, 'months').endOf('month').format('YYYY-MM-DD hh:mm');
// }else if(params.timeFilter==="6-months"){
// timeFilter= moment().add(6, 'months').endOf('month').format('YYYY-MM-DD hh:mm');
// }else if
}




var idType = (params.bookingType === 'user') ? 'user_id' : 'room_id';
var bookingsType = (idType === 'user_id') ? 'userBookings' : 'roomBookings';

if (userRole !== 'admin' && idType === 'user_id' && parseInt(params.id) !== auth.user.id) {
response.route('home');
}

searchResults = searchResults.toJSON();
const bookings = await populateBookings(searchResults);

// counts the number of approved bookings
let numberOfApprovedBookings = await Booking
.query()
.where(idType, params.id)
.where('status', 'Approved')
.whereRaw("bookings.'to' >= ?", moment().format('YYYY-MM-DDTHH:mm')) // eslint-disable-line
.getCount();

if (numberOfApprovedBookings === 0) {
numberOfApprovedBookings = '0';
}

// calculate the number of bookings a room has this month
let numberOfBookingsThisMonth = await Booking
.query()
.where(idType, params.id)
.where('status', 'Approved')
.whereRaw("bookings.'from' >= ?", moment().format('YYYY-MM-DDTHH:mm')) // eslint-disable-line
.whereRaw("strftime('%Y-%m', bookings.'from') < ?", moment().add(1, 'M').format('YYYY-MM')) // eslint-disable-line
.getCount();

if (numberOfBookingsThisMonth === 0) {
numberOfBookingsThisMonth = '0';
}

// Queries the database fr the cancelled bookings
let numberOfCancelled = await Booking
.query()
.where(idType, params.id)
.where('status', 'Cancelled')
.whereRaw("bookings.'to' >= ?", moment().format('YYYY-MM-DDTHH:mm')) // eslint-disable-line
.getCount();

if (numberOfCancelled === 0) {
numberOfCancelled = '0';
}

return view.render('userPages.manageBookings', { bookings, numberOfApprovedBookings, numberOfBookingsThisMonth, numberOfCancelled, bookingsType, canEdit });
}

/**
* Create a list of all bookings under the current user and render a view for it.
*
Expand Down
2 changes: 1 addition & 1 deletion start/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ Route.post('/reportRoom', 'IssueController.submit').as('reportRoom').middleware(
//= ========================================================================
Route.post('/goToDetails', 'RoomController.goToDetails').as('goToDetails').middleware(['auth']); // needs to be changed to get
Route.get('/:bookingType/:id/bookings', 'BookingController.getBookings').as('viewBookings').middleware(['auth']);
Route.get('/:bookingType/:id/bookings/:catFilter/:timeFilter', 'BookingController.getUserBookingz').as('viewBookingz').middleware(['auth']);
Route.post('/:bookingType/cancelBooking/:id', 'BookingController.cancelBooking').as('cancelBooking').middleware(['auth']);

// Employee user pages
// Route.on('/booking').render('userPages/booking').as('booking').middleware(['isUser']);
Route.get('/searchRooms/:view', 'RoomController.loadSearchRoomsForm').as('searchRooms').middleware(['isUser']);
Route.get('/userDash', 'HomeController.userDashboard').as('userDash').middleware(['isUser']);
Route.on('/manageBookings').render('userPages/manageBookings').as('manageBooking').middleware(['isUser']);

// Rendering Results
Route.get('/results', 'RoomController.findSpecific').as('results').middleware(['auth']).validator('SearchRoom').middleware(['isUser']);
Expand Down

0 comments on commit ee06d5f

Please sign in to comment.