diff --git a/handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Catalog.scala b/handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Catalog.scala new file mode 100644 index 0000000000..4ab79916ca --- /dev/null +++ b/handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Catalog.scala @@ -0,0 +1,56 @@ +package com.gu.newproduct.api.productcatalog + +import play.api.libs.json.{JsString, Json, Writes} + +sealed trait DayOfWeek + +case object Monday extends DayOfWeek + +case object Tuesday extends DayOfWeek + +case object Wednesday extends DayOfWeek + +case object Thursday extends DayOfWeek + +case object Friday extends DayOfWeek + +case object Saturday extends DayOfWeek + +case object Sunday extends DayOfWeek + +case class ProductInfo( + id: String, + label: String, + startDateRules: Option[StartDateRules] = None +) + +case class StartDateRules( + daysOfWeek: Option[List[DayOfWeek]] = None, + cutOffDayInclusive: Option[DayOfWeek] = None, + minDaysAfterCutOff: Option[Int] = None, + windowSizeDays: Option[Int] = None, +) + + +case class Group(label: String, products: List[ProductInfo]) + +case class Catalog(groups: List[Group]) + +object DayOfWeek { + implicit val writes: Writes[DayOfWeek] = { (day: DayOfWeek) => JsString(day.toString) } +} + +object StartDateRules{ + implicit val writes = Json.writes[StartDateRules] +} +object ProductInfo { + implicit val writes = Json.writes[ProductInfo] +} + +object Group { + implicit val writes = Json.writes[Group] +} + +object Catalog { + implicit val writes = Json.writes[Catalog] +} diff --git a/handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Handler.scala b/handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Handler.scala index 15e30fe1f3..834360f402 100644 --- a/handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Handler.scala +++ b/handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Handler.scala @@ -15,11 +15,46 @@ object Handler extends Logging { ApiGatewayHandler(LambdaIO(inputStream, outputStream, context)) { ContinueProcessing( Operation.noHealthcheck { - Req: ApiGatewayRequest => ApiGatewayResponse.successfulExecution + Req: ApiGatewayRequest => ApiGatewayResponse(body = catalog, statusCode = "200") } ) } -} + val catalog = { + val voucherEverydayRules = StartDateRules( + cutOffDayInclusive = Some(Tuesday), + daysOfWeek = Some(List(Monday)), + minDaysAfterCutOff = Some(20), + windowSizeDays = Some(28) + ) + + val voucherEveryday = ProductInfo( + id = "voucher_everyday", + label = "Every day", + startDateRules = Some(voucherEverydayRules) + ) + + val weekendsRule = voucherEverydayRules.copy( + daysOfWeek = Some(List(Saturday, Sunday)) + ) + val voucherWeekend = ProductInfo( + id = "voucher_weekend", + label = "Weekend", + startDateRules = Some(weekendsRule) + ) + val contributionRules = StartDateRules( + windowSizeDays = Some(1) + ) + + val monthlyContribution = ProductInfo( + id = "monthly_contribution", + label = "Monthly", + startDateRules = Some(contributionRules) + ) + val voucherGroup = Group("Voucher", List(voucherWeekend, voucherEveryday)) + val contributionGroup = Group("Contribution", List(monthlyContribution)) + Catalog(List(voucherGroup, contributionGroup)) + } +} diff --git a/handlers/new-product-api/src/test/scala/com/gu/newproduct/api/productcatalog/CatalogTest.scala b/handlers/new-product-api/src/test/scala/com/gu/newproduct/api/productcatalog/CatalogTest.scala new file mode 100644 index 0000000000..719dcf4a89 --- /dev/null +++ b/handlers/new-product-api/src/test/scala/com/gu/newproduct/api/productcatalog/CatalogTest.scala @@ -0,0 +1,77 @@ +package com.gu.newproduct.api.productcatalog + +import org.scalatest.{FlatSpec, Matchers} +import play.api.libs.json.Json + +class CatalogTest extends FlatSpec with Matchers { + it should "serialise catalog" in { + + val everyDayRules = StartDateRules( + cutOffDayInclusive = Some(Tuesday), + daysOfWeek = Some(List(Monday)), + minDaysAfterCutOff = Some(20), + windowSizeDays = Some(28) + ) + + val voucherEveryday = ProductInfo( + id = "voucher_everyday", + label = "Every day", + startDateRules = Some(everyDayRules) + ) + + val weekendsRule = everyDayRules.copy( + daysOfWeek = Some(List(Saturday, Sunday)) + ) + val voucherWeekend = ProductInfo( + id = "voucher_weekend", + label = "Weekend", + startDateRules = Some(weekendsRule) + ) + + val monthlyContribution = ProductInfo( + id = "monthly_contribution", + label = "Monthly" + ) + + val voucherGroup = Group("Voucher", List(voucherWeekend, voucherEveryday)) + val contributionGroup = Group("Contribution", List(monthlyContribution)) + val catalog = Catalog(List(voucherGroup, contributionGroup)) + val expected = + """ + |{ + |"groups": [{ + | "label": "Voucher", + | "products": [{ + | "id": "voucher_weekend", + | "label": "Weekend", + | "startDateRules" : { + | "cutOffDayInclusive": "Tuesday", + | "minDaysAfterCutOff" : 20, + | "windowSizeDays" : 28, + | "daysOfWeek": ["Saturday", "Sunday"] + | } + | }, + | { + | "id": "voucher_everyday", + | "label": "Every day", + | "startDateRules" : { + | "cutOffDayInclusive": "Tuesday", + | "minDaysAfterCutOff" : 20, + | "windowSizeDays" : 28, + | "daysOfWeek": ["Monday"] + | } + | } + | ] + |}, { + | "label": "Contribution", + | "products": [{ + | "id": "monthly_contribution", + | "label": "Monthly" + | }] + |}] + |} + """.stripMargin + Json.toJson(catalog) shouldBe Json.parse(expected) + + } +}