Коммит aaaf1517 создал по автору Radch-enko's avatar Radch-enko
Просмотр файлов

Refactor `GoogleCalendarProvider` and `OrganizerRepositoryImpl` for improved...

Refactor `GoogleCalendarProvider` and `OrganizerRepositoryImpl` for improved clarity and error handling

- Updated `convertToGoogleEvent` to include optional workspace attendee logic.
- Simplified `convertToBooking` by removing unused `calendarId` parameter and adding workspace ID handling.
- Improved `deleteEventByBooking` and event updates to use `defaultCalendar` ID consistently.
- Fixed user filtering in `OrganizerRepositoryImpl` to correctly map users with `employer` tag.
- Enhanced error logging and fallback mechanisms for event handling functions.
владелец 0df03829
......@@ -47,10 +47,10 @@ class GoogleCalendarProvider(
throw OverlappingBookingException("Workspace ${booking.workspace.id} is unavailable at the requested time")
}
val event = convertToGoogleEvent(booking)
val event = convertToGoogleEvent(booking, workspaceCalendarId)
val savedEvent = runCatching {
calendar.events().insert(workspaceCalendarId, event).execute()
calendar.events().insert(defaultCalendar, event).execute()
}.onFailure {
logger.error("Failed to create event", it)
return@onFailure
......@@ -72,8 +72,9 @@ class GoogleCalendarProvider(
throw OverlappingBookingException("Workspace ${booking.workspace.id} is unavailable at the requested time")
}
val event = convertToGoogleEvent(booking)
val updatedEvent = calendar.events().update(workspaceCalendarId, eventId, event).execute()
val event = convertToGoogleEvent(booking, workspaceCalendarId)
val updatedEvent = calendar.events().update(defaultCalendar, eventId, event).execute()
return booking.copy(id = updatedEvent.id)
}
......@@ -86,8 +87,7 @@ class GoogleCalendarProvider(
private fun deleteEventByBooking(booking: Booking, eventId: String) {
try {
val calendarId = getCalendarIdByWorkspace(booking.workspace.id)
calendar.events().delete(calendarId, eventId).execute()
calendar.events().delete(defaultCalendar, eventId).execute()
} catch (e: GoogleJsonResponseException) {
logger.error("Failed to delete event: {}", e.details)
if (e.statusCode != 404 && e.statusCode != 410) {
......@@ -115,7 +115,7 @@ class GoogleCalendarProvider(
val workspaceCalendarId = getCalendarIdByWorkspace(workspaceId)
val events = listEvents(workspaceCalendarId, from, to, returnInstances = returnInstances)
return events.map { convertToBooking(it, workspaceCalendarId) }
return events.map { convertToBooking(it) }
}
override fun findEventsByUser(userId: UUID, from: Instant, to: Instant?, returnInstances: Boolean): List<Booking> {
......@@ -141,7 +141,7 @@ class GoogleCalendarProvider(
event.organizer?.email == userEmail ||
event.attendees?.any { it.email == userEmail } == true
}
bookings.addAll(filteredEvents.map { convertToBooking(it, calendarId) })
bookings.addAll(filteredEvents.map { convertToBooking(it) })
}
return bookings
......@@ -158,7 +158,7 @@ class GoogleCalendarProvider(
// Try to get the event directly by ID
val event = calendar.events().get(calendarId, id).execute()
if (event != null) {
return convertToBooking(event, calendarId)
return convertToBooking(event)
}
} catch (e: Exception) {
// If the event is not found in this calendar, try the next one
......@@ -186,7 +186,7 @@ class GoogleCalendarProvider(
try {
val events = listEvents(calendarId, from, to, returnInstances = returnInstances)
logger.debug("findAllEvents -> events: {}", events.map { it.id.toString() })
bookings.addAll(events.map { convertToBooking(it, calendarId) })
bookings.addAll(events.map { convertToBooking(it) })
} catch (e: Exception) {
logger.warn("Failed to search for events in calendar {}: {}", calendarId, e.message)
}
......@@ -242,9 +242,9 @@ class GoogleCalendarProvider(
}
}
private fun convertToGoogleEvent(booking: Booking): Event {
private fun convertToGoogleEvent(booking: Booking, workspaceCalendarId: String? = null): Event {
val event = Event()
.setSummary("Meet${booking.owner?.let { " ${it.firstName} ${it.lastName}" }.orEmpty() }")
.setSummary("Meet${booking.owner?.let { " ${it.firstName} ${it.lastName}" }.orEmpty()}")
.setDescription(
"${booking.owner?.email} - почта организатора"
)
......@@ -259,22 +259,50 @@ class GoogleCalendarProvider(
// Add attendees
val attendees = booking.participants.map { user ->
EventAttendee().setEmail(user.email)
}
event.attendees = attendees
}.toMutableList()
// Add the owner as the organizer
booking.owner?.email?.let { event.organizer = Event.Organizer().setEmail(it) }
// Add workspace as an attendee if workspaceCalendarId is provided
workspaceCalendarId?.let {
val workspaceAttendee = EventAttendee()
.setEmail(it)
.setResource(true)
attendees.add(workspaceAttendee)
}
event.attendees = attendees
return event
}
private fun convertToBooking(event: Event, calendarId: String? = null): Booking {
/**
* Retrieves the calendar ID of the workspace from the event.
* If the ID is not found, returns a default value with a warning log.
*
* @param event The event from which to retrieve the calendar ID.
* @return Calendar ID of the workspace or default value.
*/
private fun getCalendarId(event: Event): String? {
val calendarId = event.attendees
?.firstOrNull { it?.resource == true }
?.email
if (calendarId == null) {
logger.warn("No resource attendee found in event with ID: ${event.id}. Using provided calendar ID as fallback.")
}
return calendarId
}
private fun convertToBooking(event: Event): Booking {
// Get the organizer's email and find the corresponding user
logger.debug("event.organizer?.email: ${event.organizer?.email}")
val organizer = event?.organizer?.email
// Check if the user found by organizer email is a system user
val user = organizer?.let { userDomainService.findByEmail(it) }
val user = organizer?.let { userDomainService.findByEmail(it) }
val email = if (user != null && user.tag == "system") {
logger.trace("[toBookingDTO] organizer email derived from event description")
event.description?.substringBefore(" ") ?: ""
......@@ -290,15 +318,25 @@ class GoogleCalendarProvider(
findOrCreateUserByEmail(attendee.email)
} ?: emptyList()
val workspace: Workspace = if (calendarId != null) {
val calendarEntity = workspaceDomainService.findCalendarEntityById(calendarId)
if (calendarEntity == null) throw IllegalStateException("CalendarEntity not found for calendar ID: $calendarId")
val foundWorkspace = workspaceDomainService.findById(calendarEntity.workspaceId)
foundWorkspace ?: throw IllegalStateException("Workspace not found for ID: ${calendarEntity.workspaceId}")
} else {
throw IllegalStateException("Workspace not found for calendar ID: $calendarId")
// Get the calendar ID from the event or use the provided one
val workspaceCalendarId = getCalendarId(event)
val workspace: Workspace = try {
if (workspaceCalendarId != null) {
val calendarEntity = workspaceDomainService.findCalendarEntityById(workspaceCalendarId)
if (calendarEntity == null) throw IllegalStateException("CalendarEntity not found for calendar ID: $workspaceCalendarId")
val foundWorkspace = workspaceDomainService.findById(calendarEntity.workspaceId)
foundWorkspace
?: throw IllegalStateException("Workspace not found for ID: ${calendarEntity.workspaceId}")
} else {
throw IllegalStateException("Workspace not found for calendar ID: $workspaceCalendarId")
}
} catch (e: IllegalStateException) {
logger.error("Workspace not found for calendar ID: $workspaceCalendarId")
Workspace(UUID.randomUUID(), "Unknown", "Unknown", emptyList())
}
// Extract recurring booking ID from event description if it exists
val recurringBookingIdStr = event.description?.let {
val regex = "Recurring Booking ID: ([0-9a-f-]+)".toRegex()
......
......@@ -63,6 +63,6 @@ class OrganizerRepositoryImpl(private val api: UserApi) : OrganizerRepository {
)
},
successMapper = { user ->
user.filter { it.tag == "employee" }.map { it.toOrganizer() }
user.filter { it.tag == "employer" }.map { it.toOrganizer() }
})
}
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать