Commit Graph

182 Commits

Author SHA1 Message Date
Chris Hill-Scott
264fbed04e Refactor postcode validation to use utils
We don’t need to reformat the postcode here once template preview takes
care of it when rendering the PDF.

It’s better (and less code) to store what people give us, so we give
them back the same thing.
2020-04-09 18:19:53 +01:00
Chris Hill-Scott
025ac3ea89 Use same template to validate and send notification
To be absolutely sure that we can send a message we should also validate
it using the same template class that we use to render it.
2020-04-07 10:41:16 +01:00
Katie Smith
6ac89c9a2f Delete old 'process-virus-scan-passed-task'
This has been replaced by a new task, `sanitise-letter`, to this deletes
all the code in the old task and ensures that when antivirus is not
enabled locally we are calling the new task.
2020-04-02 14:52:15 +01:00
Pea M. Tyczynska
cff7f7b72d Merge pull request #2751 from alphagov/validate-and-format-postcode-api-flow
Validate and format postcode for the API letter sending flow.
2020-04-01 11:09:25 +01:00
Leo Hemsted
0b3d711652 if message too big to put on high volume queue then save to queue
SQS fails if the message body is over 256kb. Normally our messages are
quite small, but if we're using the new save-api-email task with an
email that has a large body, we can get over that limit. If so, handle
the exception and fall back to the existing code path (saving to the
database and sending a deliver-email task, which only has a notification
id.
2020-03-28 09:55:31 +00:00
Rebecca Law
dc44cb29d1 To make the deployment and testing a little easier move the high volume service ids to the credential repo.
This way we can only add the ids when we are ready and all the infrastrure for the new service has been applied.
2020-03-27 08:02:51 +00:00
Rebecca Law
d0a2e0f3ce Move high volume service id to config 2020-03-25 15:25:45 +00:00
Rebecca Law
db4b4d929d - If the task runs twice and the notification already exists ignore the primary key constraint.
- Remove prints
- Add some more tests
- Only allow the new method to run for emails
2020-03-25 12:39:15 +00:00
Rebecca Law
a13bcc6697 Reduce the pressure on the db for API post email requests.
Instead of saving the email notification to the db add it to a queue to save later.
This is an attempt to alleviate pressure on the db from the api requests.
This initial PR is to trial it see if we see improvement in the api performance an a reduction in queue pool errors. If we are happy with this we could remove the hard coding of the service id.

In a nutshell:
 - If POST /v2/notification/email is from our high volume service (hard coded for now) then create a notification to send to a queue to persist the notification to the db.
 - create a save_api_email task to persist the notification
 - return the notification
 - New worker app to process the save_api_email tasks.
2020-03-25 07:59:05 +00:00
Pea M. Tyczynska
d80fef8fe8 Update postcode validation error message 2020-03-19 14:40:26 +00:00
Pea Tyczynska
24cfde3410 Validate and format postcode for the API letter sending flow. 2020-03-19 14:23:39 +00:00
Rebecca Law
95d48d40a9 Update error message, now includes the url where the service can add contact details. 2020-02-26 16:04:15 +00:00
Pea Tyczynska
ed1bc8d806 All services can send files by email if they have set contact_link 2020-02-25 16:11:53 +00:00
Leo Hemsted
1694395b17 record document count when processing api notifications
if someone doesn't send any documents, set the value to None. If it's
not specified, it defaults to None anyway.
2020-02-13 12:43:06 +00:00
Rebecca Law
bb2b514e12 Save recipient address in the "to" field of a notification
When a precompiled letter is sent via the admin app, we now pass in the address which can be set in the Notifications.to field.
Once a precompiled letters sent by the API has passed validation we can set the address in Notifications.to field.

The celery tasks to validate precompiled letters sent by the API will be done in another PR.
2020-01-07 14:35:48 +00:00
Rebecca Law
5d6886242b Check that the request payload data is valid json.
By adding `force=True` to request.get_json() the mime type is ignore. If the data is not valid json the method will return a `BadRequestError` we catch that and throw our own error with a clear error message "Invalid JSON supplied in POST data".
If the json is valid return the json data or an empty dict if None is passed in.

This PR improves the error messages if the json is invalid, previously, the error message was "None object type" message which is not very helpful.
2019-11-21 15:23:11 +00:00
Leo Hemsted
c78a5d8536 Merge pull request #2662 from alphagov/utils-bump
Utils bump
2019-11-21 15:23:11 +00:00
Katie Smith
1d6716275a Bump utils to bring in changes to logging
Freezing the requirements changes the Werkzeug version to 0.16.0, which
requires a change in how we import an exception
(https://github.com/pallets/werkzeug/blob/master/CHANGES.rst#version-0160)
2019-10-28 10:54:19 +00:00
Leo Hemsted
99eb17fc29 Merge pull request #2610 from alphagov/get-pdf-contents-via-api
add api endpoint to get pdf for letter
2019-09-17 14:55:34 +01:00
Katie Smith
0c1fa3852f Add endpoint for getting / creating a hidden template
We want a way of getting the hidden precompiled template from admin,
so this adds an endpoint which gets the template or creates it if it doesn't
exist. The function to get or create the hidden template already existed
but has been moved to the template DAO now that it is used in more
places.
2019-09-12 09:56:10 +01:00
Leo Hemsted
52f7620772 create pdfs for test templated letters
previously, we didn't create templated letters, and just marked them as
delivered straight away. However, we may need to return PDFs for these
letters, so we should create them the same as live letters. Then update
the functions so that they know where to look for these letters.
2019-09-11 15:02:12 +01:00
Pea Tyczynska
eec5f5490d Don't require precompiled_letter service permission
Everyone can send precompiled letters now yay
2019-05-13 13:04:29 +01:00
Leo Hemsted
653f1ab6b9 stub out antivirus in dev
antivirus is sometimes tough to get running locally - now in dev
antivirus is skipped unless `ANTIVIRUS_ENABLED=1` is set on the command
line. on all other environments it is always enabled.
2019-02-27 10:59:31 +00:00
Pea Tyczynska
5b9e6c2086 Set postage on basis of postage argument or template postage 2019-02-06 13:23:09 +00:00
Pea Tyczynska
9ab97d3481 Return notification postage in response for .post_precompiled_letter_notification 2019-01-16 16:57:57 +00:00
Rebecca Law
4263117189 We were getting the page count for the letter before virus scan happened.
This PR moves setting the billale_units for the letter after virus scan has passed.
2018-10-16 15:08:15 +01:00
Rebecca Law
b00308d122 Removed an update statement to notifications.
It's a small change, but we should remove any db operations that are not necessary.
2018-09-20 14:47:24 +01:00
Rebecca Law
6da552a00b Added a message before the error description, to explain that it's the input arguments that have caused a problem. 2018-07-03 09:41:02 +01:00
Rebecca Law
13155e24dc We are getting 500 errors when a POST notification has characters that can not be decoded or the json is malformed.
The exception is wrapped and a sensible error message is returned to the client.
2018-07-03 09:04:33 +01:00
Alexey Bezhan
c189f09687 Update template content values from notification personalisation
We need to set template_with_content placeholder values with an
updated personalisation dictionary after processing the document
uploads in order to avoid returning base64 file data instead of
the document URLs in the response contents.
2018-04-09 16:30:16 +01:00
Alexey Bezhan
b097a16a86 Handle and log document-download-api request errors
Catches the requests exception for document-download-api calls, logs
a warning and returns a matching response code and message.

Connection errors to document download result in 503 response to the
user.
2018-04-09 16:30:16 +01:00
Alexey Bezhan
f2e163dc43 Upload files from personalisation data to document download
Adds support for a new personalisation value type: file upload.

File uploads are represented as a dictionary with a "file" key and
a base64-encoded file data as the key's value:

```
personalisation={
  'field1': {'file': '<base64-encoded file contents>'}
}
```

Post notification endpoint checks the request personalisation data
looking for the file uploads in personalisation data. If any are
found and the service has permissions to upload documents the files
are sent to document download API and personalisation values are
replaced with the URLs returned in the document download response.

A fake document URL is returned for simulated notifications, no
documents are stored in Document Download.

Multiple files can be uploaded for one notification by providing
a file upload in more than one personalisation field.
2018-04-09 16:30:16 +01:00
Rebecca Law
b73bf4220f Refactor process_letter_notification to make the code easier to read.
- Separated the logic of precompiled and template letters.
- Remove the check for research mode, research mode is not relevant to api calls. The test key is used for testing.
Refactor upload_pdf_letter to accept a precompile boolean to save a query to template.
2018-04-09 13:56:44 +01:00
Rebecca Law
3c92f00fdb Revert "Fix research mode preview of precompiled letters" 2018-04-06 16:10:06 +01:00
Rebecca Law
39e4f63b8c Use the positive condition when checking the environment 2018-04-06 12:01:16 +01:00
Ken Tsang
8b35c1c007 Fix research mode preview of precompiled letters 2018-04-03 22:52:45 +01:00
Ken Tsang
0ee5c33084 Add antivirus check on precompiled letters sent with test key
- precompiled PDFs sent by test key uploaded to scan bucket
- set status to VIRUS-SCAN-FAILED for pdfs failing virus scan rather than PERMANENT-FAILURE
- Make call to AV app for precompiled letters sent via a test key, and set notification status to PENDING-VIRUS-SCAN
2018-03-23 12:04:37 +00:00
Ken Tsang
65733a30a1 Add send_task to antivirus app for scanning precompiled letters 2018-03-20 10:12:59 +00:00
Ken Tsang
30e371fa4c Set precompiled letters to pending virus check initially 2018-03-20 10:11:36 +00:00
Richard Chapman
2a3095d5c5 Merge pull request #1770 from alphagov/Update_notifification_response_for_precompiled
Update notification response for precompiled
2018-03-16 14:00:39 +00:00
Ken Tsang
d3537cca74 Add upload pdf to test-letters bucket
- triggered when sending a precompiled letter using a test key
2018-03-14 18:16:43 +00:00
Richard Chapman
0c775314bd Updated the precompiled response to only include the notification id
and the client reference. It could confuse the client consumer so best
to remove it from the response altogether.

* Respond with only the notification_id and client_reference
* Updated the test to check for the response without the template
2018-03-14 14:01:08 +00:00
Richard Chapman
f3cca07d19 Updated the precompiled response to not include the template.
The template in precompiled is only used internally and cannot be
accessed from the UI. It could confuse the client consumer so best to
remove it from the response altogether.

* Remove the template from the dict before returning it and therefore
ensuring it still stays similar to letter
* Updated the test to check for the response without the template
2018-03-14 13:16:55 +00:00
Katie Smith
bc619efc4e Count the number of pages in pre-compiled letters
We were already counting the billable units in PDFs that we generate.
We are now also counting the number of billable units in pre-compiled letters
and saving the result.
2018-02-28 16:14:25 +00:00
Alexey Bezhan
d803b48d23 Return 400 response for invalid per-compiled letter content 2018-02-23 17:52:25 +00:00
Alexey Bezhan
bef91bcd99 Check for precompiled letter permission in the post notification 2018-02-23 17:52:25 +00:00
Alexey Bezhan
8971a5adce Upload pre-compiled letter PDF to S3
Pre-compiled letter endpoint uploads PDF contents to S3 directly
instead of creating a letter task to generate PDF using template
preview.

This moves some of the utility functions used by existing letter
celery tasks to app.letters.utils, so that they can be reused by
the API endpoint.
2018-02-23 17:52:25 +00:00
Alexey Bezhan
5327298371 Add a view function for pre-compiled PDF letters
Adds a separate view function that is registered under the same
route as existing letter POST notification.
2018-02-23 17:52:24 +00:00
Rebecca Law
e736c90d00 Switch to using the pdf letter flow.
When sending letters always use the pdf letter flow regardless of service permissions.
2018-02-13 18:38:32 +00:00
Rebecca Law
dce79832ff As Notify matures we probably need less logging, especially to report happy path events.
This PR is a proposal to reduce the average messages we see for a single notification from about 7 messages to 2.

Messaging would change to something like this:
February 2nd 2018, 15:39:05.885	Full delivery response from Firetext for notification: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
{'status': ['0'], 'reference': ['8eda51d5-cd82-4569-bfc9-d5570cdf2126'], 'time': ['2018-02-02 15:39:01'], 'code': ['000']}
February 2nd 2018, 15:39:05.885	Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:57.727	SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to provider firetext at 2018-02-02 15:38:56.716814
February 2nd 2018, 15:38:56.727	Starting sending SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 to provider at 2018-02-02 15:38:56.408181
February 2nd 2018, 15:38:56.727	Firetext request for 8eda51d5-cd82-4569-bfc9-d5570cdf2126 finished in 0.30376038211397827
February 2nd 2018, 15:38:49.449	sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
February 2nd 2018, 15:38:49.449	sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to the priority-tasks queue for delivery

To somthing like this:
February 2nd 2018, 15:39:05.885	Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:49.449	sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
2018-02-02 15:55:25 +00:00