- Backend: FastAPI app with all models, services, schemas, routes - Admin Panel: Next.js dashboard with all modules (CRM, POS, repairs, warranties, billing, notifications, etc.) - Ecommerce: storefront (submodule) + product catalog - AI Agents: .agents/ skill library for Antigravity IDE - Docs: architecture, billing, notification gateway markdown - Submodules: Backend/OpenWA, Ecommerce/File 2/storefront
70 lines
3.1 KiB
Python
70 lines
3.1 KiB
Python
from typing import Any, Dict
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import select
|
|
from app.core.database.db_session import SessionLocal
|
|
from app.models.RoleNotificationSettingModel import RoleNotificationSetting
|
|
from app.models.UserModel import User
|
|
from app.models.RoleModel import Role
|
|
from app.services.NotificationGatewayService import NotificationGatewayService
|
|
from app.schemas.NotificationGateway import NotificationDispatchCreate
|
|
|
|
from app.services.TemplateService import TemplateService
|
|
|
|
def dispatch_internal_notification(event_type: str, message: str, subject: str = None, context: dict = None):
|
|
"""
|
|
Dispatches a notification to all staff users whose role is subscribed to `event_type`.
|
|
"""
|
|
db = SessionLocal()
|
|
try:
|
|
settings = db.execute(select(RoleNotificationSetting).where(RoleNotificationSetting.event_type == event_type)).scalars().all()
|
|
if not settings:
|
|
return
|
|
|
|
for setting in settings:
|
|
users = db.execute(
|
|
select(User)
|
|
.join(Role, User.role_id == Role.role_id)
|
|
.where(Role.role_name == setting.role_name, User.is_active == True)
|
|
).scalars().all()
|
|
for user in users:
|
|
if setting.channel_whatsapp and user.phone:
|
|
final_subj = None
|
|
final_msg = f"[{event_type}]\n{message}"
|
|
if context:
|
|
rendered = TemplateService.render_template(db, event_type, "WHATSAPP", context)
|
|
if rendered:
|
|
_, final_msg = rendered
|
|
|
|
NotificationGatewayService.dispatch_notification(
|
|
db=db,
|
|
tenant_id="tenant_default",
|
|
payload=NotificationDispatchCreate(
|
|
channel="WHATSAPP",
|
|
recipient=user.phone,
|
|
message_body=final_msg
|
|
)
|
|
)
|
|
if setting.channel_email and user.email:
|
|
final_subj = subject or f"iFixKart Notification: {event_type}"
|
|
final_msg = message
|
|
if context:
|
|
rendered = TemplateService.render_template(db, event_type, "EMAIL", context)
|
|
if rendered:
|
|
r_subj, final_msg = rendered
|
|
if r_subj:
|
|
final_subj = r_subj
|
|
|
|
NotificationGatewayService.dispatch_notification(
|
|
db=db,
|
|
tenant_id="tenant_default",
|
|
payload=NotificationDispatchCreate(
|
|
channel="EMAIL",
|
|
recipient=user.email,
|
|
subject=final_subj,
|
|
message_body=final_msg
|
|
)
|
|
)
|
|
except Exception as e:
|
|
print(f"Error dispatching internal notification {event_type}: {e}")
|
|
finally:
|
|
db.close()
|