Skip to content

Commit

Permalink
Job for updating mail deliveries
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Jan 21, 2025
1 parent 5ff4c33 commit 3720f6a
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 20 deletions.
37 changes: 20 additions & 17 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,32 @@

namespace App\Console;

use App\Utils\Ninja;
use App\Models\Account;
use App\Jobs\Ninja\QueueSize;
use App\Jobs\Util\DiskCleanup;
use App\Jobs\Util\ReminderJob;
use App\Jobs\Cron\AutoBillCron;
use App\Jobs\Cron\RecurringExpensesCron;
use App\Jobs\Cron\RecurringInvoicesCron;
use App\Jobs\Util\VersionCheck;
use App\Jobs\Ninja\TaskScheduler;
use App\Jobs\Util\SchedulerCheck;
use App\Jobs\Ninja\CheckACHStatus;
use App\Jobs\Cron\SubscriptionCron;
use App\Jobs\EDocument\EInvoicePullDocs;
use App\Jobs\Invoice\InvoiceCheckLateWebhook;
use App\Jobs\Ninja\MailWebhookSync;
use App\Jobs\Util\QuoteReminderJob;
use App\Jobs\Ninja\AdjustEmailQuota;
use App\Jobs\Ninja\BankTransactionSync;
use App\Jobs\Ninja\CheckACHStatus;
use App\Jobs\Ninja\CompanySizeCheck;
use App\Jobs\Ninja\QueueSize;
use App\Jobs\Ninja\SystemMaintenance;
use App\Jobs\Ninja\TaskScheduler;
use App\Jobs\Quote\QuoteCheckExpired;
use App\Jobs\Subscription\CleanStaleInvoiceOrder;
use App\Jobs\Util\DiskCleanup;
use App\Jobs\Util\QuoteReminderJob;
use App\Jobs\Util\ReminderJob;
use App\Jobs\Util\SchedulerCheck;
use App\Jobs\Util\UpdateExchangeRates;
use App\Jobs\Util\VersionCheck;
use App\Models\Account;
use App\PaymentDrivers\Rotessa\Jobs\TransactionReport;
use App\Utils\Ninja;
use App\Jobs\Ninja\BankTransactionSync;
use App\Jobs\Cron\RecurringExpensesCron;
use App\Jobs\Cron\RecurringInvoicesCron;
use App\Jobs\EDocument\EInvoicePullDocs;
use Illuminate\Console\Scheduling\Schedule;
use App\Jobs\Invoice\InvoiceCheckLateWebhook;
use App\Jobs\Subscription\CleanStaleInvoiceOrder;
use App\PaymentDrivers\Rotessa\Jobs\TransactionReport;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
Expand Down Expand Up @@ -121,6 +122,8 @@ protected function schedule(Schedule $schedule)
/* Checks ACH verification status and updates state to authorize when verified */
$schedule->job(new CheckACHStatus())->everySixHours()->withoutOverlapping()->name('ach-status-job')->onOneServer();

$schedule->job(new MailWebhookSync())->everyFourHours(rand(20, 45))->withoutOverlapping()->name('mail-webhook-sync-job')->onOneServer();

$schedule->command('ninja:check-data --database=db-ninja-01')->dailyAt('02:10')->withoutOverlapping()->name('check-data-db-1-job')->onOneServer();

$schedule->command('ninja:check-data --database=db-ninja-02')->dailyAt('02:20')->withoutOverlapping()->name('check-data-db-2-job')->onOneServer();
Expand Down
4 changes: 2 additions & 2 deletions app/Jobs/Invoice/ZipInvoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function handle(): void
$invitation = $this->invoices->first()->invitations->first();

if (!$invitation) {
nlog("no Invoice Invitations");
nlog("ZipInvoices:: no Invoice Invitations");
return;
}

Expand Down Expand Up @@ -118,7 +118,7 @@ public function handle(): void
broadcast(new DownloadAvailable($storage_url, $message, $this->user));

} catch (\PhpZip\Exception\ZipException $e) {
nlog('could not make zip => '.$e->getMessage());
nlog('ZipInvoices:: could not make zip => '.$e->getMessage());
} finally {
$zipFile->close();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/Ninja/AdjustEmailQuota.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function handle()
public function adjust()
{
Account::query()->cursor()->each(function ($account) {
nlog("resetting email quota for {$account->key}");
// nlog("resetting email quota for {$account->key}");

$email_count = Cache::get("email_quota".$account->key);

Expand Down
174 changes: 174 additions & 0 deletions app/Jobs/Ninja/MailWebhookSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/

namespace App\Jobs\Ninja;

use App\Utils\Ninja;
use App\Models\Account;
use App\Libraries\MultiDB;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use Illuminate\Queue\SerializesModels;
use Turbo124\Beacon\Facades\LightLogs;
use App\DataMapper\Analytics\EmailCount;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\Middleware\WithoutOverlapping;

class MailWebhookSync implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public $tries = 1;

public $deleteWhenMissingModels = true;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if (! Ninja::isHosted()) {
return;
}

/** Add to the logs any email deliveries that have not been sync'd */
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);

$this->scanSentEmails();
}
}

private function scanSentEmails()
{

$query = \App\Models\InvoiceInvitation::whereNotNull('message_id')
->whereNull('email_status')
->whereHas('company', function ($q) {
$q->where('settings->email_sending_method', 'default');
});

$this->runIterator($query);


$query = \App\Models\QuoteInvitation::whereNotNull('message_id')
->whereNull('email_status')
->whereHas('company', function ($q) {
$q->where('settings->email_sending_method', 'default');
});

$this->runIterator($query);


$query = \App\Models\RecurringInvoiceInvitation::whereNotNull('message_id')
->whereNull('email_status')
->whereHas('company', function ($q) {
$q->where('settings->email_sending_method', 'default');
});

$this->runIterator($query);


$query = \App\Models\CreditInvitation::whereNotNull('message_id')
->whereNull('email_status')
->whereHas('company', function ($q) {
$q->where('settings->email_sending_method', 'default');
});

$this->runIterator($query);


$query = \App\Models\PurchaseOrderInvitation::whereNotNull('message_id')
->whereNull('email_status')
->whereHas('company', function ($q) {
$q->where('settings->email_sending_method', 'default');
});

$this->runIterator($query);

}

private function runIterator($query)
{
$query->where('created_at' , '<', now()->subHours(1))
->each(function ($invite) {

$postmark = new \Postmark\PostmarkClient(config('services.postmark.token'));

try {
$messageDetail = $postmark->getOutboundMessageDetails($invite->message_id);
} catch (\Throwable $th) {
$postmark = new \Postmark\PostmarkClient(config('services.postmark-outlook.token'));
$messageDetail = $postmark->getOutboundMessageDetails($invite->message_id);
}

try {

if (!$messageDetail) {
return true;
}

$data = [
'RecordType' => 'Delivery',
'ServerID' => 23,
'MessageStream' => 'outbound',
'MessageID' => $invite->message_id,
'Recipient' => collect($messageDetail->recipients)->first(),
'Tag' => $invite->company->company_key,
'DeliveredAt' => '2025-01-01T16:34:52Z',
'Metadata' => [

]
];

(new \App\Jobs\PostMark\ProcessPostmarkWebhook($data))->handle();

$invite->sent_date = now();
$invite->save();

} catch (\Throwable $th) {
nlog("MailWebhookSync:: {$th->getMessage()}");
}

});

}

public function middleware()
{
return [new WithoutOverlapping('mail-webhook-sync')];
}

public function failed($exception)
{
nlog("MailWebhookSync:: Exception:: => ".$exception->getMessage());
config(['queue.failed.driver' => null]);
}
}
1 change: 1 addition & 0 deletions app/Listeners/Mail/MailSentListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function handle(MessageSent $event)
return;
}

$invitation->sent_date = now();
$invitation->message_id = str_replace(["<",">"], "", $message_id);
$invitation->save();
}
Expand Down

0 comments on commit 3720f6a

Please sign in to comment.