Ok now i have the folowing situations:
In the form i will add latitude and longitude and also a distance in metters to get the radios of the place.
With that information when the user does the chekin i need to chek not if he answered correctly the covid form but if he is on the right spot of the place associated with his account, as well the correct time added in the Turns database.
And for last but not least i need to add a function that he can chekout for lunch and comebask in the right time also specified in the Turn database associated with the place.
SO i wont need the seats taken and everything else.
The code now for the chekin process is as follows.
var checkinRequiresBooking = false;
dynamicOfficeandSpace();
logUserEmailInForm(‘Email’);
//Show loading icon for barcode scanning
function userFeedBack(status) {
if (status === true) {
if ($(’.circle-loader’).hasClass(‘failmark’)) {
$(’.circle-loader’).toggleClass(‘failmark’);
$(’.circle-loader’).toggleClass(‘checkmark’);
$(’.loader-message’).hide();
} else {
$(’.circle-loader’).toggleClass(‘checkmark load-complete’);
$(’.circle-loder > div’).toggle();
$(’.loader-message’).fadeOut();
$(’.success, .retry’).fadeIn();
}
} else {
if ($(’.circle-loader’).hasClass(‘checkmark’)) {
$(’.circle-loader’).toggleClass(‘checkmark’);
$(’.circle-loader’).toggleClass(‘failmark’);
$(’.loader-message’).hide();
} else {
$(’.circle-loader’).toggleClass(‘failmark load-complete’);
$(’.circle-loder > div’).toggle();
$(’.loader-message’).fadeOut();
$(’.success, .retry’).faceIn();
}
}
}
function getUrlParameter(name, url) {
name = name.replace(/[[]/, ‘\[’).replace(/[]]/, ‘\]’);
var regex = new RegExp(’[\?&]’ + name + ‘=([^&#]*)’);
var results = regex.exec(url);
return results === null ? ‘’ : decodeURIComponent(results[1].replace(/+/g, ’ '));
}
/display check/
function success() {
userFeedBack(true);
}
/display cross/
function failure() {
userFeedBack(false);
}
//get current capacity
function getCurrentCapacity(officeName, spaceName, requestedNumber, date) {
var checkInsQuery = {};
var checkedInTotal = 0;
var tommorowDate = moment(date)
.add(1, ‘days’)
.format(‘YYYY-MM-DD’);
checkInsQuery[‘OfficeSpaceName’] = spaceName;
checkInsQuery[‘OfficeName’] = officeName;
checkInsQuery[‘CheckedIn’] = { $and: [{ $gte: date }, { $lt: tommorowDate }] };
return Fliplet.DataSources.connectByName(‘Return to Office - Checkins’)
.then(function(connection) {
return connection.find({
where: checkInsQuery
});
})
.then(function(records) {
_.forEach(records, function(record) {
if (!record.data.CheckedOut) {
checkedInTotal = checkedInTotal + 1;
}
})
var spacesQuery = {};
spacesQuery[‘Name’] = spaceName;
return Fliplet.DataSources.connectByName(‘Return to Office - OfficeSpaces’)
.then(function(connection) {
return connection.find({
where: spacesQuery
});
})
.then(function(records) {
if (parseInt(records[0].data.Capacity) - parseInt(checkedInTotal) > 0) {
return ‘Capacity available’;
} else {
return ‘No Capacity’;
}
});
});
}
function sendOMEmail(officeName, officeSpace, email) {
Fliplet.DataSources.connectByName(‘Return to Office - Offices’).then(function(connection) {
connection
.find({
attributes: [‘ManagerEmail’],
where: {
Name: { $eq: officeName }
}
})
.then(function(records) {
var emailsArr = records[0].data[‘ManagerEmail’].split(’,’);
var emailToObj = {};
var emailToArr = [];
_.forEach(emailsArr, function(user) {
emailToObj['email'] = user;
emailToArr.push(emailToObj);
});
var body =
'<p>A check in occured at a full capacity space</p>' +
'<ul>' +
'<li>Checked in user: ' +
email +
'</li>' +
'<li>Office: ' +
officeName +
'</li>' +
'<li>Space: ' +
officeSpace +
' </li>' +
'</ul>' +
'<p>To view this check in visit ' +
Fliplet.Env.get('appName') +
'</p>' +
'<p>Reply to this email if you want to contact the person directly.</p>';
var options = {
to: emailToArr,
html: body,
subject: 'Check In for ' + officeName + ' : ' + officeSpace,
headers: {
'Reply-To': email
}
};
// Returns a promise
Fliplet.Communicate.sendEmail(options);
});
});
}
//get location name from query paramter in QR code
var insertID;
function checkIn(officeName, spaceName, userEmail) {
//$(’.test’).text(officeName, spaceName);
return Fliplet.DataSources.connectByName(‘Return to Office - Checkins’, {
offline: false // disable querying offline on mobile devices
})
.then(function(connection) {
return connection.insert({
Email: userEmail,
OfficeName: officeName,
OfficeSpaceName: spaceName,
CheckedIn: moment().toISOString()
});
})
.then(function(records) {
insertID = records.id;
success();
$(’.success-message’).text('You have been sucessfully checked into ’ + officeName + ’ office, ’ + spaceName);
});
}
function checkoutOfCurrentCheckin(email) {
return Fliplet.DataSources.connectByName(‘Return to Office - Checkins’).then(function(
connection
) {
return connection
.find({
where: {
Email: email
}
})
.then(function(records) {
var records = _.remove(records, function(n) {
if (!n.data.CheckedOut) {
return n;
}
});
console.log(records);
if (records.length > 0) {
return Fliplet.DataSources.connectByName(‘Return to Office - Checkins’).then(function(
connection
) {
return connection.update(records[0].id, {
CheckedOut: moment().toISOString()
});
});
}
});
});
}
function checkIfBookingExists(email, office, space) {
return Fliplet.DataSources.connectByName(‘Return to Office - OfficeBookings’).then(function(
connection
) {
return connection
.find({
where: {
Email: email,
Date: moment().format(‘YYYY-MM-DD’),
Office: office,
Space: space
}
})
.then(function(records) {
if (records.length > 0) {
return true;
} else {
return false;
}
});
});
}
//open barcode scanner
function openScanner(userEmail) {
var options = {
preferFrontCamera: false, // iOS and Android
showFlipCameraButton: true, // iOS and Android
showTorchButton: true, // iOS and Android
torchOn: false, // Android, launch with the torch switched on (if available)
saveHistory: true, // Android, save scan history (default false)
prompt: ‘Place a barcode inside the scan area’ // Android
};
return Fliplet.Barcode.scan(options)
.then(function(result) {
//$(’.test’).text(JSON.stringify(result));
var officeName = getUrlParameter(‘officeName’, result.text);
var spaceName = getUrlParameter(‘officespaceName’, result.text);
if (getUrlParameter(‘action’, result.text) === ‘checkin’) {
var bookingPromise;
if (checkinRequiresBooking) {
bookingPromise = checkIfBookingExists(userEmail, officeName, spaceName);
} else {
bookingPromise = Promise.resolve(true);
}
bookingPromise.then(function(bookingstatus) {
if (!bookingstatus) {
var options = {
type: ‘regular’,
tapToDismiss: true,
duration: false,
message: ‘Please make a booking for this space before checking in’
};
Fliplet.UI.Toast(options);
failure();
return;
}
$(’.test’).text(JSON.stringify(bookingstatus));
checkoutOfCurrentCheckin(userEmail).then(function() {
getCurrentCapacity(officeName, spaceName, 1, moment().format(‘YYYY-MM-DD’)).then(
function(status) {
// $(’.test’).text(status);
if (status === ‘Capacity available’) {
checkIn(officeName, spaceName, userEmail);
} else {
var options = {
title: ‘Space Full’,
message: ‘This Space is full. Are you sure you want to check in?’,
labels: [‘Agree’, ‘No’] // Native only (defaults to [OK,Cancel])
};
Fliplet.Navigate.confirm(options).then(function(result) {
if (!result) {
return console.log('Not confirmed!');
}
checkIn(officeName, spaceName, userEmail);
sendOMEmail(officeName, spaceName, userEmail);
});
}
}
);
});
});
} else if (getUrlParameter('action', result.text) === 'checkout') {
return Fliplet.DataSources.connectByName('Return to Office - Checkins').then(function(
connection
) {
return connection
.find({
where: {
Email: userEmail,
OfficeName: getUrlParameter('officeName', result.text),
OfficeSpaceName: getUrlParameter('officespaceName', result.text),
CheckedIn: { $gte: moment().format('YYYY-MM-DD') }
}
})
.then(function(records) {
$('.test').text(JSON.stringify(result.text));
var records = _.remove(records, function(n) {
if (!n.data.CheckedOut) {
return n;
}
});
return Fliplet.DataSources.connectByName('Return to Office - Checkins')
.then(function(connection) {
connection.update(records[0].id, {
CheckedOut: moment().toISOString()
});
})
.then(function() {
success();
$('.success-message').text(
'You have been successfully checked out of ' + officeName + ' , ' + spaceName
);
$('.optional-form').hide();
});
});
});
}
})
.catch(function(error) {
failure();
});
}
var loggedInEmail;
$(’.scanAgain’).click(function() {
openScanner(loggedInEmail);
});
Fliplet.User.getCachedSession().then(function(session) {
var user;
if (session.entries.dataSource) {
user = _.get(session, ‘entries.dataSource.data’);
if (!user) {
return; // user is not logged in
}
loggedInEmail = user.Email;
}
// contains all columns found on the connected dataSource entry
if (Fliplet.Env.get(‘platform’) === ‘web’) {
$(’.circle-loader’).hide();
$(’.loader-message’).hide();
} else {
var isOnline = Fliplet.Navigator.isOnline();
if (isOnline) {
// device is online
openScanner(loggedInEmail);
} else {
// device is offline
$('.loader-message').text('Device Offline');
setTimeout(function() {
failure();
}, 3000);
//failure();
}
}
});
//change to datasource Name with the checkin/checkout data will be stored.
var datasourceName = ‘Return to Office - Checkins’;
//Change to the column where the name of the office will be stored in the above datasource
var officeNameColumn = ‘OfficeName’;
//Change to the column where the name of the space will be stored in the above datasource
var spaceNameColumn = ‘OfficeSpaceName’;
function getData(userEmail, officeName, spaceName) {
var insert = {};
insert[‘Email’] = userEmail;
insert[officeNameColumn] = officeName;
insert[spaceNameColumn] = spaceName;
insert[‘CheckedIn’] = moment().toISOString();
return Fliplet.DataSources.connectByName(datasourceName).then(function(connection) {
return connection.insert(insert);
});
}
Fliplet.Hooks.on(‘beforeFormSubmit’, function(data) {
if (data.Office) {
getData(data.Email, data.Office, data.Space);
}
if (data[‘Number of guests with you (optional)’]) {
return Fliplet.DataSources.connectByName(‘Return to Office - Checkins’).then(function(
connection
) {
connection.update(insertID, {
Guests: data[‘Number of guests with you (optional)’]
});
});
}
});