From 49ce1622c031405fa5dd6fb78bbeeb2bb1b0390a Mon Sep 17 00:00:00 2001 From: Daniil Zavyalov Date: Wed, 27 Sep 2023 20:00:32 +0600 Subject: [PATCH 1/3] [~] add async FCM call --- .../common/notifications/FcmNotificationSender.kt | 8 ++++---- .../effective/features/booking/routes/BookingRouting.kt | 3 --- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/common/notifications/FcmNotificationSender.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/common/notifications/FcmNotificationSender.kt index c2ca3d78..9c63d168 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/common/notifications/FcmNotificationSender.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/common/notifications/FcmNotificationSender.kt @@ -24,7 +24,7 @@ class FcmNotificationSender(private val fcm: FirebaseMessaging): INotificationSe .setTopic(topic) .putData("message", "$topic was changed") .build() - fcm.send(msg) + fcm.sendAsync(msg) } /** @@ -42,7 +42,7 @@ class FcmNotificationSender(private val fcm: FirebaseMessaging): INotificationSe .putData("action", action.value) .putData("object", json) .build() - fcm.send(msg) + fcm.sendAsync(msg) } /** @@ -60,7 +60,7 @@ class FcmNotificationSender(private val fcm: FirebaseMessaging): INotificationSe .putData("action", action.value) .putData("object", json) .build() - fcm.send(msg) + fcm.sendAsync(msg) } /** @@ -78,6 +78,6 @@ class FcmNotificationSender(private val fcm: FirebaseMessaging): INotificationSe .putData("action", action.value) .putData("object", json) .build() - fcm.send(msg) + fcm.sendAsync(msg) } } \ No newline at end of file diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/routes/BookingRouting.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/routes/BookingRouting.kt index ff52d7e4..4a8e11a6 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/routes/BookingRouting.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/routes/BookingRouting.kt @@ -48,14 +48,12 @@ fun Route.bookingRouting() { call.response.status(HttpStatusCode.Created) val result = bookingFacade.post(dto) - sender.sendEmptyMessage("booking") call.respond(result) } put(SwaggerDocument.putBooking()) { val dto = call.receive() val result = bookingFacade.put(dto) - sender.sendEmptyMessage("booking") call.respond(result) } delete("{id}", SwaggerDocument.deleteBookingById()) { @@ -63,7 +61,6 @@ fun Route.bookingRouting() { ?: return@delete call.respond(HttpStatusCode.BadRequest) bookingFacade.deleteById(id) - sender.sendEmptyMessage("booking") call.respond(HttpStatusCode.NoContent) } } -- GitLab From c649a385c5039e37963425ca500568991410b11a Mon Sep 17 00:00:00 2001 From: Daniil Zavyalov Date: Wed, 27 Sep 2023 21:44:23 +0600 Subject: [PATCH 2/3] [~] fix booking --- .../repository/BookingCalendarRepository.kt | 31 +++++++++++-------- .../repository/BookingWorkspaceRepository.kt | 2 +- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingCalendarRepository.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingCalendarRepository.kt index f4a22190..20a17dde 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingCalendarRepository.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingCalendarRepository.kt @@ -68,7 +68,7 @@ class BookingCalendarRepository( try { calendarEvents.delete(defaultCalendar, id).execute() //We can't delete directly from workspace calendar } catch (e: GoogleJsonResponseException) { - if (e.statusCode != 404) { + if (e.statusCode != 404 && e.statusCode != 410) { throw e } } @@ -286,10 +286,13 @@ class BookingCalendarRepository( * @author Danil Kiselev */ override fun save(booking: Booking): Booking { + val workspaceCalendar = calendarIdsRepository.findByWorkspace( + booking.workspace.id ?: throw MissingIdException("Missing workspace id") + ) val event = googleCalendarConverter.toGoogleEvent(booking) val savedEvent = calendar.Events().insert(defaultCalendar, event).execute() - if (checkBookingAvailable(savedEvent)) { + if (checkBookingAvailable(savedEvent, workspaceCalendar)) { return googleCalendarConverter.toBookingModel(savedEvent) } else { deleteById(savedEvent.id) @@ -304,12 +307,15 @@ class BookingCalendarRepository( * * @param booking changed booking * @return [Booking] after change saving - * @throws MissingIdException if [Booking.id] is null + * @throws MissingIdException if [Booking.id] or [Booking.workspace].id is null * @throws InstanceNotFoundException if booking given id doesn't exist in the database * @throws WorkspaceUnavailableException if booking unavailable because of collision check * @author Daniil Zavyalov, Danil Kiselev */ override fun update(booking: Booking): Booking { + val workspaceCalendar = calendarIdsRepository.findByWorkspace( + booking.workspace.id ?: throw MissingIdException("Missing workspace id") + ) val bookingId = booking.id ?: throw MissingIdException("Update model must have id") val previousVersionOfEvent = findByCalendarIdAndBookingId(bookingId) ?: throw InstanceNotFoundException( WorkspaceBookingEntity::class, "Booking with id $bookingId not wound" @@ -319,7 +325,7 @@ class BookingCalendarRepository( val updatedEvent: Event = calendarEvents.update(defaultCalendar, bookingId, eventOnUpdate).execute() val sequence = updatedEvent.sequence - if (checkBookingAvailable(updatedEvent)) { + if (checkBookingAvailable(updatedEvent, workspaceCalendar)) { return googleCalendarConverter.toBookingModel(updatedEvent) } else { previousVersionOfEvent.sequence = sequence @@ -337,20 +343,20 @@ class BookingCalendarRepository( * @return Boolean. True if booking available * @author Kiselev Danil * */ - private fun checkBookingAvailable(incomingEvent: Event): Boolean { + private fun checkBookingAvailable(incomingEvent: Event, workspaceCalendar: String): Boolean { var isAvailable = false; if (incomingEvent.recurrence != null) { //TODO: Check, if we can receive instances without pushing this event into calendar - calendarEvents.instances(defaultCalendar, incomingEvent.id).execute().items.forEach { event -> - if (!checkSingleEventCollision(event)) { + calendarEvents.instances(defaultCalendar, incomingEvent.id).setMaxResults(50).execute().items.forEach { event -> + if (!checkSingleEventCollision(event, workspaceCalendar)) { return@checkBookingAvailable false } else { isAvailable = true } } } else { - isAvailable = checkSingleEventCollision(incomingEvent) + isAvailable = checkSingleEventCollision(incomingEvent, workspaceCalendar) } return isAvailable } @@ -361,11 +367,10 @@ class BookingCalendarRepository( * @param event: [Event] - Must take only SAVED event * @author Kiselev Danil * */ - private fun checkSingleEventCollision(event: Event): Boolean { - //+- 1 is needed because setMinTime and setMaxTime from basicQuery are exclusive conditions. - val leftBorder = event.start.dateTime.value - 1 - val rightBorder = event.end.dateTime.value + 1 - val savedEvents = basicQuery(leftBorder, rightBorder).execute().items + private fun checkSingleEventCollision(event: Event, workspaceCalendar: String): Boolean { + val savedEvents = basicQuery(event.start.dateTime.value, event.end.dateTime.value) + .setQ(workspaceCalendar) + .execute().items for (i in savedEvents) { if ( !((i.start.dateTime.value > event.end.dateTime.value) || (i.end.dateTime.value < event.start.dateTime.value)) diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingWorkspaceRepository.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingWorkspaceRepository.kt index ef3d1c77..390f9295 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingWorkspaceRepository.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingWorkspaceRepository.kt @@ -53,7 +53,7 @@ class BookingWorkspaceRepository( try { calendarEvents.delete(workspaceCalendar, id).execute() } catch (e: GoogleJsonResponseException) { - if (e.statusCode != 404) { + if (e.statusCode != 404 && e.statusCode != 410) { throw e } } -- GitLab From 5d565b85ebbde59e30a6798414038ad8a243b0d1 Mon Sep 17 00:00:00 2001 From: Daniil Zavyalov Date: Wed, 27 Sep 2023 21:46:32 +0600 Subject: [PATCH 3/3] [~] fix collision condition --- .../features/booking/repository/BookingCalendarRepository.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingCalendarRepository.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingCalendarRepository.kt index 20a17dde..2d2e4021 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingCalendarRepository.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/repository/BookingCalendarRepository.kt @@ -373,7 +373,7 @@ class BookingCalendarRepository( .execute().items for (i in savedEvents) { if ( - !((i.start.dateTime.value > event.end.dateTime.value) || (i.end.dateTime.value < event.start.dateTime.value)) + !((i.start.dateTime.value >= event.end.dateTime.value) || (i.end.dateTime.value <= event.start.dateTime.value)) && (i.id != event.id) ) { return false -- GitLab