diff --git a/app/routes/support.py b/app/routes/support.py index 23e4ddd..2db4b9e 100644 --- a/app/routes/support.py +++ b/app/routes/support.py @@ -182,7 +182,7 @@ def my_tickets(): # ── Customer: ticket detail ─────────────────────────────────────────────────── -@bp.route('/my-tickets/') +@bp.route('/my-tickets/', methods=['GET', 'POST']) @login_required def my_ticket_detail(ticket_id): if current_user.role != 'customer': @@ -192,6 +192,36 @@ def my_ticket_detail(ticket_id): if ticket is None or ticket.customer_id != current_user.id: abort(404) + if request.method == 'POST': + if ticket.status == 'closed': + flash('This ticket is closed and cannot receive new replies.', 'warning') + return redirect(url_for('support.my_ticket_detail', ticket_id=ticket_id)) + + body = request.form.get('body', '').strip() + if not body: + flash('Reply cannot be empty.', 'warning') + return redirect(url_for('support.my_ticket_detail', ticket_id=ticket_id)) + + reply = SupportTicketReply( + ticket_id = ticket.id, + user_id = current_user.id, + body = body, + created_at = now_eastern(), + ) + db.session.add(reply) + # Reopen if it was answered so admin sees there's a follow-up + if ticket.status == 'answered': + ticket.status = 'open' + db.session.commit() + + log_action(ACTION_CREATE, 'SupportTicketReply', reply.id, + f'ticket #{ticket.id}', + f'customer reply by {current_user.username}') + + _notify_admins_customer_reply(ticket, reply) + flash('Your reply has been sent.', 'success') + return redirect(url_for('support.my_ticket_detail', ticket_id=ticket_id)) + replies = ticket.replies.order_by(SupportTicketReply.created_at.asc()).all() return render_template('support/my_ticket_detail.html', ticket=ticket, replies=replies) @@ -316,3 +346,26 @@ def _notify_customer_reply(ticket, reply): send_email = True, ) db.session.commit() + + +def _notify_admins_customer_reply(ticket, reply): + """Notify admins when a customer adds a follow-up reply to their ticket.""" + admins = User.query.filter_by(role='admin', active=True).all() + if not admins: + return + + customer_label = ticket.customer.display_name if ticket.customer else 'Unknown' + link = url_for('support.admin_ticket_detail', ticket_id=ticket.id) + title = f'Customer reply on ticket #{ticket.id} from {customer_label}' + body = (f'Re: {ticket.subject}\n\n' + f'{reply.body[:400]}{"…" if len(reply.body) > 400 else ""}') + + for admin in admins: + notify( + recipient = admin, + title = title, + body = body, + link = link, + send_email = True, + ) + db.session.commit() diff --git a/app/templates/support/my_ticket_detail.html b/app/templates/support/my_ticket_detail.html index 7f2b7c0..e811146 100644 --- a/app/templates/support/my_ticket_detail.html +++ b/app/templates/support/my_ticket_detail.html @@ -60,7 +60,30 @@ {% endif %} -
+ {% if ticket.status != 'closed' %} +
+
Add a Follow-up
+
+
+ +
+ +
+ +
+
+
+ {% else %} +
+ This request is closed. + Start a new chat if you need further help. +
+ {% endif %} + +