
     oh-                         d dl m Z mZmZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZ d d	l m Z  d dlZd dlZd dlZd d
lmZmZmZmZmZ  G d de      Zy)    )datetime	timedeltatimezoneN)Session)SALES_EMAIL_CREDENTIALS)AdvisorSettingsSettingsHistoryUser)
send_email)get_current_time_in_timezone)BaseRepo)r   )parseformat_numberPhoneNumberFormatNumberParseExceptionis_valid_numberc                        e Zd Z fdZdedefdZd ZdefdZdedefd	Z	d
 Z
d Zd Zd ZdededefdZdededefdZ xZS )SettingsRepoc                 $    t         |   |       y )N)super__init__)selfdb	__class__s     @/var/www/html/DP/alpha_backend/app/repositories/settings_repo.pyr   zSettingsRepo.__init__   s        phonereturnc                 @   	 t        |d       }t        |      s|S t        |t        j                        }|j                  d      r/t        |      dk(  r!|d d }|dd }|dd }|dd  }| d| d| d| S t        |t        j                        S # t        $ r |cY S w xY w)	Nz+1            z (z) -)	r   r   r   r   E164
startswithlenINTERNATIONALr   )r   r   phone_numberrawcountry_code	area_codecentral_officeline_numbers           r   format_phone_numberz SettingsRepo.format_phone_number   s    	 -L"<0  .?.D.DEC ~~d#CB"2Aw!H	!$Qq!!"g&r)B~6Fa}UU !/@/N/NOO# 	L	s   B AB 5B BBc                 6    | j                  t        ||d       y )N)name
updated_by)creater
   )r   r2   r3   s      r   create_settings_historyz$SettingsRepo.create_settings_historyh   s    Od*%MNr   namesc                 R   t         j                  j                  |      }| j                  t         ||dd|      \  }}g }|D ]`  }| j	                  t
        |j                        }	|j                  |j                  |j                  |	j                  |j                  d       b ||fS )N
created_atdesc)page	page_sizeorder_byorder_directionfilter_condition)idr2   r3   
updated_at)
r
   r2   in_get_all_paginated	get_by_idr   r3   appendr?   r@   )
r   r6   r:   r;   r>   historytotal_pageshistory_with_userrecordusers
             r   get_settings_history_paginatedz+SettingsRepo.get_settings_history_paginatedk   s    *//33E:#55!"-  6  
 F>>$(9(9:D$$ ))"KK"&))"("3"3	  !+--r   r2   c                 B    |j                  dd      j                         S )zEConvert setting name from underscore format to title case with spaces_ )replacetitle)r   r2   s     r   format_setting_namez SettingsRepo.format_setting_name   s    ||C%++--r   c                 `   | j                   j                  t              j                  t        j                  dk(        j                         }|D cg c]  }|j                   }}|j                  d       |j                  d       |j                  d       |j                  d       dj                  |      }t        j                  d      }t        j                  |      j                  d      }| j                  |      }	d	| d
| d|	 d}
d| d| d}t        |d|
|       yc c}w )Nmanagershahzad@fluten.aihananali423@gmail.comsmalik@fluten.aimimj1800@gmail.com,America/Los_Angeles%b %d, %Y %I:%M %pLDear Team,

Please be informed that an update has been made on DealerPulse.
    –  updated the .

Regards,
DealerPulse Team<html>
<body>
<p>Dear Team,</p>
<p style="margin-bottom:0px!important">Please be informed that an update has been made on DealerPulse.</p>
<p style="margin:0px!important"><strong>Timestamp:</strong> <br>
<strong>User:</strong> z<br>
</p>
<strong>Event:</strong>
</p>
<ul style="margin:0px!important">
    <li>Updated Advance Settings.</li>
</ul>
<p>Regards,<br>
DealerPulse Team</p>
</body>
</html> DealerPulse Setting Update Alert	recipientsubjectbody	html_bodyTr   queryr   filtertypeallemailrD   joinpytzr   r   nowstrftimerP   r   )r   r2   r3   managersrR   manager_emailsmanager_emails_strpstcurrent_timeformatted_name
email_bodyre   s               r   !send_advance_setting_update_alertz.SettingsRepo.send_advance_setting_update_alert   s8   77==)001JKOOQ7?@xG'--x@12560123 XXn5mm12||C(112FG11$7 eJ<}^,< =
= >JN K"| 
$	" 	(6		
 U As   D+c           	         | j                   j                  t              j                  t        j                  dk(        j                         }|D cg c]  }|j                   }}|j                  d       |j                  d       |j                  d       |j                  d       dj                  |      }t        j                  d      }t        j                  |      j                  d      }	| j                  |      }
|d	k(  rd
}
d|	 d| d|
 d}d|	 d| ddj                  d |D               d}t        |d||       yc c}w )NrR   rS   rT   rU   rV   rW   rX   rY   OFFICE_TIMINGzOffice SchedulerZ   r[   r\   r]   r^   r_   zH<br>
<strong>Event:</strong>
</p>
<ul style="margin:0px!important">
     c              3   (   K   | ]
  }d | d  yw)z<li>z</li>N ).0msgs     r   	<genexpr>z9SettingsRepo.send_setting_update_alert.<locals>.<genexpr>   s     9.3tC5.s   z;
</ul>
<p>Regards,<br>
DealerPulse Team</p>
</body>
</html>r`   ra   Trf   )r   r2   r3   change_timingsrp   rR   rq   rr   rs   rt   ru   rv   re   s                r   send_setting_update_alertz&SettingsRepo.send_setting_update_alert   sa   77==)001JKOOQ7?@xG'--x@12560123 XXn5mm12||C(112FG11$7?".N eJ<}^,< =
= >JN K"| $ WW9.99: ;	  	(6		
 W As   Ec                    | j                   j                  t              j                  t        j                  dk(        j                         }t        |j                        }t        |      }| j                  || j                         S )NTIMEZONE_OFFSET)
r   rg   r	   rh   r2   firstintvaluer   is_office_hoursr   timezone_settingtimezone_offsetrt   s       r   is_company_onlinezSettingsRepo.is_company_online   si    GGMM(#**8==<M+MNTTV 	 .4453OD##L$''::r   c                 X   | j                   j                  t              j                  t        j                  dk(        j                         }t        |j                  d       t        |j                        }t        |d       t        |      }| j                  || j                         S )Nr   z------)r   rg   r	   rh   r2   r   printr   r   r   is_office_hours_salesr   s       r   is_sales_company_onlinez$SettingsRepo.is_sales_company_online   s    GGMM(#**8==<M+MNTTV 	 	$$h/.445ox(3OD)),@@r   rt   r   c                 2   	 |j                  t              j                  t        j                  dk(        j	                         }|r|j
                  syt        j                  |j
                        }|j                  d      }|syt        j                  |d   d      j                         }t        j                  |d   d      j                         }|j                         }||cxk  xr |k  S c S # t        $ r}	t        d|	        Y d}	~	yd}	~	ww xY w)	z
        Check if current time is within Hazel's business hours based on SALES_DEPARTMENT setting
        Format expected: {'hazel': {'end_time': '14:00', 'start_time': '09:00'}}
        SALES_DEPARTMENTFhazel
start_time%H:%Mend_timezError checking office hours: N)rg   r	   rh   r2   r   r   jsonloadsgetr   strptimetime	Exceptionr   )
r   rt   r   office_timingtiminghazel_hoursr   r   current_time_onlyes
             r   r   z"SettingsRepo.is_office_hours_sales   s   
	 "))(--;M*MNTTV  !(;(;ZZ 3 34F !**W-K "**;|+DgNSSUJ((Z)@'JOOQH !- 1 1 3 !2>h>>>> 	1!56	s+   AC5 2C5 A*C5 2C5 5	D>DDc                 z   	 | j                   j                  t              j                  t        j                  dk(        j                         }|syt        j                  |j                        }|j                  d      j                         }|j                         }||j                  dg       v ry|j                  |      }|syt        j                  |d   d      j                         }t        j                  |d   d      j                         }	||cxk  xr |	k  S c S # t        $ r
}
Y d}
~
yd}
~
ww xY w)	z`
        Check if current time is outside business hours based on OFFICE_TIMING setting
        ry   Fz%Aweekendr   r   r   N)r   rg   r	   rh   r2   r   r   r   r   ro   lowerr   r   r   r   r   )r   rt   r   r   r   current_dayr   
day_timingr   r   r   s              r   r   zSettingsRepo.is_office_hours  s   	h'..x}}/OPVVX  !ZZ 3 34F&//5;;=K , 1 1 3fjjB77K0J!**:l+CWMRRTJ((J)?INNPH!2>h>>>> 		s,   AD' A"D' 5D' 	AD' $D' '	D:5D:)__name__
__module____qualname__r   strr0   r5   listrJ   rP   rw   r   r   r   r   r   boolr   r   __classcell__)r   s   @r   r   r      s      XO.D ... . .,\-^;	A!( ! !D !FH ' d r   r   )r   r   r   osrm   sqlalchemy.ormr   app.common.constantsr   app.common.modelsr   r	   r
   r   !app.common.services.email_servicer   app.common.utilsr   app.repositories.base_repor   typingr   phonenumbersr   r   r   r   r   r   r|   r   r   <module>r      sJ    2 2 	  " 8 F F 8 9 /     `8 `r   