suspend fun syncIssues()

in kotlin/sync-api/src/main/kotlin/com/example/SyncIssues.kt [21:56]


suspend fun syncIssues(spaceClient: SpaceClient, orgId: String, prjId: String) {
    val currentEtag = getCurrentEtag(prjId)
    // Fetch all new and updated issues and new etag using the stored etag
    val (updatedIssues, newEtag) = getIssues(spaceClient, prjId, currentEtag)

    transaction {
        // update the etag value in the ProjectDb table
        with(ProjectDb) {
            replace {
                it[id] = prjId
                it[organizationId] = orgId
                it[issueEtag] = newEtag
            }
        }

        with(IssueDb) {
            // Update the issues in the database
            for (issue in updatedIssues) {
                // When an issue is deleted in Space, it still exists in the archived state.
                // For simplicity, we'll delete archived issues from our database.
                // In a real app, you should sync issue `status` in both
                // Space and the third-party system.
                if (issue.archived) {
                    deleteWhere { id eq issue.id }
                } else {
                    replace {
                        it[id] = issue.id
                        it[projectId] = prjId
                        it[title] = issue.title
                        it[description] = issue.description ?: ""
                    }
                }
            }
        }
    }
}