06/09 Allow customer to reply admin answer
This commit is contained in:
+54
-1
@@ -182,7 +182,7 @@ def my_tickets():
|
||||
|
||||
# ── Customer: ticket detail ───────────────────────────────────────────────────
|
||||
|
||||
@bp.route('/my-tickets/<int:ticket_id>')
|
||||
@bp.route('/my-tickets/<int:ticket_id>', 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()
|
||||
|
||||
Reference in New Issue
Block a user