photum/backend/internal/logistica/service.go
NANDO9322 804a566095 feat(ops): implementa modulo operacional completo (escala, logistica, equipe)
- Backend: Migrations para tabelas 'escalas' e 'logistica' (transporte)
- Backend: Handlers e Services completos para gestão de escalas e logística
- Backend: Suporte a auth vinculado a perfil profissional
- Frontend: Nova página de Detalhes Operacionais (/agenda/:id)
- Frontend: Componente EventScheduler com verificação robusta de conflitos
- Frontend: Componente EventLogistics para gestão de motoristas e caronas
- Frontend: Modal de Detalhes de Profissional unificado (Admin + Self-view)
- Frontend: Dashboard com modal de gestão de equipe e filtros avançados
- Fix: Correção crítica de timezone (UTC) em horários de agendamento
- Fix: Tratamento de URLs no campo de local do evento
- Fix: Filtros de profissional com carro na logística
2025-12-29 16:01:17 -03:00

158 lines
4.3 KiB
Go

package logistica
import (
"context"
"photum-backend/internal/db/generated"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
type Service struct {
queries *generated.Queries
}
func NewService(queries *generated.Queries) *Service {
return &Service{queries: queries}
}
type CreateCarroInput struct {
AgendaID string `json:"agenda_id"`
MotoristaID *string `json:"motorista_id"`
NomeMotorista *string `json:"nome_motorista"`
HorarioChegada *string `json:"horario_chegada"`
Observacoes *string `json:"observacoes"`
}
func (s *Service) CreateCarro(ctx context.Context, input CreateCarroInput) (*generated.LogisticaCarro, error) {
agendaID, err := uuid.Parse(input.AgendaID)
if err != nil {
return nil, err
}
params := generated.CreateCarroParams{
AgendaID: pgtype.UUID{Bytes: agendaID, Valid: true},
}
if input.MotoristaID != nil && *input.MotoristaID != "" {
if parsed, err := uuid.Parse(*input.MotoristaID); err == nil {
params.MotoristaID = pgtype.UUID{Bytes: parsed, Valid: true}
}
}
if input.NomeMotorista != nil {
params.NomeMotorista = pgtype.Text{String: *input.NomeMotorista, Valid: true}
}
if input.HorarioChegada != nil {
params.HorarioChegada = pgtype.Text{String: *input.HorarioChegada, Valid: true}
}
if input.Observacoes != nil {
params.Observacoes = pgtype.Text{String: *input.Observacoes, Valid: true}
}
carro, err := s.queries.CreateCarro(ctx, params)
if err != nil {
return nil, err
}
return &carro, nil
}
func (s *Service) ListCarros(ctx context.Context, agendaID string) ([]generated.ListCarrosByAgendaIDRow, error) {
parsedUUID, err := uuid.Parse(agendaID)
if err != nil {
return nil, err
}
return s.queries.ListCarrosByAgendaID(ctx, pgtype.UUID{Bytes: parsedUUID, Valid: true})
}
func (s *Service) DeleteCarro(ctx context.Context, id string) error {
parsedUUID, err := uuid.Parse(id)
if err != nil {
return err
}
return s.queries.DeleteCarro(ctx, pgtype.UUID{Bytes: parsedUUID, Valid: true})
}
// UpdateCarroInput matches the update fields
type UpdateCarroInput struct {
MotoristaID *string `json:"motorista_id"`
NomeMotorista *string `json:"nome_motorista"`
HorarioChegada *string `json:"horario_chegada"`
Observacoes *string `json:"observacoes"`
}
func (s *Service) UpdateCarro(ctx context.Context, id string, input UpdateCarroInput) (*generated.LogisticaCarro, error) {
parsedUUID, err := uuid.Parse(id)
if err != nil {
return nil, err
}
params := generated.UpdateCarroParams{
ID: pgtype.UUID{Bytes: parsedUUID, Valid: true},
}
if input.MotoristaID != nil {
if *input.MotoristaID == "" {
params.MotoristaID = pgtype.UUID{Valid: false}
} else {
if parsed, err := uuid.Parse(*input.MotoristaID); err == nil {
params.MotoristaID = pgtype.UUID{Bytes: parsed, Valid: true}
}
}
}
if input.NomeMotorista != nil {
params.NomeMotorista = pgtype.Text{String: *input.NomeMotorista, Valid: true}
}
if input.HorarioChegada != nil {
params.HorarioChegada = pgtype.Text{String: *input.HorarioChegada, Valid: true}
}
if input.Observacoes != nil {
params.Observacoes = pgtype.Text{String: *input.Observacoes, Valid: true}
}
carro, err := s.queries.UpdateCarro(ctx, params)
if err != nil {
return nil, err
}
return &carro, nil
}
func (s *Service) AddPassageiro(ctx context.Context, carroID, profissionalID string) error {
cID, err := uuid.Parse(carroID)
if err != nil {
return err
}
pID, err := uuid.Parse(profissionalID)
if err != nil {
return err
}
_, err = s.queries.AddPassageiro(ctx, generated.AddPassageiroParams{
CarroID: pgtype.UUID{Bytes: cID, Valid: true},
ProfissionalID: pgtype.UUID{Bytes: pID, Valid: true},
})
return err
}
func (s *Service) RemovePassageiro(ctx context.Context, carroID, profissionalID string) error {
cID, err := uuid.Parse(carroID)
if err != nil {
return err
}
pID, err := uuid.Parse(profissionalID)
if err != nil {
return err
}
return s.queries.RemovePassageiro(ctx, generated.RemovePassageiroParams{
CarroID: pgtype.UUID{Bytes: cID, Valid: true},
ProfissionalID: pgtype.UUID{Bytes: pID, Valid: true},
})
}
func (s *Service) ListPassageiros(ctx context.Context, carroID string) ([]generated.ListPassageirosByCarroIDRow, error) {
cID, err := uuid.Parse(carroID)
if err != nil {
return nil, err
}
return s.queries.ListPassageirosByCarroID(ctx, pgtype.UUID{Bytes: cID, Valid: true})
}