@@ -19,19 +19,18 @@ package v1.controllers
1919import cats .data .EitherT
2020import cats .implicits ._
2121import config .AppConfig
22-
23- import javax .inject .{Inject , Singleton }
2422import play .api .libs .json .Json
25- import play .api .mvc .{Action , AnyContent , ControllerComponents , Result }
26- import utils .{EndpointLogContext , IdGenerator , Logging }
23+ import play .api .mvc .{ Action , AnyContent , ControllerComponents , Result }
24+ import utils .{ EndpointLogContext , IdGenerator , Logging }
2725import v1 .audit .AuditEvents
2826import v1 .controllers .requestParsers .FinancialDataRequestParser
2927import v1 .models .audit .AuditResponse
3028import v1 .models .errors ._
3129import v1 .models .request .penalties .FinancialRawData
32- import v1 .services .{AuditService , EnrolmentsAuthService , PenaltiesService }
30+ import v1 .services .{ AuditService , EnrolmentsAuthService , PenaltiesService }
3331
34- import scala .concurrent .{ExecutionContext , Future }
32+ import javax .inject .{ Inject , Singleton }
33+ import scala .concurrent .{ ExecutionContext , Future }
3534
3635@ Singleton
3736class FinancialDataController @ Inject ()(val authService : EnrolmentsAuthService ,
@@ -40,64 +39,65 @@ class FinancialDataController @Inject()(val authService: EnrolmentsAuthService,
4039 auditService : AuditService ,
4140 cc : ControllerComponents ,
4241 val idGenerator : IdGenerator ,
43- appConfig : AppConfig )
44- (implicit ec : ExecutionContext )
45- extends AuthorisedController (cc) with BaseController with Logging {
42+ appConfig : AppConfig )(implicit ec : ExecutionContext )
43+ extends AuthorisedController (cc)
44+ with BaseController
45+ with Logging {
4646
4747 implicit val endpointLogContext : EndpointLogContext =
4848 EndpointLogContext (
4949 controllerName = " FinancialDataController" ,
5050 endpointName = " retrieveFinancialData"
5151 )
5252
53-
5453 def retrieveFinancialData (vrn : String , chargeReference : String ): Action [AnyContent ] = authorisedAction(vrn).async { implicit request =>
55-
5654 implicit val correlationId : String = idGenerator.getUid
5755
58- infoLog(s " ${endpointLogContext.toString} correlationId: $correlationId : " +
59- s " attempting to retrieve financial data for VRN : $vrn " )
60-
56+ infoLog(
57+ s " ${endpointLogContext.toString} correlationId : $correlationId : " +
58+ s " attempting to retrieve financial data for VRN: $vrn " )
6159
6260 val result : EitherT [Future , ErrorWrapper , Result ] = {
6361 for {
64- parsedRequest <- EitherT .fromEither[Future ](requestParser.parseRequest(FinancialRawData (vrn, chargeReference)))
62+ parsedRequest <- EitherT .fromEither[Future ](requestParser.parseRequest(FinancialRawData (vrn, chargeReference)))
6563 serviceResponse <- EitherT (service.retrieveFinancialData(parsedRequest))
6664 } yield {
6765
68- infoLog(s " ${endpointLogContext.toString} " +
69- s " Successfully retrieved Financial Data with correlationId : ${serviceResponse.correlationId} " )
70-
66+ infoLog(
67+ s " ${endpointLogContext.toString} " +
68+ s " Successfully retrieved Financial Data with correlationId : ${serviceResponse.correlationId} " )
7169
72- auditService.auditEvent(AuditEvents .auditFinancialData(serviceResponse.correlationId,
73- request.userDetails, AuditResponse (OK , Right (Some (Json .toJson(serviceResponse.responseData))))
74- ))
70+ auditService.auditEvent(
71+ AuditEvents .auditFinancialData(serviceResponse.correlationId,
72+ request.userDetails,
73+ AuditResponse (OK , Right (Some (Json .toJson(serviceResponse.responseData))))))
7574
7675 Ok (Json .toJson(serviceResponse.responseData))
7776 .withApiHeaders(serviceResponse.correlationId)
7877 }
7978 }
8079 result.leftMap { errorWrapper : ErrorWrapper =>
8180 val resCorrelationId : String = errorWrapper.correlationId
82- val leftResult = errorResult(errorWrapper).withApiHeaders(resCorrelationId)
81+ val leftResult = errorResult(errorWrapper).withApiHeaders(resCorrelationId)
8382 warnLog(ControllerError (endpointLogContext, vrn, request, leftResult.header.status, errorWrapper.error.message, resCorrelationId))
8483
85- auditService.auditEvent(AuditEvents .auditFinancialData(resCorrelationId,
86- request.userDetails, AuditResponse (httpStatus = leftResult.header.status, Left (errorWrapper.auditErrors))
87- ))
84+ auditService.auditEvent(
85+ AuditEvents .auditFinancialData(resCorrelationId,
86+ request.userDetails,
87+ AuditResponse (httpStatus = leftResult.header.status, Left (errorWrapper.auditErrors))))
8888
8989 leftResult
9090 }.merge
9191 }
9292
9393 private def errorResult (errorWrapper : ErrorWrapper ): Result = {
9494 (errorWrapper.error: @ unchecked) match {
95- case VrnFormatError | RuleIncorrectGovTestScenarioError |
96- FinancialInvalidIdNumber |
97- FinancialInvalidSearchItem |
98- MtdError (" INVALID_REQUEST" , _, _) => BadRequest (Json .toJson(errorWrapper))
95+ case VrnFormatError | RuleIncorrectGovTestScenarioError | FinancialInvalidIdNumber | FinancialInvalidSearchItem |
96+ MtdError (" INVALID_REQUEST" , _, _) =>
97+ BadRequest (Json .toJson(errorWrapper))
9998 case FinancialNotDataFound => NotFound (Json .toJson(errorWrapper))
100- case _ => InternalServerError (Json .toJson(errorWrapper))
99+ case UnauthorisedError => Forbidden (Json .toJson(errorWrapper))
100+ case _ => InternalServerError (Json .toJson(errorWrapper))
101101 }
102102 }
103103}
0 commit comments