diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index db90d3ca05c0..1dfff875d34d 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -14,6 +14,8 @@ namespace App\Http\Controllers; use App\Utils\Ninja; use App\Models\Client; use App\Models\Account; +use App\Models\Company; +use App\Models\SystemLog; use Postmark\PostmarkClient; use Illuminate\Http\Response; use App\Factory\ClientFactory; @@ -38,6 +40,7 @@ use App\Http\Requests\Client\UpdateClientRequest; use App\Http\Requests\Client\UploadClientRequest; use App\Http\Requests\Client\DestroyClientRequest; use App\Http\Requests\Client\ReactivateClientEmailRequest; +use App\Jobs\PostMark\ProcessPostmarkWebhook; /** * Class ClientController. @@ -221,7 +224,7 @@ class ClientController extends BaseController } }); - return $this->listResponse(Client::withTrashed()->company()->whereIn('id', $request->ids)); + return $this->listResponse(Client::query()->withTrashed()->company()->whereIn('id', $request->ids)); } /** @@ -320,12 +323,42 @@ class ClientController extends BaseController * Reactivate a client email * * @param ReactivateClientEmailRequest $request - * @param string $bounce_id + * @param string $bounce_id //could also be the invitationId * @return \Illuminate\Http\JsonResponse */ public function reactivateEmail(ReactivateClientEmailRequest $request, string $bounce_id) { - + /** @var \App\Models\User $user */ + $user = auth()->user(); + + if(stripos($bounce_id, '-') !== false){ + $log = + SystemLog::query() + ->where('company_id', $user->company()->id) + ->where('type_id', SystemLog::TYPE_WEBHOOK_RESPONSE) + ->where('category_id', SystemLog::CATEGORY_MAIL) + ->whereJsonContains('log', ['MessageID' => $bounce_id]) + ->orderBy('id', 'desc') + ->first(); + + $resolved_bounce_id = false; + + if($log && ($log?->log['ID'] ?? false)){ + $resolved_bounce_id = $log->log['ID'] ?? false; + } + + if(!$resolved_bounce_id){ + $ppwebhook = new ProcessPostmarkWebhook([]); + $resolved_bounce_id = $ppwebhook->getBounceId($bounce_id); + } + + if(!$resolved_bounce_id){ + return response()->json(['message' => 'Bounce ID not found'], 400); + } + + $bounce_id = $resolved_bounce_id; + } + $postmark = new PostmarkClient(config('services.postmark.token')); try { diff --git a/app/Jobs/PostMark/ProcessPostmarkWebhook.php b/app/Jobs/PostMark/ProcessPostmarkWebhook.php index 35a90f79b340..25f2d57c8cda 100644 --- a/app/Jobs/PostMark/ProcessPostmarkWebhook.php +++ b/app/Jobs/PostMark/ProcessPostmarkWebhook.php @@ -339,6 +339,32 @@ class ProcessPostmarkWebhook implements ShouldQueue } } + public function getRawMessage(string $message_id) + { + + $postmark = new PostmarkClient(config('services.postmark.token')); + $messageDetail = $postmark->getOutboundMessageDetails($message_id); + return $messageDetail; + + } + + + public function getBounceId(string $message_id): ?int + { + + $messageDetail = $this->getRawMessage($message_id); + + + $event = collect($messageDetail->messageevents)->first(function ($event) { + + return $event?->Details?->BounceID ?? false; + + }); + + return $event?->Details?->BounceID ?? null; + + } + private function fetchMessage(): array { if(strlen($this->request['MessageID']) < 1){ diff --git a/app/Models/Client.php b/app/Models/Client.php index 2c06040cc671..a056a103493f 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -306,10 +306,7 @@ class Client extends BaseModel implements HasLocalePreference return $this->hasMany(ClientContact::class)->where('is_primary', true); } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ - public function company() + public function company(): BelongsTo { return $this->belongsTo(Company::class); } diff --git a/app/Models/Company.php b/app/Models/Company.php index 1de20b8d694b..8d097d3b8baa 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -786,6 +786,26 @@ class Company extends BaseModel return $this->hasMany(CompanyUser::class)->withTrashed(); } + public function invoice_invitations(): HasMany + { + return $this->hasMany(InvoiceInvitation::class); + } + + public function quote_invitations(): HasMany + { + return $this->hasMany(QuoteInvitation::class); + } + + public function credit_invitations(): HasMany + { + return $this->hasMany(CreditInvitation::class); + } + + public function purchase_order_invitations(): HasMany + { + return $this->hasMany(PurchaseOrderInvitation::class); + } + /** * @return \App\Models\User|null */ diff --git a/app/Transformers/CreditInvitationTransformer.php b/app/Transformers/CreditInvitationTransformer.php index 3be2395ae37f..ce53e3829d9c 100644 --- a/app/Transformers/CreditInvitationTransformer.php +++ b/app/Transformers/CreditInvitationTransformer.php @@ -33,6 +33,7 @@ class CreditInvitationTransformer extends EntityTransformer 'created_at' => (int) $invitation->created_at, 'email_status' => $invitation->email_status ?: '', 'email_error' => (string) $invitation->email_error, + 'message_id' => (string) $invitation->message_id ?: '', ]; } } diff --git a/app/Transformers/InvoiceInvitationTransformer.php b/app/Transformers/InvoiceInvitationTransformer.php index 9f7aaa582b0b..0caa6bb9990b 100644 --- a/app/Transformers/InvoiceInvitationTransformer.php +++ b/app/Transformers/InvoiceInvitationTransformer.php @@ -33,6 +33,7 @@ class InvoiceInvitationTransformer extends EntityTransformer 'created_at' => (int) $invitation->created_at, 'email_status' => $invitation->email_status ?: '', 'email_error' => (string) $invitation->email_error, + 'message_id' => (string) $invitation->message_id ?: '', ]; } } diff --git a/app/Transformers/PurchaseOrderInvitationTransformer.php b/app/Transformers/PurchaseOrderInvitationTransformer.php index 5a4474fbc0ca..0c3ad649af76 100644 --- a/app/Transformers/PurchaseOrderInvitationTransformer.php +++ b/app/Transformers/PurchaseOrderInvitationTransformer.php @@ -24,6 +24,7 @@ class PurchaseOrderInvitationTransformer extends EntityTransformer 'created_at' => (int) $invitation->created_at, 'email_status' => $invitation->email_status ?: '', 'email_error' => (string) $invitation->email_error, + 'message_id' => (string) $invitation->message_id ?: '', ]; } } diff --git a/app/Transformers/QuoteInvitationTransformer.php b/app/Transformers/QuoteInvitationTransformer.php index 02c84d841dc0..394e9339c90e 100644 --- a/app/Transformers/QuoteInvitationTransformer.php +++ b/app/Transformers/QuoteInvitationTransformer.php @@ -33,6 +33,7 @@ class QuoteInvitationTransformer extends EntityTransformer 'created_at' => (int) $invitation->created_at, 'email_status' => $invitation->email_status ?: '', 'email_error' => (string) $invitation->email_error, + 'message_id' => (string) $invitation->message_id ?: '', ]; } } diff --git a/app/Transformers/RecurringInvoiceInvitationTransformer.php b/app/Transformers/RecurringInvoiceInvitationTransformer.php index 7c7b31e8584d..6dc852e37e50 100644 --- a/app/Transformers/RecurringInvoiceInvitationTransformer.php +++ b/app/Transformers/RecurringInvoiceInvitationTransformer.php @@ -33,6 +33,7 @@ class RecurringInvoiceInvitationTransformer extends EntityTransformer 'created_at' => (int) $invitation->created_at, 'email_status' => $invitation->email_status ?: '', 'email_error' => (string) $invitation->email_error, + 'message_id' => (string) $invitation->message_id ?: '', ]; } }