Skip to content

Commit 9b846a2

Browse files
HMAI-316 - Setup new alerts gateway (#754)
* Add URLs to yml files * Gateway and new models * Add pagination * Renamed models * Rename gateway * Add paginated model conversion method * Gateway tests * Remove isActive * Corrected example * Correct mock URL * Add prism mock * Make prism mock static file * Commit changes made by code formatters --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 8485f8e commit 9b846a2

22 files changed

+3046
-2
lines changed

Dockerfile.prism

+1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ RUN sed -i 's/\*\/\*/application\/json/g' /prismMocks/prisoner-offender-search.j
2626
RUN sed -i 's/\*\/\*/application\/json/g' /prismMocks/x4022-personal-relationships.json
2727
RUN sed -i 's/\*\/\*/application\/json/g' /prismMocks/x4023-manage-prison-visits.json
2828
RUN sed -i 's/\*\/\*/application\/json/g' /prismMocks/x4024-incentives.json
29+
RUN sed -i 's/\*\/\*/application\/json/g' /prismMocks/x4025-alerts-api.json
2930

3031
ENTRYPOINT sh -c 'port=4010; for file in $(ls /prismMocks/*.json | sort); do node dist/index.js mock -p $port -h 0.0.0.0 $file & port=$((port + 1)); done; wait'

docker-compose.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ services:
1818
curl -H "Authorization: Bearer abc" -f http://0.0.0.0:4020/inductions/K5995YZ/induction-schedule &&
1919
curl -H "Authorization: Bearer abc" -f http://0.0.0.0:4021/prisoner/A1234AL/non-associations &&
2020
curl -H "Authorization: Bearer abc" -f http://0.0.0.0:4022/contact/123456789/linked-prisoners &&
21-
curl -H "Authorization: Bearer abc" -f http://0.0.0.0:4024/incentive-reviews/prisoner/A1234AA'
21+
curl -H "Authorization: Bearer abc" -f http://0.0.0.0:4024/incentive-reviews/prisoner/A1234AA &&
22+
curl -H "Authorization: Bearer abc" -f http://0.0.0.0:4025/prisoners/A1234AA/alerts'
2223

2324
ports:
2425
- "4010:4010"
@@ -36,6 +37,7 @@ services:
3637
- "4022:4022"
3738
- "4023:4023"
3839
- "4024:4024"
40+
- "4025:4025"
3941

4042
local-stack-aws:
4143
image: localstack/localstack:3.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways
2+
3+
import org.springframework.beans.factory.annotation.Autowired
4+
import org.springframework.beans.factory.annotation.Value
5+
import org.springframework.http.HttpMethod
6+
import org.springframework.stereotype.Component
7+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.WebClientWrapper
8+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.WebClientWrapper.WebClientWrapperResponse
9+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
10+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
11+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisonerAlerts.PAPaginatedAlerts
12+
13+
@Component
14+
class PrisonerAlertsGateway(
15+
@Value("\${services.alerts.base-url}") baseUrl: String,
16+
) {
17+
private val webClient = WebClientWrapper(baseUrl)
18+
19+
@Autowired
20+
lateinit var hmppsAuthGateway: HmppsAuthGateway
21+
22+
private fun authenticationHeader(): Map<String, String> {
23+
val token = hmppsAuthGateway.getClientToken("PRISONER_ALERTS")
24+
return mapOf(
25+
"Authorization" to "Bearer $token",
26+
)
27+
}
28+
29+
fun getPrisonerAlerts(
30+
prisonerNumber: String,
31+
page: Int,
32+
size: Int,
33+
): Response<PAPaginatedAlerts?> {
34+
val result =
35+
webClient.request<PAPaginatedAlerts>(
36+
HttpMethod.GET,
37+
"/prisoners/$prisonerNumber/alerts?page=$page&size=$size",
38+
authenticationHeader(),
39+
UpstreamApi.PRISONER_ALERTS,
40+
badRequestAsError = true,
41+
)
42+
43+
return when (result) {
44+
is WebClientWrapperResponse.Success -> {
45+
return Response(
46+
data = result.data,
47+
)
48+
}
49+
is WebClientWrapperResponse.Error -> {
50+
Response(
51+
data = null,
52+
errors = result.errors,
53+
)
54+
}
55+
}
56+
}
57+
}

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/hmpps/Alert.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ data class Alert(
2222
val dateExpired: LocalDate? = null,
2323
@Schema(description = "Whether the alert has expired", example = "true")
2424
val expired: Boolean? = null,
25-
@Schema(description = "Whether the alert is active", example = "true")
25+
@Schema(description = "Whether the alert is active", example = "false")
2626
val active: Boolean? = null,
2727
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps
2+
3+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.interfaces.IPaginatedObject
4+
5+
data class PaginatedAlerts(
6+
override val content: List<Alert>,
7+
override val totalPages: Int,
8+
override val totalCount: Long,
9+
override val isLastPage: Boolean,
10+
override val count: Int,
11+
override val page: Int,
12+
override val perPage: Int,
13+
) : IPaginatedObject<Alert>

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/hmpps/UpstreamApi.kt

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ enum class UpstreamApi {
1818
PERSONAL_RELATIONSHIPS,
1919
MANAGE_PRISON_VISITS,
2020
INCENTIVES,
21+
PRISONER_ALERTS,
2122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisonerAlerts
2+
3+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Alert
4+
import java.time.LocalDate
5+
import java.time.LocalDateTime
6+
7+
data class PAAlert(
8+
val alertUuid: String,
9+
val prisonNumber: String,
10+
val alertCode: PAAlertCode,
11+
val description: String? = null,
12+
val authorisedBy: String? = null,
13+
val activeFrom: LocalDate,
14+
val activeTo: LocalDate? = null,
15+
val isActive: Boolean,
16+
val createdAt: LocalDateTime,
17+
val createdBy: String,
18+
val createdByDisplayName: String,
19+
val lastModifiedAt: LocalDateTime? = null,
20+
val lastModifiedBy: String? = null,
21+
val lastModifiedByDisplayName: String? = null,
22+
val activeToLastSetAt: LocalDateTime? = null,
23+
val activeToLastSetBy: String? = null,
24+
val activeToLastSetByDisplayName: String? = null,
25+
val prisonCodeWhenCreated: String? = null,
26+
) {
27+
fun toAlert() =
28+
Alert(
29+
offenderNo = this.prisonNumber,
30+
type = this.alertCode.alertTypeCode,
31+
typeDescription = this.alertCode.alertTypeDescription,
32+
code = this.alertCode.code,
33+
codeDescription = this.alertCode.description,
34+
comment = this.description,
35+
dateCreated = this.activeFrom,
36+
dateExpired = this.activeTo,
37+
expired = this.activeTo?.isBefore(LocalDate.now().plusDays(1)) ?: false,
38+
active = this.isActive,
39+
)
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisonerAlerts
2+
3+
data class PAAlertCode(
4+
val alertTypeCode: String,
5+
val alertTypeDescription: String,
6+
val code: String,
7+
val description: String,
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisonerAlerts
2+
3+
data class PAPageable(
4+
val offset: Long,
5+
val sort: PASort,
6+
val unpaged: Boolean,
7+
val pageSize: Int,
8+
val paged: Boolean,
9+
val pageNumber: Int,
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisonerAlerts
2+
3+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PaginatedAlerts
4+
5+
data class PAPaginatedAlerts(
6+
val content: List<PAAlert>,
7+
val totalElements: Long,
8+
val totalPages: Int,
9+
val first: Boolean,
10+
val last: Boolean,
11+
val size: Int,
12+
val number: Int,
13+
val sort: PASort,
14+
val numberOfElements: Int,
15+
val pageable: PAPageable,
16+
val empty: Boolean,
17+
) {
18+
fun toPaginatedAlerts() =
19+
PaginatedAlerts(
20+
content = this.content.map { it.toAlert() },
21+
totalPages = this.totalPages,
22+
totalCount = this.totalElements,
23+
isLastPage = this.last,
24+
count = this.numberOfElements,
25+
page = this.number,
26+
perPage = this.size,
27+
)
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisonerAlerts
2+
3+
data class PASort(
4+
val empty: Boolean,
5+
val sorted: Boolean,
6+
val unsorted: Boolean,
7+
)

0 commit comments

Comments
 (0)