Fix bugs with dor lock charges

This commit is contained in:
Senad Uka
2019-07-03 18:07:22 +02:00
parent 1d9d5ac684
commit 2fcb10522c
3 changed files with 234 additions and 97 deletions

View File

@@ -18,6 +18,7 @@ const {
const { fetchAllMembers } = require('../officeRnD/members');
const { getMappingsFromDatabase } = require('../officeRnD/resources');
const { getFirstReservationInBlock } = require('../officeRnD/bookings');
const extractMappingFromFileName = (fileName) => {
const contentBetweenBracketsRegex = /\[(.*?)\]/;
@@ -189,10 +190,16 @@ const getUnlockEntryForReservation = (reservation, previousReservation) => {
const attributes = ['memberName', 'event', 'timestamp', 'resourceId'];
const fromTimestamp = previousReservation && previousReservation.end ?
previousReservation.end : moment.utc(reservation.start).tz(UI_TIMEZONE).startOf('Day').toISOString();
const previousReservationEndMoment = previousReservation && previousReservation.end ?
moment.utc(previousReservation.end) : null;
const reservationStartMoment = moment.utc(reservation.start);
const fromTimestamp = previousReservationEndMoment && previousReservationEndMoment.tz(UI_TIMEZONE).isSame(reservationStartMoment.tz(UI_TIMEZONE), 'day') ?
previousReservation.end : reservationStartMoment.tz(UI_TIMEZONE).startOf('day').toISOString();
const toTimestamp = reservation.end;
const filters = {
memberId,
timestamp: {
@@ -216,21 +223,8 @@ const getUnlockEntryForReservation = (reservation, previousReservation) => {
let pairedLockEntry = null;
let eventFound = false;
// console.log('\r\n=?== SEARCH UNLOCK ==');
// console.log('ReservatioN : ', reservation.start);
const entriesBeforeReservationStart = entries.filter((entry) => moment.utc(entry.timestamp).isBefore(reservation.start));
// console.log('After end : ');
/*console.log(entriesBeforeReservationStart.map(e => {
return {
timestamp: e.timestamp,
event: e.event,
}
}));
*/
entriesBeforeReservationStart.forEach((entry) => {
if (!eventFound) {
if (entry.event === doorLockEvents.USER_UNLOCKED) {
@@ -249,35 +243,21 @@ const getUnlockEntryForReservation = (reservation, previousReservation) => {
});
if (eventFound){
// console.log('FOUND : ', candidateUnlockEntry.timestamp, candidateUnlockEntry.event);
resolve(candidateUnlockEntry);
} else {
// console.log('NOT FOUND IN FIRST ROUND');
candidateUnlockEntry = null;
const numberOfEntriesLeft = entries.length - entriesBeforeReservationStart.length;
const entriesAfterReservationStart = entries.slice(0, numberOfEntriesLeft);
// console.log('Entries in reservation time : ');
/*console.log(entriesAfterReservationStart.map(e => {
return {
timestamp: e.timestamp,
event: e.event,
}
}));
*/
entriesAfterReservationStart.forEach((entry) => {
if (!eventFound) {
if (entry.event === doorLockEvents.USER_UNLOCKED) {
//console.log('FOUND : ', entry.timestamp, entry.event);
eventFound = true;
candidateUnlockEntry = entry;
}
}
});
//console.log('===??==');
resolve(candidateUnlockEntry);
}
})
@@ -291,9 +271,13 @@ const getLockEntryForReservation = (reservation, nextReservation) => {
const attributes = ['memberName', 'event', 'timestamp'];
const nextReservationStartMoment = nextReservation && nextReservation.start ?
moment.utc(nextReservation.start) : null;
const reservationStartMoment = moment.utc(reservation.start);
const fromTimestamp = reservation.start;
const toTimestamp = nextReservation && nextReservation.start ?
nextReservation.start : moment.utc(reservation.start).tz(UI_TIMEZONE).endOf('Day').toISOString();
const toTimestamp = nextReservationStartMoment && nextReservationStartMoment.tz(UI_TIMEZONE).isSame(reservationStartMoment.tz(UI_TIMEZONE), 'day') ?
nextReservation.start : reservationStartMoment.tz(UI_TIMEZONE).endOf('day').toISOString();
const filters = {
memberId,
@@ -360,9 +344,78 @@ const getLockEntryForReservation = (reservation, nextReservation) => {
});
};
const getEntriesBetween = (fromTimestamp, toTimestamp, resourceId) => {
return new Promise((resolve, reject) => {
if (!fromTimestamp || !toTimestamp || !resourceId){
resolve([]);
}else {
const andTimestampFilters = [];
if (fromTimestamp){
andTimestampFilters.push({[Op.gt]: fromTimestamp});
}
if (toTimestamp){
andTimestampFilters.push({[Op.lt]: toTimestamp});
}
const filters = {
resourceId,
timestamp: {
[Op.and]: andTimestampFilters,
},
};
const order = [['timestamp', 'ASC']];
db.doorLockEvent.findAll({where: filters, order})
.then((results) => resolve(results))
.catch((error) => reject(error));
}
});
};
const getLastEntryForReservation = (reservation) => {
return new Promise ((resolve, reject) => {
getFirstReservationInBlock(reservation)
.then((firstReservationInBlock) => {
const { memberId, resourceId } = reservation;
let fromTimestamp = reservation.start;
const toTimestamp = reservation.end;
if (firstReservationInBlock){
fromTimestamp = firstReservationInBlock.start;
}
const filters = {
memberId,
resourceId,
timestamp: {
[Op.and]: [
{[Op.gte]: fromTimestamp},
{[Op.lte]: toTimestamp}
]
},
};
const order = [['timestamp', 'DESC']];
db.doorLockEvent.findAll({where: filters, order})
.then((entries) => {
if (entries && entries.length > 0){
resolve(entries[0]);
} else {
resolve (undefined);
}
})
.catch((error) => reject(error));
})
.catch((error) => reject(error));
});
};
module.exports = {
parseDoorLockDataFile,
writeDoorLockEvent,
getUnlockEntryForReservation,
getLockEntryForReservation,
getEntriesBetween,
getLastEntryForReservation,
};