Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| DefaultProposalTemplateSeeder | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| ensureForTenant | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
4 | |||
| defaultBody | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Proposals\Application\Services; |
| 6 | |
| 7 | use App\Models\User; |
| 8 | use ImovelZapAi\Proposals\Domain\Enums\ProposalPurpose; |
| 9 | use ImovelZapAi\Proposals\Infrastructure\Persistence\Eloquent\ProposalTemplateModel; |
| 10 | |
| 11 | final class DefaultProposalTemplateSeeder |
| 12 | { |
| 13 | public function ensureForTenant(string $tenantId, ?User $actor = null): ProposalTemplateModel |
| 14 | { |
| 15 | $defaults = [ |
| 16 | ProposalPurpose::Sale->value => 'Proposta de venda padrão', |
| 17 | ProposalPurpose::Rent->value => 'Proposta de locação padrão', |
| 18 | ProposalPurpose::Listing->value => 'Proposta de captação padrão', |
| 19 | ]; |
| 20 | |
| 21 | $sale = null; |
| 22 | foreach ($defaults as $purpose => $name) { |
| 23 | $row = ProposalTemplateModel::query() |
| 24 | ->where('tenant_id', $tenantId) |
| 25 | ->where('purpose', $purpose) |
| 26 | ->where('is_default', true) |
| 27 | ->first(); |
| 28 | |
| 29 | if ($row === null) { |
| 30 | $row = ProposalTemplateModel::query()->create([ |
| 31 | 'tenant_id' => $tenantId, |
| 32 | 'name' => $name, |
| 33 | 'purpose' => $purpose, |
| 34 | 'is_default' => true, |
| 35 | 'created_by' => $actor?->id, |
| 36 | 'body_html' => $this->defaultBody(), |
| 37 | ]); |
| 38 | } |
| 39 | |
| 40 | if ($purpose === ProposalPurpose::Sale->value) { |
| 41 | $sale = $row; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return $sale ?? ProposalTemplateModel::query() |
| 46 | ->where('tenant_id', $tenantId) |
| 47 | ->where('is_default', true) |
| 48 | ->firstOrFail(); |
| 49 | } |
| 50 | |
| 51 | public function defaultBody(): string |
| 52 | { |
| 53 | return <<<'HTML' |
| 54 | <section> |
| 55 | <h1>Proposta comercial</h1> |
| 56 | <p>Olá <strong>{{client_name}}</strong>,</p> |
| 57 | <p>Segue proposta referente ao imóvel em <strong>{{property_address}}</strong>.</p> |
| 58 | <ul> |
| 59 | <li>Valor: <strong>{{amount}}</strong></li> |
| 60 | <li>Comissão: <strong>{{commission}}</strong></li> |
| 61 | <li>Características: {{property_features}}</li> |
| 62 | <li>Corretor: {{broker_name}}</li> |
| 63 | </ul> |
| 64 | <p>{{intro_message}}</p> |
| 65 | <p><em>{{company_name}}</em></p> |
| 66 | </section> |
| 67 | HTML; |
| 68 | } |
| 69 | } |