Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
GetSignatureDashboardQuery
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 execute
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Signatures\Application\Queries;
6
7use ImovelZapAi\Signatures\Domain\Enums\SignatureRequestStatus;
8use ImovelZapAi\Signatures\Domain\Enums\SignerStatus;
9use ImovelZapAi\Signatures\Infrastructure\Persistence\Eloquent\SignatureRequestModel;
10use ImovelZapAi\Signatures\Infrastructure\Persistence\Eloquent\SignerModel;
11
12final class GetSignatureDashboardQuery
13{
14    /** @return array<string, mixed> */
15    public function execute(string $tenantId): array
16    {
17        $awaiting = SignatureRequestModel::query()
18            ->where('tenant_id', $tenantId)
19            ->whereIn('status', [
20                SignatureRequestStatus::AwaitingSignatures->value,
21                SignatureRequestStatus::PartiallySigned->value,
22            ])
23            ->count();
24
25        $pendingSigners = SignerModel::query()
26            ->where('tenant_id', $tenantId)
27            ->whereIn('status', [
28                SignerStatus::Pending->value,
29                SignerStatus::Notified->value,
30                SignerStatus::Viewed->value,
31            ])
32            ->whereHas('request', fn ($q) => $q->where('tenant_id', $tenantId)->whereIn('status', [
33                SignatureRequestStatus::AwaitingSignatures->value,
34                SignatureRequestStatus::PartiallySigned->value,
35            ]))
36            ->count();
37
38        $completed = SignatureRequestModel::query()
39            ->where('tenant_id', $tenantId)
40            ->where('status', SignatureRequestStatus::Signed)
41            ->count();
42
43        $expired = SignatureRequestModel::query()
44            ->where('tenant_id', $tenantId)
45            ->where(function ($q): void {
46                $q->where('status', SignatureRequestStatus::Expired)
47                    ->orWhere(function ($q2): void {
48                        $q2->whereIn('status', [
49                            SignatureRequestStatus::AwaitingSignatures->value,
50                            SignatureRequestStatus::PartiallySigned->value,
51                            SignatureRequestStatus::Draft->value,
52                        ])
53                            ->whereNotNull('expires_at')
54                            ->where('expires_at', '<', now());
55                    });
56            })
57            ->count();
58
59        $avgHours = SignatureRequestModel::query()
60            ->where('tenant_id', $tenantId)
61            ->where('status', SignatureRequestStatus::Signed)
62            ->whereNotNull('sent_at')
63            ->whereNotNull('completed_at')
64            ->get(['sent_at', 'completed_at'])
65            ->avg(fn (SignatureRequestModel $request) => $request->sent_at->diffInMinutes($request->completed_at) / 60);
66
67        return [
68            'awaiting' => $awaiting,
69            'pending_signers' => $pendingSigners,
70            'avg_hours' => $avgHours !== null ? round((float) $avgHours, 1) : null,
71            'completed' => $completed,
72            'expired' => $expired,
73        ];
74    }
75}