+
    `Øj#/  ã                   ó(  € ^ RI t ^ RIHtHt ^ RIHtHt ^ RIHtHtH	t	H
t
 ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHtHt ^ R
IHt ^ RIHt R tRR R lltRR R lltR R R lltR R R lltR R ltR!R R llt R"R R llt!R R lt"R R lt#R# )#é    N)ÚdatetimeÚtimezone)ÚOptionalÚList)ÚselectÚupdateÚfuncÚor_)ÚAsyncSession)ÚNotification)Ú
LoginUsers)ÚRoleModulePermission)ÚDealerOrderÚDealerOrderVehicle)ÚCustomerMaster)Ú
app_loggerc                  óJ   € \         P                  ! \        P                  4      # ©N)r   Únowr   Úutc© ó    ÚC/var/www/html/audi_dev/backend/app/modules/notifications/service.pyÚnow_utcr      s   € Ü�<Š<œŸ™Ó%Ð%r   c                ó    € V ^8„  d   QhR\         R\        P                  R\        R\        R\
        \        \        ,          \        3,          /# )é   ÚdbÚuser_idÚlimitÚunread_onlyÚreturn)r   ÚuuidÚUUIDÚintÚboolÚtupler   r   )Úformats   "r   Ú__annotate__r(      sM   € ÷ !%ñ !%Üð!%ä�Y‰Yð!%ô ð!%ô ð	!%ô
 Œ4”ÕœsÐ"Õ#ñ!%r   c              ƒ  ó*  "  € \        \        P                  ! \        P                  4      4      P                  \        P                  R8H  \        P                  V8H  \        P                  R8H  4      pV P                  V4      G Rj  x€L
 P                  4       ;'       g    ^ p\        \        4      P                  \        P                  R8H  \        P                  V8H  4      pV'       d#   VP                  \        P                  R8H  4      pVP                  \        P                  P                  4       4      P                  V4      pV P                  V4      G Rj  x€L
 P                  4       P!                  4       p\#        V4      V3#  EL L05i)zP
Fetches notifications for an internal staff user, plus the total unread count.
ÚSTAFFFN)r   r	   Úcountr   Únotification_idÚwhereÚrecipient_typer   Úis_readÚexecuteÚscalarÚorder_byÚ
created_atÚdescr   ÚscalarsÚallÚlist)r   r   r   r    Ú
count_stmtÚunread_countÚstmtÚitemss   &&&&    r   Úget_user_notificationsr<      s2  é € ô 	Œt�zŠzœ,×6Ñ6Ó7Ó8ß	‰Ü×'Ñ'¨7Ñ2Ü× Ñ  GÑ+Ü× Ñ  EÑ)ó

ð ð Ÿ*™* ZÓ0×0×8Ñ8Ó:×?Ð?¸a€Lô 	Œ|Óß	‰Ü×'Ñ'¨7Ñ2Ü× Ñ  GÑ+ó

ð 	÷ Ø�z‰zœ,×.Ñ.°%Ñ7Ó8ˆà�=‰=œ×0Ñ0×5Ñ5Ó7Ó8×>Ñ>¸uÓE€DØ—:‘:˜dÓ#×#×,Ñ,Ó.×2Ñ2Ó4€EÜ�‹;˜Ð$Ð$ò 1ñ $ùó+   ‚BFÂFÂ	FÂ"B>FÅ FÅ!.FÆFc                óŒ   € V ^8„  d   QhR\         R\        R\        R\        R\        \
        \        ,          \        3,          /# )r   r   Ú	dealer_idr   r    r!   )r   Ústrr$   r%   r&   r   r   )r'   s   "r   r(   r(   7   sI   € ÷ %ñ %Üð%äð%ô ð%ô ð	%ô
 Œ4”ÕœsÐ"Õ#ñ%r   c              ƒ  ó*  "  € \        \        P                  ! \        P                  4      4      P                  \        P                  R8H  \        P                  V8H  \        P                  R8H  4      pV P                  V4      G Rj  x€L
 P                  4       ;'       g    ^ p\        \        4      P                  \        P                  R8H  \        P                  V8H  4      pV'       d#   VP                  \        P                  R8H  4      pVP                  \        P                  P                  4       4      P                  V4      pV P                  V4      G Rj  x€L
 P                  4       P!                  4       p\#        V4      V3#  EL L05i)zQ
Fetches notifications for a dealer portal account, plus the total unread count.
ÚDEALERFN)r   r	   r+   r   r,   r-   r.   r?   r/   r0   r1   r2   r3   r4   r   r5   r6   r7   )r   r?   r   r    r8   r9   r:   r;   s   &&&&    r   Úget_dealer_notificationsrC   7   s2  é € ô 	Œt�zŠzœ,×6Ñ6Ó7Ó8ß	‰Ü×'Ñ'¨8Ñ3Ü×"Ñ" iÑ/Ü× Ñ  EÑ)ó

ð ð Ÿ*™* ZÓ0×0×8Ñ8Ó:×?Ð?¸a€Lô 	Œ|Óß	‰Ü×'Ñ'¨8Ñ3Ü×"Ñ" iÑ/ó

ð 	÷ Ø�z‰zœ,×.Ñ.°%Ñ7Ó8ˆà�=‰=œ×0Ñ0×5Ñ5Ó7Ó8×>Ñ>¸uÓE€DØ—:‘:˜dÓ#×#×,Ñ,Ó.×2Ñ2Ó4€EÜ�‹;˜Ð$Ð$ò 1ñ $ùr=   c          
      ó¨   € V ^8„  d   QhR\         R\        P                  R\        \        P                  ,          R\        \        ,          R\
        /# )r   r   r,   r   r?   r!   )r   r"   r#   r   r@   r%   )r'   s   "r   r(   r(   Y   sK   € ÷ ñ Üðä—Y‘Yðô ”d—i‘iÕ ðô œ�}ð	ô
 
ñr   c              ƒ  ó.  "  € V P                  \        V4      G Rj  x€L
 pV'       g   R# V'       d   VP                  V8w  d   R# V'       d   VP                  V8w  d   R# RVn        \        4       Vn        V P                  4       G Rj  x€L
  R#  Lt L5i)zP
Marks a single notification as read if it belongs to the requesting recipient.
NFT)Úgetr   r   r?   r/   r   Úread_atÚcommit)r   r,   r   r?   Únotifs   &&&& r   Úmark_notification_as_readrJ   Y   sn   é € ð —&‘&œ Ó7×7€EßÙç�5—=‘= GÔ+Ùß�U—_‘_¨	Ô1Ùà€E„MÜ“I€E„MØ
�)‰)‹+×ÐÙñ 8ñ ùs1   ‚BœB�B©	B³BÁ<BÂ
BÂBÂBc                óˆ   € V ^8„  d   QhR\         R\        \        P                  ,          R\        \        ,          R\
        /# )r   r   r   r?   r!   )r   r   r"   r#   r@   r$   )r'   s   "r   r(   r(   q   s=   € ÷ "ñ "Üð"ä”d—i‘iÕ ð"ô œ�}ð"ô 	ñ	"r   c              ƒ  ól  "  € \        4       pV'       df   \        \        4      P                  \        P                  R8H  \        P
                  V8H  \        P                  R8H  4      P                  RVR7      pMoV'       df   \        \        4      P                  \        P                  R8H  \        P                  V8H  \        P                  R8H  4      P                  RVR7      pM^ # V P                  V4      G Rj  x€L
 pV P                  4       G Rj  x€L
  VP                  ;'       g    ^ #  L2 L5i)z2
Marks all notifications for a recipient as read.
r*   FT)r/   rG   rB   N)r   r   r   r-   r.   r   r/   Úvaluesr?   r0   rH   Úrowcount)r   r   r?   r   r:   Úress   &&&   r   Úmark_all_notifications_as_readrP   q   s÷   é € ô ‹)€Cßä”<Ó ß‰UÜ×+Ñ+¨wÑ6Ü×$Ñ$¨Ñ/Ü×$Ñ$¨Ñ-ó÷
 ‰V˜D¨#ˆVÓ.ñ 	÷ 
ä”<Ó ß‰UÜ×+Ñ+¨xÑ7Ü×&Ñ&¨)Ñ3Ü×$Ñ$¨Ñ-ó÷
 ‰V˜D¨#ˆVÓ.ñ 	ñ à—
‘
˜4Ó ×
 €CØ
�)‰)‹+×ÐØ�<‰<×Ð˜1Ðñ !Ùùs1   ‚A?D4ÂA;D4Ã=D0Ã>D4ÄD2ÄD4Ä-D4Ä2D4c                óZ   € V ^8„  d   QhR\         R\        \        P                  ,          /# )r   r   r!   )r   r   r"   r#   )r'   s   "r   r(   r(   –   s"   € ÷ ñ ¤|ð ¼¼T¿Y¹Y½ñ r   c              ƒ  ól  "  € . ROpV P                  \        \        P                  4      P	                  \        P
                  R8H  \        \        P                  P                  V4      \        P                  P                  R4      4      4      4      G Rj  x€L
 P                  4       P                  4       pV P                  \        \        P                  4      P                  \        \        P                  \        P                  8H  4      P	                  \        P
                  R8H  \        P                  P                  RR.4      \        P                  R8H  4      4      G Rj  x€L
 P                  4       P                  4       p\!        V4      \!        V4      ,          p\#        V4      #  EL LI5i)zÝ
Resolves the specific staff users responsible for Sales & Dealer Orders.
Includes users with explicit Sales roles (SALES_MGR, SALES_EXEC, CLIENT_ADMIN, ADMIN)
or users who have view/edit permission for the SALES module.
Tz%SALES%NÚSALESÚDEALER_PORTAL_ADMIN)Ú	SALES_MGRÚ
SALES_EXECÚADMINÚCLIENT_ADMIN)r0   r   r   r   r-   Ú	is_activer
   Ú	role_codeÚin_Úiliker5   r6   Újoinr   Úmodule_codeÚcan_viewÚsetr7   )r   Údirect_rolesÚusers_by_roleÚusers_by_permÚcombineds   &    r   Ú_get_sales_team_user_idsre   –   sG  é € ò H€LØŸ:™:ÜŒz×!Ñ!Ó"×(Ñ(Ü× Ñ  DÑ(ÜÜ×$Ñ$×(Ñ(¨Ó6Ü×$Ñ$×*Ñ*¨9Ó5óó	
ó÷ ÷ �wƒy—‘“ð ð Ÿ:™:ÜŒz×!Ñ!Ó"ß	‰Ô"¤J×$8Ñ$8Ô<P×<ZÑ<ZÑ$ZÓ	[ß	‰Ü× Ñ  DÑ(Ü ×,Ñ,×0Ñ0°'Ð;PÐ1QÓRÜ ×)Ñ)¨TÑ1ó

ó÷ ÷ �wƒy—‘“ð ô �=Ó!¤C¨Ó$6Õ6€HÜ�‹>Ðò-ñùs&   ‚BF4ÂF/ÂCF4Å(F2Å)AF4Æ2F4c                ó<   € V ^8„  d   QhR\         R\        R\        /# )r   r   ÚorderÚis_resubmission)r   r   r%   )r'   s   "r   r(   r(   ·   s0   € ÷ Jbñ JbÜðJbäðJbô ñJbr   c              ƒ  ó‚  "  €  V P                  \        VP                  4      G Rj  x€L
 pV'       d   VP                  MVP                  pV P	                  \        \        P                  ! \        P                  4      4      P                  \        P                  VP                  8H  4      4      G Rj  x€L
 P                  4       ;'       g    ^pVP                  ;'       g+    \        VP                  4      R,          P                  4       pV'       g   RMRpV'       g   RT RT RV^8”  d   RMR	 R
2MRV R2pT P!                  \#        \$        P&                  ! 4       RVP                  RV'       g   RMRVVR\        VP                  4      RR\)        4       R7      4       \+        V 4      G Rj  x€L
 p	V'       g   RV 2MRV 2p
V'       g   RT RT RT RV^8”  d   RMR	 R2	MRV RV R2pV	 Fn  pT P!                  \#        \$        P&                  ! 4       RTRV'       g   RMRV
VR\        VP                  4      RVP                   2R\)        4       R 7      4       Kp  	  V P-                  4       G Rj  x€L
  R#  EL\ ELÑ LÓ L  \.         d%   p\0        P2                  ! R!T 24        Rp?R# Rp?ii ; i5i)"zÀ
Dispatches notifications when an order is created or submitted:
1. Sends confirmation notification to the Dealer Portal
2. Sends actionable notification to the Sales team in the internal ERP
NºNé   NzOrder Submitted SuccessfullyzOrder Changes ResubmittedzYour order #z (z vehicleÚsÚ zD) has been received and is currently under review by the Sales team.z&Your updated configuration for order #z' has been resubmitted for Sales review.rB   ÚORDERÚORDER_SUBMITTEDÚORDER_RESUBMITTEDÚDEALER_ORDERú/dealer-portal/ordersF©r,   r.   r?   ÚcategoryÚ
event_typeÚtitleÚmessageÚreference_typeÚreference_idÚ
action_urlr/   r3   zNew Order: #zOrder Resubmitted: #úDealer 'z' submitted new order #z for Ú.z'' resubmitted revised specs for order #r*   ÚORDER_RECEIVEDÚORDER_UPDATEDú/sales/dealer-orders?order_id=©r,   r.   r   rt   ru   rv   rw   rx   ry   rz   r/   r3   zA[NOTIFICATIONS] Failed to dispatch order submitted notification: )rF   r   r?   Únamer0   r   r	   Úsumr   Úqtyr-   Úorder_idr1   Úorder_nor@   ÚupperÚaddr   r"   Úuuid4r   re   rH   Ú	Exceptionr   Úerror)r   rg   rh   ÚdealerÚdealer_nameÚv_qtyr…   Údealer_titleÚ
dealer_msgÚsales_user_idsÚsales_titleÚ	sales_msgÚuidÚes   &&&           r   Únotify_order_submittedr•   ·   s•  é € ð@bà—v‘vœn¨e¯o©oÓ>×>ˆß%+�f—k’k°·±ˆð —z‘zÜ”4—8’8Ô.×2Ñ2Ó3Ó4ß‰UÔ%×.Ñ.°%·.±.Ñ@ÓAó
÷ 
÷ ‰6‹8÷ð ð ð 	ð
 —>‘>×DÐD¤S¨¯©Ó%8¸Õ%<×%BÑ%BÓ%Dˆ÷ >MÑ5ÐRmˆ÷ !ð ˜8˜* B u g¨X¸UÀQ¼Y±cÈBÐ5Oð P?ñ @ð 5°X°JÐ>eÐfð	 	ð 	�‰Œ|Ü ŸJšJ›LØ#Ø—o‘oØß0?Ñ(ÐEXØØØ)Ü˜UŸ^™^Ó,Ø.ØÜ“yô
ô 	ô   8¸Ó;×;ˆß7F˜ X JÑ/ÐNbÐckÐblÐLmˆ÷ !ð �{�mÐ#:¸8¸*ÀEÈ%ÈÐPXÐ`eÐhiÔ`iÑY\ÐoqÐXrÐrsÑtà�{�mÐ#JÈ8È*ÐTUÐVð 	ó "ˆCØ�F‰F”<Ü $§
¢
£Ø&ØØ ß3BÑ+ÈØ!Ø!Ø-Ü  §¡Ó0Ø;¸E¿N¹NÐ;KÐLØÜ"›9ôö ñ "ð  �i‰i‹k×Òòy ?ò
ñ@ <ñ0 	øÜô bÜ×ÒÐ\Ð]^Ð\_Ð`×aÒaûðbüs    ‚J?„#J §J¨J ´BJ Â5JÂ6J ÃJ Ã%2J ÄJ Ä$BJ Æ5J	Æ6J ÇJ ÇB(J É<JÉ=J ÊJ?ÊJ ÊJ Ê	J ÊJ ÊJ<ÊJ7Ê1J?Ê7J<Ê<J?c                óf   € V ^8„  d   QhR\         R\        R\        \        P                  ,          /# )r   r   rg   Úadmin_id)r   r   r   r"   r#   )r'   s   "r   r(   r(     s4   € ÷ hñ hÜðhäðhô ”t—y‘yÕ!ñhr   c              ƒ  óè  "  €  VP                   ;'       g+    \        VP                  4      R,          P                  4       pV P	                  \        \        P                  ! 4       RVP                  RRRRV R2R\        VP                  4      R	R
\        4       R7      4       V P                  4       G Rj  x€L
  R#  L  \         d%   p\        P                  ! RT 24        Rp?R# Rp?ii ; i5i)zi
Dispatches notification to Dealer Portal when Sales reviews and confirms an order (pending dealer ack).
rj   rB   rn   ÚSALES_CONFIRMEDzOrder Reviewed by SaleszSales has reviewed order #z<. Please review the assigned specifications and acknowledge.rq   rr   Frs   NzG[NOTIFICATIONS] Failed to dispatch order sales confirmed notification: ©r…   r@   r„   r†   r‡   r   r"   rˆ   r?   r   rH   r‰   r   rŠ   )r   rg   r—   r…   r”   s   &&&  r   Únotify_order_confirmed_by_salesr›     sÆ   é € ðhØ—>‘>×DÐD¤S¨¯©Ó%8¸Õ%<×%BÑ%BÓ%DˆØ
�‰Œ|Ü ŸJšJ›LØ#Ø—o‘oØØ(Ø+Ø0°°
Ð:vÐwØ)Ü˜UŸ^™^Ó,Ø.ØÜ“yô
ô 	ð �i‰i‹k×ÔøÜô hÜ×ÒÐbÐcdÐbeÐf×gÒgûðhüóA   ‚C2„B3C  Â7B>Â8C  Â<C2Â>C  Ã C/ÃC*Ã$C2Ã*C/Ã/C2c                ó0   € V ^8„  d   QhR\         R\        /# ©r   r   rg   ©r   r   )r'   s   "r   r(   r(   !  s    € ÷ eñ eÜðeäñer   c              ƒ  óÌ  "  €  V P                  \        VP                  4      G Rj  x€L
 pV'       d   VP                  MVP                  pVP                  ;'       g+    \        VP                  4      R,          P                  4       p\        V 4      G Rj  x€L
 pV Fn  pV P                  \        \        P                  ! 4       RVRRRV 2RV RV R	2R
\        VP                  4      RVP                   2R\        4       R7      4       Kp  	  V P                  4       G Rj  x€L
  R#  EL L• L  \         d%   p\         P"                  ! RT 24        Rp?R# Rp?ii ; i5i)zK
Dispatches notification to Sales Team when Dealer acknowledges the order.
Nrj   r*   rn   ÚORDER_ACKNOWLEDGEDzOrder Acknowledged: #r{   z' has acknowledged order #z*. Ready for final confirmation/production.rq   r   Fr€   zD[NOTIFICATIONS] Failed to dispatch order acknowledged notification: )rF   r   r?   r�   r…   r@   r„   r†   re   r‡   r   r"   rˆ   r   rH   r‰   r   rŠ   )r   rg   r‹   rŒ   r…   r�   r“   r”   s   &&      r   Ú#notify_order_acknowledged_by_dealerr¢   !  s*  é € ðeØ—v‘vœn¨e¯o©oÓ>×>ˆß%+�f—k’k°·±ˆØ—>‘>×DÐD¤S¨¯©Ó%8¸Õ%<×%BÑ%BÓ%Dˆä7¸Ó;×;ˆÛ!ˆCØ�F‰F”<Ü $§
¢
£Ø&ØØ Ø/Ø-¨h¨ZÐ8Ø" ; -Ð/IÈ(ÈÐS}Ð~Ø-Ü  §¡Ó0Ø;¸E¿N¹NÐ;KÐLØÜ"›9ôö ñ "ð �i‰i‹k×Òò) ?ñ <ñ  	øÜô eÜ×ÒÐ_Ð`aÐ_bÐc×dÒdûðeüsn   ‚E$„#D2 §D+¨D2 ´*D2 Á9D2 ÂD.ÂBD2 Ä$D0Ä%D2 Ä)E$Ä+D2 Ä.D2 Ä0D2 Ä2E!Ä=EÅE$ÅE!Å!E$c                ó0   € V ^8„  d   QhR\         R\        /# rž   rŸ   )r'   s   "r   r(   r(   B  s    € ÷ hñ hÜðhäñhr   c              ƒ  óè  "  €  VP                   ;'       g+    \        VP                  4      R,          P                  4       pV P	                  \        \        P                  ! 4       RVP                  RRRRV R2R\        VP                  4      R	R
\        4       R7      4       V P                  4       G Rj  x€L
  R#  L  \         d%   p\        P                  ! RT 24        Rp?R# Rp?ii ; i5i)ze
Dispatches notification to Dealer Portal when order is finally confirmed and locked for production.
rj   rB   rn   ÚORDER_LOCKEDzOrder Confirmed & ScheduledzOrder #zA is officially confirmed and locked into the production pipeline.rq   rr   Frs   NzG[NOTIFICATIONS] Failed to dispatch final order confirmed notification: rš   )r   rg   r…   r”   s   &&  r   Únotify_order_final_confirmedr¦   B  sÆ   é € ðhØ—>‘>×DÐD¤S¨¯©Ó%8¸Õ%<×%BÑ%BÓ%DˆØ
�‰Œ|Ü ŸJšJ›LØ#Ø—o‘oØØ%Ø/Ø˜h˜ZÐ'hÐiØ)Ü˜UŸ^™^Ó,Ø.ØÜ“yô
ô 	ð �i‰i‹k×ÔøÜô hÜ×ÒÐbÐcdÐbeÐf×gÒgûðhürœ   )é   F)NN)Fr   )$r"   r   r   Útypingr   r   Ú
sqlalchemyr   r   r	   r
   Úsqlalchemy.ext.asyncior   Úapp.models.notification_modelsr   Úapp.models.core_modelsr   Úapp.models.access_modelsr   Úapp.models.dealer_modelsr   r   Úapp.models.sales_modelsr   Úapp.core.loggingr   r   r<   rC   rJ   rP   re   r•   r›   r¢   r¦   r   r   r   Ú<module>r±      sf   ðÛ ß 'ß !ß 0Ó 0Ý /å 7Ý -Ý 9ß DÝ 2Ý 'ò&÷!%÷H%÷D÷0"õJ÷BJb÷Zhõ:e÷Bhr   