Skip to content

Commit

Permalink
Merge pull request #38 from hmrc/feature/yta-3207
Browse files Browse the repository at this point in the history
YTA-3207: Added /statements endpoint.
  • Loading branch information
jose-puente-digital-hmrc-gov-uk authored Feb 15, 2018
2 parents d775e87 + 372c68b commit 6f6b2ec
Show file tree
Hide file tree
Showing 20 changed files with 750 additions and 13 deletions.
10 changes: 10 additions & 0 deletions app/uk/gov/hmrc/epayeapi/connectors/EpayeConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,15 @@ case class EpayeConnector @Inject() (

get[EpayeMonthlyStatement](url, headers)
}

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

get[EpayeMasterData](url, headers)
}
}

60 changes: 60 additions & 0 deletions app/uk/gov/hmrc/epayeapi/controllers/GetStatementsController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.statementsJsonWrites
import uk.gov.hmrc.epayeapi.models.in.{EpayeMasterData, EpayeResponse, EpayeSuccess}
import uk.gov.hmrc.epayeapi.models.out.{EmpRefsJson, StatementsJson}

import scala.concurrent.ExecutionContext

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

def getStatements(empRef: EmpRef): EssentialAction = {
EmpRefAction(empRef) {
Action.async { implicit request =>
val masterData = epayeConnector.getMasterData(empRef, hc)
masterData.map {
successHandler(empRef) orElse errorHandler
}
}
}
}

def successHandler(empRef: EmpRef): PartialFunction[EpayeResponse[EpayeMasterData], Result] = {
case EpayeSuccess(EpayeMasterData(_, yearRegistered)) =>
val statements = StatementsJson(config.apiBaseUrl, empRef, yearRegistered)
Ok(Json.toJson(statements))
}
}
5 changes: 5 additions & 0 deletions app/uk/gov/hmrc/epayeapi/models/TaxYear.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ case class TaxYear(
firstDay.plusYears(1).minusDays(1)
}

object TaxYear {
def apply(date: LocalDate): TaxYear =
TaxYear(TaxYearResolver.taxYearFor(date))
}

object ExtractTaxYear {
private lazy val pattern = """20(\d\d)-(\d\d)""".r

Expand Down
24 changes: 24 additions & 0 deletions app/uk/gov/hmrc/epayeapi/models/in/EpayeMasterData.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 uk.gov.hmrc.epayeapi.models.TaxYear

case class EpayeMasterData(
accountsOfficeReference: Option[String],
yearRegistered: Option[TaxYear]
)
2 changes: 2 additions & 0 deletions app/uk/gov/hmrc/epayeapi/models/in/EpayeReads.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ trait EpayeReads {
implicit lazy val epayeMonthlyPaymentItemReads: Reads[EpayeMonthlyPaymentItem] = reads[EpayeMonthlyPaymentItem]
implicit lazy val epayeMonthlyBalanceReads: Reads[EpayeMonthlyBalance] = reads[EpayeMonthlyBalance]

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

implicit lazy val epayeEmpRefsResponse: Format[EpayeEmpRefsResponse] = Json.format[EpayeEmpRefsResponse]
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,11 @@ object AnnualStatementJson {
)
)

}
}

case class AllAnnualStatementLinksJson(
empRefs: Link,
self: Link,
next: Link,
previous: Link
)
6 changes: 6 additions & 0 deletions app/uk/gov/hmrc/epayeapi/models/out/JsonWrites.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ trait JsonWrites {
implicit lazy val paymentJsonWrites: Writes[PaymentJson] = writes[PaymentJson]
implicit lazy val monthlySummaryJsonWrites: Writes[MonthlySummaryJson] = writes[MonthlySummaryJson]
implicit lazy val monthlyStatementLinksJsonWrites: Writes[MonthlyStatementLinksJson] = writes[MonthlyStatementLinksJson]

implicit lazy val statementsJsonWrites: Writes[StatementsJson] = writes[StatementsJson]
implicit lazy val statementsEmbeddedJsonWrites: Writes[Embedded] = writes[Embedded]
implicit lazy val statementsStatementJsonWrites: Writes[Statement] = writes[Statement]
implicit lazy val statementsStatementLinksJsonWrites: Writes[Links] = writes[Links]
implicit lazy val statementsLinksJsonWrites: Writes[StatementLinks] = writes[StatementLinks]
}
73 changes: 73 additions & 0 deletions app/uk/gov/hmrc/epayeapi/models/out/StatementsJson.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.domain.EmpRef
import uk.gov.hmrc.epayeapi.models.TaxYear

case class StatementsJson(
_embedded: Embedded,
_links: Links
)

case class Embedded(
statements: Seq[Statement]
)

case class Statement(
taxYear: TaxYear,
_links: StatementLinks
)

case class StatementLinks(
self: Link
)

case class Links(
empRefs: Link,
self: Link
)

object StatementsJson {
def apply(apiBaseUrl: String, empRef: EmpRef, taxYearOfRegistration: Option[TaxYear]): StatementsJson = {
def taxYearStatement(taxYear: TaxYear): Statement = {
Statement(
taxYear = taxYear,
_links = StatementLinks(self = Link.anualStatementLink(apiBaseUrl, empRef, taxYear))
)
}

val currentTaxYear = TaxYear(LocalDate.now())

val statements: Seq[Statement] = for {
regYear <- taxYearOfRegistration.toSeq
taxYearFrom <- (regYear.yearFrom to currentTaxYear.yearFrom)
taxYear = TaxYear(taxYearFrom)
} yield taxYearStatement(taxYear)

val links = Links(
empRefs = Link.empRefsLink(apiBaseUrl),
self = Link.statementsLink(apiBaseUrl, empRef)
)

StatementsJson(
_embedded = Embedded(statements),
_links = links
)
}
}
12 changes: 8 additions & 4 deletions app/uk/gov/hmrc/epayeapi/router/ApiRouter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,28 @@ import play.api.routing.Router.Routes
import play.api.routing.sird._
import play.api.routing.{Router, SimpleRouter}
import uk.gov.hmrc.domain.EmpRef
import uk.gov.hmrc.epayeapi.controllers.{GetAnnualStatementController, GetEmpRefsController, GetMonthlyStatementController, GetSummaryController}
import uk.gov.hmrc.epayeapi.controllers._
import uk.gov.hmrc.epayeapi.models.{ExtractTaxYear, TaxMonth}

@Singleton
case class ApiRouter @Inject() (
prodRoutes: prod.Routes,
getEmpRefsController: GetEmpRefsController,
getTotalsController: GetSummaryController,
getSummaryController: GetSummaryController,
getAnnualStatementController: GetAnnualStatementController,
getMonthlyStatementController: GetMonthlyStatementController
getMonthlyStatementController: GetMonthlyStatementController,
getStatementsController: GetStatementsController
) extends SimpleRouter {

val appRoutes = Router.from {
case GET(p"/") =>
getEmpRefsController.getEmpRefs()

case GET(p"/${TaxOfficeNumber(ton)}/${TaxOfficeReference(tor)}") =>
getTotalsController.getSummary(EmpRef(ton, tor))
getSummaryController.getSummary(EmpRef(ton, tor))

case GET(p"/${TaxOfficeNumber(ton)}/${TaxOfficeReference(tor)}/statements") =>
getStatementsController.getStatements(EmpRef(ton, tor))

case GET(p"/${TaxOfficeNumber(ton)}/${TaxOfficeReference(tor)}/statements/${ ExtractTaxYear(taxYear) }") =>
getAnnualStatementController.getAnnualStatement(EmpRef(ton, tor), taxYear)
Expand Down
34 changes: 34 additions & 0 deletions resources/public/api/conf/1.0/application.raml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,40 @@ types:
"code" : "INVALID_EMPREF",
"message" : "Provided EmpRef is not associated with your account"
}
/{taxOfficeNumber}/{taxOfficeReference}/statements:
uriParameters:
taxOfficeNumber:
description: A unique identifier made up of tax office number
type: string
example: "001"
taxOfficeReference:
description: A unique identifier made up of the tax office reference
type: string
example: "A000001"
get:
is: [ headers.acceptHeader ]
displayName: Get links to all Annual Statements
description: This resource returns links to all available Annual Statements
(annotations.scope): "read:epaye"
securedBy: [ sec.oauth_2_0: { scopes: [ "read:epaye" ] } ]
responses:
200:
body:
application/json:
type: !include schemas/Statements.schema.json
example: !include examples/Statements.get.json
403:
body:
application/json:
type: !include schemas/ErrorCodes.schema.json
examples:
notOpenStatus:
description: You don't currently have an ePAYE enrolment on this account.
value: |
{
"code" : "INVALID_EMPREF",
"message" : "Provided EmpRef is not associated with your account"
}
/{taxOfficeNumber}/{taxOfficeReference}/statements/{taxYear}:
uriParameters:
taxOfficeNumber:
Expand Down
10 changes: 5 additions & 5 deletions resources/public/api/conf/1.0/examples/AnnualStatement.get.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"isSpecified": false,
"_links": {
"self": {
"href": "https://api.service.hmrc.gov.uk/organisations/paye/840/GZ00064/statements/2016-17/4"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements/2016-17/4"
}
}
}
Expand All @@ -51,16 +51,16 @@
"href": "https://api.service.hmrc.gov.uk/organisations/paye/"
},
"statements": {
"href": "https://api.service.hmrc.gov.uk/organisations/paye/840/GZ00064/statements"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements"
},
"self": {
"href": "https://api.service.hmrc.gov.uk/organisations/paye/840/GZ00064/statements/2016-17"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements/2016-17"
},
"next": {
"href": "https://api.service.hmrc.gov.uk/organisations/paye/840/GZ00064/statements/2017-18"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements/2017-18"
},
"previous": {
"href": "https://api.service.hmrc.gov.uk/organisations/paye/840/GZ00064/statements/2015-16"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements/2015-16"
}
}
}
46 changes: 46 additions & 0 deletions resources/public/api/conf/1.0/examples/Statements.get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"_embedded": {
"statements": [{
"taxYear": {
"year": "2016-17",
"firstDay": "2016-04-06",
"lastDay": "2017-04-05"
},
"_links": {
"self": {
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements/2016-17"
}
}
}, {
"taxYear": {
"year": "2015-16",
"firstDay": "2015-04-06",
"lastDay": "2016-04-05"
},
"_links": {
"self": {
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements/2015-16"
}
}
}, {
"taxYear": {
"year": "2014-15",
"firstDay": "2014-04-06",
"lastDay": "2015-04-05"
},
"_links": {
"self": {
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements/2014-15"
}
}
}]
},
"_links": {
"empRefs": {
"href": "https://api.service.hmrc.gov.uk/organisations/paye/"
},
"self": {
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"year": {
"type": "string",
"description": "The tax year",
"pattern": "^\\d{4}-\\d{2}$"
"pattern": "^\\d{4}-\\d{2}$",
"example": "2016-17"
},
"firstDay": {
"description": "The first day of the tax year",
Expand Down
Loading

0 comments on commit 6f6b2ec

Please sign in to comment.