1616
1717package config
1818
19+ import cats .data .Validated
20+ import cats .implicits .catsSyntaxValidatedId
1921import com .typesafe .config .Config
22+ import config .Deprecation .{Deprecated , NotDeprecated }
2023import play .api .{ConfigLoader , Configuration }
2124import routing .Version
2225import uk .gov .hmrc .auth .core .ConfidenceLevel
2326import uk .gov .hmrc .play .bootstrap .config .ServicesConfig
2427
28+ import java .time .LocalDateTime
29+ import java .time .format .{DateTimeFormatter , DateTimeFormatterBuilder }
30+ import java .time .temporal .ChronoField
2531import javax .inject .{Inject , Singleton }
2632
2733trait AppConfig {
@@ -72,6 +78,10 @@ trait AppConfig {
7278 def endpointsEnabled (version : Version ): Boolean
7379 def endpointsEnabled (version : String ): Boolean
7480
81+ def apiDocumentationUrl : String
82+
83+ def deprecationFor (version : Version ): Validated [String , Deprecation ]
84+
7585 def apiVersionReleasedInProduction (version : String ): Boolean
7686
7787 def endpointReleasedInProduction (version : String , name : String ): Boolean
@@ -84,6 +94,9 @@ trait AppConfig {
8494
8595@ Singleton
8696class AppConfigImpl @ Inject () (config : ServicesConfig , val configuration : Configuration ) extends AppConfig {
97+ // API name
98+ val appName : String = config.getString(" appName" )
99+
87100 // MTD ID Lookup Config
88101 val mtdIdBaseUrl : String = config.baseUrl(serviceName = " mtd-id-lookup" )
89102
@@ -118,8 +131,52 @@ class AppConfigImpl @Inject() (config: ServicesConfig, val configuration: Config
118131 def endpointsEnabled (version : Version ): Boolean = config.getBoolean(s " api. ${version.name}.endpoints.enabled " )
119132 def endpointsEnabled (version : String ): Boolean = config.getBoolean(s " api. $version.endpoints.enabled " )
120133
134+ private val DATE_FORMATTER = new DateTimeFormatterBuilder ()
135+ .append(DateTimeFormatter .ofPattern(" yyyy-MM-dd" ))
136+ .parseDefaulting(ChronoField .HOUR_OF_DAY , 23 )
137+ .parseDefaulting(ChronoField .MINUTE_OF_HOUR , 59 )
138+ .parseDefaulting(ChronoField .SECOND_OF_MINUTE , 59 )
139+ .toFormatter()
140+
141+ def deprecationFor (version : Version ): Validated [String , Deprecation ] = {
142+ val isApiDeprecated : Boolean = apiStatus(version) == " DEPRECATED"
143+
144+ val deprecatedOn : Option [LocalDateTime ] =
145+ configuration
146+ .getOptional[String ](s " api. $version.deprecatedOn " )
147+ .map(value => LocalDateTime .parse(value, DATE_FORMATTER ))
148+
149+ val sunsetDate : Option [LocalDateTime ] =
150+ configuration
151+ .getOptional[String ](s " api. $version.sunsetDate " )
152+ .map(value => LocalDateTime .parse(value, DATE_FORMATTER ))
153+
154+ val isSunsetEnabled : Boolean =
155+ configuration.getOptional[Boolean ](s " api. $version.sunsetEnabled " ).getOrElse(true )
156+
157+ if (isApiDeprecated) {
158+ (deprecatedOn, sunsetDate, isSunsetEnabled) match {
159+ case (Some (dO), Some (sD), true ) =>
160+ if (sD.isAfter(dO))
161+ Deprecated (dO, Some (sD)).valid
162+ else
163+ s " sunsetDate must be later than deprecatedOn date for a deprecated version $version" .invalid
164+ case (Some (dO), None , true ) => Deprecated (dO, Some (dO.plusMonths(6 ))).valid
165+ case (Some (dO), _, false ) => Deprecated (dO, None ).valid
166+ case _ => s " deprecatedOn date is required for a deprecated version $version" .invalid
167+ }
168+
169+ } else NotDeprecated .valid
170+
171+ }
172+
121173 def apiVersionReleasedInProduction (version : String ): Boolean = config.getBoolean(s " api. $version.endpoints.api-released-in-production " )
122174
175+ def apiDocumentationUrl : String =
176+ configuration
177+ .getOptional[String ](" api.documentation-url" )
178+ .getOrElse(s " https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/ $appName" )
179+
123180 def safeEndpointsEnabled (version : String ): Boolean =
124181 configuration
125182 .getOptional[Boolean ](s " api. $version.endpoints.enabled " )
0 commit comments