Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| PortalProgressCalculator | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| refresh | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\ClientPortal\Application\Services; |
| 6 | |
| 7 | use ImovelZapAi\ClientPortal\Domain\Enums\PortalStatus; |
| 8 | use ImovelZapAi\ClientPortal\Domain\Enums\RequestedDocumentStatus; |
| 9 | use ImovelZapAi\ClientPortal\Infrastructure\Persistence\Eloquent\ClientPortalModel; |
| 10 | use ImovelZapAi\ClientPortal\Infrastructure\Persistence\Eloquent\RequestedDocumentModel; |
| 11 | |
| 12 | final class PortalProgressCalculator |
| 13 | { |
| 14 | public function refresh(ClientPortalModel $portal): ClientPortalModel |
| 15 | { |
| 16 | $docs = RequestedDocumentModel::query() |
| 17 | ->where('client_portal_id', $portal->id) |
| 18 | ->get(); |
| 19 | |
| 20 | $total = $docs->count(); |
| 21 | $approved = $docs->where('status', RequestedDocumentStatus::Approved)->count(); |
| 22 | $submitted = $docs->filter( |
| 23 | fn (RequestedDocumentModel $d) => $d->status->countsAsSubmitted() |
| 24 | )->count(); |
| 25 | |
| 26 | $portal->forceFill([ |
| 27 | 'documents_total' => $total, |
| 28 | 'documents_approved' => $approved, |
| 29 | 'documents_submitted' => $submitted, |
| 30 | 'last_activity_at' => now(), |
| 31 | ]); |
| 32 | |
| 33 | if ($total > 0 && $approved === $total && $portal->status === PortalStatus::Active) { |
| 34 | $portal->forceFill([ |
| 35 | 'status' => PortalStatus::Completed, |
| 36 | 'completed_at' => now(), |
| 37 | ]); |
| 38 | } |
| 39 | |
| 40 | $portal->save(); |
| 41 | |
| 42 | return $portal->fresh(); |
| 43 | } |
| 44 | } |