improve exception saving
This commit is contained in:
@@ -65,7 +65,10 @@ export class CalendarController {
|
||||
@Param('id', ParseIntPipe) eventId: number,
|
||||
@Body() createExceptionDto: CreateExceptionDto,
|
||||
) {
|
||||
return this.calendarService.createException(eventId, createExceptionDto);
|
||||
return this.calendarService.createOrUpdateException(
|
||||
eventId,
|
||||
createExceptionDto,
|
||||
);
|
||||
}
|
||||
|
||||
// Create a booking for a specific event occurrence
|
||||
|
||||
@@ -335,6 +335,102 @@ export class CalendarService {
|
||||
},
|
||||
);
|
||||
}
|
||||
async createOrUpdateException(
|
||||
eventId: number,
|
||||
createExceptionDto: CreateExceptionDto,
|
||||
): Promise<EventException> {
|
||||
const { originalStartTime, newStartTime } = createExceptionDto;
|
||||
|
||||
const event = await this.eventRepository.findOneBy({ id: eventId });
|
||||
if (!event) {
|
||||
throw new NotFoundException(`Event with ID ${eventId} not found`);
|
||||
}
|
||||
|
||||
// This logic now requires a transaction
|
||||
return this.dataSource.manager.transaction(
|
||||
async (transactionalEntityManager) => {
|
||||
let oldNewStartTime: Date | null = null;
|
||||
// Check if an exception for this originalStartTime already exists
|
||||
let existingException = await transactionalEntityManager.findOne(
|
||||
EventException,
|
||||
{
|
||||
where: {
|
||||
eventId: eventId,
|
||||
originalStartTime: originalStartTime,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (existingException) {
|
||||
oldNewStartTime = existingException.newStartTime;
|
||||
existingException.title = createExceptionDto.title!;
|
||||
existingException.description = createExceptionDto.description!;
|
||||
existingException.isCancelled = createExceptionDto.isCancelled!;
|
||||
existingException.newStartTime = createExceptionDto.newStartTime!;
|
||||
existingException.newEndTime = createExceptionDto.newEndTime!;
|
||||
|
||||
// More complex logic might be needed here depending on exact requirements.
|
||||
} else {
|
||||
// Create new exception
|
||||
existingException = transactionalEntityManager.create(
|
||||
EventException,
|
||||
{
|
||||
...createExceptionDto,
|
||||
event: event,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const savedException =
|
||||
await transactionalEntityManager.save(existingException);
|
||||
// Step 1: Create and save the new exception
|
||||
// const exception = transactionalEntityManager.create(EventException, {
|
||||
// ...createExceptionDto,
|
||||
// event: event,
|
||||
// });
|
||||
// const savedException = await transactionalEntityManager.save(exception);
|
||||
|
||||
// Step 2: Check if this is a RESCHEDULE operation
|
||||
// A reschedule happens when there is a new start time.
|
||||
let startTimeChanged = false;
|
||||
if (newStartTime && !oldNewStartTime) {
|
||||
startTimeChanged = true;
|
||||
} else if (!newStartTime && oldNewStartTime) {
|
||||
startTimeChanged = true;
|
||||
} else if (newStartTime && oldNewStartTime) {
|
||||
if (oldNewStartTime.getTime() !== newStartTime.getTime()) {
|
||||
startTimeChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (startTimeChanged) {
|
||||
// Step 3: Find all existing bookings for the ORIGINAL time
|
||||
const bookingsToMigrate = await transactionalEntityManager.find(
|
||||
Booking,
|
||||
{
|
||||
where: {
|
||||
eventId: eventId,
|
||||
occurrenceStartTime: originalStartTime,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
// Step 4: If we found any bookings, update their start time to the NEW time
|
||||
if (bookingsToMigrate.length > 0) {
|
||||
const bookingIds = bookingsToMigrate.map((b) => b.id);
|
||||
|
||||
await transactionalEntityManager.update(
|
||||
Booking,
|
||||
bookingIds, // Update these specific bookings
|
||||
{ occurrenceStartTime: newStartTime }, // Set their time to the new value
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return savedException;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async getEventById(id: number): Promise<Event> {
|
||||
const event = await this.eventRepository.findOne({
|
||||
|
||||
Reference in New Issue
Block a user