Skip to content

Commit

Permalink
Merge pull request #42 from hmrc/feature/yta-3202
Browse files Browse the repository at this point in the history
YTA-3202: Added endpoint for Payment History.
  • Loading branch information
flashingpumpkin authored Feb 21, 2018
2 parents b719b33 + b46b936 commit 20f73ef
Show file tree
Hide file tree
Showing 26 changed files with 732 additions and 34 deletions.
12 changes: 7 additions & 5 deletions app/uk/gov/hmrc/epayeapi/connectors/EpayeConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ case class EpayeConnector @Inject() (
}

def getMasterData(empRef: EmpRef, headers: HeaderCarrier): Future[EpayeResponse[EpayeMasterData]] = {
val url =
s"${config.epayeBaseUrl}" +
s"/epaye" +
s"/${empRef.encodedValue}" +
s"/api/v1/master-data"
val url = s"${config.epayeBaseUrl}/epaye/${empRef.encodedValue}/api/v1/master-data"

get[EpayeMasterData](url, headers)
}

def getPaymentHistory(empRef: EmpRef, taxYear: TaxYear, headers: HeaderCarrier): Future[EpayeResponse[EpayePaymentHistory]] = {
val url = s"${config.epayeBaseUrl}/epaye/${empRef.encodedValue}/api/v1/payment-history/${taxYear.asString}"

get[EpayePaymentHistory](url, headers)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.epayeapi.controllers

import javax.inject.{Inject, Singleton}

import akka.stream.Materializer
import play.api.libs.json.Json
import play.api.mvc.{Action, EssentialAction, Result}
import uk.gov.hmrc.auth.core.AuthConnector
import uk.gov.hmrc.domain.EmpRef
import uk.gov.hmrc.epayeapi.connectors.{EpayeApiConfig, EpayeConnector}
import uk.gov.hmrc.epayeapi.models.Formats.paymentHistoryJsonWrites
import uk.gov.hmrc.epayeapi.models.TaxYear
import uk.gov.hmrc.epayeapi.models.in.{EpayePaymentHistory, EpayeResponse, EpayeSuccess}
import uk.gov.hmrc.epayeapi.models.out.PaymentHistoryJson

import scala.concurrent.ExecutionContext

@Singleton
case class GetPaymentHistoryController @Inject() (
config: EpayeApiConfig,
authConnector: AuthConnector,
epayeConnector: EpayeConnector,
implicit val ec: ExecutionContext,
implicit val mat: Materializer
)
extends ApiController
with EpayeErrorHandler {

def getPaymentHistory(empRef: EmpRef, taxYear: TaxYear): EssentialAction = {
EmpRefAction(empRef) {
Action.async { implicit request =>
val epayePaymentHistory = epayeConnector.getPaymentHistory(empRef, taxYear, hc)
epayePaymentHistory.map {
successHandler(empRef, taxYear) orElse errorHandler
}
}
}
}

private def successHandler(empRef: EmpRef, taxYear: TaxYear): PartialFunction[EpayeResponse[EpayePaymentHistory], Result] = {
case EpayeSuccess(epayePaymentHistory) =>
val paymentHistory = PaymentHistoryJson.transform(config.apiBaseUrl, empRef, taxYear, epayePaymentHistory)

Ok(Json.toJson(paymentHistory))
}
}
23 changes: 23 additions & 0 deletions app/uk/gov/hmrc/epayeapi/models/ImplicitOrderings.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.epayeapi.models

import org.joda.time.LocalDate

object ImplicitOrderings {
implicit val localDateDescendingOrdering = Ordering.by[LocalDate, Long](_.toDate.getTime).reverse
}
27 changes: 27 additions & 0 deletions app/uk/gov/hmrc/epayeapi/models/in/EpayePaymentHistory.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.epayeapi.models.in

import org.joda.time.LocalDate

case class EpayePaymentHistory(payments: Seq[EpayePaymentHistoryPayment])

case class EpayePaymentHistoryPayment(
dateOfPayment: Option[LocalDate],
amount: BigDecimal
)

4 changes: 4 additions & 0 deletions app/uk/gov/hmrc/epayeapi/models/in/EpayeReads.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@ trait EpayeReads {

implicit lazy val epayeMasterDataResponse: Reads[EpayeMasterData] = reads[EpayeMasterData]

implicit lazy val epayePaymentHistoryReads: Reads[EpayePaymentHistory] = reads[EpayePaymentHistory]
implicit lazy val epayePaymentHistoryPaymentReads: Reads[EpayePaymentHistoryPayment] = reads[EpayePaymentHistoryPayment]


implicit lazy val epayeEmpRefsResponse: Format[EpayeEmpRefsResponse] = Json.format[EpayeEmpRefsResponse]
}
6 changes: 3 additions & 3 deletions app/uk/gov/hmrc/epayeapi/models/out/AnnualStatementJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ object AnnualStatementJson {
_links = AnnualStatementLinksJson(
empRefs = Link.empRefsLink,
statements = Link.statementsLink(empRef),
self = Link.anualStatementLink(empRef, taxYear),
next = Link.anualStatementLink(empRef, taxYear.next),
previous = Link.anualStatementLink(empRef, taxYear.previous)
self = Link.annualStatementLink(empRef, taxYear),
next = Link.annualStatementLink(empRef, taxYear.next),
previous = Link.annualStatementLink(empRef, taxYear.previous)
)
)

Expand Down
3 changes: 1 addition & 2 deletions app/uk/gov/hmrc/epayeapi/models/out/EmpRefsJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ object EmpRefLinks {
EmpRefLinks(
self = Link.summaryLink(empRef),
statements = Link.statementsLink(empRef),
currentStatement = Link.anualStatementLink(empRef, TaxYear(TaxYearResolver.currentTaxYear))
currentStatement = Link.annualStatementLink(empRef, TaxYear(TaxYearResolver.currentTaxYear))
)
}

6 changes: 5 additions & 1 deletion app/uk/gov/hmrc/epayeapi/models/out/JsonWrites.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ trait JsonWrites {
)
}

implicit lazy val paymentWrites: Writes[Payment] = writes[Payment]

implicit lazy val empRefsLinksWrites: Writes[EmpRefsLinks] = writes[EmpRefsLinks]
implicit lazy val empRefLinksWrites: Writes[EmpRefLinks] = writes[EmpRefLinks]
implicit lazy val empRefItemWrites: Writes[EmpRefItem] = writes[EmpRefItem]
Expand All @@ -65,7 +67,6 @@ trait JsonWrites {

implicit lazy val monthlyStatementJsonWrites: Writes[MonthlyStatementJson] = writes[MonthlyStatementJson]
implicit lazy val chargeJsonWrites: Writes[ChargeJson] = writes[ChargeJson]
implicit lazy val paymentJsonWrites: Writes[PaymentJson] = writes[PaymentJson]
implicit lazy val monthlySummaryJsonWrites: Writes[MonthlySummaryJson] = writes[MonthlySummaryJson]
implicit lazy val monthlyStatementLinksJsonWrites: Writes[MonthlyStatementLinksJson] = writes[MonthlyStatementLinksJson]

Expand All @@ -74,4 +75,7 @@ trait JsonWrites {
implicit lazy val statementsStatementJsonWrites: Writes[Statement] = writes[Statement]
implicit lazy val statementsStatementLinksJsonWrites: Writes[Links] = writes[Links]
implicit lazy val statementsLinksJsonWrites: Writes[StatementLinks] = writes[StatementLinks]

implicit lazy val paymentHistoryJsonWrites: Writes[PaymentHistoryJson] = writes[PaymentHistoryJson]
implicit lazy val paymentHistoryLinksJsonWrites: Writes[PaymentHistoryLinks] = writes[PaymentHistoryLinks]
}
5 changes: 4 additions & 1 deletion app/uk/gov/hmrc/epayeapi/models/out/Link.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ object Link {
def statementsLink(empRef: EmpRef): Link =
Link(s"$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/statements")

def anualStatementLink(empRef: EmpRef, taxYear: TaxYear): Link =
def annualStatementLink(empRef: EmpRef, taxYear: TaxYear): Link =
Link(s"$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/statements/${taxYear.asString}")

def monthlyStatementLink(empRef: EmpRef, taxYear: TaxYear, taxMonth: TaxMonth): Link =
Link(s"$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/statements/${taxYear.asString}/${taxMonth.asString}")

def paymentHistoryLink(empRef: EmpRef, taxYear: TaxYear): Link =
Link(s"$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/payment-history/${taxYear.asString}")
}
18 changes: 4 additions & 14 deletions app/uk/gov/hmrc/epayeapi/models/out/MonthlyStatementJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ case class MonthlyStatementJson(
rtiCharges: Seq[ChargeJson],
interest: Seq[ChargeJson],
allocatedCredits: Seq[ChargeJson],
allocatedPayments: Seq[PaymentJson],
allocatedPayments: Seq[Payment],
writeOffs: Seq[ChargeJson],
dueDate: LocalDate,
summary: MonthlySummaryJson,
Expand All @@ -41,11 +41,6 @@ case class ChargeJson(
amount: BigDecimal
)

case class PaymentJson(
paymentDate: LocalDate,
amount: BigDecimal
)

case class MonthlySummaryJson(
amount: BigDecimal,
interest: BigDecimal,
Expand Down Expand Up @@ -101,13 +96,8 @@ object ChargeJson {
}

object Payments {
def apply(payments: EpayeMonthlyPaymentDetails): Seq[PaymentJson] =
payments.items.map { PaymentJson.apply }
}

object PaymentJson {
def apply(item: EpayeMonthlyPaymentItem): PaymentJson =
new PaymentJson(item.dateOfPayment, item.amount)
def apply(payments: EpayeMonthlyPaymentDetails): Seq[Payment] =
payments.items.map { case pi: EpayeMonthlyPaymentItem => Payment(pi.dateOfPayment, pi.amount) }
}

object MonthlySummaryJson {
Expand All @@ -130,7 +120,7 @@ object MonthlyStatementLinksJson {
statements =
Link.summaryLink(empRef),
annualStatement =
Link.anualStatementLink(empRef, taxYear),
Link.annualStatementLink(empRef, taxYear),
self =
Link.monthlyStatementLink(empRef, taxYear, taxMonth),
next =
Expand Down
30 changes: 30 additions & 0 deletions app/uk/gov/hmrc/epayeapi/models/out/Payment.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.epayeapi.models.out

import org.joda.time.LocalDate

import uk.gov.hmrc.epayeapi.models.ImplicitOrderings.localDateDescendingOrdering

case class Payment(
paymentDate: LocalDate,
amount: BigDecimal
)

object Payment {
implicit val paymentDescendingOrdering = Ordering.by[Payment, (LocalDate, BigDecimal)](payment => (payment.paymentDate, -payment.amount))
}
67 changes: 67 additions & 0 deletions app/uk/gov/hmrc/epayeapi/models/out/PaymentHistoryJson.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2018 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.epayeapi.models.out

import uk.gov.hmrc.domain.EmpRef
import uk.gov.hmrc.epayeapi.models.TaxYear
import uk.gov.hmrc.epayeapi.models.in.EpayePaymentHistory

case class PaymentHistoryJson (
taxOfficeNumber: String,
taxOfficeReference: String,
taxYear: TaxYear,
payments: Seq[Payment],
_links: PaymentHistoryLinks
)

case class PaymentHistoryLinks (
empRefs: Link,
summary: Link,
statements: Link,
self: Link,
next: Link,
previous: Link
)

object PaymentHistoryJson {
def transform(
apiBaseUrl: String,
empRef: EmpRef,
taxYear: TaxYear,
epayePaymentHistory: EpayePaymentHistory): PaymentHistoryJson = {

val payments: Seq[Payment] = for {
epayePayment <- epayePaymentHistory.payments
epayePaymentDate <- epayePayment.dateOfPayment
} yield Payment(epayePaymentDate, epayePayment.amount)

PaymentHistoryJson(
empRef.taxOfficeNumber,
empRef.taxOfficeReference,
taxYear,
payments.sorted(Payment.paymentDescendingOrdering),
PaymentHistoryLinks(
empRefs = Link.empRefsLink,
summary = Link.summaryLink(empRef),
statements = Link.statementsLink(empRef),
self = Link.paymentHistoryLink(empRef, taxYear),
next = Link.paymentHistoryLink(empRef, taxYear.next),
previous = Link.paymentHistoryLink(empRef, taxYear.previous)
)
)
}
}
2 changes: 1 addition & 1 deletion app/uk/gov/hmrc/epayeapi/models/out/StatementsJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object StatementsJson {
def taxYearStatement(taxYear: TaxYear): Statement = {
Statement(
taxYear = taxYear,
_links = StatementLinks(self = Link.anualStatementLink(empRef, taxYear))
_links = StatementLinks(self = Link.annualStatementLink(empRef, taxYear))
)
}

Expand Down
6 changes: 5 additions & 1 deletion app/uk/gov/hmrc/epayeapi/router/ApiRouter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ case class ApiRouter @Inject() (
getSummaryController: GetSummaryController,
getAnnualStatementController: GetAnnualStatementController,
getMonthlyStatementController: GetMonthlyStatementController,
getStatementsController: GetStatementsController
getStatementsController: GetStatementsController,
getPaymentHistoryController: GetPaymentHistoryController
) extends SimpleRouter {

val appRoutes = Router.from {
Expand All @@ -50,6 +51,9 @@ case class ApiRouter @Inject() (

case GET(p"/${TaxOfficeNumber(ton)}/${TaxOfficeReference(tor)}/statements/${ ExtractTaxYear(taxYear) }/${ int(month) }") if 1 <= month && month <= 12 =>
getMonthlyStatementController.getStatement(EmpRef(ton, tor), taxYear, TaxMonth(taxYear, month))

case GET(p"/${TaxOfficeNumber(ton)}/${TaxOfficeReference(tor)}/payment-history/${ ExtractTaxYear(taxYear) }") =>
getPaymentHistoryController.getPaymentHistory(EmpRef(ton, tor), taxYear)
}

val routes: Routes = prodRoutes.routes.orElse(appRoutes.routes)
Expand Down
Loading

0 comments on commit 20f73ef

Please sign in to comment.