mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-21 16:01:15 -05:00
process letters from api traffic
there are three steps to this 1. Create a job * Starts in status 'ready to send' * Created by None, since it's from the API * original file name 'letter submitted via api' 2. Create a single notification for that job * job_row_number 0 * client reference if provided * address line 1 as recipient 3. Trigger the build_dvla_file task we know that all the notifications have been created for this job (since we just created them ourselves synchronously), so this will just create the dvla-format file for the job, and upload it to s3.
This commit is contained in:
@@ -619,7 +619,7 @@ class JobStatus(db.Model):
|
|||||||
class Job(db.Model):
|
class Job(db.Model):
|
||||||
__tablename__ = 'jobs'
|
__tablename__ = 'jobs'
|
||||||
|
|
||||||
id = db.Column(UUID(as_uuid=True), primary_key=True)
|
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
original_file_name = db.Column(db.String, nullable=False)
|
original_file_name = db.Column(db.String, nullable=False)
|
||||||
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False)
|
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False)
|
||||||
service = db.relationship('Service', backref=db.backref('jobs', lazy='dynamic'))
|
service = db.relationship('Service', backref=db.backref('jobs', lazy='dynamic'))
|
||||||
|
|||||||
41
app/notifications/process_letter_notifications.py
Normal file
41
app/notifications/process_letter_notifications.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
from app import create_random_identifier
|
||||||
|
from app.models import LETTER_TYPE, JOB_STATUS_READY_TO_SEND
|
||||||
|
from app.notifications.process_notifications import persist_notification
|
||||||
|
|
||||||
|
|
||||||
|
def create_letter_api_job(template):
|
||||||
|
service = template.service
|
||||||
|
if not service.active:
|
||||||
|
raise InvalidRequest('Create job is not allowed: service is inactive', 403)
|
||||||
|
if template.archived:
|
||||||
|
raise InvalidRequest('Create job is not allowed: template is deleted', 400)
|
||||||
|
|
||||||
|
|
||||||
|
job = Job(
|
||||||
|
original_file_name='letter submitted via api',
|
||||||
|
service=service,
|
||||||
|
template=template,
|
||||||
|
template_version=template.version,
|
||||||
|
notification_count=1,
|
||||||
|
job_status=JOB_STATUS_READY_TO_SEND,
|
||||||
|
created_by=None
|
||||||
|
)
|
||||||
|
dao_create_job(job)
|
||||||
|
|
||||||
|
|
||||||
|
def create_letter_notification(letter_data, job, api_key):
|
||||||
|
notification = persist_notification(
|
||||||
|
template_id=job.template.id,
|
||||||
|
template_version=job.template.version,
|
||||||
|
recipient=letter_data['personalisation']['address line 1'], # or addressline1 or address_line_1?
|
||||||
|
service=job.service,
|
||||||
|
personalisation=letter_data['personalisation'],
|
||||||
|
notification_type=LETTER_TYPE,
|
||||||
|
api_key_id=api_key.id,
|
||||||
|
key_type=api_key.key_type,
|
||||||
|
job_id=job.id,
|
||||||
|
job_row_number=0,
|
||||||
|
reference=create_random_identifier(),
|
||||||
|
client_reference=letter_data.get('reference')
|
||||||
|
)
|
||||||
|
return notification
|
||||||
@@ -55,7 +55,7 @@ def persist_notification(
|
|||||||
created_by_id=None
|
created_by_id=None
|
||||||
):
|
):
|
||||||
notification_created_at = created_at or datetime.utcnow()
|
notification_created_at = created_at or datetime.utcnow()
|
||||||
if not notification_id and simulated:
|
if not notification_id:
|
||||||
notification_id = uuid.uuid4()
|
notification_id = uuid.uuid4()
|
||||||
notification = Notification(
|
notification = Notification(
|
||||||
id=notification_id,
|
id=notification_id,
|
||||||
|
|||||||
@@ -5,11 +5,16 @@ from flask import request, jsonify, current_app, abort
|
|||||||
from app import api_user, authenticated_service
|
from app import api_user, authenticated_service
|
||||||
from app.config import QueueNames
|
from app.config import QueueNames
|
||||||
from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, PRIORITY
|
from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, PRIORITY
|
||||||
|
from app.celery.tasks import build_dvla_file
|
||||||
from app.notifications.process_notifications import (
|
from app.notifications.process_notifications import (
|
||||||
persist_notification,
|
persist_notification,
|
||||||
send_notification_to_queue,
|
send_notification_to_queue,
|
||||||
simulated_recipient,
|
simulated_recipient,
|
||||||
persist_scheduled_notification)
|
persist_scheduled_notification)
|
||||||
|
from app.notifications.process_letter_notifications import (
|
||||||
|
create_letter_api_job,
|
||||||
|
create_letter_notification
|
||||||
|
)
|
||||||
from app.notifications.validators import (
|
from app.notifications.validators import (
|
||||||
validate_and_format_recipient,
|
validate_and_format_recipient,
|
||||||
check_rate_limiting,
|
check_rate_limiting,
|
||||||
@@ -58,10 +63,9 @@ def post_notification(notification_type):
|
|||||||
|
|
||||||
if notification_type == LETTER_TYPE:
|
if notification_type == LETTER_TYPE:
|
||||||
notification = process_letter_notification(
|
notification = process_letter_notification(
|
||||||
form=form,
|
letter_data=form,
|
||||||
api_key=api_user,
|
api_key=api_user,
|
||||||
template=template,
|
template=template,
|
||||||
service=authenticated_service,
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
notification = process_sms_or_email_notification(
|
notification = process_sms_or_email_notification(
|
||||||
@@ -140,10 +144,12 @@ def process_sms_or_email_notification(*, form, notification_type, api_key, templ
|
|||||||
return notification
|
return notification
|
||||||
|
|
||||||
|
|
||||||
def process_letter_notification(*, form, api_key, template, service):
|
def process_letter_notification(*, letter_data, api_key, template, service):
|
||||||
# create job
|
job = create_letter_api_job(template)
|
||||||
|
notification = create_letter_notification(letter_data, job, api_key)
|
||||||
# create notification
|
build_dvla_file.apply_async([str(job.id)], queue=QueueNames.JOBS)
|
||||||
|
current_app.logger.info("send job {} for api notification {} to build-dvla-file in the process-job queue".format(
|
||||||
# trigger build_dvla_file task
|
job.id,
|
||||||
raise NotImplementedError
|
notification.id
|
||||||
|
))
|
||||||
|
return notification
|
||||||
|
|||||||
Reference in New Issue
Block a user