package com.gu.notifications.events.model

import play.api.libs.json.{Json, Writes}


case class EventAggregation(platformCounts: PlatformCount)

object EventAggregation {
  implicit val jreads = Json.reads[EventAggregation]
  implicit val jwrites: Writes[EventAggregation] = Json.writes[EventAggregation]
}
package com.gu.notifications.events.model

import play.api.libs.json._


case class EventAggregation(platformCounts: PlatformCount)

object EventAggregation {
  private val defaultJsonReads = Json.reads[EventAggregation]

  implicit val jreads = new Reads[EventAggregation] {
    override def reads(json: JsValue): JsResult[EventAggregation] = json match {
      case JsObject(fields) =>
        val platformCount = fields.get("platform").orElse(fields.get("platformCounts"))
        val newFields = platformCount.map(pc => Map("platformCounts" -> pc)).getOrElse(Map.empty)
        defaultJsonReads.reads(JsObject(newFields))
      case _ => JsError("Expected a JsObject to parse EventAggregation")
    }
  }
  implicit val jwrites: Writes[EventAggregation] = Json.writes[EventAggregation]
}
Fix the report service such that it can parse the event aggregation with both attribute names (platformCounts and platform)
Fix the report service such that it can parse the event aggregation with both attribute names (platformCounts and platform)
