o
    Ah,                     @   s  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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mZmZmZmZ d dlmZ d dlmZmZ dZdZ dZ!dZ"dZ#dedej$de%fddZ&de'de'dee( dee( dee) dee( dee( dee( dee( d ee( d!ee( d"ee( d#ee( dee(ef fd$d%Z*d"e(dej$de%fd&d'Z+d"e(d(ed)e%dej$de%f
d*d+Z,d"e(dej$de%fd,d-Z-d.S )/    )datetime)parser)OptionalListDictAny)DuplicateKeyError)HTTPExceptionDepends)database)
send_email)ObjectId)str_to_objectid)AccountCreateAccountUpdateAccountAccounts)User)get_next_sequence_valueconvert_enumsaccountsusersemailsfleets	workforceaccountdbreturnc              
   C   s^  |   }t |d< dd |dg D |d< dd |dg D |d< dd |dg D |d< |d	r;|d	 jnd |d	< |d
rI|d
 jnd |d
< |d j|d< |d j|d< |t }td||d< |d }|t }|d|di}|r|dd|d< |d|d i}|r|dd|d< z|	|}W n: t
y }	 zdt|	v rtddt|	v rtdtdd }	~	w ty }	 ztddt|	 dd }	~	ww t|j|d< |d |d< z|dt|d idd|d ii}
W n ty }	 ztddt|	 dd }	~	ww |
jd krtdd!dd"}d#| j d$}t||| j |S )%Ncreated_datec                 S      g | ]}|j qS  value).0purposer    r    9/var/www/html/moveengine/app/v1/services/saas/accounts.py
<listcomp>       z*create_account_service.<locals>.<listcomp>purposes_of_usec                 S   r   r    r!   r#   
capabilityr    r    r%   r&      r'   fleet_capabilitiesc                 S   r   r    r!   r)   r    r    r%   r&      r'   workforce_capabilitiesindustrysubscription_statusworkforce_countrevenuesaccounts_idr   emailreferred_by referral_code
account_idreferred_account_idz'Account with this email already exists.mobilez/Account with this mobile number already exists.zDuplicate key error.i  zDatabase error: )status_codedetail_iduser_id$setzFailed to update user: r   z&Failed to update user with account_id.z#Your Organization has been created!zHello zQ,

Thank you for signing up for our platform. We're excited to have you on board!)dictr   utcnowgetr"   COLLECTION_NAMEr   COLLECTION_EMAILSfind_one
insert_oner   str
ValueError	Exceptionr	   inserted_id
update_oner   modified_countnamer   r2   )r   r   account_dataaccounts_collectionusers_collectionemails_collectionemails_inforeferral_inforesulteupdate_resultsubjectbodyr    r    r%   create_account_service   sb   rW   skiplimitqaccount_type	is_activer.   reg_date_fromreg_date_toworkforce_count_rangefleet_count_rangerevenue_ranger6   r7   c                 C   s  d }d }|r |  r zt|}W n ty   td| w |r<|  r<zt|}W n ty;   td| w |dddvrHtd|d }i }|ddkrg|s\|d	}|rc||d
< ntd|r~|dd}d|id|id|id|ig|d< |r||d< |r||d
< |d ur||d< |r||d< |s|ri }|r||d< |r||d< ||d< |r||d< |	r|	|d< |
r|
|d< ||| |}g }|D ]*}t	|d |d< t	|d |d	< d|v rt
|d tr|d  |d< || q||}||dS )NzInvalid reg_date_from: zInvalid reg_date_to: rolesr   )         zNot permitted to view accounts.r   rc   r6   r7   z(account_id is required but not provided.i)z$regexz$optionsaccount_namerK   countrycityz$orr[   r\   r.   z$gtez$lter   r/   fleet_countr0   r;   id)total_countr   )stripr   parserG   rF   r@   findrX   rY   rE   
isinstancer   	isoformatappendcount_documents)rX   rY   rZ   r[   r\   r.   r]   r^   r_   r`   ra   r6   r7   r   current_userdt_fromdt_to
collectionqueryregex_query
date_queryaccounts_cursoraccounts_listr   rl   r    r    r%   get_accounts_serviceX   s|   




r}   c                 C   s   |t  }|dt| i}|rYt|d |d< |d |d< |t }|t }|d| i}|d| i}||d< ||d< t|ddt|ddt|dd|d	d
|d< td|  |S )Nr;   r6   total_fleetstotal_workforcer[   r4   r.   subscription_typeactive_subscription_id)r[   r.   r   r   plan_detailsz	Account: )	rA   rC   r   rE   FLEETS_COLLECTIONWORKFORCE_COLLECTIONrs   r@   print)r6   r   rw   r   fleets_collectionworkforce_collectionrj   r/   r    r    r%   get_account_service   s$   
r   account_updatert   c           
      C   s  |t  }|t }|dt| i}|std|dt|di}|s)td|ddkrAt|d| krAtd td|jd	d
hd}t	|}|ddks[|ddkrg|
dt| id|i |dt| i}	|	rt|	d |	d< |	d |	d< |	S td)Nr;   Account not foundzUser not foundrb   rc   r6   zaccount id mismachz%Not authorized to update this accountr<   r[   )excluded   r=   zAccount not found after update)rA   USER_COLLECTION_NAMErC   r   rF   r@   rE   r   r>   r   rI   )
r6   r   rt   r   rM   rN   existing_accountuser_documentaccount_updatesupdated_accountr    r    r%   update_account_service   s,   r   c                 C   sX   |t  }|dt| i}|std|dt| i t|d |d< |d |d< |S )Nr;   r   r6   )rA   rC   r   rF   
delete_onerE   )r6   r   rw   r   r    r    r%   delete_account_service
  s   r   N).r   dateutilr   typingr   r   r   r   pymongo.errorsr   fastapir	   r
   app.dbr   app.v1.integrations.SMTPmailerr   bsonr   app.v1.libraries.objectr   app.v1.models.saas.accountmodelr   r   r   r   app.v1.models.saas.usersmodelr   app.v1.services.sequencer   r   rA   r   rB   r   r   MongoDBr>   rW   intrE   boolr}   r   r   r   r    r    r    r%   <module>   sx    D	


c
1