|
| 1 | +package me.snoty.integration.todoist |
| 2 | + |
| 3 | +import io.ktor.client.* |
| 4 | +import kotlinx.coroutines.flow.toList |
| 5 | +import kotlinx.serialization.Serializable |
| 6 | +import me.snoty.integration.common.annotation.RegisterNode |
| 7 | +import me.snoty.integration.common.diff.DiffResult |
| 8 | +import me.snoty.integration.common.model.NodePosition |
| 9 | +import me.snoty.integration.common.model.metadata.FieldCensored |
| 10 | +import me.snoty.integration.common.wiring.Node |
| 11 | +import me.snoty.integration.common.wiring.NodeHandleContext |
| 12 | +import me.snoty.integration.common.wiring.data.IntermediateData |
| 13 | +import me.snoty.integration.common.wiring.data.NodeOutput |
| 14 | +import me.snoty.integration.common.wiring.get |
| 15 | +import me.snoty.integration.common.wiring.node.NodeHandler |
| 16 | +import me.snoty.integration.common.wiring.node.NodePersistenceFactory |
| 17 | +import me.snoty.integration.common.wiring.node.NodeSettings |
| 18 | +import me.snoty.integration.common.wiring.node.invoke |
| 19 | +import org.bson.Document |
| 20 | +import org.koin.core.annotation.Single |
| 21 | + |
| 22 | +@Serializable |
| 23 | +data class TodoistSettings( |
| 24 | + override val name: String = "Todoist", |
| 25 | + @FieldCensored |
| 26 | + val token: String, |
| 27 | + val projectId: String?, |
| 28 | + val sectionId: String?, |
| 29 | +) : NodeSettings |
| 30 | + |
| 31 | +@RegisterNode( |
| 32 | + type = "todoist", |
| 33 | + displayName = "Todoist", |
| 34 | + position = NodePosition.END, |
| 35 | + settingsType = TodoistSettings::class, |
| 36 | + inputType = TodoistInput::class, |
| 37 | +) |
| 38 | +@Single |
| 39 | +class TodoistNodeHandler( |
| 40 | + private val httpClient: HttpClient, |
| 41 | + private val apiFactory: (String) -> TodoistAPI = { TodoistAPIImpl(httpClient, it) }, |
| 42 | + persistenceFactory: NodePersistenceFactory, |
| 43 | +) : NodeHandler { |
| 44 | + data class Task(val externalId: String, val todoistId: String) |
| 45 | + |
| 46 | + private val taskService = persistenceFactory<Task>("tasks") |
| 47 | + |
| 48 | + override suspend fun NodeHandleContext.process(node: Node, input: Collection<IntermediateData>): NodeOutput { |
| 49 | + val settings = node.settings as TodoistSettings |
| 50 | + val api = apiFactory(settings.token) |
| 51 | + |
| 52 | + val tasks = taskService.getEntities(node).toList() |
| 53 | + input.forEach { |
| 54 | + val data = get<TodoistInput>(it) |
| 55 | + val diff = get<Document>(it)["diff"] as DiffResult |
| 56 | + |
| 57 | + suspend fun create() { |
| 58 | + api.createTask(data.toTaskCreateDTO( |
| 59 | + projectId = settings.projectId, |
| 60 | + sectionId = settings.sectionId, |
| 61 | + )) |
| 62 | + .also { response -> |
| 63 | + taskService.persistEntity( |
| 64 | + node = node, |
| 65 | + entityId = data.id, |
| 66 | + entity = Task(externalId = data.id, todoistId = response.id), |
| 67 | + ) |
| 68 | + } |
| 69 | + logger.info("Created task: {}", data.content) |
| 70 | + } |
| 71 | + |
| 72 | + when (diff) { |
| 73 | + is DiffResult.Created -> create() |
| 74 | + is DiffResult.Updated -> { |
| 75 | + tasks.getTodoistId(data.id) |
| 76 | + ?.let { taskId -> |
| 77 | + api.updateTask(taskId, data.toTaskUpdateDTO()) |
| 78 | + logger.info("Updated task: {}", data.content) |
| 79 | + } |
| 80 | + ?: create() |
| 81 | + } |
| 82 | + is DiffResult.Deleted -> { |
| 83 | + val taskId = tasks.getTodoistId(data.id) |
| 84 | + ?: run { |
| 85 | + logger.error("Task {} for entity {} not found", data.content, data.id) |
| 86 | + return@forEach |
| 87 | + } |
| 88 | + api.closeTask(taskId) |
| 89 | + logger.info("Closed task: {}", data.content) |
| 90 | + } |
| 91 | + is DiffResult.Unchanged -> {} |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return emptyList() |
| 96 | + } |
| 97 | + |
| 98 | + private fun List<Task>.getTodoistId(externalId: String) = |
| 99 | + firstOrNull { it.externalId == externalId }?.todoistId |
| 100 | +} |
0 commit comments