@tomei/rental
Version:
Tomei Rental Package
289 lines • 12.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Booking = void 0;
const general_1 = require("@tomei/general");
const booking_repository_1 = require("./booking.repository");
const luxon_1 = require("luxon");
const config_1 = require("@tomei/config");
const sequelize_1 = require("sequelize");
const rental_1 = require("../rental/rental");
const activity_history_1 = require("@tomei/activity-history");
class Booking extends general_1.ObjectBase {
get BookingNo() {
return this.ObjectId;
}
set BookingNo(value) {
this.ObjectId = value;
}
get CreatedById() {
return this._CreatedById;
}
get CreatedAt() {
return this._CreatedAt;
}
get UpdatedById() {
return this._UpdatedById;
}
get UpdatedAt() {
return this._UpdatedAt;
}
get ScheduledStartDateTime() {
return this._ScheduledStartDateTime;
}
get ScheduledEndDateTime() {
return this._ScheduledEndDateTime;
}
async setScheduledStartDateTime(datetime) {
const dateTime = luxon_1.DateTime.fromJSDate(datetime);
const currentDateTime = luxon_1.DateTime.now();
if (dateTime <= currentDateTime) {
throw new general_1.ClassError('Booking', 'BookingErrMsg01', 'Booking must be for future date time.');
}
this._ScheduledStartDateTime = datetime;
}
async setScheduledEndDateTime(datetime) {
const dateTime = luxon_1.DateTime.fromJSDate(datetime);
const currentDateTime = luxon_1.DateTime.now();
if (dateTime <= currentDateTime) {
throw new general_1.ClassError('Booking', 'BookingErrMsg01', 'Booking must be for future date time.');
}
if (!this._ScheduledStartDateTime) {
throw new general_1.ClassError('Booking', 'BookingErrMsg01', 'Booking start date time must be set before setting end date time.');
}
const startDateTime = luxon_1.DateTime.fromJSDate(this._ScheduledStartDateTime);
if (dateTime <= startDateTime) {
throw new general_1.ClassError('Booking', 'BookingErrMsg03', 'Booking end date time must be more than start date time.');
}
this._ScheduledEndDateTime = datetime;
}
constructor(bookingAttr) {
super();
this.ObjectType = 'Booking';
this.TableName = 'booking_Booking';
if (bookingAttr) {
this.ObjectId = bookingAttr.BookingNo;
this.CustomerId = bookingAttr.CustomerId;
this.CustomerType = bookingAttr.CustomerType;
this.ItemId = bookingAttr.ItemId;
this.ItemType = bookingAttr.ItemType;
this.PriceId = bookingAttr.PriceId;
this._ScheduledStartDateTime = bookingAttr.ScheduledStartDateTime;
this._ScheduledEndDateTime = bookingAttr.ScheduledEndDateTime;
this.BookingFee = bookingAttr.BookingFee;
this.Status = bookingAttr.Status;
this.CancelRemarks = bookingAttr.CancelRemarks;
this._CreatedById = bookingAttr.CreatedById;
this._CreatedAt = bookingAttr.CreatedAt;
this._UpdatedById = bookingAttr.UpdatedById;
this._UpdatedAt = bookingAttr.UpdatedAt;
}
}
static async init(dbTransaction, bookingNo) {
try {
if (bookingNo) {
const booking = await Booking._Repo.findOne({
where: {
BookingNo: bookingNo,
},
transaction: dbTransaction,
});
if (booking) {
return new Booking(booking.get({ plain: true }));
}
else {
throw new Error('Booking not found');
}
}
return new Booking();
}
catch (error) {
throw error;
}
}
async create(dbTransaction, loginUser, rentalPrice) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Booking - Create');
if (!isPrivileged) {
throw new general_1.ClassError('Booking', 'BookingErrMsg02', "You do not have 'Booking - Create' privilege.");
}
const isRentalAvailable = await rental_1.Rental.isItemAvailable(this.ItemId, this.ItemType, this._ScheduledStartDateTime, this._ScheduledEndDateTime, dbTransaction);
const isBookingAvailable = await Booking.isBookingItemAvailable(dbTransaction, this.ItemId, this.ItemType, this._ScheduledStartDateTime, this._ScheduledEndDateTime);
if (!isRentalAvailable || !isBookingAvailable) {
throw new general_1.ClassError('Booking', 'BookingErrMsg02', 'Booking is not available for the item on the chosen date.');
}
const rp = await rentalPrice.create(loginUser, dbTransaction);
this.BookingNo = this.createId();
this.PriceId = rp.PriceId;
this._CreatedById = loginUser.ObjectId;
this._CreatedAt = luxon_1.DateTime.now().toJSDate();
this._UpdatedById = loginUser.ObjectId;
this._UpdatedAt = luxon_1.DateTime.now().toJSDate();
const data = {
BookingNo: this.BookingNo,
CustomerId: this.CustomerId,
CustomerType: this.CustomerType,
ItemId: this.ItemId,
ItemType: this.ItemType,
PriceId: this.PriceId,
ScheduledStartDateTime: this._ScheduledStartDateTime,
ScheduledEndDateTime: this._ScheduledEndDateTime,
BookingFee: this.BookingFee,
Status: this.Status,
CancelRemarks: this.CancelRemarks,
CreatedById: this._CreatedById,
CreatedAt: this._CreatedAt,
UpdatedById: this._UpdatedById,
UpdatedAt: this._UpdatedAt,
};
await Booking._Repo.create(data, {
transaction: dbTransaction,
});
const activity = new activity_history_1.Activity();
activity.ActivityId = activity.createId();
activity.Action = activity_history_1.ActionEnum.CREATE;
activity.Description = 'Add Booking';
activity.EntityId = this.BookingNo;
activity.EntityType = this.ObjectType;
activity.EntityValueBefore = JSON.stringify({});
activity.EntityValueAfter = JSON.stringify(data);
await activity.create(loginUser.ObjectId, dbTransaction);
return this;
}
catch (error) {
throw error;
}
}
static async isBookingItemAvailable(dbTransaction, itemId, itemType, startDateTime, endDateTime) {
try {
const booking = await Booking._Repo.findOne({
where: {
ItemId: itemId,
ItemType: itemType,
[sequelize_1.Op.and]: [
{
ScheduledStartDateTime: {
[sequelize_1.Op.lte]: endDateTime,
},
},
{
ScheduledEndDateTime: {
[sequelize_1.Op.gte]: startDateTime,
},
},
],
},
transaction: dbTransaction,
});
if (booking) {
return false;
}
return true;
}
catch (error) {
throw error;
}
}
static async findAll(dbTransaction, page, row, search) {
try {
const queryObj = {};
let options = {
transaction: dbTransaction,
order: [['CreatedAt', 'DESC']],
};
if (page && row) {
options = Object.assign(Object.assign({}, options), { limit: row, offset: row * (page - 1) });
}
if (search) {
Object.entries(search).forEach(([key, value]) => {
if (key === 'ScheduledStartDateTime') {
queryObj[key] = {
[sequelize_1.Op.gte]: value,
};
}
else if (key === 'ScheduledEndDateTime') {
queryObj[key] = {
[sequelize_1.Op.lte]: value,
};
}
else {
queryObj[key] = {
[sequelize_1.Op.substring]: value,
};
}
});
options = Object.assign(Object.assign({}, options), { where: queryObj });
}
return await Booking._Repo.findAndCountAll(options);
}
catch (err) {
throw err;
}
}
static async findOne(loginUser, dbTransaction, searchOptions) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'BOOKING_VIEW');
if (!isPrivileged) {
throw new general_1.ClassError('Booking', 'BookingErrMsg3', "You do not have 'BOOKING_VIEW' privilege.");
}
const record = await Booking._Repo.findOne({
where: searchOptions,
transaction: dbTransaction,
});
if (record) {
const booking = await Booking.init(dbTransaction, record.BookingNo);
return booking;
}
else {
return null;
}
}
catch (error) {
throw error;
}
}
static async updateLeadToCustomer(loginUser, dbTransaction, LeadId, CustomerId) {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(systemCode, 'BOOKING_UPDATE');
if (!isPrivileged) {
throw new general_1.ClassError('Booking', 'BookingErrMsg02', "You do not have 'BOOKING_UPDATE' privilege");
}
if (!LeadId || !CustomerId) {
throw new general_1.ClassError('Booking', 'BookingErrMsg02', 'LeadId and CustomerId cannot be null or invalid');
}
const bookings = await Booking._Repo.findAll({
where: {
[sequelize_1.Op.and]: [{ CustomerId: LeadId }, { CustomerType: 'Lead' }],
},
transaction: dbTransaction,
});
for (const booking of bookings) {
const EntityValueBefore = Object.assign({}, booking.get({ plain: true }));
booking.CustomerId = CustomerId;
booking.CustomerType = 'Customer';
booking.UpdatedById = loginUser.ObjectId;
booking.UpdatedAt = luxon_1.DateTime.now().toJSDate();
await booking.save({
transaction: dbTransaction,
});
const activity = new activity_history_1.Activity();
activity.ActivityId = activity.createId();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Update Lead to Customer';
activity.EntityType = 'Booking';
activity.EntityId = booking.BookingNo;
activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
activity.EntityValueAfter = JSON.stringify(booking.get({ plain: true }));
await activity.create(loginUser.ObjectId, dbTransaction);
}
}
catch (error) {
throw error;
}
}
}
exports.Booking = Booking;
Booking._Repo = new booking_repository_1.BookingRepository();
//# sourceMappingURL=booking.js.map