Calculate door lock charges

This commit is contained in:
Senad Uka
2019-06-14 17:41:09 +02:00
parent 8e4eb0cf1f
commit 393e9b8aec
37 changed files with 1736 additions and 66 deletions

View File

@@ -0,0 +1,230 @@
'use strict';
const db = require('../../models');
const fs = require('fs');
const csv = require('csv-parser');
const moment = require('moment/moment');
const Op = require('sequelize').Op;
const {
USER_ENTRY_EVENT,
ENABLE_PASSAGE_MODE,
DISABLE_PASSAGE_MODE,
VALID_CSV_HEADERS,
doorLockEvents,
csvParserErrors,
} = require('../../constants/constants');
const { fetchAllMembers, findMember } = require('../officeRnD/members');
const { getMappingsFromDatabase } = require('../officeRnD/resources');
const extractMappingFromFileName = (fileName) => {
const contentBetweenBracketsRegex = /\[(.*?)\]/;
const rawContent = fileName.match(contentBetweenBracketsRegex)[1];
const mappingContent = rawContent.split('-').map(word => word.trim());
return {
officeSlug: mappingContent[0],
resourceSlug: mappingContent[1],
}
};
const checkIfMappingExsists = (mappingFromFileName, mappings) => {
const { officeSlug, resourceSlug } = mappingFromFileName;
return mappings.find(mapping => (mapping.officeSlug === officeSlug) && (mapping.resourceSlug === resourceSlug));
};
const parseDoorLockDataFile = (file) => {
return new Promise ((resolve, reject) => {
const results = [];
const errors = [];
const unknownMembers = [];
let isValidFile = true;
const prefetchDataJobs = [getMappingsFromDatabase(), fetchAllMembers()];
Promise.all(prefetchDataJobs)
.then(result => {
const mappings = result[0];
const mappingFromFileName = extractMappingFromFileName(file.name);
const mappingObject = checkIfMappingExsists(mappingFromFileName, mappings);
if (!mappingObject){
reject('Error ! File contains unknown location');
return;
}
fs.createReadStream(file.path)
.pipe(csv({
mapHeaders: ({ header, index }) => header.trim().toLowerCase(),
mapValues: ({ header, index, value }) => value.trim()
}))
.on('headers', (headers) => {
const sortedValidHeadersArray = VALID_CSV_HEADERS.concat().sort();
const sortedParsedHeadersArray = headers.map(header => header.trim()).sort();
let validHeaders = true;
if (sortedParsedHeadersArray.length !== sortedValidHeadersArray.length) {
validHeaders = false;
}else {
for (let i = 0; i < sortedValidHeadersArray.length; i++){
validHeaders = validHeaders
&& (sortedValidHeadersArray[i] === sortedParsedHeadersArray[i]);
}
}
if (!validHeaders){
isValidFile = false;
errors.push({
error: csvParserErrors.INVALID_HEADERS,
details: `Expected headers : ${JSON.stringify(VALID_CSV_HEADERS)}`,
file: file.name,
});
}
})
.on('data', (data) => {
if (!isValidFile) {
return;
}
const eventType = data.event.trim();
if ([USER_ENTRY_EVENT, ENABLE_PASSAGE_MODE, DISABLE_PASSAGE_MODE].includes(eventType)){
results.push(data);
}
})
.on('end', () => {
const parsedData = [];
let i = 0;
while (i < results.length){
//Verify pair
//First entry type should be user entry and second should be enable / disable passage
const firstEntry = results[i];
const secondEntry = results[i+1];
if (firstEntry && (firstEntry.event === USER_ENTRY_EVENT)){
const memberObject = findMember(firstEntry.name);
if (!memberObject){
//Check if member is already labeled as unknown
const unknownMember = unknownMembers.find((member) => member.details === firstEntry.name);
if (!unknownMember){
unknownMembers.push({
error: csvParserErrors.UNKNOWN_MEMBER,
details: firstEntry.name,
file: file.name,
});
}
}
if (secondEntry && (secondEntry.event === ENABLE_PASSAGE_MODE || secondEntry.event === DISABLE_PASSAGE_MODE)){
const event = (secondEntry.event === ENABLE_PASSAGE_MODE) ?
doorLockEvents.USER_UNLOCKED : doorLockEvents.USER_LOCKED;
const dateTimeString = `${firstEntry.date} ${firstEntry.time}`;
const timestamp = moment.utc(dateTimeString, 'MM/DD/YY HH:mm:ss A').toISOString();
//Verify that member is registered in OfficeRnD system
if (memberObject){
const entryData = {
memberName: firstEntry.name,
memberNumber: firstEntry['user no'],
memberId: memberObject.memberId,
timestamp,
event,
resourceId: mappingObject.resourceId,
};
parsedData.push(entryData);
}
i+=2;
} else {
errors.push({
error: csvParserErrors.INVALID_ENTRY_EXPECTED_PASSAGE_MODE,
details: firstEntry,
file: file.name,
});
i+=1;
}
} else {
errors.push({
error: csvParserErrors.INVALID_ENTRY_EXPECTED_USER,
details: firstEntry,
file: file.name,
});
i+=1;
}
}
resolve({
parsedData,
unknownMembers,
errors
});
});
})
.catch(error => {
reject(error);
});
});
};
const writeDoorLockEvent = (entry) => {
return db.doorLockEvent.findOrCreate({where: {...entry}, defaults: {...entry}});
};
const getUnlockEntryForReservation = (reservation) => {
const { start, end, memberId, resourceId } = reservation;
const attributes = ['memberName', 'event', 'timestamp'];
const earliestUnlock = parseInt(process.env.EARLIEST_UNLOCK) || 0;
const fromTimestamp = moment(start).subtract(earliestUnlock).toISOString();
const toTimestamp = end;
const filters = {
memberId,
timestamp: {
[Op.and]: [
{[Op.gt]: fromTimestamp},
{[Op.lte]: toTimestamp}
]
},
event: doorLockEvents.USER_UNLOCKED,
resourceId,
};
return db.doorLockEvent.findOne({
attributes,
where: filters,
})
};
const getRelatedDoorLockEntries = (fromTimestamp, toTimestamp, memberId, resourceId) => {
const attributes = ['memberName', 'event', 'timestamp'];
const filters = {
memberId,
timestamp: {
[Op.and]: [
{[Op.gt]: fromTimestamp},
{[Op.lte]: toTimestamp}
]
},
event: doorLockEvents.USER_LOCKED,
resourceId,
};
return db.doorLockEvent.findOne({
attributes,
where: filters
})
};
module.exports = {
parseDoorLockDataFile,
writeDoorLockEvent,
getRelatedDoorLockEntries,
getUnlockEntryForReservation,
};

View File

@@ -0,0 +1,502 @@
'use strict';
const moment = require('moment-timezone');
const db = require('../../models/index');
const { incidentType, unlockedIncidentLevelsPrices } = require('../../constants/constants');
const { getUnlockEntryForReservation, getRelatedDoorLockEntries } = require('../doorLock/doorLock');
const { getFirstPreviousBooking, getFirstNextBooking, getAllFinishedBookings } = require('../officeRnD/bookings');
const getSortedIncidentsForMember = (memberId) => {
const attributes = ['bookingStart', 'incidentLevel', 'incidentLevelPrice'];
const filters = {
memberId
};
const order = [['bookingStart', 'DESC']];
return db.unlockedIncident.findAll({
attributes,
where: filters,
order,
})
};
const createUnlockedIncident = (reservation) => {
return new Promise((resolve, reject) => {
const { reservationId, memberId, resourceId, start, end } = reservation;
getLastIncidentForMember(memberId)
.then(incidents => {
const lastIncident = incidents && incidents[0] ? incidents[0] : undefined;
const incident = {
reservationId,
memberId,
resourceId,
bookingStart: start,
bookingEnd: end,
incidentLevel: null,
incidentLevelPrice: null,
};
console.log('=> UNLOCKED INCIDENT');
console.log('\tMember : ', memberId);
console.log('\tStart : ', start);
console.log('\tEnd : ', end);
console.log('\tMore details : ');
/*
if (lastIncident){
const lastIncidentLevel = lastIncident.incidentLevel;
const lastIncidentBeginningOfTheMonth = moment(lastIncident.bookingStart).startOf('month');
const beginningOfTheMonth = moment.utc().startOf('month');
const timePassedFromLastIncident = Math.abs(beginningOfTheMonth.diff(lastIncidentBeginningOfTheMonth, 'months'));
if (timePassedFromLastIncident >= 6){
console.log('\t\t-> This is first incident for this member in last 6 months');
incident.incidentLevel = unlockedIncidentLevelsPrices.UNLOCKED_0.title;
incident.incidentLevelPrice = unlockedIncidentLevelsPrices.UNLOCKED_0.price;
} else {
console.log('\t\t-> This member had incident(s) in past 6 months !!!');
incident.incidentLevel = lastIncidentLevel;
incident.incidentLevelPrice = unlockedIncidentLevelsPrices[lastIncidentLevel].price;
}
console.log('\t\tLast incident details : ');
console.log('\t\tStart : ', lastIncident.bookingStart);
console.log('\t\tCalculated diff : ', timePassedFromLastIncident);
console.log('\t\t------------------');
console.log('\tNew incident level : ', incident.incidentLevel);
} else {
console.log('\t\tThis is first incident for this member, EVER !');
incident.incidentLevel = unlockedIncidentLevelsPrices.UNLOCKED_0.title;
incident.incidentLevelPrice = unlockedIncidentLevelsPrices.UNLOCKED_0.price;
}
*/
db.unlockedIncident.findOrCreate({
where: {
reservationId,
memberId,
resourceId,
bookingStart: start,
bookingEnd: end,
},
defaults: {
...incident
}
})
.then(()=>resolve())
.catch((error)=>reject(error));
})
.catch((error) => {
reject(error);
});
});
};
const createUnscheduledUseIncident = (reservation, doorLockEntry) => {
return new Promise((resolve, reject) => {
const timeResolution = parseInt(process.env.UNSCHEDULED_USE_TIME_RESOLUTION);
const chargePrice = parseFloat(process.env.UNSCHEDULED_USE_CHARGE_FEE);
const reservationEndTime = moment(reservation.end);
const lockedTime = moment(doorLockEntry.timestamp);
const timeDifference = Math.abs(reservationEndTime.diff(lockedTime, 'minutes'));
const timeIntervalsToCharge = Math.floor(timeDifference / timeResolution);
const totalChargeFee = timeIntervalsToCharge * chargePrice;
if (timeIntervalsToCharge > 0){
const incident = {
reservationId: reservation.reservationId,
memberId: reservation.memberId,
resourceId: reservation.resourceId,
bookingStart: reservation.start,
bookingEnd: reservation.end,
doorLockEventTimestamp: doorLockEntry.timestamp,
doorLockEventType: doorLockEntry.event,
chargePrice,
timeIntervalsToCharge,
totalChargeFee,
};
db.unscheduledIncident.findOrCreate({where: {...incident}, defaults: {...incident}})
.then(()=>resolve())
.catch((error)=>reject(error));
}else{
resolve();
}
});
};
const createDoorLockIncident = (reservation, doorLockEntry) => {
return new Promise((resolve, reject) => {
if (!doorLockEntry){
// Check if there is unlock entry for this reservation
getUnlockEntryForReservation(reservation)
.then((unlockEntry) => {
if (!unlockEntry){
// check if there is back-to-back booking before current one
getFirstPreviousBooking(reservation)
.then((previousReservation) => {
if (previousReservation){
const previousReservationEnd = moment(previousReservation.end);
const currentReservationStart = moment(reservation.start);
const timeDifference = Math.abs(currentReservationStart.diff(previousReservationEnd, 'minutes'));
const maxBackToBackDifference = parseInt(process.env.MAX_BACK_TO_BACK_DIFFERENCE) || 0;
if (timeDifference <= maxBackToBackDifference) {
createUnlockedIncident(reservation)
.then(() => resolve())
.catch((error) => reject(error));
}else{
resolve();
}
}else{
resolve();
}
})
.catch((error)=>reject(error));
}else {
createUnlockedIncident(reservation)
.then(()=>resolve())
.catch((error)=>reject(error));
}
})
.catch((error) => {
reject(error);
});
}else{
createUnscheduledUseIncident(reservation, doorLockEntry)
.then(()=>resolve())
.catch((error) => reject(error));
}
});
};
const insertUnscheduledIncidents = (incidents) => {
const asyncJobs = [];
incidents.forEach((incident) => {
const { reservation, lockEntry, chargePrice, timeIntervalsToCharge, totalChargeFee } = incident;
const { reservationId, memberId, resourceId, start, end } = reservation;
const { timestamp, event } = lockEntry;
const incidentForDB = {
reservationId,
memberId,
resourceId,
bookingStart: start,
bookingEnd: end,
doorLockEventTimestamp: timestamp,
doorLockEventType: event,
chargePrice,
timeIntervalsToCharge,
totalChargeFee,
};
asyncJobs.push(db.unscheduledIncident.findOrCreate({
where: {
reservationId,
memberId,
resourceId,
bookingStart: start,
bookingEnd: end,
doorLockEventTimestamp: timestamp,
doorLockEventType: event
},
defaults: {...incidentForDB},
}));
});
return Promise.all(asyncJobs);
};
const insertUnlockedIncidents = (incidents) => {
const asyncJobs = [];
incidents.forEach((incident) => {
const { reservationId, memberId, resourceId, bookingStart, bookingEnd } = incident;
asyncJobs.push(db.unlockedIncident.findOrCreate({
where: {
reservationId,
memberId,
resourceId,
bookingStart,
bookingEnd,
},
defaults: {...incident},
}));
});
return Promise.all(asyncJobs);
};
const setUnlockedIncidentsLevel = (incidentReservations) => {
return new Promise ((resolve, reject) => {
const sortingFunction = (reservationA, reservationB) => {
const sortCondition = moment.utc(reservationA.start).isBefore(moment.utc(reservationB.start));
return sortCondition ? -1 : 1;
};
incidentReservations.sort(sortingFunction);
const membersLastIncident = {};
incidentReservations.forEach((reservation) => {
membersLastIncident[reservation.memberId] = {
incidentLevel: null,
incidentTimestamp: null,
};
});
const asyncJobs = [];
Object.keys(membersLastIncident).forEach((memberId) => {
asyncJobs.push(getSortedIncidentsForMember(memberId));
});
Promise.all(asyncJobs)
.then((results) => {
results.forEach((result) => {
const lastIncident = result && result[0] ? result[0] : null;
if (lastIncident) {
membersLastIncident[lastIncident.memberId] = {
incidentLevel: lastIncident.incidentLevel,
incidentTimestamp: lastIncident.bookingStart,
}
}
});
const incidentsWithLevel = [];
incidentReservations.forEach((reservation) => {
const memberLastIncident = membersLastIncident[reservation.memberId];
const incident = {
reservationId: reservation.reservationId,
memberId: reservation.memberId,
resourceId: reservation.resourceId,
bookingStart: reservation.start,
bookingEnd: reservation.end,
incidentLevel: undefined,
incidentLevelPrice: undefined,
};
if (!memberLastIncident.incidentLevel) {
incident.incidentLevel = unlockedIncidentLevelsPrices.UNLOCKED_0.title;
incident.incidentLevelPrice = unlockedIncidentLevelsPrices.UNLOCKED_0.price;
} else {
const lastIncidentTime = moment.utc(memberLastIncident.incidentTimestamp).startOf('month');
const currentIncidentTime = moment.utc(reservation.start).startOf('month');
const timeDiff = Math.abs(lastIncidentTime.diff(currentIncidentTime, 'months'));
if (timeDiff >= (parseInt(process.env.UNLOCK_STREAK_REPAIR_AFTER) || 6)){
incident.incidentLevel = unlockedIncidentLevelsPrices.UNLOCKED_0.title;
incident.incidentLevelPrice = unlockedIncidentLevelsPrices.UNLOCKED_0.price;
} else {
const lastIncidentLevelId = unlockedIncidentLevelsPrices[memberLastIncident.incidentLevel].id;
const maxId = 5;
if ((lastIncidentLevelId && (lastIncidentLevelId >= maxId)) || (timeDiff === 0)){
incident.incidentLevel = memberLastIncident.incidentLevel;
incident.incidentLevelPrice = unlockedIncidentLevelsPrices[incident.incidentLevel].price;
} else {
const nextId = lastIncidentLevelId + 1;
Object.keys(unlockedIncidentLevelsPrices).forEach((key) => {
if (unlockedIncidentLevelsPrices[key].id === nextId){
incident.incidentLevel = unlockedIncidentLevelsPrices[key].title;
incident.incidentLevelPrice = unlockedIncidentLevelsPrices[key].price
}
});
}
}
}
memberLastIncident.incidentLevel = incident.incidentLevel;
memberLastIncident.incidentTimestamp = incident.bookingStart;
incidentsWithLevel.push(incident);
});
resolve(incidentsWithLevel);
})
.catch((error) => reject(error));
});
};
const getIncidentData = (reservation) => {
return new Promise ((resolve, reject) => {
getFirstNextBooking(reservation)
.then(nextReservation => {
const endOfTheDay = moment.tz(reservation.end, reservation.timezone).endOf('Day').toISOString();
let doorLockEntriesEndTime = nextReservation ? nextReservation.start : endOfTheDay;
if (nextReservation){
// Check if next reservations is immediately after (back to back reservation)
// If yes, then there is no need to check door lock entries related to this booking
const firstReservationEnd = moment(reservation.end);
const secondReservationStart = moment(nextReservation.start);
const timeDifference = Math.abs(secondReservationStart.diff(firstReservationEnd, 'minutes'));
const maxBackToBackDifference = parseInt(process.env.MAX_BACK_TO_BACK_DIFFERENCE) || 0;
if (timeDifference <= maxBackToBackDifference){
// It is back to back reservation, no need to check door lock entries
resolve({
incidentType: incidentType.NOT_AN_INCIDENT,
});
return;
}
}
// Find door lock entries related to this member, between booking start time and
// next booking start time OR end of the day
getRelatedDoorLockEntries(reservation.start, doorLockEntriesEndTime, reservation.memberId, reservation.resourceId)
.then((lockEntry) => {
if (lockEntry){
const timeResolution = parseInt(process.env.UNSCHEDULED_USE_TIME_RESOLUTION);
const chargePrice = parseFloat(process.env.UNSCHEDULED_USE_CHARGE_FEE);
const reservationEndTime = moment(reservation.end);
const lockedTime = moment(lockEntry.timestamp);
const timeDifference = Math.abs(reservationEndTime.diff(lockedTime, 'minutes'));
const timeIntervalsToCharge = Math.floor(timeDifference / timeResolution);
const totalChargeFee = timeIntervalsToCharge * chargePrice;
if (timeIntervalsToCharge > 0){
resolve({
incidentType: incidentType.UNSCHEDULED_INCIDENT,
reservation,
lockEntry,
chargePrice,
timeIntervalsToCharge,
totalChargeFee,
})
} else {
resolve({
incidentType: incidentType.NOT_AN_INCIDENT,
});
}
} else {
// Check if there is unlock entry for this reservation
getUnlockEntryForReservation(reservation)
.then((unlockEntry) => {
if (unlockEntry){
// This is unlocked incident
resolve({
incidentType: incidentType.UNLOCKED_INCIDENT,
reservation,
});
}else{
// Check if there is back-to-back booking before current one
getFirstPreviousBooking(reservation)
.then((previousReservation) => {
if (previousReservation){
const previousReservationEnd = moment(previousReservation.end);
const currentReservationStart = moment(reservation.start);
const timeDifference = Math.abs(currentReservationStart.diff(previousReservationEnd, 'minutes'));
const maxBackToBackDifference = parseInt(process.env.MAX_BACK_TO_BACK_DIFFERENCE) || 0;
if (timeDifference <= maxBackToBackDifference) {
resolve({
incidentType: incidentType.UNLOCKED_INCIDENT,
reservation,
});
}else{
resolve({
incidentType: incidentType.NOT_AN_INCIDENT,
});
}
}else{
resolve({
incidentType: incidentType.NOT_AN_INCIDENT,
});
}
})
.catch((error) => {
console.log('Error finding first previous reservation', error);
resolve({
error,
});
});
}
})
.catch((error) => {
console.log('Error finding unlock entry', error);
resolve({
error
});
});
}
})
.catch((error) => {
console.log('Error finding related door lock entry', error);
resolve({
error,
});
});
})
.catch((error) => {
console.log('Error finding first next booking', error);
resolve({
error,
});
});
});
};
const calculateDoorLockCharges = () => {
getAllFinishedBookings()
.then((reservations) => {
const unlockedIncidents = [];
const unscheduledIncidents = [];
const asyncCheckForIncidents = [];
reservations.forEach((reservation) => {
asyncCheckForIncidents.push(getIncidentData(reservation));
});
Promise.all(asyncCheckForIncidents)
.then((results) => {
results.forEach((result) => {
if (result.error){
console.log('Error checking incident : ', result.error);
}else if(result.incidentType) {
switch (result.incidentType) {
case incidentType.UNLOCKED_INCIDENT:
unlockedIncidents.push(result.reservation);
break;
case incidentType.UNSCHEDULED_INCIDENT:
const { reservation, lockEntry, chargePrice, timeIntervalsToCharge, totalChargeFee } = result;
unscheduledIncidents.push({
reservation,
lockEntry,
chargePrice,
timeIntervalsToCharge,
totalChargeFee,
});
break;
}
}
});
insertUnscheduledIncidents(unscheduledIncidents)
.catch((error) => console.log(error));
setUnlockedIncidentsLevel(unlockedIncidents)
.then((completedUnlockedIncidents) => {
insertUnlockedIncidents(completedUnlockedIncidents)
.catch((error) => console.log(error));
})
.catch((error) => console.log(error));
})
.catch((error) => console.log(error));
})
.catch((error) => console.log(error));
};
module.exports = {
calculateDoorLockCharges
};

View File

@@ -1,8 +1,11 @@
'use strict';
const db = require('../../models/index');
const moment = require('moment-timezone');
const Op = require('sequelize').Op;
const { API } = require('../../helpers/api');
const { officeRnDAPIErrors } = require('../../constants/constants');
const fetchAllBookings = () => {
return new Promise((resolve, reject) => {
@@ -15,26 +18,118 @@ const fetchAllBookings = () => {
cleanedBookingReservations.push({
reservationId: fullBookingEntry['_id'],
memberId: fullBookingEntry.member,
resource: fullBookingEntry.resourceId,
officeId: fullBookingEntry.office,
resourceId: fullBookingEntry.resourceId,
start: fullBookingEntry.start.dateTime,
end: fullBookingEntry.end.dateTime,
timezone: fullBookingEntry.timezone,
canceled: fullBookingEntry.canceled || false,
});
});
resolve(cleanedBookingReservations);
})
.catch((error) => {
reject(error);
console.log(officeRnDAPIErrors.FAILED_TO_FETCH_BOOKINGS);
console.log('Details : ', error);
reject(officeRnDAPIErrors.FAILED_TO_FETCH_BOOKINGS);
});
});
};
const getAllFinishedBookings = () => {
const attributes = ['reservationId', 'memberId', 'resourceId', 'start', 'end', 'timezone'];
const filters = {
canceled: false,
end: {
[Op.lt]: moment().toISOString()
}
};
return db.bookingReservation.findAll({
attributes,
where: filters,
order: [
['start', 'ASC'],
]
})
};
const getFirstNextBooking = (reservation) => {
return new Promise ((resolve, reject) => {
const {resourceId, start, timezone} = reservation;
const endOfTheDay = moment.tz(start, timezone).endOf('Day').toISOString();
const attributes = ['reservationId', 'memberId', 'resourceId', 'start', 'end', 'timezone'];
const filters = {
canceled: false,
start: {
[Op.gt]: start
},
end: {
[Op.lte]: endOfTheDay
},
resourceId,
};
const order = [['start', 'ASC']];
db.bookingReservation.findAll({
attributes,
where: filters,
order,
})
.then((reservations) => {
if (reservations && reservations[0]){
resolve(reservations[0]);
}else{
resolve(undefined);
}
})
.catch((error) => reject(error));
});
};
const getFirstPreviousBooking = (reservation) => {
return new Promise ((resolve, reject) => {
const {resourceId, start, timezone} = reservation;
const startOfTheDay = moment.tz(start, timezone).startOf('Day').toISOString();
const attributes = ['reservationId', 'memberId', 'resourceId', 'start', 'end', 'timezone'];
const filters = {
canceled: false,
start: {
[Op.gte]: startOfTheDay
},
end: {
[Op.lte]: start
},
resourceId,
};
const order = [['end', 'DESC']];
db.bookingReservation.findAll({
attributes,
where: filters,
order,
})
.then((reservations) => {
if (reservations && reservations[0]){
resolve(reservations[0]);
}else{
resolve(undefined);
}
})
.catch((error) => reject(error));
});
};
const writeBookingReservation = (bookingReservation) => {
db.bookingReservation.findOrCreate({where: {...bookingReservation}, defaults: {...bookingReservation}})
.then()
.catch();
return db.bookingReservation.findOrCreate({where: {...bookingReservation}, defaults: {...bookingReservation}});
};
module.exports = {
fetchAllBookings,
writeBookingReservation,
getAllFinishedBookings,
getFirstNextBooking,
getFirstPreviousBooking,
};

View File

@@ -0,0 +1,61 @@
'use strict';
const db = require('../../models/index');
const { API } = require('../../helpers/api');
const fetchOffices = () => {
return new Promise((resolve, reject) => {
API.get('/offices')
.then((result) => {
const offices = result.data || [];
const cleanedOffices = [];
offices.forEach(office => {
cleanedOffices.push({
officeId: office['_id'],
officeName: office.name,
});
});
resolve(cleanedOffices);
})
.catch((error) => {
reject(error);
});
});
};
const fetchResources = () => {
return new Promise((resolve, reject) => {
API.get('/resources')
.then((result) => {
const resources = result.data || [];
const cleanedResources = [];
resources.forEach(resource => {
cleanedResources.push({
resourceId: resource['_id'],
resourceName: resource.name,
officeId: resource.office,
});
});
resolve(cleanedResources);
})
.catch((error) => {
reject(error);
});
});
};
const getMappingsFromDatabase = () => {
return db.officeResourceMapping.findAll();
};
const saveNewMappingToDatabase = (mapping) => {
return db.officeResourceMapping.findOrCreate({where: {...mapping}, defaults: {...mapping}});
};
module.exports = {
getMappingsFromDatabase,
fetchOffices,
fetchResources,
saveNewMappingToDatabase,
};