UNPKG

@tomei/rental

Version:
314 lines 14.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; 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; } setScheduledStartDateTime(datetime) { return __awaiter(this, void 0, void 0, function* () { 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; }); } setScheduledEndDateTime(datetime) { return __awaiter(this, void 0, void 0, function* () { 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 init(dbTransaction, bookingNo) { return __awaiter(this, void 0, void 0, function* () { try { if (bookingNo) { const booking = yield 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; } }); } create(dbTransaction, loginUser, rentalPrice) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'Booking - Create'); if (!isPrivileged) { throw new general_1.ClassError('Booking', 'BookingErrMsg02', "You do not have 'Booking - Create' privilege."); } const isRentalAvailable = yield rental_1.Rental.isItemAvailable(this.ItemId, this.ItemType, this._ScheduledStartDateTime, this._ScheduledEndDateTime, dbTransaction); const isBookingAvailable = yield 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 = yield 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, }; yield 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); yield activity.create(loginUser.ObjectId, dbTransaction); return this; } catch (error) { throw error; } }); } static isBookingItemAvailable(dbTransaction, itemId, itemType, startDateTime, endDateTime) { return __awaiter(this, void 0, void 0, function* () { try { const booking = yield 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 findAll(dbTransaction, page, row, search) { return __awaiter(this, void 0, void 0, function* () { 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 yield Booking._Repo.findAndCountAll(options); } catch (err) { throw err; } }); } static findOne(loginUser, dbTransaction, searchOptions) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'BOOKING_VIEW'); if (!isPrivileged) { throw new general_1.ClassError('Booking', 'BookingErrMsg3', "You do not have 'BOOKING_VIEW' privilege."); } const record = yield Booking._Repo.findOne({ where: searchOptions, transaction: dbTransaction, }); if (record) { const booking = yield Booking.init(dbTransaction, record.BookingNo); return booking; } else { return null; } } catch (error) { throw error; } }); } static updateLeadToCustomer(loginUser, dbTransaction, LeadId, CustomerId) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield 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 = yield 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(); yield 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 })); yield activity.create(loginUser.ObjectId, dbTransaction); } } catch (error) { throw error; } }); } } exports.Booking = Booking; Booking._Repo = new booking_repository_1.BookingRepository(); //# sourceMappingURL=booking.js.map