Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | import { Injectable, Logger, BadRequestException, NotFoundException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { BookingRequest, BookingHold, BookingRule } from './schemas'; import { ConflictDetectionService } from './conflict-detection.service'; @Injectable() export class BookingEngine { private readonly logger = new Logger(BookingEngine.name); constructor( @InjectModel(BookingRequest.name) private readonly bookingRequestModel: Model<BookingRequest>, @InjectModel(BookingHold.name) private readonly bookingHoldModel: Model<BookingHold>, @InjectModel(BookingRule.name) private readonly bookingRuleModel: Model<BookingRule>, private readonly conflictService: ConflictDetectionService ) {} /** * Place a temporary slot hold for a resource. */ async createHold( tenantId: string, resourceId: string, startAt: Date, endAt: Date, userId: string, holdDurationMinutes = 15 ): Promise<BookingHold> { // Check conflicts first const conflict = await this.conflictService.checkConflicts(tenantId, [], [resourceId], startAt, endAt, false); if (conflict.hasConflict && conflict.severity === 'blocking') { throw new BadRequestException(`Cannot hold slot: ${conflict.reason}`); } const holdId = `hold_${Date.now()}_${Math.random().toString(36).substr(2, 5)}`; const expiresAt = new Date(Date.now() + holdDurationMinutes * 60000); const hold = new this.bookingHoldModel({ tenantId, holdId, resourceId, startAt, endAt, expiresAt, heldByUserId: userId }); return hold.save(); } /** * Release or expire a booking hold. */ async releaseHold(tenantId: string, holdId: string): Promise<void> { await this.bookingHoldModel.deleteOne({ tenantId, holdId }).exec(); } /** * Confirm or request a booking. */ async createBooking( tenantId: string, data: { bookingId: string; bookingType: string; startAt: Date; endAt: Date; bookerId: string; participantIds?: string[]; resourceIds?: string[]; notes?: string; } ): Promise<BookingRequest> { // Verify booking rule constraints ( notice time, max horizon ) const rule = await this.bookingRuleModel.findOne({ tenantId, resourceType: data.bookingType }).exec(); if (rule) { const minNoticeDate = new Date(Date.now() + rule.minNoticeMinutes * 60000); if (new Date(data.startAt) < minNoticeDate) { throw new BadRequestException(`Booking violates minimum notice constraint of ${rule.minNoticeMinutes} minutes.`); } const maxHorizonDate = new Date(Date.now() + rule.maxHorizonDays * 24 * 60 * 60000); if (new Date(data.endAt) > maxHorizonDate) { throw new BadRequestException(`Booking violates maximum booking horizon of ${rule.maxHorizonDays} days.`); } } // Verify conflicts const conflict = await this.conflictService.checkConflicts( tenantId, data.participantIds || [], data.resourceIds || [], data.startAt, data.endAt, false ); if (conflict.hasConflict && conflict.severity === 'blocking') { throw new BadRequestException(`Double booking conflict: ${conflict.reason}`); } const status = rule?.approvalRequired ? 'pending_approval' : 'confirmed'; const booking = new this.bookingRequestModel({ tenantId, bookingId: data.bookingId, bookingType: data.bookingType, startAt: data.startAt, endAt: data.endAt, bookerId: data.bookerId, participantIds: data.participantIds || [], resourceIds: data.resourceIds || [], status, notes: data.notes }); return booking.save(); } /** * Cancel booking */ async cancelBooking(tenantId: string, bookingId: string): Promise<BookingRequest> { const booking = await this.bookingRequestModel.findOneAndUpdate( { tenantId, bookingId }, { status: 'cancelled' }, { new: true } ).exec(); if (!booking) throw new NotFoundException('Booking not found'); return booking; } } |