+
    diCr                       ^ RI Ht ^ RIt^ RIt^ RIt^ RIHt ^ RIHt ^ RIt^ RI	H
t ^ RIHt ^ RIHt ^ RIHtHt ^ R	IHtHtHt ^ R
IHtHt ^ RIHt ^ RIHtHtHtH t H!t!H"t" ^ RI#H$t$ ^ RI%H&t&H't' ]PP                  ! ])4      t*RR R llt+R# )    )annotationsN)datetime)Any)parser)text)Session)build_mcube_refurlget_settings)call_history_tablecampaign_contacts_tablecampaigns_table)
has_column	has_table)campaign_core)check_business_time_windowcheck_remaining_minutesfetch_dial_status_and_updateget_bot_concurrent_limitget_bot_connecting_countget_bot_id_from_campaign_did)trigger_call_history_sync)	format_dtnow_appc               4    V ^8  d   QhRRRRRRRRRRR	RR
R/# )   session_defaultr   session_clustercampaign_idz
int | Nonebusiness_id
retry_onlyboolpriority_onlyreturnzdict[str, Any] )formats   "%app\services\outbound_call_service.py__annotate__r'       s`     `	M `	M`	M`	M `	M 	`	M
 `	M `	M `	M    c                `0   \        4       pVP                  pV'       g    V'       d   \        P                  ! WV4      pV'       g#   RV 2p\        P                  V4       RRRVRV/# VP                  ;'       g    RP                  4       '       g	   RRRRRV/# \        V \        V4      4      p	V	R,          '       g$   RRRV	R	,          R
RRV	P                  R^ 4      RV/# \        V \        V4      4      p
V
R,          '       gE   RRRV
R	,          RRRV
P                  R4      RV
P                  R4      RV
P                  R4      RV/# \        \        V4      4      p\        \        V4      4      p\        \        V4      4      p\        W4      '       g#   RV 2p\        P                  V4       RRRVRV/# RpV'       Ed   VP!                  \#        RV R24      RV/4      P%                  4       P'                  4       pV'       g%   RV 2p\        P                  V4       RRRVRVRV/# V'       g   VP                  R4      R8X  d   RRRRRVRVP                  R4      RR/# VP                  R4      R9  d9   RRRR\)        VP                  R4      4      ,           RVRVP                  R4      /# MVP                  R4      R9   dR   \        P+                  RW.P                  R4      4       RRRRVP                  R4       R 2RVRVP                  R4      /# \        P+                  R!W.P                  R4      4       \-        4       pVR",          pVR#,          p\/        V\0        4      '       g    \2        P4                  ! \)        V4      4      p\/        V\0        4      '       g    \2        P4                  ! \)        V4      4      pVP6                  f   VP9                  VP6                  R$7      pVP6                  f   VP9                  VP6                  R$7      p\        VP                  R%4      ;'       g    ^ 4      ^ 8X  dO   VVP;                  VP6                  4      8  d/   RRRR&\=        V4      ,           R'RR"\=        V4      R\=        V4      /# VVP;                  VP6                  4      8  d/   RRRR(\=        V4      ,           R)RR#\=        V4      R\=        V4      /# \        W4      '       g	   RRRR*RV/# V'       d%   \        VP                  R+4      ;'       g    ^
4      M^
pV'       d%   \        VP                  R,4      ;'       g    ^4      M^pV'       d3   \)        VP                  R-4      ;'       g    R4      P                  4       MRp\?        WR.4      p\=        \-        4       4      pR/.p\?        WR04      '       d   VPA                  R04       MVPA                  R14       VPA                  R2V R324       R4V/pV'       d   VPA                  R54       VVR&   V'       d.   R6pV'       d   VR7V R82,          pVPA                  R9V R:24       M:R;pR<pV'       d   VR7V R82,          pVR:,          pVPA                  R=V R>V R?24       V'       d$   \?        WR04      '       d   VPA                  R@4       \?        WR04      '       d   VPA                  RA4       MVPA                  RB4       RPC                  V4      pVP!                  \#        V4      V4      P%                  4       PE                  4       p\G        V4      pV Uu/ uFF  p\        VRC,          4      \)        VP                  RD4      ;'       g    R4      P                  4       bKH  	  p pV Uu. uF#  p\)        VRE,          4      P                  4       NK%  	  p!pV Uu. uF  p\        VRC,          4      NK  	  p"pV!'       g	   RRRRFRV/# RGVRHV/p#^ p$. p%\I        V!4      p&\K        ^ \I        V!4      V4       U'u. uF  p'V!V'V'V,            NK  	  p(p'\I        V(4      p)^
p*^p+RIp,Rp-Rp.VR8w  dJ   \M        V \        V4      V4      p-V-e0   \O        V\        V4      V-4      p.\        P+                  RJVV-VV.4        VP!                  \#        RKV RL24      4      PQ                  4       p/\        P+                  RMV/4       V.e,   VR8w  d%   \S        WV4      p0\        P+                  RNVV0V.4       Rp2\W        T(4       EF  w  p3p4T'       di   TP!                  \#        RPT R24      RT/4      P'                  4       p5T5'       d2   T5^ ,          R8X  d$   \        P+                  RQTT3^,           4       Rp2 EMT2'       d    EMT"T3T,          T3T,          T,            p6\W        T44       EF  w  p7p8T'       da   TP!                  \#        RPT R24      RT/4      P'                  4       p5T5'       d*   T5^ ,          R8X  d   \        P+                  RRT4       Rp2 E
M\        T \        T4      4      p9T9R,          '       g   \        PY                  RST4       TP[                  4        T P[                  4        RRRT9R	,          RRRT9P                  R4      RT9P                  R4      RT9P                  R4      RT/u u # T2'       d    E	MT3T,          T7,           p:T7\I        T64      8  d
   T6T7,          MRp;T;'       d   T'       d   TP!                  \#        RTT RU24      RCT;RT/4      P%                  4       P'                  4       p<T<'       d   \)        T<P                  R4      ;'       g    R4      P                  4       P]                  4       RV8X  d   \        P+                  RWT;4       EK  \        T<P                  RX4      ;'       g    ^ 4      ^ 8:  d   \        P+                  RYT;4       EK>  \^        P^                  ! 4       p= T'       dI   TP!                  \#        RPT R24      RT/4      P'                  4       p5T5'       d   T5^ ,          R8X  d   Rp2MT2'       d   M \        TP!                  \#        RKT RL24      4      PQ                  4       4      p>T'       d   \S        YT4      M^ p?T>T*8  p@T.RJ ;'       g    T?T.8  pA\        P+                  RZT:^,           T>T*T?4       X@'       d
   XA'       d   MP\^        P^                  ! 4       T=,
          T,8  d   \        PY                  R[4       M\^        P`                  ! T+4       EK=  T2'       d    EMrT'       dK   TP!                  \#        RPT R24      RT/4      P'                  4       p5T5'       d   T5^ ,          R8X  d   Rp2 EM T2'       d    EMT;'       d   T P                  \        T;4      R4      MRpB \c        T4      pC/ T#CR]T8R^XBR_XCR`T/CpDT'       gj   RapE\        P                  TE4       T%PA                  TE4       T;'       d7   T'       d/    TP!                  \#        RbT Rc24      Rd\-        4       RCT;RT/4       EK  T:^ 8  d   \^        P`                  ! Rf4       ^pF^ pGRpHRpIRpJRpKXGXF8:  Ed   XH'       g    \f        Ph                  ! RgRh7      ;_uu_ 4       pLTLPk                  TPl                  XDRiRjRGRkT 2/Rl7      pRRR4       XP"                  pITPn                  pJRpKXJ'       d   ^XJu;8:  d   RI8  d   M MRpHK  XJ'       d   RmXJu;8:  d   Rn8  d   M MMXXJ'       dP   XJRn8  dI   XGXF8  dB   XG^,          pGTG^,          pM\        P+                  RoTM4       \^        P`                  ! TM4       EK   XH'       gj   XK'       db   T%PA                  RpT: RqT8 RrXK 24       T;'       d=   T'       d2    TP!                  \#        RbT Rc24      Rd\-        4       RCT;RT/4       EKE  EKH  EKK  XH'       Ed   XJ'       Ed   ^XJu;8:  d	   RI8  Ed   M EM \p        Pr                  ! XI;'       g    Rs4      pNXNP                  R4      pOTNP                  Rt4      ;'       g    RpPXORu8X  Ed-   XNP                  Rv4      '       Ed   T$^,          p$XNP                  Rv4      pQ\        P+                  RwT8XI4       RpRT'       g   T;'       d   T'       d    TP!                  \#        RxT RU24      RCT;RT/4      P'                  4       pSTS'       d   \        XS^ ,          ;'       g    ^ 4      M^ pTTT^ 8  d;   XT^,
          pUTP!                  \#        RbT Ry24      RzTURd\-        4       RCT;RT/4       RpRT;'       d9   T'       d1    \w        TXQ\        T;4      \        T4      \        T4      TTT8TXR4
       \        P+                  R}4       \^        P`                  ! ^4        \^        P^                  ! 4       pV T'       dK   TP!                  \#        RPT R24      RT/4      P'                  4       p5T5'       d   T5^ ,          R8X  d   Rp2EK  T2'       d   EK  \        TP!                  \#        RKT RL24      4      PQ                  4       4      pWT'       d   \S        YT4      M^ pXXWT*8  p@T.RJ ;'       g    XXT.8  pAX@'       d   XA'       d   \^        P`                  ! ^4       \        TP!                  \#        RKT RL24      4      PQ                  4       4      pYT'       d   \S        YT4      M^ pZXYT*8  d$   T.e   XZT.8  d   \        P+                  R~4       E	K  EK]  \^        P^                  ! 4       XV,
          T,8  d   \        PY                  R4       E	K  \^        P`                  ! T+4       EK  RpT: RqT8 RXP RXI R:2	pET%PA                  TE4       T;'       d=   T'       d2    TP!                  \#        RbT Rc24      Rd\-        4       RCT;RT/4       E
KS  E
KV  E
KY  XH'       d   E
Kd  XI;'       g    RR,          ;'       g    Rp[T%PA                  RpT: RqT8 RXJ RT[ 24       TJ'       d   XJRn8  d   \        P                  RT8XI4       T;'       g   E
K  T'       g   E
K   TP!                  \#        RbT Rc24      Rd\-        4       RCT;RT/4       EK  	  T2'       d    MT$^ 8  d    \y        \        T4      R4       T'       d[   T2'       gS   \        Pz                  ! Y\        T4      \        T4      4      p\T\'       d!   \        P+                  RTT3^,           4        MJT3T)^,
          8  g   EKN  \        P+                  RT3^,           T4       \^        P`                  ! T4       EK  	  T'       d3   T2'       g+   \        Pz                  ! Y\        T4      \        T4      4       T$^ 8  d(   RRR	RT$ R\I        T!4       R2R. RT$R\I        T!4      RT%/# RRRRRT%/# u upi u upi u upi u up'i   \T         d"   p1\        P                  ROT14        Rp1?1EL?Rp1?1ii ; i  \T         d#   p1\        P                  R\T14        Rp1?1E	K+  Rp1?1ii ; i  \d         d    p1RRR\)        T14      RT/u Rp1?1u u # Rp1?1ii ; i  \T         d"   p1\        PY                  ReT14        Rp1?1ELrRp1?1ii ; i  + '       g   i     EL; i  \T         d   p1\)        T14      pK^ pJRpI Rp1?1EL
Rp1?1ii ; i  \T         d     EKj  i ; i  \p        Pt                   d    / pN ELi ; i  \T         d"   p1\        P                  R{T14        Rp1?1ELRp1?1ii ; i  \T         d"   p1\        PY                  R|T14        Rp1?1ELRp1?1ii ; i  \T         d#   p1\        P                  RT14        Rp1?1EK  Rp1?1ii ; i  \T         d     EK)  i ; i  \T         d     EK<  i ; i  \T         d"   p1\        PY                  RT14        Rp1?1EL3Rp1?1ii ; i)z]
Port of CampaignController::initiateOutboundCall (synchronous, uses sleep/usleep like PHP).
z4Business ID is required but not found. Campaign ID: successFerrorr    zMCUBE_PUBLIC_WS_URL_BASE is not set (required for refurl). Copy MCUBE_PUBLIC_WS_URL_BASE from backend .env into campaign_port environment.validmessageinsufficient_minutesT
remain_min
error_codeBUSINESS_TIME_BLOCKEDbusiness_timecurrent_timecurrent_dayzCampaigns table not found: 
table_nameNzSELECT * FROM `z"` WHERE campaign_id = :cid LIMIT 1cidzCampaign not found: statuspausedz<Campaign is paused. Please resume it to continue processing.current_statuszPCampaign status must be active or processing to initiate calls. Current status: z0Skipping retry calls for campaign %s - status %szCampaign is z. Retry calls are not allowed.z4Allowing retry calls for campaign %s with status: %s
start_timeend_time)tzinforun_nowz"Campaign is scheduled to start at 	scheduledzCampaign has ended at endedz!Campaign contacts table not found
batch_sizebatch_intervaldidretry_atz3SELECT id, number, name, status, call_id, `retry`, priorityz0 AS priorityz FROM `zB` WHERE business_id = :bid AND number IS NOT NULL AND number != ''bidz AND campaign_id = :cidzDUPPER(COALESCE(status, '')) != 'ANSWER' AND COALESCE(`retry`, 0) > 0z AND (retry_at <= 'z' OR retry_at IS NULL)z AND ()z&(status = 'pending' OR status IS NULL)z](UPPER(COALESCE(status, '')) != 'ANSWER' AND call_id IS NOT NULL AND COALESCE(`retry`, 0) > 0z AND ((z) OR (z))z AND priority = 1z ORDER BY priority DESC, id ASCz ORDER BY id ASCidnamenumberz+No customer numbers found for this campaignAuthorization	exenumberi,  zCBot concurrent limit campaign_id=%s bot_id=%s exenumber=%s limit=%szSELECT COUNT(*) FROM `z$` WHERE dialstatus IN ('CONNECTING')zInitial CONNECTING count: %sz,Initial bot CONNECTING for %s: %s (limit %s)z%Error checking initial CONNECTING: %szSELECT status FROM `zCampaign %s paused at batch %szCampaign %s paused at contactz!Campaign %s blocked business timez!SELECT id, status, `retry` FROM `z/` WHERE id = :id AND campaign_id = :cid LIMIT 1ANSWERzSkipping contact %s - ANSWERretryz Skipping contact %s - no retriesz/Concurrent check call %s: business=%s/%s bot=%szMax wait exceeded, proceedingzCONNECTING check error: %s
custnumbercustnamerefurlrefidz1Missing required field for call. exenumber: emptyzUPDATE `zN` SET status = 'failed', updated_at = :u WHERE id = :id AND campaign_id = :ciduzFailed update contact: %sg      ?g      >@)timeoutzContent-Typezapplication/jsonzBearer )jsonheadersi  i  zRetrying after %s szCall z
 (Number: z) - cURL Error: z{}msgsucccallidz#Call placed successfully for %s: %szSELECT `retry` FROM `zJ` SET `retry` = :nr, updated_at = :u WHERE id = :id AND campaign_id = :cidnrzDecrement retry failed: %sz#fetchDialStatusAndUpdate failed: %sz,Waiting 5 seconds for call to be recorded...z)Concurrent counts OK; ready for next callzMax wait on disconnect exceededz Error re-checking CONNECTING: %sz) - API Error: z (Response: :N   Nzempty responsez) - HTTP Error z: z Full response failed call %s: %sz$trigger_call_history_sync failed: %sz+Campaign %s marked completed after batch %szBatch %s done; waiting %s szSuccessfully initiated z out of z callscall_idssuccess_counttotal_callserrorszAll calls failed)active
processing)	cancelledstopped)>r
   outbound_bearer_tokenr    resolve_business_id_for_campaignloggerr+   mcube_public_ws_url_basestripr   intgetr   r   r   r   r   executer   mappingsfirststrinfor   
isinstancer   date_parserparser=   replace
astimezoner   r   appendjoinalllistlenranger   r   
scalar_oner   	Exception	enumeratewarningcommituppertimesleepr	   
ValueErrorhttpxClientpostoutbound_call_urlstatus_coderU   loadsJSONDecodeErrorr   r   !check_and_mark_campaign_completed)]r   r   r   r   r    r"   settings
auth_tokenerrminutes_checkbusiness_time_checkcampaigns_table_namer6   call_history_tblcampaignnowr;   r<   rA   rB   rL   has_retry_atnow_strq_partsparamssubpending
retry_partsqlrowscustomer_contactsrcontact_names_mapcustomer_numberscontact_ids	base_datar]   r_   total_contactsibatchestotal_batchesmax_connecting_callscheck_intervalmax_wait_timecampaign_bot_idbot_concurrent_limitinitial_connectingibe	is_pausedbatch_indexbatchcurbatch_contact_idsindexrJ   btcglobal_index
contact_idct
wait_startconnecting_countbot_connectingbusiness_okbot_okcustomer_namerQ   dataerrmmax_retriesretry_countcall_successfulresponse_text	http_code
curl_errorclientdelayresponse_data
api_statusapi_messagecall_idretry_already_decrementedcurrent_contact	old_retry	new_retrywait_for_disconnect_startcurrent_connectingcurrent_botfinal_business	final_botpreviewwas_completeds]   &&&&&&                                                                                       r&   initiate_outbound_callr       s    ~H//J;#DD_grsD[MRS5'3{KK--33::<<u ^;	
 	
 ,OS=MNM!!u]9-"D-++L!<;
 	
 5_c+FVWw''u(31044_E/33NC.22=A;
 	
 +3{+;<([)9:J)#k*:;_;;+,@+ABS5'3>RSSH{"**?#7"88Z[\K 
 (*UUW 	 (6CLL5{2	  ||H%1u[!;$hll8&<d  ||H%-EEuo(,,x012!;$hll8&<  F ||H%)AANP[]i]ijr]stu|HLL,B+CCab!;$hll8&<	  KKNP[]i]ijr]stil+
J'*h//$**3z?;J(H--"((X7H$#++3::+>J??"''szz':Hx||I&++!,1cJ<Q<QRUR\R\<]6]5=	*@UUTi
3	#  $$SZZ0051Ih4GGIh/	#  _115'+NP\^hii:BX\\,/5526JAIS&67<<1=qN:BHLL'--2.446Io:FL	"G 	>G /z::z"'NN
*_` $[1F01#uXC,WI5KLLNNVC5?+>Go   3G9<RSS
#JNNWWIVJ<rBCOLL*+/z::89)*
'''
C""49f5>>@DDFDT
QbcQbAQtWs155=+>+>B'?'E'E'GGQbc:KL:KQAhK(..0:KL):;):A3qw<):K;5'+XZgituu!0*k9 UIMF)*N=B1cJZF[]g=hi=hA
N3=hGiLMNMOB6KHXZcd&#;OSQ\M]_n#o KKU$	A,44)*:);;_`a

*, 	 	24FG+	R)/YWBKKF	SUWkl I'0U!))+,@+AAcde$ eg  s1v)<k;YZ?[ 	'j(@;Q[C[^hChi&u-ME6%--/0D/EEghiK( %'  3q6X-KK ?M $I,_c+>NOCw<<BKP&&(&&(uS^ "9#SWW_%="CGGN$;!377=#9!;  &3e;L5:SAR=S5S*51Y]Jk$,,;J<Gvw :uk:	
 (*UUW  266(+11r288:@@BhN$BJO 266'?//a0A5$F
S J)1134H3IIklm, eg  s1v1$(	'*'// "89I8JJn o %*,($ py%=oaj%k~N"25I"IK1T9bb^Nb=bFKKI$q((,& #v		j0]B'FGJJ~.
 %--/0D/EEghiK( %'  3q6X- $IJT-11#j/2FZ\MW+H5fM&D JT"d#+	G'// "*:, 7H !H !')T:ukR a

3KK#O MIJ,_)d33v"KK$66!% .0B /7:,1G% (  4 %&FFM !I!%J 	!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z1A[Z<A[[A[[ A[1[+	A[4[4A\[?A\\A\\A\,\+A\,\/A]
]	A]
]A]9]A]4]4A]9]<A^(^A^#^#A^(^+A_^6A__A__A_+_*A_+_.A_>_=A_>`A`-`A`(`(A`-)FF),
__future__r   rU   loggingr   r   typingr   r   dateutilr   rq   
sqlalchemyr   sqlalchemy.ormr   
app.configr	   r
   app.utils.cluster_table_namesr   r   r   app.repositories.schema_helperr   r   app.servicesr   app.services.campaign_corer   r   r   r   r   r   app.utils.rabbitmq_campaignr   app.utils.timezone_utilr   r   	getLogger__name__rf   r   r$   r(   r&   <module>r      sc    "       *  " 7 f f @ &  B 6			8	$`	M `	Mr(   