Skip to content

Commit

Permalink
ADJUST1-28 Able to update an adjustment and the form for any adjustme… (
Browse files Browse the repository at this point in the history
#15)

* ADJUST1-28 Able to update an adjustment and the form for any adjustment type.

* ADJUST1-28 improving generic views.

* ADJUST1-28 fixing int test.

* ADJUST1-28 linting.
  • Loading branch information
ldlharper authored Jul 4, 2023
1 parent e684a24 commit 34bc3ed
Show file tree
Hide file tree
Showing 31 changed files with 640 additions and 278 deletions.
1 change: 1 addition & 0 deletions integration_tests/e2e/rada.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ context('Enter a RADA', () => {
cy.task('stubGetRelevantRemand')
cy.task('stubValidateAdjustmentWithWarning')
cy.task('stubCreateAdjustment')
cy.task('stubGetAdjustment')
})

it('Enter a RADA', () => {
Expand Down
23 changes: 23 additions & 0 deletions integration_tests/mockApis/adjustmentsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,32 @@ export default {
urlPattern: '/adjustments-api/adjustments',
},
response: {
jsonBody: { adjustmentId: 'this-is-an-id' },
status: 201,
headers: { 'Content-Type': 'application/json;charset=UTF-8' },
},
})
},
stubGetAdjustment: (): SuperAgentRequest => {
return stubFor({
request: {
method: 'GET',
urlPattern: '/adjustments-api/adjustments/this-is-an-id',
},
response: {
jsonBody: {
id: 'this-is-an-id',
adjustmentType: 'RESTORATION_OF_ADDITIONAL_DAYS_AWARDED',
bookingId: '1234',
fromDate: '2023-04-05',
toDate: null,
person: 'A1234AB',
days: 25,
sentenceSequence: null,
},
status: 200,
headers: { 'Content-Type': 'application/json;charset=UTF-8' },
},
})
},
}
2 changes: 1 addition & 1 deletion server/@types/adjustments/adjustmentsTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { components } from './index'

export type AdjustmentDetails = components['schemas']['AdjustmentDetailsDto']
export type AdjustmentTypes = components['schemas']['AdjustmentDto']['adjustmentType']
export type Adjustment = components['schemas']['AdjustmentDto']
export type CreateResponse = components['schemas']['CreateResponseDto']
export type ValidationMessage = components['schemas']['ValidationMessage']
43 changes: 25 additions & 18 deletions server/@types/adjustments/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,20 @@ export interface components {
/** @description The NOMIS active or inactive flag */
active: boolean
}
/** @description The details of an adjustment to release dates */
AdjustmentDetailsDto: {
/** @description The details of an additional days awarded (ADA) adjustment */
AdditionalDaysAwardedDto: {
/** @description The id of the adjudication that resulted in the ADA */
adjudicationId: string
/** @description Is the ADA consecutive or concurrent */
consecutive: boolean
}
/** @description The adjustment and its identifier */
AdjustmentDto: {
/**
* Format: uuid
* @description The ID of the adjustment
*/
id?: string
/**
* Format: int64
* @description The NOMIS booking ID of the adjustment
Expand All @@ -169,6 +181,7 @@ export interface components {
| 'ADDITIONAL_DAYS_AWARDED'
| 'RESTORATION_OF_ADDITIONAL_DAYS_AWARDED'
| 'SPECIAL_REMISSION'
| 'TIME_SPENT_IN_CUSTODY_ABROAD'
/**
* Format: date
* @description The end date of the adjustment
Expand All @@ -184,8 +197,11 @@ export interface components {
* @description The number of adjustment days
*/
days?: number
status?: string
additionalDaysAwarded?: components['schemas']['AdditionalDaysAwardedDto']
/** @description The person last updating this adjustment */
lastUpdatedBy?: string
/** @description The status of this adjustment */
status?: string
}
LegacyAdjustmentCreatedResponse: {
/** Format: uuid */
Expand Down Expand Up @@ -218,15 +234,6 @@ export interface components {
messagesReturnedCount: number
messages: components['schemas']['DlqMessage'][]
}
/** @description The adjustment and its identifier */
AdjustmentDto: {
/**
* Format: uuid
* @description The ID of the adjustment
*/
id: string
adjustment: components['schemas']['AdjustmentDetailsDto']
}
}
responses: never
parameters: never
Expand Down Expand Up @@ -370,19 +377,19 @@ export interface operations {
/** @description Adjustment found */
200: {
content: {
'application/json': components['schemas']['AdjustmentDetailsDto']
'application/json': components['schemas']['AdjustmentDto']
}
}
/** @description Unauthorised, requires a valid Oauth2 token */
401: {
content: {
'application/json': components['schemas']['AdjustmentDetailsDto']
'application/json': components['schemas']['AdjustmentDto']
}
}
/** @description Adjustment not found */
404: {
content: {
'application/json': components['schemas']['AdjustmentDetailsDto']
'application/json': components['schemas']['AdjustmentDto']
}
}
}
Expand All @@ -400,7 +407,7 @@ export interface operations {
}
requestBody: {
content: {
'application/json': components['schemas']['AdjustmentDetailsDto']
'application/json': components['schemas']['AdjustmentDto']
}
}
responses: {
Expand Down Expand Up @@ -523,7 +530,7 @@ export interface operations {
create_1: {
requestBody: {
content: {
'application/json': components['schemas']['AdjustmentDetailsDto']
'application/json': components['schemas']['AdjustmentDto']
}
}
responses: {
Expand All @@ -548,7 +555,7 @@ export interface operations {
validate: {
requestBody: {
content: {
'application/json': components['schemas']['AdjustmentDetailsDto']
'application/json': components['schemas']['AdjustmentDto']
}
}
responses: {
Expand Down
4 changes: 2 additions & 2 deletions server/@types/express/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdjustmentDetails } from '../adjustments/adjustmentsTypes'
import { Adjustment } from '../adjustments/adjustmentsTypes'

export default {}

Expand All @@ -7,7 +7,7 @@ declare module 'express-session' {
interface SessionData {
returnTo: string
nowInMinutes: number
adjustments: { string?: AdjustmentDetails }
adjustments: { string?: Adjustment }
}
}

Expand Down
17 changes: 6 additions & 11 deletions server/api/adjustmentsClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import config, { ApiConfig } from '../config'
import RestClient from '../data/restClient'
import {
Adjustment,
AdjustmentDetails,
CreateResponse,
ValidationMessage,
} from '../@types/adjustments/adjustmentsTypes'
import { Adjustment, CreateResponse, ValidationMessage } from '../@types/adjustments/adjustmentsTypes'

export default class AdjustmentsClient {
restClient: RestClient
Expand All @@ -14,8 +9,8 @@ export default class AdjustmentsClient {
this.restClient = new RestClient('Adjustments API', config.apis.adjustments as ApiConfig, token)
}

async get(adjustmentsId: string): Promise<AdjustmentDetails> {
return this.restClient.get({ path: `/adjustments/${adjustmentsId}` }) as Promise<AdjustmentDetails>
async get(adjustmentsId: string): Promise<Adjustment> {
return this.restClient.get({ path: `/adjustments/${adjustmentsId}` }) as Promise<Adjustment>
}

async findByPerson(person: string): Promise<Adjustment[]> {
Expand All @@ -26,19 +21,19 @@ export default class AdjustmentsClient {
return this.restClient.get({ path: `/adjustments?person=${person}&source=${source}` }) as Promise<Adjustment[]>
}

async create(adjustment: AdjustmentDetails): Promise<CreateResponse> {
async create(adjustment: Adjustment): Promise<CreateResponse> {
return this.restClient.post({ path: `/adjustments`, data: adjustment }) as Promise<CreateResponse>
}

async update(adjustmentsId: string, adjustment: AdjustmentDetails): Promise<void> {
async update(adjustmentsId: string, adjustment: Adjustment): Promise<void> {
return this.restClient.put({ path: `/adjustments/${adjustmentsId}`, data: adjustment }) as Promise<void>
}

async delete(adjustmentsId: string): Promise<void> {
return this.restClient.delete({ path: `/adjustments/${adjustmentsId}` }) as Promise<void>
}

async validate(adjustment: AdjustmentDetails): Promise<ValidationMessage[]> {
async validate(adjustment: Adjustment): Promise<ValidationMessage[]> {
return this.restClient.post({ path: `/adjustments/validate`, data: adjustment }) as Promise<ValidationMessage[]>
}
}
4 changes: 4 additions & 0 deletions server/model/abstractForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export default abstract class AbstractForm<T> {
return null
}

invalidNumber(value: string): boolean {
return !value || Number.isNaN(Number(value)) || Number(value) < 0
}

fieldHasError(field: string): boolean {
return fieldHasErrors(this.errors, field)
}
Expand Down
15 changes: 4 additions & 11 deletions server/model/adjustmentForm.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import dayjs from 'dayjs'
import { AdjustmentDetails } from '../@types/adjustments/adjustmentsTypes'
import { Adjustment, AdjustmentTypes } from '../@types/adjustments/adjustmentsTypes'
import { dateItems } from '../utils/utils'

export default class AdjustmentForm {
constructor(params: Partial<AdjustmentForm>) {
Object.assign(this, params)
}

type:
| 'REMAND'
| 'TAGGED_BAIL'
| 'UNLAWFULLY_AT_LARGE'
| 'LAWFULLY_AT_LARGE'
| 'ADDITIONAL_DAYS_AWARDED'
| 'RESTORATION_OF_ADDITIONAL_DAYS_AWARDED'
| 'SPECIAL_REMISSION'
type: AdjustmentTypes

'from-day': string

Expand All @@ -40,7 +33,7 @@ export default class AdjustmentForm {
return dateItems(this['to-year'], this['to-month'], this['to-day'], 'to', [])
}

static fromAdjustment(adjustment: AdjustmentDetails): AdjustmentForm {
static fromAdjustment(adjustment: Adjustment): AdjustmentForm {
return new AdjustmentForm({
type: adjustment.adjustmentType,
'from-day': dayjs(adjustment.fromDate).get('date').toString(),
Expand All @@ -54,7 +47,7 @@ export default class AdjustmentForm {
})
}

toAdjustmentDetails(bookingId: number, nomsId: string): AdjustmentDetails {
toAdjustment(bookingId: number, nomsId: string): Adjustment {
return {
adjustmentType: this.type,
bookingId,
Expand Down
58 changes: 58 additions & 0 deletions server/model/adjustmentFormFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import dayjs from 'dayjs'
import { Request } from 'express'
import { Adjustment, AdjustmentTypes } from '../@types/adjustments/adjustmentsTypes'
import { AdjustmentType } from './adjustmentTypes'
import AdjustmentsForm from './adjustmentsForm'
import RestoredAdditionalDaysForm from './restoredAdditionalDaysForm'
import GenericAdjustmentForm, { GenericAdjustmentFormOptions } from './genericAdjustmentForm'

export default class AdjustmentsFormFactory {
static fromAdjustment<T extends AdjustmentsForm<unknown>>(adjustment: Adjustment): AdjustmentsForm<T> {
if (adjustment.adjustmentType === 'RESTORATION_OF_ADDITIONAL_DAYS_AWARDED') {
return new RestoredAdditionalDaysForm({
'from-day': dayjs(adjustment.fromDate).get('date').toString(),
'from-month': (dayjs(adjustment.fromDate).get('month') + 1).toString(),
'from-year': dayjs(adjustment.fromDate).get('year').toString(),
days: adjustment.days.toString(),
})
}
return new GenericAdjustmentForm({
options: this.options(adjustment.adjustmentType),
'from-day': dayjs(adjustment.fromDate).get('date').toString(),
'from-month': (dayjs(adjustment.fromDate).get('month') + 1).toString(),
'from-year': dayjs(adjustment.fromDate).get('year').toString(),
'to-day': dayjs(adjustment.toDate).get('date').toString(),
'to-month': (dayjs(adjustment.toDate).get('month') + 1).toString(),
'to-year': dayjs(adjustment.toDate).get('year').toString(),
days: adjustment.days.toString(),
sentence: adjustment.sentenceSequence.toString(),
})
}

static fromType<T extends AdjustmentsForm<unknown>>(adjustmentType: AdjustmentType): AdjustmentsForm<T> {
if (adjustmentType.value === 'RESTORATION_OF_ADDITIONAL_DAYS_AWARDED') {
return new RestoredAdditionalDaysForm({})
}
return new GenericAdjustmentForm({
options: this.options(adjustmentType.value),
})
}

static fromRequest<T extends AdjustmentsForm<unknown>>(
req: Request,
adjustmentType: AdjustmentType,
): AdjustmentsForm<T> {
if (adjustmentType.value === 'RESTORATION_OF_ADDITIONAL_DAYS_AWARDED') {
return new RestoredAdditionalDaysForm(req.body)
}
return new GenericAdjustmentForm({ ...req.body, options: this.options(adjustmentType.value) })
}

private static options(adjustmentType: AdjustmentTypes): GenericAdjustmentFormOptions {
return {
hasSentence: ['REMAND', 'TAGGED_BAIL'].indexOf(adjustmentType) !== -1,
hasToDate: adjustmentType === 'REMAND',
adjustmentType,
}
}
}
4 changes: 3 additions & 1 deletion server/model/adjustmentTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AdjustmentTypes } from '../@types/adjustments/adjustmentsTypes'

export type AdjustmentType = {
value: string
value: AdjustmentTypes
text: string
shortText: string
url: string
Expand Down
11 changes: 11 additions & 0 deletions server/model/adjustmentsForm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Adjustment } from '../@types/adjustments/adjustmentsTypes'
import AbstractForm from './abstractForm'
import { AdjustmentType } from './adjustmentTypes'

export default abstract class AdjustmentsForm<T> extends AbstractForm<T> {
abstract toAdjustment(bookingId: number, nomsId: string, idw: string): Adjustment

abstract adjustmentType(): AdjustmentType

abstract fragment(): string
}
6 changes: 3 additions & 3 deletions server/model/adjustmentsListModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import adjustmentTypes, { AdjustmentType } from './adjustmentTypes'
export type Message = {
type: string
days: number
action: 'CREATE' | 'REMOVE'
action: 'CREATE' | 'REMOVE' | 'UPDATE'
}
export default class AdjustmentsListViewModel {
public adjustmentTypes = adjustmentTypes
Expand Down Expand Up @@ -46,8 +46,8 @@ export default class AdjustmentsListViewModel {

public getTotalDays(adjustmentType: AdjustmentType) {
return this.adjustments
.filter(it => it.adjustment.adjustmentType === adjustmentType.value)
.map(a => a.adjustment.days)
.filter(it => it.adjustmentType === adjustmentType.value)
.map(a => a.days)
.reduce((sum, current) => sum + current, 0)
}

Expand Down
Loading

0 comments on commit 34bc3ed

Please sign in to comment.