Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #481 from Agroarca/i480
Browse files Browse the repository at this point in the history
desenvolvimento notificações admin
  • Loading branch information
eduardopilati authored Sep 12, 2023
2 parents f4badb6 + 96f3fd5 commit 6a6d908
Show file tree
Hide file tree
Showing 32 changed files with 1,060 additions and 9 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

[Diretrizes de Programação](https://marbled-sky-376.notion.site/Diretrizes-de-programa-o-b33a13e75d074dffbd73a9a7b2c39be0)


## Documentações

[Laravel](https://laravel.com/docs/10.x)
Expand Down Expand Up @@ -43,5 +42,5 @@ php artisan migrate:rollback
Checks antes do PR

```
npm run build && npm run eslint && vendor/bin/phpcs && ./container.sh php artisan test
npm run build && npm run eslint && ./container.sh vendor/bin/phpcs && ./container.sh php artisan test
```
21 changes: 21 additions & 0 deletions app/Enums/Administracao/Notificacoes/TipoNotificacaoEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Enums\Administracao\Notificacoes;

use App\Enums\EnumToArray;

enum TipoNotificacaoEnum: int
{
use EnumToArray;

case Placeholder = 0;
case ProdutoEnviadoAprovacao = 1;

public static function arrayNomes(): array
{
return [
self::Placeholder->value => 'Placeholder',
self::ProdutoEnviadoAprovacao->value => 'Produto Enviado para Aprovação',
];
}
}
8 changes: 8 additions & 0 deletions app/Enums/Usuario/TipoPessoa.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ enum TipoPessoa: int
{
case Fisica = 0;
case Juridica = 1;

public static function arrayNomes(): array
{
return [
self::Fisica->value => 'Física',
self::Juridica->value => 'Jurídica',
];
}
}
4 changes: 4 additions & 0 deletions app/Enums/Usuario/TipoUsuario.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace App\Enums\Usuario;

use App\Enums\EnumToArray;

enum TipoUsuario: int
{
use EnumToArray;

case Normal = 0;
case Admin = 1;
}
18 changes: 18 additions & 0 deletions app/Events/Equipamento/Cadastro/EquipamentoSaved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Events\Equipamento\Cadastro;

use App\Models\Equipamentos\Cadastro\Equipamento;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class EquipamentoSaved
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(
public Equipamento $equipamento
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Http\Controllers\Admin\Administracao\Usuarios;

use App\Enums\Usuario\TipoPessoa;
use App\Enums\Usuario\TipoUsuario;
use App\Http\Controllers\Controller;
use App\Models\Usuario;
use App\Services\Notificacoes\NotificacaoAdminService;
use Illuminate\Http\Request;
use Inertia\Inertia;

class UsuarioController extends Controller
{
public function __construct(
private NotificacaoAdminService $notAdmService
) {
}

public function inicio(): mixed
{
$usuarios = Usuario::paginate();
$usuarios->setCollection($usuarios->getCollection()->makeVisible(['cpf', 'cnpj']));

$tipoPessoaEnum = TipoPessoa::arrayNomes();
return Inertia::render('Admin/Administracao/Usuarios/Inicio', compact('usuarios', 'tipoPessoaEnum'));
}

public function visualizar(Usuario $usuario): mixed
{
$usuario->makeVisible(['cpf', 'cnpj', 'celular']);
$usuario->load('grupos');

$tipoPessoaEnum = TipoPessoa::arrayNomes();
$tipoUsuarioEnum = TipoUsuario::toArray();

$preferenciasNotificacao = [];
if ($usuario->tipo_usuario == TipoUsuario::Admin) {
$preferenciasNotificacao = $this->notAdmService->getPreferenciasNotificacaoUsuario($usuario);
}

return Inertia::render('Admin/Administracao/Usuarios/Visualizar', [
'usuario' => $usuario,
'tipoPessoaEnum' => $tipoPessoaEnum,
'tipoUsuarioEnum' => $tipoUsuarioEnum,
'preferenciasNotificacao' => (array) $preferenciasNotificacao,
]);
}

public function salvarPreferencias(Request $request, Usuario $usuario)
{
$this->notAdmService->salvarPreferenciasNotificacaoUsuario($usuario, $request->all());
return redirect()->route('admin.administracao.usuarios.visualizar', $usuario->id);
}
}
41 changes: 41 additions & 0 deletions app/Listeners/Equipamentos/Cadastro/EquipamentoSavedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Listeners\Equipamentos\Cadastro;

use App\Enums\Equipamentos\Cadastro\StatusEquipamento;
use App\Events\Equipamento\Cadastro\EquipamentoSaved;
use App\Models\Equipamentos\Cadastro\Equipamento;
use App\Notifications\Equipamentos\Cadastro\EquipamentoAprovadoNotification;
use App\Notifications\Equipamentos\Cadastro\EquipamentoReprovadoNotification;
use App\Services\Notificacoes\NotificacaoAdminService;
use Illuminate\Support\Facades\Notification;

class EquipamentoSavedListener
{
/**
* Handle the event.
*/
public function handle(EquipamentoSaved $event): void
{
$this->handleStatus($event->equipamento);
}

private function handleStatus(Equipamento $equipamento): void
{
if (!$equipamento->isDirty('status')) {
return;
}

if ($equipamento->status == StatusEquipamento::Aprovado) {
Notification::send($equipamento->usuario, new EquipamentoAprovadoNotification($equipamento));
}

if ($equipamento->status == StatusEquipamento::Reprovado) {
Notification::send($equipamento->usuario, new EquipamentoReprovadoNotification($equipamento));
}

if ($equipamento->status == StatusEquipamento::Criado) {
app(NotificacaoAdminService::class)->notificarEquipamentoEnviadoAprovacao();
}
}
}
58 changes: 58 additions & 0 deletions app/Mail/Equipamentos/Cadastro/EquipamentoAprovado.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Mail\Equipamentos\Cadastro;

use App\Models\Equipamentos\Cadastro\Equipamento;
use App\Models\Usuario;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class EquipamentoAprovado extends Mailable
{
use Queueable, SerializesModels;

public function __construct(
private Usuario $usuario,
private Equipamento $equipamento
) {
}

public function envelope(): Envelope
{
return new Envelope(
from: new Address(
address: config('mail.from.address'),
name: config('mail.from.name')
),
to: [
new Address(
address: $this->usuario->email,
name: $this->usuario->nome
)
],
subject: 'Equipamento aprovado'
);
}

public function content(): Content
{
return new Content(
view: 'equipamentos.cadastro.equipamento-aprovado',
text: 'equipamentos.cadastro.equipamento-aprovado-text',
with: [
'nome' => $this->usuario->nome,
'nomeEquipamento' => $this->equipamento->titulo,
'url' => url()->route('site.equipamento', $this->equipamento->id),
]
);
}

public function attachments(): array
{
return [];
}
}
55 changes: 55 additions & 0 deletions app/Mail/Equipamentos/Cadastro/EquipamentoEnviadoAprovacao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Mail\Equipamentos\Cadastro;

use App\Models\Usuario;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class EquipamentoEnviadoAprovacao extends Mailable
{
use Queueable, SerializesModels;

public function __construct(
private Usuario $usuario
) {
}

public function envelope(): Envelope
{
return new Envelope(
from: new Address(
address: config('mail.from.address'),
name: config('mail.from.name')
),
to: [
new Address(
address: $this->usuario->email,
name: $this->usuario->nome
)
],
subject: 'Equipamento enviado para aprovação'
);
}

public function content(): Content
{
return new Content(
view: 'equipamentos.cadastro.equipamento-enviado-aprovacao',
text: 'equipamentos.cadastro.equipamento-enviado-aprovacao-text',
with: [
'nome' => $this->usuario->nome,
'url' => url()->route('admin.equipamentos'),
]
);
}

public function attachments(): array
{
return [];
}
}
58 changes: 58 additions & 0 deletions app/Mail/Equipamentos/Cadastro/EquipamentoReprovado.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Mail\Equipamentos\Cadastro;

use App\Models\Equipamentos\Cadastro\Equipamento;
use App\Models\Usuario;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class EquipamentoReprovado extends Mailable
{
use Queueable, SerializesModels;

public function __construct(
private Usuario $usuario,
public Equipamento $equipamento
) {
}

public function envelope(): Envelope
{
return new Envelope(
from: new Address(
address: config('mail.from.address'),
name: config('mail.from.name')
),
to: [
new Address(
address: $this->usuario->email,
name: $this->usuario->nome
)
],
subject: 'Equipamento em revisão'
);
}

public function content(): Content
{
return new Content(
view: 'equipamentos.cadastro.equipamento-reprovado',
text: 'equipamentos.cadastro.equipamento-reprovado-text',
with: [
'nome' => $this->usuario->nome,
'nomeEquipamento' => $this->equipamento->titulo,
'url' => url()->route('site.equipamento.editar', $this->equipamento->id),
]
);
}

public function attachments(): array
{
return [];
}
}
23 changes: 23 additions & 0 deletions app/Models/Administracao/Notificacoes/NotificacaoAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models\Administracao\Notificacoes;

use App\Enums\Administracao\Notificacoes\TipoNotificacaoEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class NotificacaoAdmin extends Model
{
use HasFactory;

protected $table = 'admininistracao_notificacao_admins';

protected $fillable = [
'usuario_id',
'tipo',
];

protected $casts = [
'tipo' => TipoNotificacaoEnum::class,
];
}
5 changes: 5 additions & 0 deletions app/Models/Equipamentos/Cadastro/Equipamento.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models\Equipamentos\Cadastro;

use App\Enums\Equipamentos\Cadastro\StatusEquipamento;
use App\Events\Equipamento\Cadastro\EquipamentoSaved;
use App\Models\Cadastro\Cidade;
use App\Models\Equipamentos\Caracteristicas\CaracteristicaEquipamento;
use App\Models\Equipamentos\Conversas\EquipamentoConversa;
Expand Down Expand Up @@ -37,6 +38,10 @@ class Equipamento extends Model
'status' => StatusEquipamento::class,
];

protected $dispatchesEvents = [
'saved' => EquipamentoSaved::class,
];

public function scopeAprovado(Builder $query): Builder
{
return $query->where('status', StatusEquipamento::Aprovado);
Expand Down
Loading

0 comments on commit 6a6d908

Please sign in to comment.