Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
madhead committed Oct 26, 2023
1 parent edd5484 commit d854518
Show file tree
Hide file tree
Showing 59 changed files with 1,238 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.madhead.tyzenhaus.core.service

import me.madhead.tyzenhaus.entity.transaction.Transaction
import me.madhead.tyzenhaus.repository.TransactionRepository

data class TransactionsSearchParams(
val title: String? = null,
)

class TransactionsSearchService(
private val transactionRepository: TransactionRepository,
) {
fun search(group: Long, searchParams: TransactionsSearchParams): List<Transaction> {
return transactionRepository.search(group)
}
}
4 changes: 4 additions & 0 deletions launcher/fly/requests.http
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ Authorization: Bearer {{api_token}}
### Get group currencies
GET https://{{ngrok}}/app/api/group/currencies
Authorization: Bearer {{api_token}}

### Search for transactions
GET https://{{ngrok}}/app/api/group/transactions
Authorization: Bearer {{api_token}}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package me.madhead.tyzenhaus.launcher.fly.koin

import me.madhead.tyzenhaus.core.service.GroupCurrenciesService
import me.madhead.tyzenhaus.core.service.GroupMembersService
import me.madhead.tyzenhaus.core.service.TransactionsSearchService
import org.koin.dsl.module

val serviceModule = module {
Expand All @@ -17,4 +18,10 @@ val serviceModule = module {
groupConfigRepository = get(),
)
}

single {
TransactionsSearchService(
transactionRepository = get()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.madhead.tyzenhaus.launcher.fly.modules

import io.ktor.http.HttpStatusCode
import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.Application
import io.ktor.server.application.install
Expand All @@ -10,6 +11,8 @@ import io.ktor.server.plugins.callloging.CallLogging
import io.ktor.server.plugins.compression.Compression
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.defaultheaders.DefaultHeaders
import io.ktor.server.response.header
import io.ktor.server.response.respond
import io.ktor.server.routing.routing
import io.micrometer.prometheus.PrometheusMeterRegistry
import java.time.Instant
Expand Down Expand Up @@ -68,8 +71,19 @@ fun Application.tyzenhaus() {
return@authenticate null
}
val tokenRepository by inject<APITokenRepository>()
val apiToken = tokenRepository.get(token)

tokenRepository.get(token)?.takeIf { it.validUntil > Instant.now() }?.let { APITokenPrincipal(it.groupId, it.scope) }
return@authenticate when {
apiToken == null -> null

apiToken.validUntil < Instant.now() -> {
this.response.header("X-Token-Expired", apiToken.validUntil)
this.respond(HttpStatusCode.Forbidden)
null
}

else -> APITokenPrincipal(apiToken.groupId, apiToken.scope)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import io.ktor.server.routing.route
import io.ktor.utils.io.core.toByteArray
import me.madhead.tyzenhaus.core.service.GroupCurrenciesService
import me.madhead.tyzenhaus.core.service.GroupMembersService
import me.madhead.tyzenhaus.core.service.TransactionsSearchParams
import me.madhead.tyzenhaus.core.service.TransactionsSearchService
import me.madhead.tyzenhaus.launcher.fly.security.APITokenPrincipal
import org.koin.ktor.ext.get
import org.koin.ktor.ext.inject
Expand All @@ -28,6 +30,7 @@ fun Route.miniAppAPI() {
val config by inject<ApplicationConfig>()
val groupMembersService by inject<GroupMembersService>()
val groupCurrenciesService by inject<GroupCurrenciesService>()
val transactionsSearchService by inject<TransactionsSearchService>()
val webAppDataSecretKeyHash by lazy {
HMAC.hmacSHA256(
"WebAppData".toByteArray(),
Expand Down Expand Up @@ -77,6 +80,12 @@ fun Route.miniAppAPI() {

call.respond(currencies)
}

get("transactions") {
val principal = call.principal<APITokenPrincipal>()!!

call.respond(transactionsSearchService.search(principal.groupId, TransactionsSearchParams()))
}
}
}
}
Expand Down
Loading

0 comments on commit d854518

Please sign in to comment.