Charges/invoices update
This commit is contained in:
@@ -24,10 +24,10 @@ const chargeBookingChanges = (changes) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (Array.isArray(changes)){
|
||||
const incidents = [];
|
||||
const errors = [];
|
||||
changes.forEach((change) => {
|
||||
const { oldReservation, newReservation } = change;
|
||||
if (oldReservation && newReservation){
|
||||
const oldResourceId = oldReservation.resourceId;
|
||||
const oldStart = oldReservation.start ? moment.utc(oldReservation.start) : null;
|
||||
const oldEnd = oldReservation.end ? moment.utc(oldReservation.end) : null;
|
||||
|
||||
@@ -35,7 +35,8 @@ const chargeBookingChanges = (changes) => {
|
||||
const newEnd = newReservation.end ? moment.utc(newReservation.end) : null;
|
||||
|
||||
const reservationTimezone = newReservation.timezone ? newReservation.timezone : UI_TIMEZONE;
|
||||
const reservationHourlyRate = newReservation.hourlyRate ? newReservation.hourlyRate : undefined;
|
||||
const reservationHourlyRate = oldReservation.hourlyRate ? oldReservation.hourlyRate : undefined;
|
||||
const canceled = newReservation.canceled;
|
||||
|
||||
if (oldStart && oldEnd && newStart && newEnd && reservationHourlyRate){
|
||||
const oldReservationLength = oldEnd.diff(oldStart, 'hours', true);
|
||||
@@ -43,66 +44,78 @@ const chargeBookingChanges = (changes) => {
|
||||
|
||||
const differenceFromNow = oldStart.diff(moment.utc(), 'hours');
|
||||
|
||||
if (differenceFromNow && (differenceFromNow < 24)){
|
||||
if (differenceFromNow < 24){
|
||||
// Changed reservation that was within 24hrs from now
|
||||
const { reservationId, memberId, resourceId } = newReservation;
|
||||
|
||||
// Check if new reservation is on same day
|
||||
const sameDay = oldStart.tz(reservationTimezone).isSame(newStart.tz(reservationTimezone), 'day');
|
||||
if (!canceled) {
|
||||
// Check if new reservation is on same day
|
||||
const sameDay = oldStart.tz(reservationTimezone).isSame(newStart.tz(reservationTimezone), 'day');
|
||||
|
||||
const { reservationId, memberId, resourceId, hourlyRate } = newReservation;
|
||||
if (sameDay) {
|
||||
// Reservation moved in same day
|
||||
// Check if member shortened the reservation
|
||||
|
||||
if (sameDay){
|
||||
// Reservation moved in same day
|
||||
// Check if member shortened the reservation
|
||||
if (newReservationLength < oldReservationLength) {
|
||||
const differenceInLength = oldReservationLength - newReservationLength;
|
||||
const chargeFee = differenceInLength * reservationHourlyRate * BOOKING_CHANGE_PERCENTAGE_CHARGE / 100;
|
||||
|
||||
if (newReservationLength < oldReservationLength){
|
||||
const incident = {
|
||||
reservationId,
|
||||
memberId,
|
||||
oldResourceId: oldResourceId || resourceId,
|
||||
newResourceId: resourceId,
|
||||
oldBookingStart: oldReservation.start,
|
||||
oldBookingEnd: oldReservation.end,
|
||||
newBookingStart: newReservation.start,
|
||||
newBookingEnd: newReservation.end,
|
||||
incidentType: incidentType.BOOKING_SHORTENED,
|
||||
chargeFee,
|
||||
};
|
||||
|
||||
const differenceInLength = oldReservationLength - newReservationLength;
|
||||
const chargeFee = differenceInLength*hourlyRate*BOOKING_CHANGE_PERCENTAGE_CHARGE/100;
|
||||
incidents.push(incident);
|
||||
}
|
||||
} else {
|
||||
// Reservation moved to another day
|
||||
// Add cancellation charge
|
||||
const chargeFee = oldReservationLength * reservationHourlyRate * BOOKING_CHANGE_PERCENTAGE_CHARGE / 100;
|
||||
|
||||
const incident = {
|
||||
reservationId,
|
||||
memberId,
|
||||
resourceId,
|
||||
oldResourceId: oldResourceId || resourceId,
|
||||
newResourceId: resourceId,
|
||||
oldBookingStart: oldReservation.start,
|
||||
oldBookingEnd: oldReservation.end,
|
||||
newBookingStart: newReservation.start,
|
||||
newBookingEnd: newReservation.end,
|
||||
incidentType: incidentType.BOOKING_SHORTENED,
|
||||
incidentType: incidentType.BOOKING_MOVED_TO_ANOTHER_DAY,
|
||||
chargeFee,
|
||||
};
|
||||
|
||||
incidents.push(incident);
|
||||
}
|
||||
}else{
|
||||
// Reservation moved to another day
|
||||
// Add cancellation charge
|
||||
const chargeFee = oldReservationLength*hourlyRate*BOOKING_CHANGE_PERCENTAGE_CHARGE/100;
|
||||
|
||||
const chargeFee = 2 * reservationHourlyRate * oldReservationLength * BOOKING_CHANGE_PERCENTAGE_CHARGE / 100;
|
||||
const incident = {
|
||||
reservationId,
|
||||
memberId,
|
||||
resourceId,
|
||||
oldResourceId: oldResourceId || resourceId,
|
||||
newResourceId: null,
|
||||
oldBookingStart: oldReservation.start,
|
||||
oldBookingEnd: oldReservation.end,
|
||||
newBookingStart: newReservation.start,
|
||||
newBookingEnd: newReservation.end,
|
||||
incidentType: incidentType.BOOKING_MOVED_TO_ANOTHER_DAY,
|
||||
newBookingStart: null,
|
||||
newBookingEnd: null,
|
||||
incidentType: incidentType.BOOKING_CANCELED_LATE,
|
||||
chargeFee,
|
||||
};
|
||||
|
||||
incidents.push(incident);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
errors.push(change);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (errors.length > 0){
|
||||
console.log('There were some errors with incomplete bookings : ');
|
||||
console.log(errors);
|
||||
}
|
||||
resolve(bulkWriteBookingChangeIncidents(incidents));
|
||||
}else{
|
||||
reject('Input argument is not an array !');
|
||||
|
||||
48
services/integration/bookingChangeLog.js
Normal file
48
services/integration/bookingChangeLog.js
Normal file
@@ -0,0 +1,48 @@
|
||||
'use strict';
|
||||
|
||||
const db = require('../../models/index');
|
||||
|
||||
const bulkWriteChanges = ((changes) => {
|
||||
const changeLogsForDB = [];
|
||||
|
||||
changes.forEach((change) => {
|
||||
const { oldReservation, newReservation } = change;
|
||||
const { reservationId, memberId, officeId, resourceId, start, end, canceled } = newReservation;
|
||||
|
||||
const logEntry = {
|
||||
reservationId: oldReservation.reservationId || reservationId,
|
||||
memberId: oldReservation.memberId || memberId,
|
||||
officeId: oldReservation.officeId || officeId,
|
||||
oldResourceId: oldReservation.resourceId || resourceId,
|
||||
newResourceId: resourceId,
|
||||
oldStart: oldReservation.start || start,
|
||||
newStart: start,
|
||||
oldEnd: oldReservation.end || end,
|
||||
newEnd: end,
|
||||
canceled,
|
||||
};
|
||||
|
||||
if (!oldReservation.start && !oldReservation.end && !oldReservation.resourceId){
|
||||
// new reservation
|
||||
logEntry.oldResourceId = null;
|
||||
logEntry.oldStart = null;
|
||||
logEntry.oldEnd = null;
|
||||
}
|
||||
|
||||
if (newReservation.canceled){
|
||||
logEntry.newResourceId = null;
|
||||
logEntry.newStart = null;
|
||||
logEntry.newEnd = null;
|
||||
}
|
||||
|
||||
changeLogsForDB.push(logEntry);
|
||||
});
|
||||
|
||||
return db.bookingReservationChangeLog.bulkCreate(changeLogsForDB);
|
||||
// console.log(changeLogsForDB);
|
||||
// return new Promise((resolve) => resolve());
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
bulkWriteChanges,
|
||||
};
|
||||
@@ -116,7 +116,8 @@ const getBookingChangeIncidents = (startDate, endDate, memberId) => {
|
||||
'id',
|
||||
'reservationId',
|
||||
'memberId',
|
||||
'resourceId',
|
||||
'oldResourceId',
|
||||
'newResourceId',
|
||||
'oldBookingStart',
|
||||
'oldBookingEnd',
|
||||
'newBookingStart',
|
||||
@@ -251,7 +252,8 @@ const getAllIncidents = (dateRange, memberId) => {
|
||||
const {
|
||||
id,
|
||||
memberId,
|
||||
resourceId,
|
||||
oldResourceId,
|
||||
newResourceId,
|
||||
oldBookingStart,
|
||||
oldBookingEnd,
|
||||
newBookingStart,
|
||||
@@ -261,14 +263,17 @@ const getAllIncidents = (dateRange, memberId) => {
|
||||
createdAt,
|
||||
} = bookingChangeIncident;
|
||||
const memberName = membersMap[memberId].name;
|
||||
const resource = resourcesMap[resourceId];
|
||||
const resourceName = resource.resourceName;
|
||||
const officeName = officesMap[resource.officeId].officeName;
|
||||
const oldResource = resourcesMap[oldResourceId];
|
||||
const newResource = newResourceId ? resourcesMap[newResourceId] : null;
|
||||
const oldResourceName = oldResource.resourceName;
|
||||
const newResourceName = newResource ? newResource.resourceName : null;
|
||||
const officeName = officesMap[oldResource.officeId].officeName;
|
||||
allIncidents.push({
|
||||
incidentId: id,
|
||||
memberId,
|
||||
memberName,
|
||||
resourceName,
|
||||
oldResourceName,
|
||||
newResourceName,
|
||||
officeName,
|
||||
oldBookingStart: formatTime(oldBookingStart),
|
||||
oldBookingEnd: formatTime(oldBookingEnd),
|
||||
|
||||
@@ -174,7 +174,7 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
|
||||
const changedKeys = instance.changed();
|
||||
const previous = instance.previous();
|
||||
|
||||
const lookupKeys = ['start', 'end'];
|
||||
const lookupKeys = ['start', 'end', 'resourceId', 'canceled'];
|
||||
|
||||
let realChange = false;
|
||||
lookupKeys.forEach((key) => {
|
||||
@@ -184,6 +184,8 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
|
||||
}
|
||||
});
|
||||
|
||||
instance.setDataValue('hourlyRate', previous.hourlyRate);
|
||||
|
||||
if (realChange){
|
||||
changes.push({
|
||||
oldReservation: previous,
|
||||
@@ -193,25 +195,41 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
|
||||
});
|
||||
|
||||
reservations.forEach((reservation) => {
|
||||
asyncJobs.push(
|
||||
db.bookingReservation.update(reservation, {
|
||||
where: {
|
||||
reservationId: reservation.reservationId,
|
||||
},
|
||||
returning: true,
|
||||
individualHooks: true,
|
||||
})
|
||||
.then(([updateCount, updatedInstances]) => {
|
||||
if (updateCount === 0){
|
||||
db.bookingReservation.upsert(reservation);
|
||||
}
|
||||
const asyncReservationUpdate = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.bookingReservation.update(reservation, {
|
||||
where: {
|
||||
reservationId: reservation.reservationId,
|
||||
},
|
||||
returning: true,
|
||||
individualHooks: true,
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('Error updating');
|
||||
console.log(error);
|
||||
reject(error);
|
||||
})
|
||||
);
|
||||
.then(([updateCount, updatedInstances]) => {
|
||||
if (updateCount === 0){
|
||||
const oldReservation = {
|
||||
start: null,
|
||||
end: null,
|
||||
resourceId: null,
|
||||
};
|
||||
|
||||
changes.push({
|
||||
oldReservation,
|
||||
newReservation: reservation,
|
||||
});
|
||||
resolve(db.bookingReservation.upsert(reservation));
|
||||
}else{
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('Error updating');
|
||||
console.log(error);
|
||||
reject(error);
|
||||
})
|
||||
});
|
||||
};
|
||||
|
||||
asyncJobs.push(asyncReservationUpdate());
|
||||
});
|
||||
|
||||
Promise.all(asyncJobs)
|
||||
|
||||
Reference in New Issue
Block a user