-
Notifications
You must be signed in to change notification settings - Fork 0
/
EloquentStockNotificationRepository.php
63 lines (56 loc) · 2.17 KB
/
EloquentStockNotificationRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Src\Infrastructure\repositories\Eloquent;
use App\Models\LowStockNotification;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\UuidInterface;
use Src\Application\ports\infrastructure\repositories\StockNotificationRepository;
use Src\Infrastructure\types\LowStockNotificationType;
class EloquentStockNotificationRepository implements StockNotificationRepository
{
public function save(array $notifications): void
{
$dataToInsert = [];
foreach ($notifications as $notification) {
$dataToInsert[] = [
'notification_id' => $notification["notification_id"],
'ingredient_id' => $notification["ingredient_id"],
"status" => $notification["status"],
];
}
LowStockNotification::factory()->createMany($dataToInsert);
}
/**
* @return array<string>
*/
public function getStuckNotifications(): array
{
DB::beginTransaction();
$notificationIds = LowStockNotification::query()
->where("status", LowStockNotificationType::PENDING)
->where('updated_at', '<=', Carbon::now('UTC')->subMinutes(30))
->pluck('notification_id')
->toArray();
if (count($notificationIds)) {
LowStockNotification::query()
->whereIn('notification_id', $notificationIds)
->where("status", LowStockNotificationType::PENDING)
->update(['updated_at' => Carbon::now('UTC')]);
}
DB::commit();
return $notificationIds;
}
public function getPendingWithIds($notificationIds): array
{
return LowStockNotification::with(['ingredientStock', 'ingredientStock.merchant'])
->whereIn('notification_id', $notificationIds)
->where('status', LowStockNotificationType::PENDING)
->get()->all();
}
public function markSent(array $notificationIds): void
{
LowStockNotification::query()->whereIn('notification_id', $notificationIds)
->where('status', LowStockNotificationType::PENDING)
->update(['status' => LowStockNotificationType::SENT]);
}
}