Created
May 19, 2020 14:13
-
-
Save cygeorgel/7aeec080ebb670e25d3156cb550aee60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function store(Request $request) | |
{ | |
//app('log')->debug(request()->all()); | |
$body = "body-html"; | |
$body = $request->$body; | |
if (! $body) { | |
$body = ''; | |
} | |
$bodyStripped = "stripped-html"; | |
$bodyStripped = $request->$bodyStripped; | |
if (! $bodyStripped) { | |
$bodyStripped = ''; | |
} | |
$records = null; | |
$newMap = null; | |
if (isset($request->attachments)) { | |
$attachments = json_decode($request->attachments); | |
$records = []; | |
$replacements = []; | |
$map = "content-id-map"; | |
if (isset($request->$map)) { | |
$map = json_decode($request->$map, 1); | |
} | |
foreach ($attachments as $attachment) { | |
$response = (new Client())->get($attachment->url, [ | |
'auth' => ['api', config('services.mailgun.secret')], | |
]); | |
$filename = time() . $attachment->name; | |
$content = $response->getBody(); | |
Storage::disk('local')->put('attachments/' . $filename, $content); | |
array_push($replacements, url('/attachments/' . $filename)); | |
array_push($records, $filename); | |
} | |
$i = 0; | |
$newMap = []; | |
if ($map) { | |
if (is_array($map)) { | |
foreach ($map as $key => $value) { | |
$cleanKey = str_replace(['<', '>'], '', $key); | |
$newMap[$cleanKey] = $replacements[$i]; | |
$i++; | |
} | |
} | |
} | |
} | |
if ($newMap) { | |
if (is_array($newMap)) { | |
$body = str_replace(array_keys($newMap), array_values($newMap), $body); | |
$bodyStripped = str_replace(array_keys($newMap), array_values($newMap), $bodyStripped); | |
} | |
} | |
$body = str_replace('cid:', '', $body); | |
$bodyStripped = str_replace('cid:', '', $bodyStripped); | |
$analysis = []; | |
$analysis['contact'] = null; | |
$analysis['customer'] = null; | |
$analysis['isNewTicket'] = false; | |
$analysis['isReplyToExistingTicket'] = false; | |
$analysis['ticket_id'] = null; | |
$ticket = null; | |
$thread = null; | |
$reply = false; | |
if (strpos($body, '##') !== false) { | |
$thread = substr(strstr($body, '##'), 2, 6); | |
if ($thread) { | |
$ticket = Ticket::where('number', $thread)->first(); | |
$contact = $this->getContact($request->sender); | |
$bodyElements = explode('##' . $thread, $body); | |
if ($ticket) { | |
$reply = Reply::create([ | |
'user_id' => 1, | |
'contact_id' => $contact->id ?? null, | |
'ticket_id' => $ticket->id, | |
// 'body' => nl2br2(utf8_encode(utf8_decode($bodyElements[0]))), | |
// 'body' => $bodyElements[0], | |
'body' => $bodyStripped, | |
'sendToContacts' => false, | |
]); | |
if ($records) { | |
if (count($records)) { | |
foreach ($records as $record) { | |
Attachment::create([ | |
'attachable_id' => $reply->id, | |
'attachable_type' => 'App\Reply', | |
'filename' => $record, | |
]); | |
} | |
} | |
} | |
$analysis['contact'] = $contact->name ?? null; | |
$analysis['customer'] = $contact->customer->name ?? null; | |
$analysis['isNewTicket'] = false; | |
$analysis['isReplyToExistingTicket'] = true; | |
$analysis['ticket_id'] = $ticket->id; | |
} | |
} | |
} | |
if ( ! $reply) { | |
$contact = $this->getContact($request->sender); | |
if ($contact) { | |
$ticket = Ticket::create([ | |
'number' => ticketNumber(), | |
'user_id' => 1, | |
'contact_id' => $contact->id, | |
'email' => $request->sender, | |
'ticketable_id' => $contact->customer_id, | |
'ticketable_type' => 'App\Customer', | |
'priority_id' => 1, | |
'status_id' => 1, | |
'subject' => $request->subject, | |
// 'body' => nl2br2(utf8_encode(utf8_decode($body))), | |
'body' => $body, | |
'open' => 1, | |
'opened_via' => 'email', | |
'opened_by' => 1, | |
'opened_at' => now(), | |
]); | |
if ($records) { | |
if (count($records)) { | |
foreach ($records as $record) { | |
Attachment::create([ | |
'attachable_id' => $ticket->id, | |
'attachable_type' => 'App\Ticket', | |
'filename' => $record, | |
]); | |
} | |
} | |
} | |
$ticket->contacts()->attach($contact->id); | |
$analysis['contact'] = $contact->name; | |
$analysis['customer'] = $contact->customer->name; | |
$analysis['isNewTicket'] = true; | |
$analysis['isReplyToExistingTicket'] = false; | |
$analysis['ticket_id'] = $ticket->id; | |
} | |
} | |
$incomingMail = IncomingMail::create([ | |
'sender' => $request->sender, | |
'recipient' => $request->recipient, | |
'subject' => $request->subject, | |
'body' => $body, | |
]); | |
$incomingMail->ticket_id = $ticket->id ?? null; | |
$incomingMail->analysis = json_encode($analysis, JSON_PRETTY_PRINT); | |
$incomingMail->save(); | |
return response()->json(['store' => 'ok'], 200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment