2016-02-24 17:12:30 +00:00
|
|
|
from app import create_uuid
|
2016-02-17 17:48:23 +00:00
|
|
|
from app import notify_celery, encryption, firetext_client, aws_ses_client
|
|
|
|
|
from app.clients.email.aws_ses import AwsSesClientException
|
2016-02-17 13:59:01 +00:00
|
|
|
from app.clients.sms.firetext import FiretextClientException
|
2016-02-24 11:51:02 +00:00
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2016-02-25 09:59:50 +00:00
|
|
|
from app.dao.notifications_dao import dao_create_notification, dao_update_notification
|
2016-02-24 17:12:30 +00:00
|
|
|
from app.dao.jobs_dao import dao_update_job, dao_get_job_by_id
|
2016-02-15 16:01:14 +00:00
|
|
|
from app.models import Notification
|
|
|
|
|
from flask import current_app
|
2016-02-16 17:42:04 +00:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2016-02-24 17:12:30 +00:00
|
|
|
from app.aws import s3
|
2016-02-25 09:59:50 +00:00
|
|
|
from app.csv import get_recipient_from_csv
|
|
|
|
|
from datetime import datetime
|
2016-02-24 17:12:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="process-job")
|
|
|
|
|
def process_job(job_id):
|
2016-02-25 11:23:04 +00:00
|
|
|
start = datetime.utcnow()
|
2016-02-24 17:12:30 +00:00
|
|
|
job = dao_get_job_by_id(job_id)
|
|
|
|
|
job.status = 'in progress'
|
|
|
|
|
dao_update_job(job)
|
|
|
|
|
|
|
|
|
|
file = s3.get_job_from_s3(job.bucket_name, job_id)
|
2016-02-25 09:59:50 +00:00
|
|
|
recipients = get_recipient_from_csv(file)
|
2016-02-24 17:12:30 +00:00
|
|
|
|
2016-02-25 09:59:50 +00:00
|
|
|
for recipient in recipients:
|
|
|
|
|
encrypted = encryption.encrypt({
|
2016-02-24 17:12:30 +00:00
|
|
|
'template': job.template_id,
|
|
|
|
|
'job': str(job.id),
|
2016-02-25 09:59:50 +00:00
|
|
|
'to': recipient
|
2016-02-24 17:12:30 +00:00
|
|
|
})
|
|
|
|
|
|
2016-02-25 09:59:50 +00:00
|
|
|
if job.template.template_type == 'sms':
|
|
|
|
|
send_sms.apply_async((
|
|
|
|
|
str(job.service_id),
|
|
|
|
|
str(create_uuid()),
|
2016-02-25 11:23:04 +00:00
|
|
|
encrypted,
|
|
|
|
|
str(datetime.utcnow())),
|
2016-02-25 09:59:50 +00:00
|
|
|
queue='bulk-sms'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if job.template.template_type == 'email':
|
|
|
|
|
send_email.apply_async((
|
|
|
|
|
str(job.service_id),
|
|
|
|
|
str(create_uuid()),
|
|
|
|
|
job.template.subject,
|
|
|
|
|
"{}@{}".format(job.service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
2016-02-25 11:23:04 +00:00
|
|
|
encrypted,
|
|
|
|
|
str(datetime.utcnow())),
|
2016-02-25 09:59:50 +00:00
|
|
|
queue='bulk-email')
|
2016-02-24 17:12:30 +00:00
|
|
|
|
2016-02-25 11:23:04 +00:00
|
|
|
finished = datetime.utcnow()
|
2016-02-24 17:12:30 +00:00
|
|
|
job.status = 'finished'
|
2016-02-25 11:23:04 +00:00
|
|
|
job.processing_started = start
|
|
|
|
|
job.processing_finished = finished
|
2016-02-24 17:12:30 +00:00
|
|
|
dao_update_job(job)
|
2016-02-25 11:23:04 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"Job {} created at {} started at {} finished at {}".format(job_id, job.created_at, start, finished)
|
|
|
|
|
)
|
2016-02-09 13:31:45 +00:00
|
|
|
|
|
|
|
|
|
2016-02-16 17:17:02 +00:00
|
|
|
@notify_celery.task(name="send-sms")
|
2016-02-25 09:59:50 +00:00
|
|
|
def send_sms(service_id, notification_id, encrypted_notification, created_at):
|
2016-02-16 15:28:30 +00:00
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2016-02-24 11:51:02 +00:00
|
|
|
template = dao_get_template_by_id(notification['template'])
|
2016-02-15 16:01:14 +00:00
|
|
|
|
2016-02-25 11:23:04 +00:00
|
|
|
client = firetext_client
|
|
|
|
|
|
2016-02-15 16:01:14 +00:00
|
|
|
try:
|
2016-02-25 11:23:04 +00:00
|
|
|
sent_at = datetime.utcnow()
|
2016-02-16 17:42:04 +00:00
|
|
|
notification_db_object = Notification(
|
|
|
|
|
id=notification_id,
|
|
|
|
|
template_id=notification['template'],
|
|
|
|
|
to=notification['to'],
|
|
|
|
|
service_id=service_id,
|
2016-02-23 17:39:08 +00:00
|
|
|
job_id=notification.get('job', None),
|
2016-02-25 09:59:50 +00:00
|
|
|
status='sent',
|
|
|
|
|
created_at=created_at,
|
2016-02-25 11:23:04 +00:00
|
|
|
sent_at=sent_at,
|
|
|
|
|
sent_by=client.get_name()
|
|
|
|
|
|
2016-02-16 17:42:04 +00:00
|
|
|
)
|
2016-02-25 09:59:50 +00:00
|
|
|
dao_create_notification(notification_db_object)
|
2016-02-15 16:01:14 +00:00
|
|
|
|
2016-02-16 17:42:04 +00:00
|
|
|
try:
|
2016-02-25 11:23:04 +00:00
|
|
|
client.send_sms(notification['to'], template.content)
|
2016-02-17 13:59:01 +00:00
|
|
|
except FiretextClientException as e:
|
2016-02-16 17:42:04 +00:00
|
|
|
current_app.logger.debug(e)
|
2016-02-25 09:59:50 +00:00
|
|
|
notification_db_object.status = 'failed'
|
|
|
|
|
dao_update_notification(notification_db_object)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
2016-02-25 11:23:04 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"SMS {} created at {} sent at {}".format(notification_id, created_at, sent_at)
|
|
|
|
|
)
|
2016-02-22 17:17:29 +00:00
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.debug(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="send-email")
|
2016-02-25 09:59:50 +00:00
|
|
|
def send_email(service_id, notification_id, subject, from_address, encrypted_notification, created_at):
|
2016-02-22 17:17:29 +00:00
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2016-02-24 11:51:02 +00:00
|
|
|
template = dao_get_template_by_id(notification['template'])
|
2016-02-22 17:17:29 +00:00
|
|
|
|
2016-02-25 11:23:04 +00:00
|
|
|
client = aws_ses_client
|
|
|
|
|
|
2016-02-22 17:17:29 +00:00
|
|
|
try:
|
2016-02-25 11:23:04 +00:00
|
|
|
sent_at = datetime.utcnow()
|
2016-02-22 17:17:29 +00:00
|
|
|
notification_db_object = Notification(
|
|
|
|
|
id=notification_id,
|
|
|
|
|
template_id=notification['template'],
|
|
|
|
|
to=notification['to'],
|
|
|
|
|
service_id=service_id,
|
2016-02-24 09:55:05 +00:00
|
|
|
job_id=notification.get('job', None),
|
2016-02-25 09:59:50 +00:00
|
|
|
status='sent',
|
|
|
|
|
created_at=created_at,
|
2016-02-25 11:23:04 +00:00
|
|
|
sent_at=sent_at,
|
|
|
|
|
sent_by=client.get_name()
|
2016-02-22 17:17:29 +00:00
|
|
|
)
|
2016-02-25 09:59:50 +00:00
|
|
|
dao_create_notification(notification_db_object)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
|
|
|
|
try:
|
2016-02-25 11:23:04 +00:00
|
|
|
client.send_email(
|
2016-02-22 17:17:29 +00:00
|
|
|
from_address,
|
|
|
|
|
notification['to'],
|
|
|
|
|
subject,
|
|
|
|
|
template.content
|
|
|
|
|
)
|
|
|
|
|
except AwsSesClientException as e:
|
|
|
|
|
current_app.logger.debug(e)
|
2016-02-25 09:59:50 +00:00
|
|
|
notification_db_object.status = 'failed'
|
|
|
|
|
dao_update_notification(notification_db_object)
|
2016-02-15 16:01:14 +00:00
|
|
|
|
2016-02-25 11:23:04 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"Email {} created at {} sent at {}".format(notification_id, created_at, sent_at)
|
|
|
|
|
)
|
2016-02-16 17:42:04 +00:00
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.debug(e)
|
2016-02-17 15:41:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='send-sms-code')
|
2016-02-18 09:59:18 +00:00
|
|
|
def send_sms_code(encrypted_verification):
|
|
|
|
|
verification_message = encryption.decrypt(encrypted_verification)
|
2016-02-17 15:41:33 +00:00
|
|
|
try:
|
2016-02-18 09:59:18 +00:00
|
|
|
firetext_client.send_sms(verification_message['to'], verification_message['secret_code'])
|
2016-02-17 15:52:09 +00:00
|
|
|
except FiretextClientException as e:
|
2016-02-17 17:48:23 +00:00
|
|
|
current_app.logger.error(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='send-email-code')
|
2016-02-18 09:52:27 +00:00
|
|
|
def send_email_code(encrypted_verification_message):
|
|
|
|
|
verification_message = encryption.decrypt(encrypted_verification_message)
|
2016-02-17 17:48:23 +00:00
|
|
|
try:
|
2016-02-22 13:02:09 +00:00
|
|
|
aws_ses_client.send_email(current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
|
|
|
|
|
verification_message['to'],
|
|
|
|
|
"Verification code",
|
|
|
|
|
verification_message['secret_code'])
|
2016-02-17 17:48:23 +00:00
|
|
|
except AwsSesClientException as e:
|
|
|
|
|
current_app.logger.error(e)
|