[go: nahoru, domu]

Skip to content

Commit

Permalink
[new] update pusher logic (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
notRealLi committed Nov 29, 2019
1 parent 5bc8488 commit 4b5ea35
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 61 deletions.
16 changes: 9 additions & 7 deletions app/Controllers/Http/RoomController.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,10 +646,10 @@ class RoomController {
timeStepStart.add(30, 'm');
} while (timeframeEnd.diff(end, 'minutes') >= meetingDuration);

Room.FlexibleSearchRoomsByTime({ timeSlots: timeSlots, rooms: rooms, options: options, csrfToken: request.csrfToken, code: code, antl: antl });
Room.FlexibleSearchRoomsByTime({ timeSlots: timeSlots, rooms: rooms, options: options, csrfToken: request.csrfToken, code: code, antl: antl, userId: auth.user.id });
}

return view.render('userPages.flexibleSearchResults', { code: code, roomsLength: rooms.length, timeSlots: timeSlots });
return view.render('userPages.flexibleSearchResults', { code: code, roomsLength: rooms.length, timeSlots: timeSlots, userId: auth.user.id });
}

/**
Expand All @@ -676,12 +676,12 @@ class RoomController {

if (rooms.length) {
// search room availability and push to results page
Room.FixedSearchRoomsBy({ antl: antl, rooms: rooms, options: options, csrfToken: request.csrfToken, code: code, view: view });
Room.FixedSearchRoomsBy({ antl: antl, rooms: rooms, options: options, csrfToken: request.csrfToken, code: code, view: view, userId: auth.user.id });
// load floors
floors = (await Floor.all()).toJSON();
}

return view.render('userPages.fixedSearchResults', { code: code, roomsLength: rooms.length, floors: floors });
return view.render('userPages.fixedSearchResults', { code: code, roomsLength: rooms.length, floors: floors, userId: auth.user.id });
}

async saveSearchRecord ({ userId, type }) {
Expand All @@ -699,7 +699,7 @@ class RoomController {
* @param {view}
*
*/
async currentlyAvailable ({ antl, view, request }) {
async currentlyAvailable ({ antl, view, request, auth }) {
try {
const body = request.all();
// check if user is valid
Expand Down Expand Up @@ -750,7 +750,8 @@ class RoomController {
if (numberOfRooms !== 0 && result) {
Event.fire('send.room', {
card: view.render('components.smallCard', { room: room, datetime: { date: formattedDate, time: formattedFrom + ' - ' + formattedTo } }),
code: code
code: code,
userId: auth.user.id
});
numberOfRooms--;
}
Expand All @@ -760,7 +761,8 @@ class RoomController {
// once 2 rooms are found or searched all rooms then send END sifnal
if (numberOfRooms === 0 || rooms.length === roomsSearched) {
Event.fire('send.done', {
code: code
code: code,
userId: auth.user.id
});
}
});
Expand Down
36 changes: 0 additions & 36 deletions app/Controllers/Http/TokenController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const Env = use('Env');
const Token = use('App/Models/Token');
const JWT = require('jsonwebtoken');
const Event = use('Event');

// The credentials for Microsoft Graph
const credentials = {
Expand Down Expand Up @@ -150,41 +149,6 @@ class TokenController {
httpOnly: true
});
}

async push ({ request, session, response, view }) {
this.show();
console.log(view.render('components.pageHeader', { title: '1' }));
return response.redirect('back');
}

show () {
console.log('hi');
const message = ['1', '2', '3', '4', '5'];
setTimeout(() => {
console.log('yo');
Event.fire('send.message', message[0]);
}, 1000);

setTimeout(() => {
console.log('yo');
Event.fire('send.message', message[1]);
}, 1500);

setTimeout(() => {
console.log('yo');
Event.fire('send.message', message[2]);
}, 2000);

setTimeout(() => {
console.log('yo');
Event.fire('send.message', message[3]);
}, 2500);

setTimeout(() => {
console.log('yo');
Event.fire('send.message', message[4]);
}, 3000);
}
}

module.exports = TokenController;
12 changes: 10 additions & 2 deletions app/Controllers/Http/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,11 @@ class UserController {
user.lastBooking = '';

if (user.bookings.length !== 0) {
const last = user.bookings.reduce((prev, cur) => {
const bookings = user.bookings.filter(booking => {
return booking.from !== null;
});

const last = bookings.reduce((prev, cur) => {
return cur.from > prev.from ? cur : prev;
});

Expand Down Expand Up @@ -388,7 +392,11 @@ class UserController {
user.lastBooking = '';

if (user.bookings.length !== 0) {
const last = user.bookings.reduce((prev, cur) => {
const bookings = user.bookings.filter(booking => {
return booking.from !== null;
});

const last = bookings.reduce((prev, cur) => {
return cur.from > prev.from ? cur : prev;
});

Expand Down
19 changes: 11 additions & 8 deletions app/Models/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Room extends Model {
*
* return 0 if not available
*/
static async FlexibleSearchRoomsByTime ({ rooms, timeSlots, options, csrfToken, code, antl }) {
static async FlexibleSearchRoomsByTime ({ rooms, timeSlots, options, csrfToken, code, antl, userId }) {
try {
// delay to allow page to laod before pushing results
setTimeout(function () {
Expand All @@ -70,15 +70,16 @@ class Room extends Model {
// Asynchronous check with agent if room is available and push room card to results page
rooms.forEach(async room => {
promises.push(
Room.pushAvailableRoomCard({ room: room, options: searchOptions, csrfToken: csrfToken, code: code, antl: antl })
Room.pushAvailableRoomCard({ room: room, options: searchOptions, csrfToken: csrfToken, code: code, antl: antl, userId })
);
});
});

Promise.all(promises).then(values => {
Event.fire('send.done', {
message: 'done',
code: code
code: code,
userId
});
});

Expand All @@ -95,7 +96,7 @@ class Room extends Model {
*
* return 0 if not available
*/
static async FixedSearchRoomsBy ({ rooms, options, csrfToken, code, antl }) {
static async FixedSearchRoomsBy ({ rooms, options, csrfToken, code, antl, userId }) {
try {
// delay to allow page to laod before pushing results
setTimeout(function () {
Expand All @@ -110,15 +111,16 @@ class Room extends Model {
// Asynchronous check with agent if room is available and push room card to results page
rooms.forEach(async room => {
promises.push(
Room.pushAvailableRoomCard({ room: room, options: searchOptions, csrfToken: csrfToken, code: code, antl: antl })
Room.pushAvailableRoomCard({ room: room, options: searchOptions, csrfToken: csrfToken, code: code, antl: antl, userId })
);
});

// once all searchs are done then send done signal to resutls page
Promise.all(promises).then(values => {
Event.fire('send.done', {
message: 'done',
code: code
code: code,
userId
});
});

Expand All @@ -135,7 +137,7 @@ class Room extends Model {
*
* return 0 if not available
*/
static async pushAvailableRoomCard ({ room, options, csrfToken, code, antl }) {
static async pushAvailableRoomCard ({ room, options, csrfToken, code, antl, userId }) {
try {
// check room availability
let roomAvailability = await Outlook.getRoomAvailability({
Expand All @@ -159,7 +161,8 @@ class Room extends Model {
card: card,
floor: room.floor,
startTimeID: options.from.slice(0, 2) + options.from.slice(3, 5),
code: code
code: code,
userId
});
}
return roomAvailability;
Expand Down
2 changes: 1 addition & 1 deletion resources/views/userPages/fixedSearchResults.edge
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
});
//Subscribe to the channel we specified in our Adonis Application
let channel = pusher.subscribe('adonis-channel')
let channel = pusher.subscribe('adonis-channel-' + '{{ userId }}')
//Listen for events on the channel
channel.bind('send-room', function (data) {
Expand Down
2 changes: 1 addition & 1 deletion resources/views/userPages/flexibleSearchResults.edge
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
});
//Subscribe to the channel we specified in our Adonis Application
let channel = pusher.subscribe('adonis-channel')
let channel = pusher.subscribe('adonis-channel-' + '{{ userId }}')
//Listen for events on the channel
channel.bind('send-room', function (data) {
Expand Down
4 changes: 2 additions & 2 deletions resources/views/userPages/userDash.edge
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@
});
//Subscribe to the channel we specified in our Adonis Application
let channel = pusher.subscribe('adonis-channel')
let availableCount=0;
let channel = pusher.subscribe('adonis-channel-' + '{{auth.user.id}}')
let availableCount = 0;
//Listen for events on the channel
channel.bind('send-room', function (data) {
Expand Down
8 changes: 4 additions & 4 deletions start/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ let pusher = new Pusher({
});

Event.when('send.room', async (message) => {
pusher.trigger('adonis-channel', 'send-room', {
pusher.trigger('adonis-channel-' + message.userId, 'send-room', {
message
});
});

Event.when('send.hasResults', async (message) => {
pusher.trigger('adonis-channel', 'send-hasResults', {
pusher.trigger('adonis-channel-' + message.userId, 'send-hasResults', {
message
});
});

Event.when('send.empty', async (message) => {
pusher.trigger('adonis-channel', 'send-empty', {
pusher.trigger('adonis-channel-' + message.userId, 'send-empty', {
message
});
});

Event.when('send.done', async (message) => {
pusher.trigger('adonis-channel', 'send-done', {
pusher.trigger('adonis-channel-' + message.userId, 'send-done', {
message
});
});

0 comments on commit 4b5ea35

Please sign in to comment.