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

Add Create Sroc Bill Run endpoint #51

Merged
merged 20 commits into from
Dec 15, 2022
Merged

Conversation

StuAA78
Copy link
Contributor

@StuAA78 StuAA78 commented Dec 12, 2022

https://eaflood.atlassian.net/browse/WATER-3854

We need to create an endpoint for the UI to hit in order to create an sroc supplementary bill run.

This PR is to create our initial endpoint and apply validation. Creating the billing batch will be done in a future PR.

Note that we expect to receive the bill run type (ie. supplementary) in the payload as type, and the object it returns also contains this as type. This is in contrast to the water-abstraction-service which refers to this as batchType. We chose not to use batchType as we aim to be consistent within this repo as far as possible, and therefore we don't refer to batches if we can help it. It will therefore be necessary when updating the service to ensure that we send and receive it as type.

Since this is the first time we're validating incoming data, we needed to decide on how we will implement validation. The solution we have come up with is to create app/validators, with validators sitting in an appropriate subfolder. In this case, since the validation relates to bill runs and specifically the create bill run endpoint, we create the file app/validators/bill-runs/create-bill-run.validator.js

As part of this we also updated ErrorPagesPlugin to accept a plainOutput setting, which when set true in an endpoint's route will not attempt to change the response to be an HTML page. We did this so that our endpoint will return a standard JSON error response.

https://eaflood.atlassian.net/browse/WATER-3854

We need to create an endpoint for the UI to hit in order to create an sroc supplementary bill run.

This PR is to create our initial endpoint and apply validation. Creating the billing batch will be done in a future PR.
@StuAA78 StuAA78 added the enhancement New feature or request label Dec 12, 2022
@StuAA78 StuAA78 self-assigned this Dec 12, 2022
While looking at how we want to return errors from our endpoint, we realised that `@hapi/boom` wasn't installed even though we currently use it -- instead of simply making use of it as a dependency of a dependency. We therefore install it as a direct dependecy
We don't use `boom` to generate errors as we previously intended, as this causes `BoomNotifierLib` to intercept the response and present an HTML page instead of the JSON response we want. We therefore return our own simple error response, passing back the failed validation message as this gives a wealth of info that can be used to diagnose any issues
Our `ErrorPagesPlugin` will format Boom errors into friendly HTML error pages. We therefore started down the path of manually setting the http status code in our error response. On reflection, we would prefer to use Boom errors everywhere. We therefore add a `plainOutput` option to the plugin so we can override the formatting if we wish.
Following our change to `ErrorPagesPlugin`, we add the `plainOutput` option to our route and update it return a Boom error
We started using Joi's `validateAsync` method to perform validation, which returns the validated data or throws an error if validation fails. On reflection this would make error handling more complicated -- we'd need to wrap the validation call within a controller in its own try-catch block, so that we can ensure the right kind of Boom error is returned. We therefore change this out for the standard `validate` method, which returns an object containing the validated data (within the `value` key) and an `error` key with any validation errors.
@StuAA78 StuAA78 marked this pull request as ready for review December 15, 2022 14:14
@StuAA78 StuAA78 merged commit 2706120 into main Dec 15, 2022
@StuAA78 StuAA78 deleted the add-create-sroc-bill-run-endpoint branch December 15, 2022 14:52
Cruikshanks added a commit that referenced this pull request Dec 16, 2022
In [Add Create Sroc Bill Run endpoint](#51) we realised the `error-pages.plugin.js` had been written with the assumption we would always want to return a HTML response. But we're now building API only endpoints for use by the other apps in the service. For this we just want the standard response to be returned.

So, we made a tweak and added additional options to the routes of our API only endpoints that allow us to set whether error-pages should use 'plain output' (no HTML).

Only, we've inadvertently broken things for routes that don't have that additional option set! 🤦

This change adds some tests in the hope we catch this sooner in future. It also fixes the issue.
Cruikshanks added a commit that referenced this pull request Dec 16, 2022
In [Add Create Sroc Bill Run endpoint](#51) we realised the `error-pages.plugin.js` had been written with the assumption we would always want to return a HTML response. But we're now building API only endpoints for use by the other apps in the service. For this we just want the standard response to be returned.

So, we made a tweak and added additional options to the routes of our API only endpoints that allow us to set whether error-pages should use 'plain output' (no HTML).

Only, we've inadvertently broken things for routes that don't have that additional option set! 🤦

This change adds some tests in the hope we catch this sooner in future. It also fixes the issue.
Cruikshanks added a commit that referenced this pull request Dec 16, 2022
In [Add Create Sroc Bill Run endpoint](#51) we realised the `error-pages.plugin.js` had been written with the assumption we would always want to return an HTML response. But we're now building API-only endpoints for use by the other apps in the service. For this we want the standard response to be returned.

So, we tweaked and added additional options to the routes of our API-only endpoints that allow us to set whether error-pages should use 'plain output' (no HTML).

But in doing so we inadvertently broke things for routes that don't have that additional option set! 🤦

This change adds some tests in the hope we catch this sooner in future. It also fixes the issue.

```
Airbrake controller: GET /status/airbrake
  ✔ 19) returns a 500 error (6 ms)
Debug: internal, implementation, error 
    TypeError: Cannot read properties of undefined (reading 'plainOutput')
    at /home/repos/water-abstraction-system/app/plugins/error-pages.plugin.js:25:363
    at exports.Manager.execute (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/toolkit.js:57:29)
    at Request._invoke (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/request.js:397:55)
    at Request._postCycle (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/request.js:468:86)
    at Request._reply (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/request.js:447:20)
    at Request._execute (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/request.js:281:14)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants