Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Feature/fix calendar retreival into DEV #176

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Koja is a system designed to enable more efficient time utilization through dynamic and fixed time allocation for specific tasks a user wants to complete. The primary goal of Koja is to alleviate the challenges of schedule management with minimal effort from the user.

# Advertisement
https://github.com/COS301-SE-2023/Koja/assets/105129419/1d39ca2c-c081-4534-94d4-7558d399c484
https://github.com/COS301-SE-2023/Koja/assets/105129419/9a3a9de2-e4e1-48c0-ba80-0d4e6b4751f9

# Documentation
* [SRSv4](https://www.canva.com/design/DAFm6EdVuUo/d1PDPN7l5k5e_cx3V7UT1A/edit?utm_content=DAFm6EdVuUo&utm_campaign=designshare&utm_medium=link2&utm_source=sharebutton)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class AIUserDataService(private val userRepository: UserRepository, private val
OffsetDateTime.now(),
)
} else {
adapter.getUserEvents(it.getAccessToken()).values.toList()
adapter.getUserEvents(it.getAccessToken(), userAccount.email).values.toList()
}
}
return events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class CalendarAdapterService(authProvider: AuthProviderEnum) {
): RedirectView
abstract fun authorize(): String?
abstract fun oauth2Callback(authCode: String?, deviceType: CallbackConfigEnum): String
abstract fun getUserEvents(accessToken: String): Map<String, UserEventDTO>
abstract fun getUserEvents(accessToken: String, emailAddress: String): Map<String, UserEventDTO>
abstract fun getUserEventsKojaSuggestions(accessToken: String): Map<String, UserEventDTO>

abstract fun getUserEventsInRange(accessToken: String?, startDate: OffsetDateTime?, endDate: OffsetDateTime?): List<UserEventDTO>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,34 +373,47 @@ class GoogleCalendarAdapterService(
userRepository.save(storedUser)
}

override fun getUserEvents(accessToken: String): Map<String, UserEventDTO> {
override fun getUserEvents(accessToken: String, emailAddress: String): Map<String, UserEventDTO> {
try {
val calendar = buildCalendarService(accessToken)

val request = calendar.events().list("primary")
.setOrderBy("startTime")
.setSingleEvents(true)
.setMaxResults(1000)

val events: Events? = request.execute()
var events = fetchEventsFromCalendar(calendar, emailAddress.trim())

val userEvents = mutableMapOf<String, UserEventDTO>()
if (events.isNullOrEmpty()) {
events = fetchEventsFromCalendar(calendar, "primary")
}

events?.items?.map {
val eventSummary = it.summary ?: ""
val eventStartTime = it.start.dateTime ?: it.start.date
val eventEndTime = it.end.dateTime ?: it.end.date
val key = Base64.getEncoder()
.encodeToString("${eventSummary.trim()}${eventStartTime}$eventEndTime".trim().toByteArray())
userEvents[key] = UserEventDTO(it)
if (events.isNullOrEmpty()) {
val userCalendars = calendar.calendarList().list().execute().items
events = if (userCalendars.isNotEmpty()) {
fetchEventsFromCalendar(calendar, userCalendars.first().id)
} else {
null
}
}

return userEvents
return events?.items?.mapNotNull {
it?.let { event ->
val eventSummary = event.summary ?: ""
val eventStartTime = event.start.dateTime ?: event.start.date
val eventEndTime = event.end.dateTime ?: event.end.date
val key = Base64.getEncoder().encodeToString("$eventSummary$eventStartTime$eventEndTime".trim().toByteArray())
key to UserEventDTO(event)
}
}?.toMap() ?: emptyMap()
} catch (e: ExpiredJwtException) {
return emptyMap()
}
}

fun fetchEventsFromCalendar(calendar: com.google.api.services.calendar.Calendar, calendarId: String): Events? {
return calendar.events().list(calendarId)
.setOrderBy("startTime")
.setSingleEvents(true)
.setMaxResults(1000)
.execute()
}

override fun getUserEventsKojaSuggestions(accessToken: String): Map<String, UserEventDTO> {
try {
val calendar = buildCalendarService(accessToken)
Expand Down Expand Up @@ -760,6 +773,8 @@ class GoogleCalendarAdapterService(
val startDateTime = DateTime(startDate.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME))
val endDateTime = DateTime(endDate.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME))

val userCalendars = calendar.calendarList().list().execute().items

val request = calendar.events().list("primary")
.setTimeMin(startDateTime)
.setTimeMax(endDateTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class UserCalendarService(
for (authDetails in userAuthDetails) {
if (authDetails.getRefreshToken() == userAccount.refreshToken) {
val accessToken = authDetails.getAccessToken()
userEvents.putAll(adapter.getUserEvents(accessToken))
userEvents.putAll(adapter.getUserEvents(accessToken, userAccount.email))
}
}
}
Expand Down Expand Up @@ -794,7 +794,7 @@ class UserCalendarService(
for (authDetails in userAuthDetails) {
if (authDetails.getRefreshToken() == userAccount.refreshToken) {
val accessToken = authDetails.getAccessToken()
userEvents.putAll(adapter.getUserEvents(accessToken))
userEvents.putAll(adapter.getUserEvents(accessToken, userAccount.email))
}
}
}
Expand Down
Loading