Skip to content

Commit

Permalink
SDIT-1309 ImageResource
Browse files Browse the repository at this point in the history
Switch /images prison-api calls to use system / client token
  • Loading branch information
steverendell committed Dec 8, 2023
1 parent a77bb66 commit bdb5870
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ module.exports = function createApp({

app.use(
'/hdc/taskList/',
secureRoute(taskListRouter(prisonerService, licenceService, audit, caService), {
secureRoute(taskListRouter(prisonerService, licenceService, audit, caService, signInService), {
licenceRequired: false,
})
)
Expand Down
11 changes: 9 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,17 @@ const probationTeamsClient = new ProbationTeamsClient(

const roService = new RoService(deliusClient, nomisClientBuilder)
const caService = createCaService(roService, activeLduClient)
const prisonerService = createPrisonerService(nomisClientBuilder, roService)
const prisonerService = createPrisonerService(nomisClientBuilder, roService, signInService)
const caseListFormatter = createCaseListFormatter(licenceClient)
const caseListService = createCaseListService(nomisClientBuilder, roService, licenceClient, caseListFormatter)
const pdfService = new PdfService(logger, licenceService, conditionsServiceFactory, prisonerService, pdfFormatter)
const pdfService = new PdfService(
logger,
licenceService,
conditionsServiceFactory,
prisonerService,
pdfFormatter,
signInService
)
const formService = new FormService(pdfFormatter, prisonerService, configClient)
const reportingService = createReportingService(audit)
const userAdminService = new UserAdminService(nomisClientBuilder, userClient, probationTeamsClient)
Expand Down
5 changes: 3 additions & 2 deletions server/routes/taskList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const determineAccessLevel = (licence, postRelease, role) => {
}
}

export = (prisonerService: PrisonerService, licenceService, audit, caService: CaService) => (router) => {
export = (prisonerService: PrisonerService, licenceService, audit, caService: CaService, signInService) => (router) => {
router.get(
'/:bookingId',
asyncMiddleware(async (req, res) => {
Expand Down Expand Up @@ -178,7 +178,8 @@ export = (prisonerService: PrisonerService, licenceService, audit, caService: Ca
router.get(
'/image/:imageId',
asyncMiddleware(async (req, res) => {
const prisonerImage = await prisonerService.getPrisonerImage(req.params.imageId, res.locals.token)
const systemToken = await signInService.getClientCredentialsTokens()
const prisonerImage = await prisonerService.getPrisonerImage(req.params.imageId, systemToken)

if (!prisonerImage) {
const placeHolder = path.join(process.cwd(), '/assets/images/no-photo.png')
Expand Down
6 changes: 4 additions & 2 deletions server/services/pdfService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export default class PdfService {
private readonly licenceService: LicenceService,
private readonly conditionsServiceFactory: ConditionsServiceFactory,
private readonly prisonerService: PrisonerService,
private readonly pdfFormatter
private readonly pdfFormatter,
private readonly signInService
) {}

async getPdfLicenceData(bookingId, rawLicence: LicenceRecord, token) {
Expand All @@ -22,8 +23,9 @@ export default class PdfService {
this.prisonerService.getPrisonerDetails(bookingId, token),
this.prisonerService.getEstablishmentForPrisoner(bookingId, token),
])
const systemToken = await this.signInService.getClientCredentialsTokens()

const image = prisonerInfo.facialImageId ? await this.getImage(prisonerInfo.facialImageId, token) : null
const image = prisonerInfo.facialImageId ? await this.getImage(prisonerInfo.facialImageId, systemToken) : null
const pssConditions = conditionsService.getPssConditions()

return this.pdfFormatter.formatPdfData(
Expand Down
6 changes: 4 additions & 2 deletions server/services/prisonerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = { createPrisonerService }
* @param {RoService} roService
* @return {PrisonerService}
*/
function createPrisonerService(nomisClientBuilder, roService) {
function createPrisonerService(nomisClientBuilder, roService, signInService) {
function selectEntriesWithTypes(identifiers, types) {
return identifiers.reduce((selected, element) => {
if (types.includes(element.type)) {
Expand All @@ -27,8 +27,10 @@ function createPrisonerService(nomisClientBuilder, roService) {
async getPrisonerDetails(bookingId, token) {
try {
logger.info(`getPrisonerDetail: ${bookingId}`)
const systemToken = await signInService.getClientCredentialsTokens()

const nomisClient = nomisClientBuilder(token)
const nomisSystemClient = nomisClientBuilder(systemToken)

const prisoners = await nomisClient.getOffenderSentencesByBookingId(bookingId)
const prisoner = prisoners.length > 0 && prisoners[0]
Expand All @@ -45,7 +47,7 @@ function createPrisonerService(nomisClientBuilder, roService) {
const { CRO, PNC } = selectEntriesWithTypes(await nomisClient.getIdentifiers(bookingId), ['PNC', 'CRO'])

const image = prisoner.facialImageId
? await nomisClient.getImageInfo(prisoner.facialImageId)
? await nomisSystemClient.getImageInfo(prisoner.facialImageId)
: { imageId: false }

return formatObjectForView({
Expand Down
5 changes: 4 additions & 1 deletion test/services/pdfService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ describe('pdfService', () => {
formatPdfData: jest.fn().mockReturnValue({ values }),
DEFAULT_PLACEHOLDER: 'placeholder',
}
const signInService = {
getClientCredentialsTokens: jest.fn().mockReturnValue('systemToken'),
}

const conditionServiceFactory = createConditionsServiceFactoryStub()
conditionServiceFactory.forLicence.mockReturnValue(conditionsService)
service = new PdfService(logger, licenceService, conditionServiceFactory, prisonerService, pdfFormatter)
service = new PdfService(logger, licenceService, conditionServiceFactory, prisonerService, pdfFormatter, signInService)
})

afterEach(() => {
Expand Down
5 changes: 4 additions & 1 deletion test/services/prisonerService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ describe('prisonerDetailsService', () => {
const nomisClientBuilder = jest.fn().mockReturnValue(nomisClientMock)
roService = new RoService(undefined, undefined)
roService.findResponsibleOfficer.mockReturnValue(responsibleOfficerResponse)
const signInService = {
getClientCredentialsTokens: jest.fn().mockResolvedValue('systemToken' )
}

service = createPrisonerService(nomisClientBuilder, roService)
service = createPrisonerService(nomisClientBuilder, roService, signInService)
})

describe('getPrisonerDetails', () => {
Expand Down

0 comments on commit bdb5870

Please sign in to comment.