add calendar dashboard edit

This commit is contained in:
Schneider Roland
2025-12-01 22:39:39 +01:00
parent a043d64229
commit fa098f4a1b
13 changed files with 310 additions and 199 deletions

View File

@@ -27,24 +27,6 @@ import { CancelBookingDto } from './dto/cancel-booking.dto';
export class CalendarController {
constructor(private readonly calendarService: CalendarService) {}
@Get('test')
getCalendarTest(
@Query('startDate') startDate: string,
@Query('endDate') endDate: string,
) {
console.log('--- TEST ENDPOINT ---');
console.log('startDate received:', startDate, '| Type:', typeof startDate);
console.log('endDate received:', endDate, '| Type:', typeof endDate);
return {
message: 'Test successful. Check your server console logs.',
received: {
startDate,
endDate,
},
};
}
// The primary endpoint to get event occurrences
@Get()
getCalendarEvents(@Query() getCalendarDto: GetCalendarDto) {
return this.calendarService.getEventsInRange(

View File

@@ -148,7 +148,7 @@ export class CalendarService {
if (!freq) continue;
let untilDate: Date|undefined = undefined;
let untilDate: Date | undefined = undefined;
// typeorm does not convert postgres type 'date' to javascript date object
if (event?.recurrenceRule?.endDate) {
untilDate = new Date(event.recurrenceRule.endDate);
@@ -204,9 +204,6 @@ export class CalendarService {
// --- Other service methods (createEvent, etc.) remain unchanged ---
async createEvent(createEventDto: CreateEventDto): Promise<Event> {
console.log('[CalendarService] Entering createEvent method.');
console.log('[CalendarService] Received DTO:', JSON.stringify(createEventDto, null, 2));
const { recurrenceRule, ...eventData } = createEventDto;
const newEvent: Omit<
@@ -224,32 +221,44 @@ export class CalendarService {
// check if event type exists
if (eventData.eventTypeId) {
console.log(`[CalendarService] Event has eventTypeId: ${eventData.eventTypeId}. Fetching event type...`);
console.log(
`[CalendarService] Event has eventTypeId: ${eventData.eventTypeId}. Fetching event type...`,
);
const eventType = await this.eventTypeRepository.findOneBy({
id: eventData.eventTypeId,
});
if (!eventType) {
console.error(`[CalendarService] Event type with ID ${eventData.eventTypeId} not found.`);
console.error(
`[CalendarService] Event type with ID ${eventData.eventTypeId} not found.`,
);
throw new BadRequestException(
{},
'Event type not found ' + eventData.eventTypeId,
);
}
newEvent.eventType = eventType;
console.log('[CalendarService] Successfully attached event type:', eventType);
console.log(
'[CalendarService] Successfully attached event type:',
eventType,
);
} else {
console.log('[CalendarService] No eventTypeId provided.');
}
console.log('[CalendarService] Creating event entity...');
const event = this.eventRepository.create(newEvent);
console.log('[CalendarService] Saving event entity to the database...');
const savedEvent = await this.eventRepository.save(event);
console.log('[CalendarService] Event saved successfully. Saved event ID:', savedEvent.id);
console.log(
'[CalendarService] Event saved successfully. Saved event ID:',
savedEvent.id,
);
if (savedEvent.isRecurring && recurrenceRule) {
console.log(`[CalendarService] Event is recurring. Creating recurrence rule...`);
console.log(
`[CalendarService] Event is recurring. Creating recurrence rule...`,
);
const rule = this.recurrenceRuleRepository.create({
...recurrenceRule,
event: savedEvent,
@@ -257,10 +266,14 @@ export class CalendarService {
await this.recurrenceRuleRepository.save(rule);
console.log('[CalendarService] Recurrence rule saved successfully.');
} else {
console.log('[CalendarService] Event is not recurring or no recurrence rule provided.');
console.log(
'[CalendarService] Event is not recurring or no recurrence rule provided.',
);
}
console.log(`[CalendarService] Fetching final event by ID ${savedEvent.id} to return.`);
console.log(
`[CalendarService] Fetching final event by ID ${savedEvent.id} to return.`,
);
const finalEvent = await this.getEventById(savedEvent.id);
console.log('[CalendarService] Exiting createEvent method.');
return finalEvent;
@@ -333,7 +346,59 @@ export class CalendarService {
id: number,
updateEventDto: CreateEventDto,
): Promise<Event> {
await this.eventRepository.update(id, updateEventDto);
// 1. Fetch the existing event (ensure relations are loaded if needed)
const event = await this.getEventById(id);
// 2. Update basic fields
event.updatedAt = new Date();
event.title = updateEventDto.title;
event.description = updateEventDto.description;
event.startTime = updateEventDto.startTime;
event.endTime = updateEventDto.endTime;
event.isRecurring = !!updateEventDto.isRecurring;
// 3. Handle Event Type Relationship
if (updateEventDto.eventTypeId) {
const eventType = await this.eventTypeRepository.findOneBy({
id: updateEventDto.eventTypeId,
});
if (!eventType) {
throw new NotFoundException(
`EventType with ID ${updateEventDto.eventTypeId} not found`,
);
}
// FIX: You forgot to assign this in your original code
event.eventType = eventType;
} else {
// Handle case where event type is removed/null
event.eventType = undefined;
}
// 4. Handle Recurrence Rule
// Because your entity has @OneToOne(..., { cascade: true }),
// modifying event.recurrenceRule and calling eventRepository.save(event)
// will automatically save/update the rule.
if (event.isRecurring && updateEventDto.recurrenceRule) {
const updateRRule = updateEventDto.recurrenceRule;
// If no rule exists yet, initialize a new one
if (!event.recurrenceRule) {
event.recurrenceRule = this.recurrenceRuleRepository.create({});
}
// Update the properties on the relation object
event.recurrenceRule.frequency = updateRRule.frequency;
event.recurrenceRule.interval = updateRRule.interval;
event.recurrenceRule.byDay = updateRRule.byDay as string;
event.recurrenceRule.endDate = updateRRule.endDate as Date;
event.recurrenceRule.count = updateRRule.count as number;
}
// 5. Use SAVE instead of UPDATE
// This handles the relations correctly and prevents the "one-to-many" error.
await this.eventRepository.save(event);
// 6. Return the updated event
return this.getEventById(id);
}

View File

@@ -80,7 +80,10 @@ export class EventsService {
}
async findOne(id: number) {
const record = await this.eventRepository.findOneBy({ id: id as any });
const record = await this.eventRepository.findOne({
where: { id: id },
relations: ['eventType', 'recurrenceRule', 'exceptions'],
});
if (!record) {
throw new NotFoundException(`Event with ID ${id} not found`);
}
@@ -100,4 +103,4 @@ export class EventsService {
}
return { deleted: true, id };
}
}
}