Skip to content

Commit

Permalink
skip job queue for existing deal (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
psuzn authored Dec 16, 2023
1 parent c8873c1 commit 59ed0a4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ interface DealRepository {
suspend fun getPotentiallyExpiredDeals(): List<DealEntity>

suspend fun getNewDeals(since: OffsetDateTime): List<DealEntity>

suspend fun getDealByPackageName(packageName: String): DealEntity?
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@ class PersistentDealRepository(
.await()
.map { it.asAppDeal() }
}

override suspend fun getDealByPackageName(packageName: String): DealEntity? {
return sqlClient.preparedQuery(
"""
SELECT * FROM "deal" where id = $1
""".trimIndent()
).exec(packageName)
.await()
.firstOrNull()?.asAppDeal()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.sujanpoudel.playdeals.usecases

import me.sujanpoudel.playdeals.jobs.AppDetailScrapper
import me.sujanpoudel.playdeals.repositories.DealRepository
import org.jobrunr.scheduling.JobRequestScheduler
import org.kodein.di.DI
import org.kodein.di.instance
Expand All @@ -11,9 +12,17 @@ class NewDealUseCase(
) : UseCase<String, Unit> {

private val jobRequestScheduler by di.instance<JobRequestScheduler>()
private val dealsRepository by di.instance<DealRepository>()

override suspend fun doExecute(input: String) {
val existingDeal = dealsRepository.getDealByPackageName(input)

if (existingDeal != null) {
return
}

val id = UUID.nameUUIDFromBytes(input.toByteArray())

jobRequestScheduler.enqueue(id, AppDetailScrapper.Request(input))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import io.kotest.matchers.shouldBe
import io.vertx.core.Vertx
import io.vertx.kotlin.core.json.jsonObjectOf
import io.vertx.kotlin.coroutines.await
import me.sujanpoudel.playdeals.Constants
import me.sujanpoudel.playdeals.IntegrationTest
import me.sujanpoudel.playdeals.domain.NewDeal
import me.sujanpoudel.playdeals.domain.entities.DealType
import me.sujanpoudel.playdeals.get
import me.sujanpoudel.playdeals.repositories.DealRepository
import org.jobrunr.jobs.states.StateName
import org.jobrunr.storage.StorageProvider
import org.junit.jupiter.api.Test
import java.time.OffsetDateTime
import java.util.UUID

class NewDealApiTest(vertx: Vertx) : IntegrationTest(vertx) {
Expand Down Expand Up @@ -53,4 +58,37 @@ class NewDealApiTest(vertx: Vertx) : IntegrationTest(vertx) {

response.statusCode() shouldBe 200
}

@Test
fun `should should 200 if the app already exists`() = runTest {
di.get<StorageProvider>()
val repository = di.get<DealRepository>()

val packageName = "com.example.app"

val newDeal = NewDeal(
id = packageName,
name = "name",
icon = "icon",
images = listOf("img0", "img1"),
normalPrice = 12f,
currentPrice = 12f,
currency = "USD",
storeUrl = "store_url",
category = "unknown",
downloads = "12+",
rating = "12",
offerExpiresIn = OffsetDateTime.now(),
type = DealType.ANDROID_APP,
source = Constants.DealSources.APP_DEAL_SUBREDDIT
)

repository.upsert(newDeal)

val response = httpClient.post("/api/deals")
.sendJson(jsonObjectOf("packageName" to packageName))
.await()

response.statusCode() shouldBe 200
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,25 @@ class PersistentDealRepositoryTest(vertx: Vertx) : IntegrationTest(vertx) {

count.shouldContainAll(deal1, deal2)
}

@Test
fun `getDealByPackageName should return correct deal by packageName`() = runTest {
val deal0 = repository.upsert(newDeal)

repository.upsert(newDeal.copy(id = "id_1"))
repository.upsert(newDeal.copy(id = "id_2"))

val deal01 = repository.getDealByPackageName(deal0.id)

deal0 shouldBe deal01
}

@Test
fun `getDealByPackageName should return null when there is no deal`() = runTest {
val deal0 = repository.upsert(newDeal)

val deal1 = repository.getDealByPackageName("id_3")

deal1 shouldBe null
}
}

0 comments on commit 59ed0a4

Please sign in to comment.