Merge branch 'fix-wrong-unlock-entry-detection' into 'master'
Fix wrong unlock entry detection See merge request saburly/psihologija!76
This commit was merged in pull request #76.
This commit is contained in:
@@ -238,7 +238,25 @@ const getUnlockEntryForReservation = (reservation, previousReservation) => {
|
|||||||
|
|
||||||
const entriesBeforeReservationStart = entries.filter((entry) => moment.utc(entry.timestamp).isBefore(reservation.start));
|
const entriesBeforeReservationStart = entries.filter((entry) => moment.utc(entry.timestamp).isBefore(reservation.start));
|
||||||
|
|
||||||
|
// if (memberId === '5ce785af422bdd00967fb781') {
|
||||||
|
// console.log('Start : ', moment.tz(reservation.start, UI_TIMEZONE).format('DD.MM HH:mm'));
|
||||||
|
// console.log('End : ', moment.tz(reservation.end, UI_TIMEZONE).format('DD.MM HH:mm'));
|
||||||
|
// console.log('\tPrevious reservation ');
|
||||||
|
// console.log('\tStart : ', previousReservation ? moment.tz(previousReservation.start, UI_TIMEZONE).format('DD.MM HH:mm') : '-');
|
||||||
|
// console.log('\tEnd : ', previousReservation ? moment.tz(previousReservation.end, UI_TIMEZONE).format('DD.MM HH:mm') : '-');
|
||||||
|
// console.log('\t---------------------------');
|
||||||
|
// console.log('\tSearch for entries : ');
|
||||||
|
// console.log('\tFrom : ', fromTimestamp ? moment.tz(fromTimestamp, UI_TIMEZONE).format('DD.MM HH:mm') : '-');
|
||||||
|
// console.log('\tTo : ', toTimestamp ? moment.tz(toTimestamp, UI_TIMEZONE).format('DD.MM HH:mm') : '-');
|
||||||
|
// console.log('\t---------------------------');
|
||||||
|
// console.log('\tEntries before reservation start : ');
|
||||||
|
// }
|
||||||
|
|
||||||
entriesBeforeReservationStart.forEach((entry) => {
|
entriesBeforeReservationStart.forEach((entry) => {
|
||||||
|
// if (memberId === '5ce785af422bdd00967fb781') {
|
||||||
|
// console.log('\t', entry.event, '\t', moment.tz(entry.timestamp, UI_TIMEZONE).format('DD.MM HH:mm'));
|
||||||
|
// }
|
||||||
|
|
||||||
if (!eventFound) {
|
if (!eventFound) {
|
||||||
if (entry.event === doorLockEvents.USER_UNLOCKED) {
|
if (entry.event === doorLockEvents.USER_UNLOCKED) {
|
||||||
if (pairedLockEntry) {
|
if (pairedLockEntry) {
|
||||||
@@ -256,13 +274,25 @@ const getUnlockEntryForReservation = (reservation, previousReservation) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (eventFound){
|
if (eventFound){
|
||||||
|
// if (memberId === '5ce785af422bdd00967fb781') {
|
||||||
|
// console.log('\t=> FOUND UNLOCK ENTRY - NO NEED TO LOOK AFTER <=');
|
||||||
|
// }
|
||||||
resolve(candidateUnlockEntry);
|
resolve(candidateUnlockEntry);
|
||||||
} else {
|
} else {
|
||||||
candidateUnlockEntry = null;
|
candidateUnlockEntry = null;
|
||||||
const numberOfEntriesLeft = entries.length - entriesBeforeReservationStart.length;
|
const numberOfEntriesLeft = entries.length - entriesBeforeReservationStart.length;
|
||||||
const entriesAfterReservationStart = entries.slice(0, numberOfEntriesLeft);
|
const entriesAfterReservationStart = entries.slice(0, numberOfEntriesLeft);
|
||||||
|
const invertedEntriesAfterReservationStart = entriesAfterReservationStart.reverse();
|
||||||
|
|
||||||
|
// if (memberId === '5ce785af422bdd00967fb781') {
|
||||||
|
// console.log('\t-----------------------------');
|
||||||
|
// console.log('\tEntries after reservation start : ');
|
||||||
|
// }
|
||||||
|
invertedEntriesAfterReservationStart.forEach((entry) => {
|
||||||
|
// if (memberId === '5ce785af422bdd00967fb781') {
|
||||||
|
// console.log('\t', entry.event, '\t', moment.tz(entry.timestamp, UI_TIMEZONE).format('DD.MM HH:mm'));
|
||||||
|
// }
|
||||||
|
|
||||||
entriesAfterReservationStart.forEach((entry) => {
|
|
||||||
if (!eventFound) {
|
if (!eventFound) {
|
||||||
if (entry.event === doorLockEvents.USER_UNLOCKED) {
|
if (entry.event === doorLockEvents.USER_UNLOCKED) {
|
||||||
eventFound = true;
|
eventFound = true;
|
||||||
@@ -303,7 +333,7 @@ const getLockEntryForReservation = (reservation, nextReservation) => {
|
|||||||
resourceId,
|
resourceId,
|
||||||
};
|
};
|
||||||
|
|
||||||
const order = [['timestamp', 'ASC']];
|
const order = [['timestamp', 'DESC']];
|
||||||
|
|
||||||
db.doorLockEvent.findAll({
|
db.doorLockEvent.findAll({
|
||||||
attributes,
|
attributes,
|
||||||
@@ -311,47 +341,31 @@ const getLockEntryForReservation = (reservation, nextReservation) => {
|
|||||||
order,
|
order,
|
||||||
})
|
})
|
||||||
.then((entries) => {
|
.then((entries) => {
|
||||||
let candidateLockEntry = null;
|
|
||||||
let pairedUnlockEntry = null;
|
|
||||||
let eventFound = false;
|
|
||||||
|
|
||||||
const entriesAfterReservationEnd = entries.filter((entry) => moment.utc(entry.timestamp).isAfter(reservation.end));
|
const entriesBeforeReservationEnd = entries.filter(entry => moment.utc(entry.timestamp).isBefore(reservation.end));
|
||||||
|
|
||||||
entriesAfterReservationEnd.forEach((entry) => {
|
if (entriesBeforeReservationEnd.length > 0){
|
||||||
if (!eventFound) {
|
const lastEntryInReservationTime = entriesBeforeReservationEnd[0];
|
||||||
if (entry.event === doorLockEvents.USER_LOCKED) {
|
if (lastEntryInReservationTime.event === doorLockEvents.USER_LOCKED){
|
||||||
if (pairedUnlockEntry) {
|
resolve(lastEntryInReservationTime);
|
||||||
pairedUnlockEntry = null;
|
return;
|
||||||
candidateLockEntry = null;
|
|
||||||
} else {
|
|
||||||
candidateLockEntry = entry;
|
|
||||||
eventFound = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (entry.event === doorLockEvents.USER_UNLOCKED){
|
|
||||||
pairedUnlockEntry = entry;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
if (eventFound){
|
|
||||||
resolve(candidateLockEntry);
|
|
||||||
} else {
|
|
||||||
candidateLockEntry = null;
|
|
||||||
const numberOfEntriesLeft = entries.length - entriesAfterReservationEnd.length;
|
|
||||||
const entriesBeforeReservationEnd = entries.slice(0, numberOfEntriesLeft);
|
|
||||||
|
|
||||||
entriesBeforeReservationEnd.reverse().forEach((entry) => {
|
|
||||||
if (!eventFound) {
|
|
||||||
if (entry.event === doorLockEvents.USER_LOCKED) {
|
|
||||||
eventFound = true;
|
|
||||||
candidateLockEntry = entry;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
resolve(candidateLockEntry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Phase 2
|
||||||
|
const numberOfEntriesLeft = entries.length - entriesBeforeReservationEnd.length;
|
||||||
|
const entriesAfterReservationEnd = entries.slice(0, numberOfEntriesLeft).reverse();
|
||||||
|
if (entriesAfterReservationEnd.length > 0){
|
||||||
|
const firstEntryAfterReservation = entriesAfterReservationEnd[0];
|
||||||
|
if (firstEntryAfterReservation.event === doorLockEvents.USER_LOCKED){
|
||||||
|
resolve(firstEntryAfterReservation);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(null);
|
||||||
})
|
})
|
||||||
.catch((error) => reject(error));
|
.catch((error) => reject(error));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user