Skip to content

Commit

Permalink
alerts-server: Return triggeredOn field on fixed price alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC committed Jan 21, 2018
1 parent 8859e74 commit 55030b4
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FixedPriceAlertPostgresDAO @Inject() (
|VALUES
| ({user_id}, {currency_id}, {is_greater_than}, {price}, {base_price})
|RETURNING
| fixed_price_alert_id, user_id, currency_id, is_greater_than, price, base_price, created_on
| fixed_price_alert_id, user_id, currency_id, is_greater_than, price, base_price, created_on, triggered_on
""".stripMargin
).on(
"user_id" -> userId.string,
Expand Down Expand Up @@ -55,7 +55,7 @@ class FixedPriceAlertPostgresDAO @Inject() (
SQL(
"""
|SELECT fixed_price_alert_id, user_id, currency_id, is_greater_than, price, base_price,
| exchange, market, currency, created_on
| exchange, market, currency, created_on, triggered_on
|FROM fixed_price_alerts INNER JOIN currencies USING (currency_id)
|WHERE triggered_on IS NULL AND
| currency_id = {currency_id} AND
Expand Down Expand Up @@ -85,7 +85,7 @@ class FixedPriceAlertPostgresDAO @Inject() (
SQL(
s"""
|SELECT fixed_price_alert_id, user_id, currency_id, is_greater_than, price, base_price,
| exchange, market, currency, created_on
| exchange, market, currency, created_on, triggered_on
|FROM fixed_price_alerts INNER JOIN currencies USING (currency_id)
|${whereClause.sql}
|$orderBySQL
Expand Down Expand Up @@ -124,7 +124,7 @@ class FixedPriceAlertPostgresDAO @Inject() (
|WHERE fixed_price_alert_id = {id} AND
| user_id = {user_id} AND
| triggered_on IS NULL
|RETURNING fixed_price_alert_id, user_id, currency_id, is_greater_than, price, base_price, created_on
|RETURNING fixed_price_alert_id, user_id, currency_id, is_greater_than, price, base_price, created_on, triggered_on
""".stripMargin
).on(
"id" -> id.long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object CommonParsers {

val parseCreatedOn = get[OffsetDateTime]("created_on")(timestamptzToOffsetDateTime)

private def timestamptzToOffsetDateTime: Column[OffsetDateTime] = Column.nonNull { case (value, meta) =>
def timestamptzToOffsetDateTime: Column[OffsetDateTime] = Column.nonNull { case (value, meta) =>
val MetaDataItem(qualified, _, clazz) = meta
value match {
case timestamp: java.sql.Timestamp =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.alexitc.coinalerts.data.anorm.parsers

import java.time.OffsetDateTime

import anorm.SqlParser.{bool, get, long}
import anorm.~
import com.alexitc.coinalerts.models.{FixedPriceAlert, FixedPriceAlertId, FixedPriceAlertWithCurrency}
Expand All @@ -14,6 +16,8 @@ object FixedPriceAlertParsers {
val parseisGreaterThan = bool("is_greater_than")
val parsePrice = get[BigDecimal]("price")
val parseBasePrice = get[BigDecimal]("base_price")
val parseTriggeredOn = get[OffsetDateTime]("triggered_on")(timestamptzToOffsetDateTime)


val parseFixedPriceAlert = (
parseFixedPriceAlertId ~
Expand All @@ -22,10 +26,11 @@ object FixedPriceAlertParsers {
parseisGreaterThan ~
parsePrice ~
parseBasePrice.? ~
parseCreatedOn).map {
parseCreatedOn ~
parseTriggeredOn.?).map {

case alertId ~ userId ~ currencyId ~ isGreaterThan ~ price ~ basePrice ~ createdOn =>
FixedPriceAlert(alertId, userId, currencyId, isGreaterThan, price, basePrice, createdOn)
case alertId ~ userId ~ currencyId ~ isGreaterThan ~ price ~ basePrice ~ createdOn ~ triggeredOn =>
FixedPriceAlert(alertId, userId, currencyId, isGreaterThan, price, basePrice, createdOn, triggeredOn)
}

val parseFixedPriceAlertWithCurrency = (
Expand All @@ -38,9 +43,32 @@ object FixedPriceAlertParsers {
parseisGreaterThan ~
parsePrice ~
parseBasePrice.? ~
parseCreatedOn).map {
parseCreatedOn ~
parseTriggeredOn.?).map {

case alertId ~
userId ~
exchangeCurrencyId ~
exchange ~
market ~
currency ~
isGreaterThan ~
price ~
basePrice ~
createdOn ~
triggeredOn =>

case alertId ~ userId ~ exchangeCurrencyId ~ exchange ~ market ~ currency ~ isGreaterThan ~ price ~ basePrice ~ createdOn =>
FixedPriceAlertWithCurrency(alertId, userId, exchangeCurrencyId, exchange, market, currency, isGreaterThan, price, basePrice, createdOn)
FixedPriceAlertWithCurrency(
alertId,
userId,
exchangeCurrencyId,
exchange,
market,
currency,
isGreaterThan,
price,
basePrice,
createdOn,
triggeredOn)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ case class FixedPriceAlert(
isGreaterThan: Boolean,
price: BigDecimal,
basePrice: Option[BigDecimal] = None,
createdOn: OffsetDateTime
createdOn: OffsetDateTime,
triggeredOn: Option[OffsetDateTime]
)

/**
Expand All @@ -31,7 +32,8 @@ case class FixedPriceAlertWithCurrency(
isGreaterThan: Boolean,
price: BigDecimal,
basePrice: Option[BigDecimal] = None,
createdOn: OffsetDateTime
createdOn: OffsetDateTime,
triggeredOn: Option[OffsetDateTime]
)
object FixedPriceAlertWithCurrency {

Expand All @@ -46,7 +48,8 @@ object FixedPriceAlertWithCurrency {
fixedPriceAlert.isGreaterThan,
fixedPriceAlert.price,
fixedPriceAlert.basePrice,
fixedPriceAlert.createdOn
fixedPriceAlert.createdOn,
fixedPriceAlert.triggeredOn
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ class FixedPriceAlertsControllerSpec extends PlayAPISpec {
}
}

"return triggeredOn field on a triggered alert" in {
val user = createVerifiedUser()
val token = jwtService.createToken(user)
val currencies = exchangeCurrencyDataHandler.getAll().get
val alert = createFixedPriceAlert(user.id, RandomDataGenerator.item(currencies).id).get
alertDataHandler.markAsTriggered(alert.id)

val query = PaginatedQuery(Offset(0), Limit(10))
val response = GET(url.withQueryParams(query), token.toHeader)
val json = contentAsJson(response)
status(response) mustEqual OK

val alertJsonList = (json \ "data").as[List[JsValue]]
alertJsonList.length mustEqual 1
(json \ "data").as[List[JsValue]].foreach { alertJson =>
(alertJson \ "triggeredOn").asOpt[OffsetDateTime].isDefined mustEqual true
}
}

"Allow to not set limit and offset params" in {
val user = createVerifiedUser()
val token = jwtService.createToken(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ trait FixedPriceAlertInMemoryDataHandler extends FixedPriceAlertBlockingDataHand
createAlertModel.isGreaterThan,
createAlertModel.price,
createAlertModel.basePrice,
OffsetDateTime.now)
OffsetDateTime.now,
None)

alertList += fixedPriceAlert

Expand Down Expand Up @@ -58,8 +59,18 @@ trait FixedPriceAlertInMemoryDataHandler extends FixedPriceAlertBlockingDataHand
if (triggeredAlertList.contains(alertId)) {
Bad(FixedPriceAlertNotFoundError).accumulating
} else {
triggeredAlertList += alertId
Good(())
val maybe = alertList.find(_.id == alertId).map { alert =>
alertList -= alert

val updatedAlert = alert.copy(triggeredOn = Some(OffsetDateTime.now()))
alertList += updatedAlert
triggeredAlertList += alertId

updatedAlert
}

Or.from(maybe, One(FixedPriceAlertNotFoundError))
.map(_ => ())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,11 @@ class FixedPriceAlertPostgresDataHandlerSpec extends PostgresDataHandlerSpec {
val result = alertPostgresDataHandler.getAlerts(conditions, defaultOrderByConditions, query).get

result.data.length mustEqual 1
result.data.head.id mustEqual triggeredAlert.id
result.total mustEqual Count(1)

val alert = result.data.head
alert.id mustEqual triggeredAlert.id
alert.triggeredOn.isDefined mustEqual true
}

"return alerts sorted by createdOn" in {
Expand Down

0 comments on commit 55030b4

Please sign in to comment.