Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hia 649 create basic cvl licences endpoint #353

Merged
merged 9 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Dockerfile.setup-create-and-vary-a-licence-api
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:current-alpine3.17

RUN apk update && apk add bash

COPY src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/prismMocks/create-and-vary-a-licence-api-docs.json /create-and-vary-a-licence-api-docs.json

RUN npm install -g @stoplight/prism-cli

CMD prism mock -p 4010 -h 0.0.0.0 /create-and-vary-a-licence-api-docs.json
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ services:
ports:
- '4045:4010'

create-and-vary-licence-api:
build:
context: .
dockerfile: Dockerfile.setup-create-and-vary-a-licence-api
container_name: create-and-vary-licence-api
healthcheck:
test: 'wget --header="Authorization: Bearer abc" http://0.0.0.0:4010/public/licences/id/abc -O /dev/null'
ports:
- '4070:4010'

local-stack-aws:
image: localstack/localstack:0.14.0
container_name: local-stack-aws
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1.person

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.EntityNotFoundException
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.decodeUrlCharacters
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetLicenceConditionService
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.util.PaginatedResponse
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.util.paginateWith

@RestController
@RequestMapping("/v1/persons")
class LicenceConditionController(
@Autowired val auditService: AuditService,
@Autowired val getLicenceConditionService: GetLicenceConditionService,
) {

@GetMapping("{encodedHmppsId}/licences/conditions")
fun getLicenceConditions(
@PathVariable encodedHmppsId: String,
@RequestParam(required = false, defaultValue = "1", name = "page") page: Int,
@RequestParam(required = false, defaultValue = "8", name = "perPage") perPage: Int,
): PaginatedResponse<Licence> {
val hmppsId = encodedHmppsId.decodeUrlCharacters()
val response = getLicenceConditionService.execute(hmppsId)

if (response.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND)) {
throw EntityNotFoundException("Could not find person with id: $hmppsId")
}
auditService.createEvent("GET_PERSON_LICENCE_CONDITION", "Person licence condition details with hmpps id: $hmppsId has been retrieved")
return response.data.paginateWith(page, perPage)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpMethod
import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.WebClientWrapper
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.WebClientWrapper.WebClientWrapperResponse
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.createAndVaryLicence.CvlLicence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.createAndVaryLicence.CvlLicenceSummary
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.LicenceCondition
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi

@Component
class CreateAndVaryLicenceGateway(@Value("\${services.create-and-vary-licence.base-url}") baseUrl: String) {
private val webClient = WebClientWrapper(baseUrl)

@Autowired
lateinit var hmppsAuthGateway: HmppsAuthGateway

fun getLicenceSummaries(id: String): Response<List<Licence>> {
val result = webClient.requestList<CvlLicenceSummary>(
HttpMethod.GET,
"/public/licence-summaries/crn/$id",
authenticationHeader(),
UpstreamApi.CVL,
)

return when (result) {
is WebClientWrapperResponse.Success -> {
Response(data = result.data.map { it.toLicence() })
}

is WebClientWrapperResponse.Error -> {
Response(
data = emptyList(),
errors = result.errors,
)
}
}
}

fun getLicenceConditions(id: String): Response<List<LicenceCondition>> {
val result = webClient.request<CvlLicence>(
HttpMethod.GET,
"/public/licences/id/$id",
authenticationHeader(),
UpstreamApi.CVL,
)

return when (result) {
is WebClientWrapperResponse.Success -> {
Response(data = result.data.toLicenceConditions())
}

is WebClientWrapperResponse.Error -> {
Response(
data = emptyList(),
errors = result.errors,
)
}
}
}

private fun authenticationHeader(): Map<String, String> {
val token = hmppsAuthGateway.getClientToken("CVL")
return mapOf(
"Authorization" to "Bearer $token",
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.createAndVaryLicence

class CvlAPCondition(
val standard: List<CvlCondition>? = null,

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.createAndVaryLicence

import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.LicenceCondition

class CvlCondition(
val text: String? = null,
) {
fun toLicenceCondition(): LicenceCondition = LicenceCondition(
condition = this.text,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.createAndVaryLicence

import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.LicenceCondition

class CvlLicence(
val conditions: CvlLicenceCondition? = null,
) {
fun toLicenceConditions(): List<LicenceCondition> {
val result = mutableListOf<LicenceCondition>()
conditions?.AP?.standard?.forEach { result.add(it.toLicenceCondition()) }
return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.createAndVaryLicence

import com.fasterxml.jackson.annotation.JsonProperty

class CvlLicenceCondition(
@JsonProperty("AP")
val AP: CvlAPCondition? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.createAndVaryLicence

import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence
data class CvlLicenceSummary(
val id: String,
val prisonNumber: String? = null,
) {
fun toLicence(): Licence = Licence(
id = this.id,
offenderNumber = this.prisonNumber,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps

import com.fasterxml.jackson.annotation.JsonIgnore

data class Licence(
@JsonIgnore
val id: String,
val offenderNumber: String? = null,
var conditions: List<LicenceCondition> = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps

class LicenceCondition(
val condition: String? = null,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps

enum class UpstreamApi {
NOMIS, PRISONER_OFFENDER_SEARCH, PROBATION_OFFENDER_SEARCH, NDELIUS, ASSESS_RISKS_AND_NEEDS, ADJUDICATIONS, TEST
NOMIS, PRISONER_OFFENDER_SEARCH, PROBATION_OFFENDER_SEARCH, NDELIUS, ASSESS_RISKS_AND_NEEDS, ADJUDICATIONS, CVL, TEST
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.CreateAndVaryLicenceGateway
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response

@Service
class GetLicenceConditionService(
@Autowired val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway,
@Autowired val getPersonService: GetPersonService,
) {
fun execute(hmppsId: String): Response<List<Licence>> {
val personResponse = getPersonService.execute(hmppsId = hmppsId)
val crn = personResponse.data?.identifiers?.deliusCrn

var licences: Response<List<Licence>> = Response(data = emptyList())

if (crn != null) {
licences = createAndVaryLicenceGateway.getLicenceSummaries(id = crn)
licences.data.forEach {
val conditions = createAndVaryLicenceGateway.getLicenceConditions(it.id)
it.conditions = conditions.data
}
}

return Response(
data = licences.data,
errors = personResponse.errors + licences.errors,
)
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
base-url: https://effective-proposal-framework-and-delius-dev.hmpps.service.justice.gov.uk
adjudications:
base-url: https://manage-adjudications-api-dev.hmpps.service.justice.gov.uk
create-and-vary-licence:
base-url: https://create-and-vary-a-licence-api-dev.hmpps.service.justice.gov.uk
hmpps-auth:
base-url: https://sign-in-dev.hmpps.service.justice.gov.uk
username: ${CLIENT_ID}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application-local-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
base-url: http://probation-integration-epf-api:4010
adjudications:
base-url: http://adjudications-api:4010
create-and-vary-licence:
base-url: http://create-and-vary-licence-api:4010
hmpps-auth:
base-url: http://hmpps-auth:8080

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ services:
base-url: http://localhost:9090
adjudications:
base-url: http://localhost:4045
create-and-vary-licence:
base-url: http://localhost:4070

hmpps.sqs:
provider: localstack
Expand Down Expand Up @@ -43,4 +45,4 @@ authorisation:
- "/health/ping"
- "/health/readiness"
- "/health/liveness"
- "/info"
- "/info"
2 changes: 2 additions & 0 deletions src/main/resources/application-preprod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
base-url: https://effective-proposal-framework-and-delius-preprod.hmpps.service.justice.gov.uk
adjudications:
base-url: https://manage-adjudications-api-preprod.hmpps.service.justice.gov.uk
create-and-vary-licence:
base-url: https://create-and-vary-a-licence-api-preprod.hmpps.service.justice.gov.uk
hmpps-auth:
base-url: https://sign-in-preprod.hmpps.service.justice.gov.uk
username: ${CLIENT_ID}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
base-url: https://effective-proposal-framework-and-delius.hmpps.service.justice.gov.uk
adjudications:
base-url: https://manage-adjudications-api-prod.hmpps.service.justice.gov.uk
create-and-vary-licence:
base-url: https://create-and-vary-a-licence-api-prod.hmpps.service.justice.gov.uk
hmpps-auth:
base-url: https://sign-in.hmpps.service.justice.gov.uk
username: ${CLIENT_ID}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ services:
base-url: http://localhost:4005
adjudications:
base-url: http://localhost:4006
create-and-vary-licence:
base-url: http://localhost:4007

hmpps.sqs:
provider: localstack
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ services:
base-url: http://localhost:4060
adjudications:
base-url: http://localhost:4045
create-and-vary-licence:
base-url: http://localhost:4070

sentry:
traces-sample-rate: "0.05"
Expand Down
Loading