Files
IT_Ticket_System/app/services/notification_service.py
T
2026-04-06 16:31:50 -04:00

365 lines
16 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import logging
import re
from flask import current_app, render_template_string
from flask_mail import Message
from app import db, mail, socketio
from app.models import Notification, NotificationType, User, UserRole
logger = logging.getLogger(__name__)
def _strip_tags(html: str) -> str:
"""Strip HTML tags from a stored comment body for use in notification text.
Comment bodies are persisted as sanitized HTML. When a snippet is included
in an in-app notification message or email, raw tags like <strong>, <p>
render as literal text in notification dropdowns and look unsightly.
This helper produces a clean plain-text preview for those contexts.
"""
return re.sub(r'<[^>]+>', '', html or '')
# ─── Email Templates ──────────────────────────────────────────────────────────
_NEW_TICKET_EMAIL = """
<html><body style="font-family:Arial,sans-serif;background:#f4f4f4;padding:20px;">
<div style="max-width:600px;margin:0 auto;background:#fff;border-radius:8px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,.1);">
<div style="background:#1a1a2e;padding:24px 32px;">
<h1 style="color:#e94560;margin:0;font-size:22px;">🎫 New IT Ticket Created</h1>
</div>
<div style="padding:32px;">
<p style="color:#555;margin-top:0;">A new support ticket has been submitted.</p>
<table style="width:100%;border-collapse:collapse;margin:16px 0;">
<tr><td style="padding:8px;color:#888;width:140px;">Ticket #</td><td style="padding:8px;font-weight:bold;">{{ ticket_number }}</td></tr>
<tr style="background:#f9f9f9;"><td style="padding:8px;color:#888;">Title</td><td style="padding:8px;">{{ title }}</td></tr>
<tr><td style="padding:8px;color:#888;">Category</td><td style="padding:8px;">{{ category }}</td></tr>
<tr style="background:#f9f9f9;"><td style="padding:8px;color:#888;">Priority</td><td style="padding:8px;"><span style="background:{{ priority_color }};color:#fff;padding:2px 8px;border-radius:4px;">{{ priority }}</span></td></tr>
<tr><td style="padding:8px;color:#888;">Submitted by</td><td style="padding:8px;">{{ submitted_by }}</td></tr>
</table>
<div style="background:#f9f9f9;border-left:4px solid #e94560;padding:16px;margin:16px 0;border-radius:0 4px 4px 0;">
<p style="margin:0;color:#333;">{{ description }}</p>
</div>
<a href="{{ ticket_url }}" style="display:inline-block;background:#e94560;color:#fff;padding:12px 24px;border-radius:6px;text-decoration:none;margin-top:8px;">View Ticket</a>
</div>
<div style="background:#f4f4f4;padding:16px 32px;text-align:center;color:#999;font-size:12px;">
IT Helpdesk System &bull; This is an automated notification.
</div>
</div>
</body></html>
"""
_STATUS_UPDATE_EMAIL = """
<html><body style="font-family:Arial,sans-serif;background:#f4f4f4;padding:20px;">
<div style="max-width:600px;margin:0 auto;background:#fff;border-radius:8px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,.1);">
<div style="background:#1a1a2e;padding:24px 32px;">
<h1 style="color:#e94560;margin:0;font-size:22px;">🔄 Ticket Update</h1>
</div>
<div style="padding:32px;">
<p style="color:#555;margin-top:0;">Your ticket <strong>{{ ticket_number }}</strong> has been updated.</p>
<p><strong>{{ title }}</strong></p>
<p style="color:#555;">{{ message }}</p>
<a href="{{ ticket_url }}" style="display:inline-block;background:#e94560;color:#fff;padding:12px 24px;border-radius:6px;text-decoration:none;margin-top:8px;">View Ticket</a>
</div>
<div style="background:#f4f4f4;padding:16px 32px;text-align:center;color:#999;font-size:12px;">
IT Helpdesk System &bull; This is an automated notification.
</div>
</div>
</body></html>
"""
def _priority_badge_color(priority):
return {'low': '#28a745', 'medium': '#ffc107', 'high': '#dc3545', 'critical': '#343a40'}.get(priority, '#6c757d')
# ─── In-App Notification ──────────────────────────────────────────────────────
def create_notification(user_id, notif_type, title, message, ticket_id=None, link=None):
"""Persist an in-app notification and push via WebSocket.
Transaction note
----------------
This function does NOT call db.session.commit(). It flushes the new
Notification row to surface constraint violations early, then defers
the final commit to the caller. This keeps all notifications for a
given event (e.g. notifying every IT staff member on a new ticket)
in a single atomic transaction rather than N separate commits, and
prevents partial notification state if an error occurs mid-loop.
Callers that use create_notification in a loop (notify_new_ticket,
notify_comment_added) must commit after the loop completes.
Callers that use it standalone (notify_status_change, notify_assignment)
must also commit after calling this function.
"""
try:
notif = Notification(
user_id = user_id,
ticket_id= ticket_id,
type = notif_type,
title = title,
message = message,
link = link,
)
db.session.add(notif)
# Flush to obtain notif.id for the WebSocket payload without committing.
# The caller is responsible for the final db.session.commit().
db.session.flush()
logger.info(f'[NOTIFICATION CREATE] user_id={user_id} type={notif_type} ticket_id={ticket_id}')
# Real-time push — schedule via socketio.start_background_task so the
# emit runs inside eventlet's green-thread pool, not inline in the WSGI
# request context. Inline emits during a polling→WebSocket upgrade can
# race with the upgrade handshake and disconnect the client.
payload = {
'id' : notif.id,
'type' : notif_type,
'title' : title,
'message' : message,
'link' : link,
'created_at': notif.created_at.isoformat(),
}
def _emit():
socketio.emit(
'new_notification',
payload,
to=f'user_{user_id}', # 'to' is the modern alias for 'room'
namespace='/', # explicit default namespace — avoids
) # ambiguity under reverse-proxy setups
socketio.start_background_task(_emit)
except Exception as exc:
# Expunge only the failed notification entry — do NOT roll back the
# full session, as that would undo the parent operation (e.g. a ticket
# update) that triggered this notification call.
try:
db.session.expunge(notif)
except Exception:
pass
logger.error(f'[NOTIFICATION ERROR] Failed to create notification: {exc}')
# ─── Email Notification ───────────────────────────────────────────────────────
def send_email(subject, recipients, html_body):
"""
Send an email in a background thread so it never blocks the request.
Flask-Mail opens an SMTP connection synchronously. Running it in the main
request thread means a slow or failing SMTP server delays the entire
response — including the WebSocket notification push that follows.
Using a daemon thread isolates SMTP failures from the request lifecycle.
"""
from threading import Thread
from flask import current_app
app = current_app._get_current_object() # real app, not the proxy
def _send():
with app.app_context():
try:
msg = Message(subject=subject, recipients=recipients, html=html_body)
mail.send(msg)
logger.info(f'[EMAIL SENT] subject="{subject}" to={recipients}')
except Exception as exc:
logger.error(f'[EMAIL ERROR] Failed to send "{subject}" to {recipients}: {exc}')
t = Thread(target=_send, daemon=True)
t.start()
# ─── Ticket Event Helpers ─────────────────────────────────────────────────────
def notify_new_ticket(ticket):
"""Notify IT staff (email + in-app) and confirm receipt to the creator."""
base_url = current_app.config.get('APP_BASE_URL', '')
ticket_url = f"{base_url}/tickets/{ticket.id}"
# Email IT staff
html = render_template_string(_NEW_TICKET_EMAIL,
ticket_number = ticket.ticket_number,
title = ticket.title,
description = ticket.description[:500],
category = ticket.category.replace('_', ' ').title(),
priority = ticket.priority.upper(),
priority_color= _priority_badge_color(ticket.priority),
submitted_by = ticket.creator.full_name,
ticket_url = ticket_url,
)
it_email = current_app.config.get('IT_DEPT_EMAIL')
send_email(f'[New Ticket] {ticket.ticket_number} {ticket.title}', [it_email], html)
# In-app: all IT staff
it_users = User.query.filter(
User.role.in_([UserRole.IT_STAFF, UserRole.ADMIN]),
User.is_active == True,
).all()
for staff in it_users:
create_notification(
user_id = staff.id,
notif_type= NotificationType.TICKET_CREATED,
title = f'New Ticket: {ticket.ticket_number}',
message = f'{ticket.creator.full_name} submitted: {ticket.title}',
ticket_id = ticket.id,
link = f'/tickets/{ticket.id}',
)
# Confirm to creator
create_notification(
user_id = ticket.created_by_id,
notif_type= NotificationType.TICKET_CREATED,
title = f'Ticket {ticket.ticket_number} Created',
message = 'Your ticket has been received. Our IT team will review it shortly.',
ticket_id = ticket.id,
link = f'/tickets/{ticket.id}',
)
# Commit all notifications in a single transaction.
# create_notification() flushes but does not commit — the loop above
# accumulates all Notification rows and this single commit persists them all.
db.session.commit()
def notify_status_change(ticket, old_status, changed_by):
"""Notify ticket creator of status change."""
base_url = current_app.config.get('APP_BASE_URL', '')
ticket_url = f"{base_url}/tickets/{ticket.id}"
msg_text = f'Status changed from {old_status.replace("_"," ").title()} to {ticket.status.replace("_"," ").title()} by {changed_by.full_name}.'
html = render_template_string(_STATUS_UPDATE_EMAIL,
ticket_number = ticket.ticket_number,
title = ticket.title,
message = msg_text,
ticket_url = ticket_url,
)
if ticket.creator.email_notif:
send_email(
f'[Ticket Update] {ticket.ticket_number} Status Changed',
[ticket.creator.email],
html,
)
create_notification(
user_id = ticket.created_by_id,
notif_type= NotificationType.STATUS_CHANGED,
title = f'Ticket {ticket.ticket_number} Updated',
message = msg_text,
ticket_id = ticket.id,
link = f'/tickets/{ticket.id}',
)
db.session.commit()
logger.info(f'[TICKET STATUS] ticket_id={ticket.id} {old_status} -> {ticket.status} by user_id={changed_by.id}')
def notify_comment_added(comment):
"""Notify relevant parties when a comment is added."""
ticket = comment.ticket
base_url = current_app.config.get('APP_BASE_URL', '')
ticket_url = f"{base_url}/tickets/{ticket.id}"
# ── Real-time push to everyone viewing this ticket ────────────────────────
# Emit to the ticket room so all users currently on the ticket detail page
# receive the new comment immediately without needing to reload.
payload = {
'id' : comment.id,
'author_name' : comment.author.full_name,
'author_init' : comment.author.full_name[0].upper(),
'author_avatar': comment.author.avatar_url or '',
'is_it_staff' : comment.author.is_it_staff,
'is_internal': comment.is_internal,
'body' : comment.body,
'created_at' : comment.created_at.strftime('%b %d, %Y %H:%M'),
'author_id' : comment.author_id,
'can_delete' : True, # the author always can; recipient-side JS checks role too
'attachments': [
{
'id' : a.id,
'filename' : a.filename,
'mime_type': a.mime_type or '',
'is_image' : (a.mime_type or '').startswith('image/'),
}
for a in comment.attachments.all()
],
}
def _emit_comment():
socketio.emit(
'new_comment',
payload,
to = f'ticket_{ticket.id}',
namespace = '/',
)
socketio.start_background_task(_emit_comment)
# ─────────────────────────────────────────────────────────────────────────
notified = set()
def _notify(user_id, is_internal=False):
if user_id in notified:
return
notified.add(user_id)
# Strip HTML tags from the stored comment body before embedding in
# the notification message — raw tags render as literal text in the
# in-app notification dropdown and look unsightly.
plain_body = _strip_tags(comment.body)
create_notification(
user_id = user_id,
notif_type= NotificationType.COMMENT_ADDED,
title = f'New Comment on {ticket.ticket_number}',
message = f'{comment.author.full_name} commented: {plain_body[:100]}',
ticket_id = ticket.id,
link = f'/tickets/{ticket.id}#comment-{comment.id}',
)
user = db.session.get(User, user_id)
if user and user.email_notif and not is_internal:
html = render_template_string(_STATUS_UPDATE_EMAIL,
ticket_number = ticket.ticket_number,
title = ticket.title,
message = f'{comment.author.full_name} added a comment: {plain_body[:300]}',
ticket_url = ticket_url,
)
send_email(
f'[Ticket Comment] {ticket.ticket_number}',
[user.email],
html,
)
# Notify creator (skip if internal-only comment)
if not comment.is_internal:
_notify(ticket.created_by_id)
# Notify assignee
if ticket.assigned_to_id and ticket.assigned_to_id != comment.author_id:
_notify(ticket.assigned_to_id, is_internal=comment.is_internal)
# Commit all accumulated Notification rows in a single transaction.
# create_notification() flushes but does not commit.
db.session.commit()
logger.info(f'[COMMENT ADD] comment_id={comment.id} ticket_id={ticket.id} author_id={comment.author_id} internal={comment.is_internal}')
def notify_assignment(ticket, assigned_by):
"""Notify newly assigned IT staff member."""
if not ticket.assigned_to_id:
return
base_url = current_app.config.get('APP_BASE_URL', '')
ticket_url = f"{base_url}/tickets/{ticket.id}"
create_notification(
user_id = ticket.assigned_to_id,
notif_type= NotificationType.TICKET_ASSIGNED,
title = f'Ticket Assigned: {ticket.ticket_number}',
message = f'You have been assigned ticket "{ticket.title}" by {assigned_by.full_name}.',
ticket_id = ticket.id,
link = f'/tickets/{ticket.id}',
)
db.session.commit()
if ticket.assignee and ticket.assignee.email_notif:
html = render_template_string(_STATUS_UPDATE_EMAIL,
ticket_number = ticket.ticket_number,
title = ticket.title,
message = f'This ticket has been assigned to you by {assigned_by.full_name}.',
ticket_url = ticket_url,
)
send_email(
f'[Ticket Assigned] {ticket.ticket_number}',
[ticket.assignee.email],
html,
)
logger.info(f'[TICKET ASSIGN] ticket_id={ticket.id} assigned_to={ticket.assigned_to_id} by={assigned_by.id}')