@@ -30,13 +30,69 @@ public function processRawContentMail(Request $request)
3030
3131 if ($ rawEmail != false && !empty ($ rawEmail )) {
3232 $ this ->mailboxService ->processMail ($ rawEmail );
33- }else {
33+ } else {
3434 dump ("Empty Text file not allow " );
35- }
35+ }
36+
3637 exit (0 );
3738 }
39+
40+ public function loadMailboxesXHR (Request $ request )
41+ {
42+ $ mailboxConfiguration = $ this ->mailboxService ->parseMailboxConfigurations ();
43+
44+ $ defaultMailbox = $ mailboxConfiguration ->getDefaultMailbox ();
45+
46+ $ collection = array_map (function ($ mailbox ) use ($ defaultMailbox ) {
47+ return [
48+ 'id ' => $ mailbox ->getId (),
49+ 'name ' => $ mailbox ->getName (),
50+ 'isEnabled ' => $ mailbox ->getIsEnabled (),
51+ ];
52+ }, array_values ($ mailboxConfiguration ->getMailboxes ()));
53+
54+ return new JsonResponse ($ collection ?? []);
55+ }
56+
57+ public function removeMailboxConfiguration ($ id , Request $ request )
58+ {
59+ $ mailboxService = $ this ->mailboxService ;
60+ $ existingMailboxConfiguration = $ mailboxService ->parseMailboxConfigurations ();
61+
62+ foreach ($ existingMailboxConfiguration ->getMailboxes () as $ configuration ) {
63+ if ($ configuration ->getId () == $ id ) {
64+ $ mailbox = $ configuration ;
65+
66+ break ;
67+ }
68+ }
69+
70+ if (empty ($ mailbox )) {
71+ return new JsonResponse ([
72+ 'alertClass ' => 'danger ' ,
73+ 'alertMessage ' => "No mailbox found with id ' $ id'. " ,
74+ ], 404 );
75+ }
76+
77+ $ mailboxConfiguration = new MailboxConfiguration ();
78+
79+ foreach ($ existingMailboxConfiguration ->getMailboxes () as $ configuration ) {
80+ if ($ configuration ->getId () == $ id ) {
81+ continue ;
82+ }
83+
84+ $ mailboxConfiguration ->addMailbox ($ configuration );
85+ }
86+
87+ file_put_contents ($ mailboxService ->getPathToConfigurationFile (), (string ) $ mailboxConfiguration );
88+
89+ return new JsonResponse ([
90+ 'alertClass ' => 'success ' ,
91+ 'alertMessage ' => $ this ->translator ->trans ('Mailbox configuration removed successfully. ' ),
92+ ]);
93+ }
3894
39- public function processMailXHR (Request $ request )
95+ public function processMailXHR (Request $ request, MailboxService $ mailboxService )
4096 {
4197 if ("POST " != $ request ->getMethod ()) {
4298 return new JsonResponse ([
@@ -51,7 +107,7 @@ public function processMailXHR(Request $request)
51107 }
52108
53109 try {
54- $ processedThread = $ this -> mailboxService ->processMail ($ request ->get ('email ' ));
110+ $ processedThread = $ mailboxService ->processMail ($ request ->get ('email ' ));
55111 } catch (\Exception $ e ) {
56112 return new JsonResponse ([
57113 'success ' => false ,
@@ -66,7 +122,7 @@ public function processMailXHR(Request $request)
66122 }
67123
68124 if (!empty ($ processedThread ['content ' ]['ticket ' ]) && !empty ($ processedThread ['content ' ]['thread ' ])) {
69- $ responseMessage .= " <comment>[tickets/ " . $ processedThread ['content ' ]['ticket ' ] . "/# " . $ processedThread ['content ' ]['thread ' ] . "]</comment> " ;
125+ $ responseMessage .= " <comment>[tickets/ " . $ processedThread ['content ' ]['ticket ' ] . "/# " . $ processedThread ['content ' ]['ticket ' ] . "]</comment> " ;
70126 } else if (!empty ($ processedThread ['content ' ]['ticket ' ])) {
71127 $ responseMessage .= " <comment>[tickets/ " . $ processedThread ['content ' ]['ticket ' ] . "]</comment> " ;
72128 }
@@ -76,56 +132,49 @@ public function processMailXHR(Request $request)
76132 'message ' => $ responseMessage ,
77133 ]);
78134 }
79-
80- public function loadMailboxesXHR (Request $ request )
81- {
82- $ collection = array_map (function ($ mailbox ) {
83- return [
84- 'id ' => $ mailbox ->getId (),
85- 'name ' => $ mailbox ->getName (),
86- 'isEnabled ' => $ mailbox ->getIsEnabled (),
87- 'isDeleted ' => $ mailbox ->getIsDeleted () ? $ mailbox ->getIsDeleted () : false ,
88- ];
89- }, $ this ->mailboxService ->parseMailboxConfigurations ()->getMailboxes ());
90-
91- return new JsonResponse ($ collection ?? []);
92- }
93135
94- public function removeMailboxConfiguration ( $ id , Request $ request )
136+ public function processOutlookMailXHR ( Request $ request , MailboxService $ mailboxService )
95137 {
96- $ mailboxService = $ this ->mailboxService ;
97- $ existingMailboxConfiguration = $ mailboxService ->parseMailboxConfigurations ();
98-
99- foreach ($ existingMailboxConfiguration ->getMailboxes () as $ configuration ) {
100- if ($ configuration ->getId () == $ id ) {
101- $ mailbox = $ configuration ;
102-
103- break ;
104- }
138+ if ("POST " != $ request ->getMethod ()) {
139+ return new JsonResponse ([
140+ 'success ' => false ,
141+ 'message ' => 'Request not supported. '
142+ ], 500 );
143+ } else if (null == $ request ->get ('email ' )) {
144+ return new JsonResponse ([
145+ 'success ' => false ,
146+ 'message ' => 'Missing required email data in request content. '
147+ ], 500 );
105148 }
106149
107- if (empty ($ mailbox )) {
150+ try {
151+ $ processedThread = $ mailboxService ->processOutlookMail ($ request ->get ('email ' ));
152+ } catch (\Exception $ e ) {
108153 return new JsonResponse ([
109- 'alertClass ' => 'danger ' ,
110- 'alertMessage ' => "No mailbox found with id ' $ id'. " ,
111- ], 404 );
154+ 'success ' => false ,
155+ 'message ' => $ e ->getMessage (),
156+ 'params ' => $ request ->get ('email ' )
157+ ], 500 );
112158 }
113159
114- $ mailboxConfiguration = new MailboxConfiguration ();
115-
116- foreach ($ existingMailboxConfiguration ->getMailboxes () as $ configuration ) {
117- if ($ configuration ->getId () == $ id ) {
118- continue ;
119- }
160+ $ responseMessage = $ processedThread ['message ' ];
120161
121- $ mailboxConfiguration ->addMailbox ($ configuration );
162+ if (! empty ($ processedThread ['content ' ]['from ' ])) {
163+ $ responseMessage = "Received email from <info> " . $ processedThread ['content ' ]['from ' ]. "</info>. " . $ responseMessage ;
122164 }
123165
124- file_put_contents ($ mailboxService ->getPathToConfigurationFile (), (string ) $ mailboxConfiguration );
166+ if (
167+ ! empty ($ processedThread ['content ' ]['ticket ' ])
168+ && !empty ($ processedThread ['content ' ]['thread ' ])
169+ ) {
170+ $ responseMessage .= " <comment>[tickets/ " . $ processedThread ['content ' ]['ticket ' ] . "/# " . $ processedThread ['content ' ]['ticket ' ] . "]</comment> " ;
171+ } else if (! empty ($ processedThread ['content ' ]['ticket ' ])) {
172+ $ responseMessage .= " <comment>[tickets/ " . $ processedThread ['content ' ]['ticket ' ] . "]</comment> " ;
173+ }
125174
126175 return new JsonResponse ([
127- 'alertClass ' => ' success ' ,
128- 'alertMessage ' => $ this -> translator -> trans ( ' Mailbox configuration removed successfully. ' ),
176+ 'success ' => true ,
177+ 'message ' => $ responseMessage ,
129178 ]);
130179 }
131180}
0 commit comments