Skip to content

Commit

Permalink
feat(backend): merge existing data between OpenPlanner and the project.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Apr 5, 2024
1 parent 6dfd464 commit 2a5ffa5
Show file tree
Hide file tree
Showing 13 changed files with 405 additions and 151 deletions.
11 changes: 10 additions & 1 deletion backend/src/main/java/org/gdglille/devfest/backend/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import org.gdglille.devfest.backend.third.parties.conferencehall.ConferenceHallA
import org.gdglille.devfest.backend.third.parties.conferencehall.registerConferenceHallRoutes
import org.gdglille.devfest.backend.third.parties.geocode.GeocodeApi
import org.gdglille.devfest.backend.third.parties.openplanner.OpenPlannerApi
import org.gdglille.devfest.backend.third.parties.openplanner.registerOpenPlannerRoutes
import org.gdglille.devfest.backend.third.parties.welovedevs.WeLoveDevsApi
import org.gdglille.devfest.backend.third.parties.welovedevs.registerWLDRoutes
import org.gdglille.devfest.models.inputs.Validator
Expand Down Expand Up @@ -160,7 +161,6 @@ fun main() {
routing {
registerEventRoutes(
geocodeApi,
openPlannerApi,
eventDao,
speakerDao,
qAndADao,
Expand Down Expand Up @@ -209,6 +209,15 @@ fun main() {
categoryDao,
formatDao
)
registerOpenPlannerRoutes(
openPlannerApi,
eventDao,
speakerDao,
talkDao,
categoryDao,
formatDao,
scheduleItemDao
)
registerWLDRoutes(wldApi, eventDao, partnerDao, jobDao)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,8 @@ class CategoryDao(private val database: Database) {
)
}

suspend fun createIfNotExist(eventId: String, item: CategoryDb) {
if (item.id == null) {
database.insert(
eventId = eventId,
collectionName = CollectionName
) { item.copy(id = it) }
} else {
val existing = get(eventId, item.id)
if (existing == null) {
database.insert(
eventId = eventId,
collectionName = CollectionName
) { item.copy(id = it) }
}
}
suspend fun deleteDiff(eventId: String, ids: List<String>) {
val diff = database.diff(eventId, CollectionName, ids)
database.deleteAll(eventId, CollectionName, diff)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ import org.gdglille.devfest.backend.talks.TalkDao
import org.gdglille.devfest.backend.talks.convertToModel
import org.gdglille.devfest.backend.third.parties.geocode.GeocodeApi
import org.gdglille.devfest.backend.third.parties.geocode.convertToDb
import org.gdglille.devfest.backend.third.parties.openplanner.OpenPlannerApi
import org.gdglille.devfest.backend.third.parties.openplanner.convertToDb
import org.gdglille.devfest.backend.third.parties.openplanner.convertToScheduleDb
import org.gdglille.devfest.backend.third.parties.openplanner.convertToTalkDb
import org.gdglille.devfest.models.Agenda
import org.gdglille.devfest.models.CreatedEvent
import org.gdglille.devfest.models.Event
Expand All @@ -39,7 +35,6 @@ import java.time.LocalDateTime
@Suppress("LongParameterList")
class EventRepository(
private val geocodeApi: GeocodeApi,
private val openPlannerApi: OpenPlannerApi,
private val eventDao: EventDao,
private val speakerDao: SpeakerDao,
private val qAndADao: QAndADao,
Expand Down Expand Up @@ -150,47 +145,4 @@ class EventRepository(
}.awaitAll().associate { it }.toSortedMap()
return@coroutineScope Agenda(talks = schedules)
}

suspend fun openPlanner(eventId: String, apiKey: String) =
coroutineScope {
val event = eventDao.getVerified(eventId, apiKey)
val config = event.openPlannerConfig
?: throw NotAcceptableException("OpenPlanner config not initialized")
val openPlanner = openPlannerApi.fetchPrivateJson(config.eventId, config.privateId)
openPlanner.event.categories
.map { async { categoryDao.createIfNotExist(eventId, it.convertToDb()) } }
.awaitAll()
openPlanner.event.formats
.map { async { formatDao.createOrUpdate(eventId, it.convertToDb()) } }
.awaitAll()
val allSpeakers = openPlanner.sessions
.map { it.speakerIds }.flatten()
openPlanner.speakers
.filter { allSpeakers.contains(it.id) }
.map { async { speakerDao.createOrUpdate(eventId, it.convertToDb()) } }
.awaitAll()
openPlanner.sessions
.map { async { talkDao.createOrUpdate(eventId, it.convertToTalkDb()) } }
val trackIds = openPlanner.event.tracks.map { it.id }
openPlanner.sessions
.filter { it.trackId != null && it.dateStart != null && it.dateEnd != null }
.groupBy { it.dateStart }
.map {
async {
it.value
.sortedWith { sessionA, sessionB ->
trackIds.indexOf(sessionA.trackId)
.compareTo(trackIds.indexOf(sessionB.trackId))
}
.forEachIndexed { index, sessionOP ->
scheduleItemDao.createOrUpdate(
eventId,
sessionOP.convertToScheduleDb(index, openPlanner.event.tracks)
)
}
}
}
.awaitAll()
eventDao.updateAgendaUpdatedAt(event)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import io.ktor.server.routing.Route
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.put
import org.gdglille.devfest.backend.NotAuthorized
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
import org.gdglille.devfest.backend.NotFoundException
import org.gdglille.devfest.backend.categories.CategoryDao
import org.gdglille.devfest.backend.formats.FormatDao
Expand All @@ -28,22 +31,16 @@ import org.gdglille.devfest.backend.schedulers.ScheduleItemDao
import org.gdglille.devfest.backend.speakers.SpeakerDao
import org.gdglille.devfest.backend.talks.TalkDao
import org.gdglille.devfest.backend.third.parties.geocode.GeocodeApi
import org.gdglille.devfest.backend.third.parties.openplanner.OpenPlannerApi
import org.gdglille.devfest.backend.version
import org.gdglille.devfest.models.inputs.CoCInput
import org.gdglille.devfest.models.inputs.CreatingEventInput
import org.gdglille.devfest.models.inputs.EventInput
import org.gdglille.devfest.models.inputs.FeaturesActivatedInput
import org.gdglille.devfest.models.inputs.LunchMenuInput
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZonedDateTime

@Suppress("LongMethod", "LongParameterList", "MagicNumber")
fun Route.registerEventRoutes(
geocodeApi: GeocodeApi,
openPlannerApi: OpenPlannerApi,
eventDao: EventDao,
speakerDao: SpeakerDao,
qAndADao: QAndADao,
Expand All @@ -55,7 +52,6 @@ fun Route.registerEventRoutes(
) {
val repository = EventRepository(
geocodeApi,
openPlannerApi,
eventDao,
speakerDao,
qAndADao,
Expand Down Expand Up @@ -155,9 +151,4 @@ fun Route.registerEventRoutes(
val eventId = call.parameters["eventId"]!!
call.respond(HttpStatusCode.OK, repositoryV2.openFeedback(eventId))
}
post("/events/{eventId}/openplanner") {
val eventId = call.parameters["eventId"]!!
val apiKey = call.request.queryParameters["api_key"] ?: throw NotAuthorized
call.respond(HttpStatusCode.Created, repository.openPlanner(eventId, apiKey))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ class FormatDao(private val database: Database) {
item = item
)
}

suspend fun deleteDiff(eventId: String, ids: List<String>) {
val diff = database.diff(eventId, CollectionName, ids)
database.deleteAll(eventId, CollectionName, diff)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ interface Database {
suspend fun <T : Any> update(eventId: String, collectionName: String, id: String, item: T)
suspend fun delete(eventId: String, collectionName: String, id: String)
suspend fun delete(eventId: String, collectionName: String)
suspend fun deleteAll(eventId: String, collectionName: String, ids: List<String>)
suspend fun diff(eventId: String, collectionName: String, ids: List<String>): List<String>

object Factory {
fun create(firestore: Firestore, projectName: String): Database = FirestoreDatabase(firestore, projectName)
Expand Down
Loading

0 comments on commit 2a5ffa5

Please sign in to comment.