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-16 17:17:02 +00:00
|
|
|
from app.dao.notifications_dao import save_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
|
|
|
|
|
from app.csv import get_mobile_numbers_from_csv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="process-job")
|
|
|
|
|
def process_job(job_id):
|
|
|
|
|
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)
|
|
|
|
|
mobile_numbers = get_mobile_numbers_from_csv(file)
|
|
|
|
|
|
|
|
|
|
for mobile_number in mobile_numbers:
|
|
|
|
|
notification = encryption.encrypt({
|
|
|
|
|
'template': job.template_id,
|
|
|
|
|
'job': str(job.id),
|
|
|
|
|
'to': mobile_number
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
send_sms.apply_async((
|
|
|
|
|
str(job.service_id),
|
|
|
|
|
str(create_uuid()),
|
|
|
|
|
notification),
|
|
|
|
|
queue='sms'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
job.status = 'finished'
|
|
|
|
|
dao_update_job(job)
|
2016-02-09 13:31:45 +00:00
|
|
|
|
|
|
|
|
|
2016-02-16 17:17:02 +00:00
|
|
|
@notify_celery.task(name="send-sms")
|
2016-02-23 17:39:08 +00:00
|
|
|
def send_sms(service_id, notification_id, encrypted_notification):
|
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
|
|
|
|
|
|
|
|
try:
|
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-16 17:42:04 +00:00
|
|
|
status='sent'
|
|
|
|
|
)
|
|
|
|
|
save_notification(notification_db_object)
|
2016-02-15 16:01:14 +00:00
|
|
|
|
2016-02-16 17:42:04 +00:00
|
|
|
try:
|
2016-02-17 12:57:51 +00:00
|
|
|
firetext_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)
|
|
|
|
|
save_notification(notification_db_object, {"status": "failed"})
|
2016-02-22 17:17:29 +00:00
|
|
|
|
|
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.debug(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="send-email")
|
|
|
|
|
def send_email(service_id, notification_id, subject, from_address, encrypted_notification):
|
|
|
|
|
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
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
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-22 17:17:29 +00:00
|
|
|
status='sent'
|
|
|
|
|
)
|
|
|
|
|
save_notification(notification_db_object)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
aws_ses_client.send_email(
|
|
|
|
|
from_address,
|
|
|
|
|
notification['to'],
|
|
|
|
|
subject,
|
|
|
|
|
template.content
|
|
|
|
|
)
|
|
|
|
|
except AwsSesClientException as e:
|
|
|
|
|
current_app.logger.debug(e)
|
|
|
|
|
save_notification(notification_db_object, {"status": "failed"})
|
2016-02-15 16:01:14 +00:00
|
|
|
|
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)
|