diff --git a/server/data/index.ts b/server/data/index.ts index f135f1117..0a4950030 100644 --- a/server/data/index.ts +++ b/server/data/index.ts @@ -3,14 +3,10 @@ * Do appinsights first as it does some magic instrumentation work, i.e. it affects other 'require's * In particular, applicationinsights automatically collects bunyan logs */ -import config, { ApiConfig } from '../config' import { buildAppInsightsClient, initialiseAppInsights } from '../utils/azureAppInsights' import HmppsAuthClient from './hmppsAuthClient' -import { PrisonApiClient } from './interfaces/prisonApiClient' -import PrisonApiRestClientTwo from './prisonApiClientTwo' import { createRedisClient } from './redisClient' -import RestClient, { RestClientBuilder as CreateRestClientBuilder } from './restClient' import TokenStore from './tokenStore' initialiseAppInsights() @@ -18,22 +14,8 @@ buildAppInsightsClient() type RestClientBuilder = (token: string) => T -export default function restClientBuilder( - name: string, - options: ApiConfig, - constructor: new (client: RestClient) => T, -): RestClientBuilder { - const restClient = CreateRestClientBuilder(name, options) - return token => new constructor(restClient(token)) -} - export const dataAccess = () => ({ hmppsAuthClient: new HmppsAuthClient(new TokenStore(createRedisClient())), - prisonApiClientBuilder: restClientBuilder( - 'Prison API', - config.apis.prisonApi, - PrisonApiRestClientTwo, - ), }) export type DataAccess = ReturnType diff --git a/server/data/prisonApiClientTwo.ts b/server/data/prisonApiClientTwo.ts deleted file mode 100644 index 2a16f2782..000000000 --- a/server/data/prisonApiClientTwo.ts +++ /dev/null @@ -1,297 +0,0 @@ -import { Readable } from 'stream' -import config from '../config' -import RestClient from './restClient' -import { CaseLoadsDummyDataA } from './localMockData/caseLoad' -import { CaseLoad } from '../interfaces/caseLoad' -import { NonAssociationDetails } from '../interfaces/nonAssociationDetails' -import nonAssociationDetailsDummyData from './localMockData/nonAssociations' -import { PrisonApiClient } from './interfaces/prisonApiClient' -import { AccountBalances } from '../interfaces/accountBalances' -import { AdjudicationSummary } from '../interfaces/adjudicationSummary' -import { VisitSummary } from '../interfaces/visitSummary' -import { VisitBalances } from '../interfaces/visitBalances' -import { Assessment } from '../interfaces/prisonApi/assessment' -import { ContactDetail } from '../interfaces/staffContacts' -import { mapToQueryString } from '../utils/utils' -import { CaseNote } from '../interfaces/caseNote' -import { ScheduledEvent } from '../interfaces/scheduledEvent' -import dummyScheduledEvents from './localMockData/eventsForToday' -import { PrisonerDetail } from '../interfaces/prisonerDetail' -import { InmateDetail } from '../interfaces/prisonApi/inmateDetail' -import { PersonalCareNeeds } from '../interfaces/personalCareNeeds' -import { OffenderActivitiesHistory } from '../interfaces/offenderActivitiesHistory' -import { OffenderAttendanceHistory } from '../interfaces/offenderAttendanceHistory' -import { SecondaryLanguage } from '../interfaces/prisonApi/secondaryLanguage' -import { PagedListQueryParams, PagedList } from '../interfaces/prisonApi/pagedList' -import { PropertyContainer } from '../interfaces/prisonApi/propertyContainer' -import { CourtCase } from '../interfaces/prisonApi/courtCase' -import { OffenceHistoryDetail } from '../interfaces/prisonApi/offenceHistoryDetail' -import { OffenderSentenceTerms } from '../interfaces/prisonApi/offenderSentenceTerms' -import { PrisonerSentenceDetails } from '../interfaces/prisonerSentenceDetails' -import { Address } from '../interfaces/prisonApi/address' -import { OffenderContacts } from '../interfaces/prisonApi/offenderContacts' -import { ReferenceCode, ReferenceCodeDomain } from '../interfaces/prisonApi/referenceCode' -import { ReasonableAdjustments } from '../interfaces/prisonApi/reasonableAdjustment' -import { CaseNoteUsage } from '../interfaces/prisonApi/caseNoteUsage' -import { formatDateISO } from '../utils/dateHelpers' -import { CaseNoteCount } from '../interfaces/prisonApi/caseNoteCount' -import { CourtDateResults } from '../interfaces/courtDateResults' -import { MainOffence } from '../interfaces/prisonApi/mainOffence' -import { FullStatus } from '../interfaces/prisonApi/fullStatus' -import { SentenceSummary } from '../interfaces/prisonApi/sentenceSummary' -import { OffenderIdentifier } from '../interfaces/prisonApi/offenderIdentifier' -import { StaffRole } from '../interfaces/prisonApi/staffRole' - -export default class PrisonApiRestClientTwo implements PrisonApiClient { - constructor(private restClient: RestClient) {} - - private async get(args: object, localMockData?: T): Promise { - try { - return await this.restClient.get(args) - } catch (error) { - if (config.localMockData === 'true' && localMockData) { - return localMockData - } - return error - } - } - - async getOffenderAttendanceHistory( - prisonerNumber: string, - fromDate: string, - toDate: string, - ): Promise { - return this.get({ - path: `/api/offender-activities/${prisonerNumber}/attendance-history?fromDate=${fromDate}&toDate=${toDate}&page=0&size=20`, - }) - } - - async getUserCaseLoads(): Promise { - return this.get({ path: '/api/users/me/caseLoads', query: 'allCaseloads=true' }, CaseLoadsDummyDataA) - } - - async getPrisonerImage(offenderNumber: string, fullSizeImage: boolean): Promise { - try { - return await this.restClient.stream({ - path: `/api/bookings/offenderNo/${offenderNumber}/image/data?fullSizeImage=${fullSizeImage}`, - }) - } catch (error) { - return error - } - } - - async getNonAssociationDetails(prisonerNumber: string): Promise { - return this.get( - { path: `/api/offenders/${prisonerNumber}/non-association-details` }, - nonAssociationDetailsDummyData, - ) - } - - async getAccountBalances(bookingId: number): Promise { - return this.get({ - path: `/api/bookings/${bookingId}/balances`, - }) - } - - async getAdjudications(bookingId: number): Promise { - return this.get({ path: `/api/bookings/${bookingId}/adjudications` }) - } - - async getVisitSummary(bookingId: number): Promise { - return this.get({ path: `/api/bookings/${bookingId}/visits/summary` }) - } - - async getVisitBalances(prisonerNumber: string): Promise { - return this.get({ - path: `/api/bookings/offenderNo/${prisonerNumber}/visit/balances`, - }) - } - - async getAssessments(bookingId: number): Promise { - return this.get({ path: `/api/bookings/${bookingId}/assessments` }) - } - - async getEventsScheduledForToday(bookingId: number): Promise { - return this.get( - { - path: `/api/bookings/${bookingId}/events/today`, - }, - dummyScheduledEvents, - ) - } - - async getBookingContacts(bookingId: number): Promise { - try { - return await this.restClient.get({ path: `/api/bookings/${bookingId}/contacts` }) - } catch (error) { - return error - } - } - - async getPrisoner(prisonerNumber: string): Promise { - const prisoner = await this.get({ path: `/api/prisoners/${prisonerNumber}` }) - // API returns array with one entry, so extract this to return a single object - if (Array.isArray(prisoner)) { - return prisoner[0] - } - return prisoner - } - - async getCaseNoteSummaryByTypes(params: object): Promise { - try { - return await this.restClient.get({ path: `/api/case-notes/summary?${mapToQueryString(params)}` }) - } catch (error) { - return error - } - } - - async getInmateDetail(bookingId: number): Promise { - return this.get({ path: `/api/bookings/${bookingId}` }) - } - - async getPersonalCareNeeds(bookingId: number, types?: string[]): Promise { - let query - if (types?.length) { - query = `type=${types.join()}` - } - return this.get({ path: `/api/bookings/${bookingId}/personal-care-needs`, query }) - } - - async getOffenderActivitiesHistory( - prisonerNumber: string, - earliestEndDate: string, - ): Promise { - try { - return await this.restClient.get({ - path: `/api/offender-activities/${prisonerNumber}/activities-history?earliestEndDate=${earliestEndDate}`, - }) - } catch (error) { - return error - } - } - - async getSecondaryLanguages(bookingId: number): Promise { - return this.get({ path: `/api/bookings/${bookingId}/secondary-languages` }) - } - - async getAlerts(bookingId: number, queryParams?: PagedListQueryParams): Promise { - // Set defaults then apply queryParams - const params: PagedListQueryParams = { - size: queryParams?.showAll ? 9999 : 20, - ...queryParams, - } - return this.get({ path: `/api/bookings/${bookingId}/alerts/v2`, query: mapToQueryString(params) }) - } - - async getProperty(bookingId: number): Promise { - return this.get({ path: `/api/bookings/${bookingId}/property` }) - } - - async getCourtCases(bookingId: number): Promise { - return this.get({ path: `/api/bookings/${bookingId}/court-cases` }) - } - - async getOffenceHistory(prisonerNumber: string): Promise { - return this.get({ path: `/api/bookings/offenderNo/${prisonerNumber}/offenceHistory` }) - } - - async getSentenceTerms(bookingId: number): Promise { - return this.get({ - path: `/api/offender-sentences/booking/${bookingId}/sentenceTerms?filterBySentenceTermCodes=IMP&filterBySentenceTermCodes=LIC`, - }) - } - - async getPrisonerSentenceDetails(prisonerNumber: string): Promise { - try { - return this.get({ path: `/api/offenders/${prisonerNumber}/sentences` }) - } catch (error) { - return error - } - } - - async getAddresses(prisonerNumber: string): Promise { - return this.get({ path: `/api/offenders/${prisonerNumber}/addresses` }) - } - - async getAddressesForPerson(personId: number): Promise { - return this.get({ path: `/api/persons/${personId}/addresses` }) - } - - async getOffenderContacts(prisonerNumber: string): Promise { - return this.get({ path: `/api/offenders/${prisonerNumber}/contacts` }) - } - - async getImage(imageId: string, getFullSizedImage: boolean): Promise { - try { - return await this.restClient.stream({ - path: `/api/images/${imageId}/data?fullSizeImage=${getFullSizedImage}`, - }) - } catch (error) { - return error - } - } - - async getReferenceCodesByDomain(domain: ReferenceCodeDomain): Promise { - return this.get({ - path: `/api/reference-domains/domains/${domain}`, - headers: { 'page-limit': '1000' }, - }) - } - - async getReasonableAdjustments(bookingId: number, treatmentCodes: string[]): Promise { - return this.get({ - path: `/api/bookings/${bookingId}/reasonable-adjustments?type=${treatmentCodes.join()}`, - }) - } - - async getCaseNotesUsage(prisonerNumber: string): Promise { - const today = formatDateISO(new Date()) - return this.get({ - path: `/api/case-notes/usage`, - query: `offenderNo=${prisonerNumber}&toDate=${today}&numMonths=1200`, - }) - } - - async getCaseNoteCount( - bookingId: number, - type: string, - subType: string, - fromDate: string, - toDate: string, - ): Promise { - return this.get({ - path: `/api/bookings/${bookingId}/caseNotes/${type}/${subType}/count`, - query: `fromDate=${fromDate}&toDate=${toDate}`, - }) - } - - async getMainOffence(bookingId: number): Promise { - return this.get({ path: `/api/bookings/${bookingId}/mainOffence` }) - } - - async getFullStatus(prisonerNumber: string): Promise { - return this.get({ path: `/api/prisoners/${prisonerNumber}/full-status` }) - } - - async getCourtDateResults(prisonerNumber: string): Promise { - return this.get({ - path: `/api/digital-warrant/court-date-results/${prisonerNumber}`, - }) - } - - async getIdentifiers(bookingId: number): Promise { - return this.get({ - path: `/api/bookings/${bookingId}/identifiers`, - }) - } - - async getSentenceSummary(prisonerNumber: string): Promise { - return this.get({ - path: `/api/offenders/${prisonerNumber}/booking/latest/sentence-summary`, - }) - } - - async getStaffRoles(staffId: number, agencyId: string): Promise { - return this.get({ path: `/api/staff/${staffId}/${agencyId}/roles` }) - } -} diff --git a/server/data/restClient.ts b/server/data/restClient.ts index ed32f847a..1c4860d21 100644 --- a/server/data/restClient.ts +++ b/server/data/restClient.ts @@ -30,10 +30,6 @@ interface StreamRequest { errorLogger?: (e: UnsanitisedError) => void } -export function RestClientBuilder(name: string, config: ApiConfig) { - return (token: string): RestClient => new RestClient(name, config, token) -} - export default class RestClient { agent: Agent diff --git a/server/services/index.ts b/server/services/index.ts index e47d4e71e..bebb9fbaa 100644 --- a/server/services/index.ts +++ b/server/services/index.ts @@ -5,9 +5,9 @@ import PrisonerSearchService from './prisonerSearch' import UserService from './userService' export const services = () => { - const { hmppsAuthClient, prisonApiClientBuilder } = dataAccess() + const { hmppsAuthClient } = dataAccess() const userService = new UserService(hmppsAuthClient) - const offenderService = new OffenderService(prisonApiClientBuilder) + const offenderService = new OffenderService() const commonApiRoutes = new CommonApiRoutes(offenderService) return { diff --git a/server/services/offenderService.ts b/server/services/offenderService.ts index 1ef28097b..41a554c40 100644 --- a/server/services/offenderService.ts +++ b/server/services/offenderService.ts @@ -1,15 +1,12 @@ import { Readable } from 'stream' -import { RestClientBuilder } from '../data' -import { PrisonApiClient } from '../data/interfaces/prisonApiClient' +import PrisonApiClient from '../data/prisonApiClient' export default class OffenderService { - constructor(private readonly prisonClientBuilder: RestClientBuilder) {} - getPrisonerImage(token: string, offenderNumber: string): Promise { - return this.prisonClientBuilder(token).getPrisonerImage(offenderNumber, true) + return new PrisonApiClient(token).getPrisonerImage(offenderNumber, true) } getImage(token: string, imageId: string): Promise { - return this.prisonClientBuilder(token).getImage(imageId, true) + return new PrisonApiClient(token).getImage(imageId, true) } }