| @@ -2,10 +2,273 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Services\MailerInbox; |
| 4 | 4 | |
| 5 | 5 | use FluentSupport\App\App; |
| 6 | +use FluentSupport\App\Models\Agent; | |
| 7 | +use FluentSupport\App\Models\MailBox; | |
| 8 | +use FluentSupport\App\Models\Meta; | |
| 9 | +use FluentSupport\App\Models\Ticket; | |
| 10 | +use FluentSupport\App\Services\EmailNotification\Settings; | |
| 11 | +use FluentSupport\App\Services\Tickets\AgentTicketAccess; | |
| 6 | 12 | use FluentSupport\Framework\Support\Arr; |
| 13 | +use Exception; | |
| 7 | 14 | |
| 8 | 15 | class MailBoxService |
| 9 | 16 | { |
| 17 | + /** | |
| 18 | + * This `getMailBoxes` method is used to get all mailboxes. | |
| 19 | + * @return array | |
| 20 | + */ | |
| 21 | + public function getMailBoxes () | |
| 22 | + { | |
| 23 | + $mailboxes = MailBox::all(); | |
| 10 | 24 | |
| 25 | + foreach ($mailboxes as $mailbox) { | |
| 26 | + $mailbox->tickets_count = Ticket::countTicketByMailBoxId($mailbox->id); | |
| 27 | + } | |
| 28 | + | |
| 29 | + return [ | |
| 30 | + 'mailboxes' => $mailboxes | |
| 31 | + ]; | |
| 32 | + } | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * This `deleteMailBox` method is used to delete a mailbox. | |
| 36 | + * @param int $mailBoxId The id of the mailbox to be deleted. | |
| 37 | + * @param int $fallbackId The id of the fallback mailbox to be used. | |
| 38 | + * @return array | |
| 39 | + */ | |
| 40 | + public function deleteMailBox($mailBoxId, $fallbackId ) | |
| 41 | + { | |
| 42 | + if ( $mailBoxId == $fallbackId ) { | |
| 43 | + throw new \Exception(esc_html__('Fallback Box cannot be the same as MailBox ID', 'fluent-support')); | |
| 44 | + } | |
| 45 | + | |
| 46 | + $box = MailBox::findOrFail($mailBoxId); | |
| 47 | + $fallbackBox = MailBox::findOrFail($fallbackId); | |
| 48 | + | |
| 49 | + // if the mailbox is default, then make the fallback mailbox default | |
| 50 | + if($box->is_default == 'yes'){ | |
| 51 | + $fallbackBox->is_default = 'yes'; | |
| 52 | + $fallbackBox->save(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /* | |
| 56 | + * Action before delete a mailbox | |
| 57 | + * | |
| 58 | + * @since v1.0.0 | |
| 59 | + * @param object $box Mailbox | |
| 60 | + * @param object $fallbackBox Fallback mailbox | |
| 61 | + */ | |
| 62 | + do_action('fluent_support/before_delete_email_box', $box, $fallbackBox); | |
| 63 | + | |
| 64 | + $this->deleteAllMailBoxMeta($mailBoxId); | |
| 65 | + | |
| 66 | + MailBox::where('id', $mailBoxId)->delete(); | |
| 67 | + | |
| 68 | + // let's transfer the tickets now | |
| 69 | + Ticket::syncMailBoxId($mailBoxId, $fallbackId); | |
| 70 | + | |
| 71 | + /* | |
| 72 | + * Action on mailbox delete | |
| 73 | + * | |
| 74 | + * @since v1.0.0 | |
| 75 | + * @param integer $mailBoxId | |
| 76 | + * @param object $fallbackBox Fallback mailbox | |
| 77 | + */ | |
| 78 | + do_action('fluent_support/mailbox_deleted', $mailBoxId, $fallbackBox); | |
| 79 | + | |
| 80 | + return [ | |
| 81 | + 'message' => __('Selected Business has been deleted', 'fluent-support') | |
| 82 | + ]; | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * This `moveTickets` method is used to move tickets from one mailbox to another. | |
| 87 | + * @param array $data | |
| 88 | + * @param int $mailBoxId | |
| 89 | + * @return array | |
| 90 | + */ | |
| 91 | + public function moveTickets ($data, $mailBoxId ) | |
| 92 | + { | |
| 93 | + $this->validateTicketMoving( $data, $mailBoxId ); | |
| 94 | + | |
| 95 | + $newBox = MailBox::findOrFail($data['new_box_id']); | |
| 96 | + $oldBox = MailBox::findOrFail($mailBoxId); | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Action before tickets moved | |
| 100 | + * @since v1.5.7 | |
| 101 | + * @param array $data | |
| 102 | + * @param object $oldBox Current mailbox | |
| 103 | + * @param object $newBox Mailbox where selected tickets will be moved | |
| 104 | + */ | |
| 105 | + do_action( 'fluent_support/before_move_tickets', $data, $oldBox, $newBox ); | |
| 106 | + | |
| 107 | + if (!empty($data['ticket_ids'])) { | |
| 108 | + Ticket::whereIn('id', $data['ticket_ids']) | |
| 109 | + ->update([ | |
| 110 | + 'mailbox_id' => $newBox->id | |
| 111 | + ]); | |
| 112 | + } else { | |
| 113 | + // Move all ticket for the MailBox | |
| 114 | + Ticket::where('mailbox_id', $mailBoxId) | |
| 115 | + ->update([ | |
| 116 | + 'mailbox_id' => $newBox->id | |
| 117 | + ]); | |
| 118 | + } | |
| 119 | + /** | |
| 120 | + * Action after tickets moved | |
| 121 | + * @since v1.5.7 | |
| 122 | + * @param array $data | |
| 123 | + * @param object $oldBox Mailbox where selected tickets were moved from | |
| 124 | + * @param object $newBox Mailbox where selected tickets were moved to | |
| 125 | + */ | |
| 126 | + do_action( 'fluent_support/tickets_moved', $data, $oldBox, $newBox ); | |
| 127 | + | |
| 128 | + return [ | |
| 129 | + 'message' => __('All ticket moves to the selected Business', 'fluent-support') | |
| 130 | + ]; | |
| 131 | + | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * This `setAsDefault` method will set a business box as default. Update is_default column to yes for the selected mailbox and no for others. | |
| 136 | + * @param int $mailBoxId | |
| 137 | + * @return array | |
| 138 | + */ | |
| 139 | + public function setAsDefault ($mailBoxId ) | |
| 140 | + { | |
| 141 | + $box = MailBox::findOrFail($mailBoxId); | |
| 142 | + | |
| 143 | + $box->is_default = 'yes'; | |
| 144 | + $box->save(); | |
| 145 | + | |
| 146 | + MailBox::where('id', '!=', $mailBoxId) | |
| 147 | + ->update([ | |
| 148 | + 'is_default' => 'no' | |
| 149 | + ]); | |
| 150 | + | |
| 151 | + return [ | |
| 152 | + 'message' => __('Selected Business has been set as default', 'fluent-support') | |
| 153 | + ]; | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * This `getEmailSetups` method is used to get the email setups | |
| 158 | + * @param int $boxId | |
| 159 | + * @return array | |
| 160 | + */ | |
| 161 | + public function getEmailsSetups ( $boxId ) | |
| 162 | + { | |
| 163 | + $settings = new Settings(); | |
| 164 | + $box = MailBox::findOrFail( $boxId ); | |
| 165 | + | |
| 166 | + $types = $settings->getEmailSettingsKeys(); | |
| 167 | + | |
| 168 | + $req = []; | |
| 169 | + foreach ( $types as $type ) { | |
| 170 | + $req[] = $settings->getBoxEmailSettings( $box, $type ); | |
| 171 | + } | |
| 172 | + | |
| 173 | + return [ | |
| 174 | + 'email_configs' => $req, | |
| 175 | + 'email_keys' => $types | |
| 176 | + ]; | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * This `saveEmailSettings` method is used to save the email settings for the mailbox | |
| 181 | + * @param object $request | |
| 182 | + * @param int $boxId | |
| 183 | + * @return array | |
| 184 | + */ | |
| 185 | + public function saveEmailSettings ($emailType, $boxId, $data ) | |
| 186 | + { | |
| 187 | + $settings = new Settings(); | |
| 188 | + | |
| 189 | + $data['email_body'] = wp_kses_post($data['email_body']); | |
| 190 | + | |
| 191 | + $box = MailBox::findOrFail($boxId); | |
| 192 | + | |
| 193 | + $settings->saveBoxEmailSettings($box, $emailType, $data); | |
| 194 | + | |
| 195 | + return [ | |
| 196 | + 'message' => __('Settings has been updated', 'fluent-support') | |
| 197 | + ]; | |
| 198 | + } | |
| 199 | + | |
| 200 | + /** | |
| 201 | + * This `getTickets` method is used to get the tickets for the given mailbox | |
| 202 | + * @param array $filters | |
| 203 | + * @param int $boxId | |
| 204 | + * @return array | |
| 205 | + */ | |
| 206 | + public function getTickets ($filters, $boxId ) | |
| 207 | + { | |
| 208 | + if (!$boxId) { | |
| 209 | + throw new \Exception(esc_html__('MailBox ID must be provided', 'fluent-support')); | |
| 210 | + } | |
| 211 | + | |
| 212 | + $ticketsQuery = Ticket::getTicketsQuery(); | |
| 213 | + | |
| 214 | + $ticketsQuery->where('mailbox_id', $boxId); | |
| 215 | + | |
| 216 | + if ( !empty($filters['customer_id']) ) { | |
| 217 | + $ticketsQuery->where('customer_id', $filters['customer_id']); | |
| 218 | + } | |
| 219 | + | |
| 220 | + if ( !empty($filters['product_id']) ) { | |
| 221 | + $ticketsQuery->where('product_id', $filters['product_id']); | |
| 222 | + } | |
| 223 | + | |
| 224 | + if ( !empty($filters['ticket_title']) ) { | |
| 225 | + $ticketsQuery->where('title', 'LIKE', "%". $filters['ticket_title'] ."%"); | |
| 226 | + } | |
| 227 | + | |
| 228 | + return [ | |
| 229 | + 'tickets' => $ticketsQuery->paginate() | |
| 230 | + ]; | |
| 231 | + } | |
| 232 | + | |
| 233 | + private function validateTicketMoving ( $data, $mailBoxId ) | |
| 234 | + { | |
| 235 | + | |
| 236 | + if ($data['new_box_id'] == $mailBoxId) { | |
| 237 | + throw new \Exception(esc_html__('New Box cannot be the same as MailBox ID', 'fluent-support')); | |
| 238 | + } | |
| 239 | + | |
| 240 | + if ( $data['move_type'] == 'Custom' && empty($data['ticket_ids']) ) { | |
| 241 | + throw new \Exception(esc_html__('Invalid request submitted. Please select a ticket first', 'fluent-support')); | |
| 242 | + } | |
| 243 | + $ticketIDs = $data['ticket_ids']; | |
| 244 | + $newMailBoxId = (int)$data['new_box_id']; | |
| 245 | + | |
| 246 | + foreach ($ticketIDs as $ticketID) { | |
| 247 | + $ticket = Ticket::findOrFail($ticketID); | |
| 248 | + if (!$ticket->agent_id) { | |
| 249 | + continue; | |
| 250 | + } | |
| 251 | + | |
| 252 | + $agent = Agent::find($ticket->agent_id); | |
| 253 | + if (!$agent) { | |
| 254 | + continue; | |
| 255 | + } | |
| 256 | + | |
| 257 | + $restrictedBoxes = (new AgentTicketAccess())->getRestrictedMailboxIds($agent); | |
| 258 | + | |
| 259 | + if (in_array($newMailBoxId, $restrictedBoxes, true)) { | |
| 260 | + throw new \Exception(esc_html__('Agent is restricted for this mailbox ticket', 'fluent-support')); | |
| 261 | + } | |
| 262 | + } | |
| 263 | + return true; | |
| 264 | + } | |
| 265 | + | |
| 266 | + private function deleteAllMailBoxMeta($id) | |
| 267 | + { | |
| 268 | + $class = __NAMESPACE__ . '\MailBox'; | |
| 269 | + | |
| 270 | + Meta::where('object_type', $class) | |
| 271 | + ->where('object_id', $id) | |
| 272 | + ->delete(); | |
| 273 | + } | |
| 11 | 274 | } |