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

Email admin about journal response #1096

Merged
merged 5 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions api/app/responses/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ def post(self):
user = user_repository.get_by_id(user_id)
response = response_repository.get_by_id_and_user_id(response.id, user_id)
self.send_confirmation(user, response)

event = event_repository.get_event_by_response_id(response.id)
if event.event_type == EventType.CONTINUOUS_JOURNAL:
all_admin_users = user_repository.get_all_admin()
for admin_user in all_admin_users:
self.send_confirmation(admin_user, response, admin_user=True)
shreyalpandit marked this conversation as resolved.
Show resolved Hide resolved
except:
LOGGER.warn('Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully'.format(id=response.id))
finally:
Expand Down Expand Up @@ -152,6 +158,12 @@ def put(self):
user = user_repository.get_by_id(user_id)
response = response_repository.get_by_id_and_user_id(response.id, user_id)
self.send_confirmation(user, response)

event = event_repository.get_event_by_response_id(response.id)
if event.event_type == EventType.CONTINUOUS_JOURNAL:
all_admin_users = user_repository.get_all_admin()
for admin_user in all_admin_users:
self.send_confirmation(admin_user, response, admin_user=True)
shreyalpandit marked this conversation as resolved.
Show resolved Hide resolved
except:
LOGGER.warn('Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully'.format(id=response.id))
finally:
Expand Down Expand Up @@ -191,7 +203,7 @@ def delete(self):

return {}, 204

def send_confirmation(self, user, response):
def send_confirmation(self, user, response, admin_user=None):
try:
answers = response.answers
if not answers:
Expand All @@ -215,15 +227,26 @@ def send_confirmation(self, user, response):
else:
event_description = event.get_description('en')

emailer.email_user(
'confirmation-response-call' if event.event_type == EventType.CALL else 'confirmation-response',
if admin_user:
shreyalpandit marked this conversation as resolved.
Show resolved Hide resolved
emailer.email_user(
'assign-action-editor',
template_parameters=dict(
event_description=event_description,
question_answer_summary=question_answer_summary,
),
event=event,
user=user
)
else:
emailer.email_user(
'confirmation-response-call' if event.event_type == EventType.CALL else 'confirmation-response',
template_parameters=dict(
event_description=event_description,
question_answer_summary=question_answer_summary,
),
event=event,
user=user
)

except Exception as e:
LOGGER.error('Could not send confirmation email for response with id : {response_id} due to: {e}'.format(response_id=response.id, e=e))
Expand Down
8 changes: 8 additions & 0 deletions api/app/users/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,11 @@ def get_all_with_responses_or_invited_guests_for(event_id):
# both in the filter.
.filter(or_(InvitedGuest.event_id==event_id, ApplicationForm.event_id==event_id))
).all()

@staticmethod
def get_all_admin():
shreyalpandit marked this conversation as resolved.
Show resolved Hide resolved
return (
db.session.query(AppUser)
.filter_by(active=True, is_deleted=False, is_admin=True)\
.all()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Adding email templates for assigning an action editor to a journal application

Revision ID: cebfdfef31cd
Revises: 6a073dd1e30d
Create Date: 2023-03-02 14:03:29.661901

"""

# revision identifiers, used by Alembic.
revision = 'cebfdfef31cd'
down_revision = '6a073dd1e30d'

from alembic import op
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base
from app import db

Base = declarative_base()



class EmailTemplate(Base):

__tablename__ = 'email_template'
__table_args__ = {'extend_existing': True}

id = db.Column(db.Integer(), primary_key=True)
key = db.Column(db.String(50), nullable=False)
event_id = db.Column(db.Integer(), nullable=True)
language = db.Column(db.String(2), nullable=False)
template = db.Column(db.String(), nullable=False)
subject = db.Column(db.String(), nullable=False)

def __init__(self, key, event_id, subject, template, language):
self.key = key
self.event_id = event_id
self.subject = subject
self.template = template
self.language = language


def upgrade():
Base.metadata.bind = op.get_bind()
session = orm.Session(bind=Base.metadata.bind)

op.execute("""SELECT setval('email_template_id_seq', (SELECT max(id) FROM email_template));""")
shreyalpandit marked this conversation as resolved.
Show resolved Hide resolved

template = """Dear {title} {firstname} {lastname},

A new application for {event_name} was received. Please assign an action editor to this application as soon as possible.

"""

session.add(EmailTemplate('assign-action-editor', None, '{event_name} Response Receieved', template, 'en'))

template = """Dear {title} {firstname} {lastname},

A new application {event_name} was received but has not been assigned to an action editor yet. Please assign an action editor to this application as soon as possible.
shreyalpandit marked this conversation as resolved.
Show resolved Hide resolved

"""

session.add(EmailTemplate('action-editor-not-assigned', None, '{event_name} Action Editor ', template, 'en'))

session.commit()



def downgrade():
added_keys = ['assign-action-editor', 'action-editor-not-assigned']
op.execute("""DELETE FROM email_template WHERE key in ({})""".format(', '.join(["'" + k + "'" for k in added_keys])))
op.execute("""SELECT setval('email_template_id_seq', (SELECT max(id) FROM email_template));""")