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

ADJUST1-452 DR Edit remand #189

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions server/model/remandChangeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { offencesForAdjustment, remandRelatedValidationSummary } from '../utils/
export default class RemandChangeModel {
constructor(
public adjustment: Adjustment,
private dbAdjustment: Adjustment,
private sentencesAndOffences: PrisonApiOffenderSentenceAndOffences[],
private calculatedUnusedDeductions: UnusedDeductionCalculationResponse,
public showUnusedMessage: boolean,
Expand All @@ -18,4 +19,31 @@ export default class RemandChangeModel {
public remandRelatedValidationSummary() {
return remandRelatedValidationSummary(this.calculatedUnusedDeductions?.validationMessages)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally we try and use more functional programming techniques in modern typescript, e.g. moving away from using traditional for loops and mutable variables.. something like this.

public isModified(): boolean {
  if (!this.dbAdjustment) {
    return false;
  }

  const sessionCharges = this.adjustment.remand.chargeId.sort((a, b) => a - b);
  const dbCharges = this.dbAdjustment.remand.chargeId.sort((a, b) => a - b);

  const chargeIdModified = !sessionCharges.every((chargeId, index) => chargeId === dbCharges[index]);

  const dateModified = this.adjustment.toDate !== this.dbAdjustment.toDate || 
                       this.adjustment.fromDate !== this.dbAdjustment.fromDate;

  return chargeIdModified || dateModified;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've adjusted this as you've suggested

public isModified(): boolean {
let modified = false
if (this.dbAdjustment) {
if (this.adjustment.remand.chargeId.length !== this.dbAdjustment.remand.chargeId.length) {
modified = true
} else {
const sessionAdjustmentCharges = this.adjustment.remand.chargeId.sort((a, b) => a - b)
const dbAdjustmentCharges = this.dbAdjustment.remand.chargeId.sort((a, b) => a - b)

for (let i = 0; i < sessionAdjustmentCharges.length; i += 1) {
if (sessionAdjustmentCharges[i] !== dbAdjustmentCharges[i]) {
modified = true
}
}
}

if (
!modified &&
(this.adjustment.toDate !== this.dbAdjustment.toDate || this.adjustment.fromDate !== this.dbAdjustment.fromDate)
) {
modified = true
}
}

return modified
}
}
4 changes: 3 additions & 1 deletion server/routes/remandRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,15 @@ export default class RemandRoutes {
)

return res.render('pages/adjustments/remand/remove', {
model: new RemandChangeModel(adjustment, sentencesAndOffences, unusedDeductions, showUnusedMessage),
model: new RemandChangeModel(adjustment, null, sentencesAndOffences, unusedDeductions, showUnusedMessage),
})
}

public edit: RequestHandler = async (req, res): Promise<void> => {
const { token } = res.locals.user
const { nomsId, id } = req.params
const { bookingId } = res.locals.prisoner
const dbAdjustment = await this.adjustmentsService.get(id, token)
let sessionAdjustment = this.adjustmentsStoreService.getById(req, nomsId, id)
sessionAdjustment = sessionAdjustment || (await this.adjustmentsService.get(id, token))
this.adjustmentsStoreService.store(req, nomsId, id, sessionAdjustment)
Expand Down Expand Up @@ -334,6 +335,7 @@ export default class RemandRoutes {
...sessionAdjustment,
days: daysBetween(new Date(sessionAdjustment.fromDate), new Date(sessionAdjustment.toDate)),
},
dbAdjustment,
sentencesAndOffences,
unusedDeductions,
showUnusedMessage,
Expand Down
6 changes: 2 additions & 4 deletions server/views/pages/adjustments/remand/edit.njk
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,16 @@
isEdit: true
}) }}

<h2 class="govuk-heading-m">Now save the remand time</h2>

{% if model.showUnusedMessage %}
<p class="govuk-body">The updates will change the amount of unused deductions. Check the unused remand alert on NOMIS.</p>
{% endif %}

<form method="post">
<input type="hidden" name="_csrf" value="{{ csrfToken }}"/>
<div class="govuk-button-group">
{% if not model.remandRelatedValidationSummary().errorList.length %}
{% if not model.remandRelatedValidationSummary().errorList.length and model.isModified() %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth adding a unit test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've written 2 unit tests for this

{{ govukButton({
text: "Save",
text: "Confirm and save",
type: submit,
preventDoubleClick: true
}) }}
Expand Down