
    i/                     8    d dl Z d dlmZ d dlmZ  G d d      Zy)    N)Path)settingsc            
          e Zd ZddZdefdZddZddZdee   fdZ	de
dedz  fd	Zd
e
de
defdZde
de
defdZde
ddfdZde
dee   fdZde
de
dedz  fdZde
d
e
de
dee
   def
dZde
de
de
dee
   def
dZde
de
ddfdZy)	KBServicereturnNc                 j    t        t        j                        | _        | j	                         | _        y N)r   r   
FLOWS_FILEpath
_load_datadataselfs    K/home/techsaathi/public_html/it_helpdesk_chatbot/app/services/kb_service.py__init__zKBService.__init__   s"    ,,-	OO%	    c                     | j                   j                         sdg iS t        | j                   dd      5 }t        j                  |      cd d d        S # 1 sw Y   y xY w)N
categoriesrutf-8encoding)r   existsopenjsonloadr   fs     r   r   zKBService._load_data   sO    yy! "%%$))S73 	 q99Q<	  	  	 s   AAc                 .    | j                         | _        y r	   )r   r   r   s    r   reloadzKBService.reload   s    OO%	r   c                     t        | j                  dd      5 }t        j                  | j                  |dd       d d d        y # 1 sw Y   y xY w)Nwr   r      F)indentensure_ascii)r   r   r   dumpr   r   s     r   savezKBService.save   sC    $))S73 	BqIIdii15A	B 	B 	Bs   $AAc                 :    | j                   j                  dg       S )Nr   )r   getr   s    r   get_categorieszKBService.get_categories   s    yy}}\2..r   category_codec                 ^    | j                         D ]  }|j                  d      |k(  s|c S  y Ncode)r*   r)   r   r+   categorys      r   get_categoryzKBService.get_category   s5    ++- 	 H||F#}4	  r   r.   titlec                     | j                  |      rt        d      |j                         |j                         g d}| j                  d   j	                  |       | j                          |S )NzCategory code already exists)r.   r2   sub_optionsr   )r1   
ValueErrorstripr   appendr'   )r   r.   r2   r0   s       r   add_categoryzKBService.add_category"   sa    T";<< JJL[[]

 			,&&x0		r   c                     | j                  |      }|st        d      |j                         |d<   | j                          |S )NCategory not foundr2   )r1   r5   r6   r'   )r   r+   r2   r0   s       r   update_categoryzKBService.update_category/   s?    $$]3122!KKM		r   c                     | j                         }|D cg c]  }|j                  d      |k7  s| }}t        |      t        |      k(  rt        d      || j                  d<   | j                          y c c}w )Nr.   r:   r   )r*   r)   lenr5   r   r'   )r   r+   r   cnew_categoriess        r   delete_categoryzKBService.delete_category8   sl    ((*
%/R155=M3Q!RR~#j/1122"0		,			 Ss
   A7A7c                 P    | j                  |      }|sg S |j                  dg       S )Nr4   )r1   r)   r/   s      r   get_sub_optionszKBService.get_sub_options@   s+    $$]3I||M2..r   sub_codec                 `    | j                  |      D ]  }|j                  d      |k(  s|c S  y r-   )rB   r)   )r   r+   rC   
sub_options       r   get_sub_optionzKBService.get_sub_optionF   s8    ..}= 	"J~~f%1!!	" r   stepsc                 l   | j                  |      }|st        d      | j                  ||      rt        d      |j                         |j                         |D cg c]#  }|j                         s|j                         % c}d}|d   j	                  |       | j                          |S c c}w )Nr:   z*Issue code already exists in this category)r.   r2   rG   r4   )r1   r5   rF   r6   r7   r'   )r   r+   r.   r2   rG   r0   sitems           r   add_sub_optionzKBService.add_sub_optionL   s    $$]3122}d3IJJ JJL[[]).<A!'')aggi<

 	&&t,			 =s   B14B1c                     | j                  ||      }|st        d      |j                         |d<   |D cg c]#  }|j                         s|j                         % c}|d<   | j                          |S c c}w )NIssue not foundr2   rG   )rF   r5   r6   r'   )r   r+   rC   r2   rG   rJ   rI   s          r   update_sub_optionzKBService.update_sub_option]   sh    ""=(;.//W,1?qQWWY?W		 @s   A7A7c                 $   | j                  |      }|st        d      |j                  dg       }|D cg c]  }|j                  d      |k7  s| }}t        |      t        |      k(  rt        d      ||d<   | j	                          y c c}w )Nr:   r4   r.   rM   )r1   r5   r)   r=   r'   )r   r+   rC   r0   currenti	new_itemss          r   delete_sub_optionzKBService.delete_sub_optiong   s    $$]3122,,}b1 'E1155=H+DQE	Ey>S\).//"+		 Fs   BB)r   N)__name__
__module____qualname__r   dictr   r    r'   listr*   strr1   r8   r;   r@   rB   rF   rK   rN   rS    r   r   r   r      s9   & D  &B/T
 /# $+  S T S   S T /S /T$Z /C 3 4$; C s 3 tTWy ]a "s c # VZ[^V_ dh s c d r   r   )r   pathlibr   app.core.configr   r   rZ   r   r   <module>r]      s      $k kr   