Calculate door lock charges
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
61
services/officeRnD/resources.js
Normal file
61
services/officeRnD/resources.js
Normal 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,
|
||||
};
|
||||
Reference in New Issue
Block a user