Skip to content

Commit

Permalink
add default catch to the fetch resources from credentials job
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkregel committed Feb 4, 2024
1 parent 14ddc63 commit af4e44b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 72 deletions.
3 changes: 3 additions & 0 deletions app/Jobs/FetchResourcesFromCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class FetchResourcesFromCredentials implements ShouldQueue
{
Expand Down Expand Up @@ -49,6 +50,8 @@ public function handle(Dispatcher $dispatcher)
Credential::TYPE_SERVER => new FetchServersForCredential($credential),
Credential::TYPE_DEVELOPMENT, 'forge' => new LaravelForgeServersSyncJob($credential),
Credential::TYPE_FINANCE => new SyncPlaidTransactionsJob($credential, now()->subWeek(), now(), false),
Crtdential::TYPE_EMAIL => new
default => Log::error(sprintf('Found unsupported credential type for FetchResourcesFromCredentialsJob: %s', $credential->type), []),
});
}
}
Expand Down
140 changes: 68 additions & 72 deletions app/Jobs/SyncMailboxIfCredentialsAreSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,92 +24,88 @@ class SyncMailboxIfCredentialsAreSet implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
protected Credential $credential,
protected ?Person $me = null,
protected ?Carbon $since = null,
) {
$this->me = Person::first();
$this->since = now()->subDay();
}

public function handle(CredentialRepositoryContract $credentialRepository, ImapFactoryService $imapFactory): void
public function handle(ImapFactoryService $imapFactory): void
{
$limit = 10;
$page = 1;

do {
$credentials = $credentialRepository->findAllOfType(Credential::TYPE_EMAIL, $limit, $page++);

foreach ($credentials->items() as $credential) {
$imapService = $imapFactory->make($credential);

info('Imap service seems to have credentials, trying to access inbox');
$start = now();
$messages = $imapService->findAllFromDate('INBOX', $this->since);
$end = now();

info('Found '.count($messages).' messages in '.$start->diffInSeconds($end).' seconds');

foreach ($messages as $i => $message) {
$trackedMessage = Message::query()->firstWhere([
'type' => 'email',
'event_id' => $message['id'],
]);

if (empty($trackedMessage)) {
$body = $imapService->findMessage((string) $message['id']);
$trackedMessage = Message::create([
'from_person' => $this->getPersonFromEmail($message),
'from_email' => (empty($message['from']['email']) ? null : $message['from']['email']) ?? $message['addressed-from']['email'] ?? null,
'to_email' => (empty($message['to']['email']) ? null : $message['to']['email']) ?? $message['addressed-to']['email'] ?? null,
'type' => 'email',
'event_id' => $message['id'],
'originated_at' => $message['date'],
'subject' => $body['subject'],
'is_decrypted' => true,
'message' => $body['body'],
'html_message' => $body['view'],
'seen' => $body['seen'],
'spam' => $body['spam'],
'answered' => $body['answered'],
]);
} else {
$body = $imapService->findMessage((string) $message['id']);
collect([
'originated_at' => $message['date'],
'is_decrypted' => true,
'message' => $body['body'],
'html_message' => $body['view'],
'from_email' => (empty($message['from']['email']) ? null : $message['from']['email']) ?? $message['addressed-from']['email'] ?? null,
'to_email' => (empty($message['to']['email']) ? null : $message['to']['email']) ?? $message['addressed-to']['email'] ?? null,
'subject' => $body['subject'],
'seen' => $body['seen'],
'spam' => $body['spam'],
'answered' => $body['answered'],
])->map(function ($value, $key) use ($trackedMessage) {
if ($value !== $trackedMessage->$key) {
$trackedMessage->$key = $value;
}
});

if ($trackedMessage->isDirty([
'originated_at',
'is_decrypted',
'message',
'html_message',
'seen',
'spam',
'answered',
])) {
$this->getPersonToEmail($message);
$this->getPersonFromEmail($message);
$trackedMessage->save();
}
}

info('Processed '.$i.'/'.count($messages));
$imapService = $imapFactory->make($this->credential);

info('Imap service seems to have credentials, trying to access inbox');
$start = now();
$messages = $imapService->findAllFromDate('INBOX', $this->since);
$end = now();

info('Found '.count($messages).' messages in '.$start->diffInSeconds($end).' seconds');

foreach ($messages as $i => $message) {
$trackedMessage = Message::query()->firstWhere([
'type' => 'email',
'event_id' => $message['id'],
]);

if (empty($trackedMessage)) {
$body = $imapService->findMessage((string) $message['id']);
$trackedMessage = Message::create([
'from_person' => $this->getPersonFromEmail($message),
'from_email' => (empty($message['from']['email']) ? null : $message['from']['email']) ?? $message['addressed-from']['email'] ?? null,
'to_email' => (empty($message['to']['email']) ? null : $message['to']['email']) ?? $message['addressed-to']['email'] ?? null,
'type' => 'email',
'event_id' => $message['id'],
'originated_at' => $message['date'],
'subject' => $body['subject'],
'is_decrypted' => true,
'message' => $body['body'],
'html_message' => $body['view'],
'seen' => $body['seen'],
'spam' => $body['spam'],
'answered' => $body['answered'],
]);
} else {
$body = $imapService->findMessage((string) $message['id']);
collect([
'originated_at' => $message['date'],
'is_decrypted' => true,
'message' => $body['body'],
'html_message' => $body['view'],
'from_email' => (empty($message['from']['email']) ? null : $message['from']['email']) ?? $message['addressed-from']['email'] ?? null,
'to_email' => (empty($message['to']['email']) ? null : $message['to']['email']) ?? $message['addressed-to']['email'] ?? null,
'subject' => $body['subject'],
'seen' => $body['seen'],
'spam' => $body['spam'],
'answered' => $body['answered'],
])->map(function ($value, $key) use ($trackedMessage) {
if ($value !== $trackedMessage->$key) {
$trackedMessage->$key = $value;
}
});

if ($trackedMessage->isDirty([
'originated_at',
'is_decrypted',
'message',
'html_message',
'seen',
'spam',
'answered',
])) {
$this->getPersonToEmail($message);
$this->getPersonFromEmail($message);
$trackedMessage->save();
}
}
} while ($credentials->hasMorePages());

info('Processed '.$i.'/'.count($messages));
}

}

Expand Down

0 comments on commit af4e44b

Please sign in to comment.