Correção de itens que estão fora do plano valores no dash principal

This commit is contained in:
2026-08-24 16:26:27 -03:00
parent 04b78bc7de
commit 94bbe7556f
10 changed files with 403 additions and 48 deletions

View File

@@ -6,11 +6,11 @@ RSpec.describe Analytics::NotasForaOperacao do
# Visitas já filtradas pelo SQL (NFs que não estão em nenhuma gade_entregas_*).
let(:visitas) do
[
row(nf: 100, status: 'failed', driver: 'Carlos', checkout: '2026-08-10 09:00:00', title: 'NF 100 - FULANO (Avulsa)'),
row(nf: 100, status: 'completed', driver: 'Carlos', checkout: '2026-08-12 09:00:00', title: 'NF 100 - FULANO (Avulsa)'),
row(nf: 101, status: 'completed', driver: 'Pedro', checkout: '2026-08-11 09:00:00', title: 'INCLUSÃO'),
row(nf: 102, status: 'failed', driver: 'Pedro', checkout: '2026-08-13 09:00:00', title: 'INCLUSÃO', obs: 'ÓBITO'),
row(nf: 103, status: 'pending', driver: 'Marcos', planned: '2026-08-20', title: nil)
row(nf: 100, status: 'failed', driver: 'Carlos', sts: 'STS PENHA', checkout: '2026-08-10 09:00:00'),
row(nf: 100, status: 'completed', driver: 'Carlos', sts: 'STS PENHA', checkout: '2026-08-12 09:00:00'),
row(nf: 101, status: 'completed', driver: 'Pedro', sts: 'STS PENHA', checkout: '2026-08-11 09:00:00'),
row(nf: 102, status: 'failed', driver: 'Pedro', sts: 'STS SAPOPEMBA', checkout: '2026-08-13 09:00:00', obs: 'ÓBITO'),
row(nf: 103, status: 'pending', driver: nil, sts: 'STS SAPOPEMBA', planned: '2026-08-20')
]
end
@@ -23,18 +23,55 @@ RSpec.describe Analytics::NotasForaOperacao do
expect(fora.pendentes).to eq(1)
end
it 'agrupa por plano de origem (Avulsa x INCLUSÃO)' do
expect(fora.por_plano).to contain_exactly(
{ nome: 'INCLUSÃO', total: 2 },
{ nome: 'NF 100 - FULANO (Avulsa)', total: 1 },
{ nome: 'SEM PLANO IDENTIFICADO', total: 1 }
)
end
it 'ordena a listagem da visita mais recente para a mais antiga' do
expect(fora.listagem.map { |r| r['reference_id'] }).to eq([102, 100, 101, 103])
end
it 'traduz o resultado da última visita' do
linha = fora.linhas.find { |r| r['reference_id'] == 103 }
expect(fora.resultado(linha)).to eq('Em aberto')
expect(fora.sucesso?(linha)).to be(false)
end
# Regressão do dado real (24/08/2026): `title` traz "NF 89096 - KAIQUE TAUAN DA
# SILVA" — o destinatário, não o plano. Agrupar por ele daria um grupo por NF.
context 'quando o espelho só tem `title` (destinatário)' do
let(:visitas) do
[
row(nf: 100, status: 'completed', driver: 'Thiago', sts: 'STS PENHA', checkout: '2026-08-12 09:00:00', title: 'NF 100 - KAIQUE TAUAN DA SILVA'),
row(nf: 101, status: 'pending', driver: nil, sts: 'STS PENHA', planned: '2026-08-03', title: 'NF 101 - MARIA APARECIDA')
]
end
it 'não trata o título como plano' do
expect(fora.plano(fora.linhas.first)).to be_nil
expect(fora.titulo(fora.linhas.first)).to start_with('NF ')
expect(fora.plano_identificado?).to be(false)
end
it 'cai para o agrupamento por unidade' do
rotulo, grupos = fora.agrupamento
expect(rotulo).to eq('Unidade')
expect(grupos).to eq([{ nome: 'STS PENHA', total: 2 }])
end
end
context 'quando o espelho traz o plano' do
let(:visitas) do
[
row(nf: 100, status: 'completed', driver: 'Carlos', sts: 'STS PENHA', checkout: '2026-08-12 09:00:00', notes: '(Avulsa)'),
row(nf: 101, status: 'completed', driver: 'Pedro', sts: 'STS PENHA', checkout: '2026-08-11 09:00:00', notes: 'INCLUSÃO'),
row(nf: 102, status: 'failed', driver: 'Pedro', sts: 'STS PENHA', checkout: '2026-08-13 09:00:00', notes: 'INCLUSÃO')
]
end
it 'separa (Avulsa) de INCLUSÃO' do
rotulo, grupos = fora.agrupamento
expect(rotulo).to eq('Plano')
expect(grupos).to eq([{ nome: 'INCLUSÃO', total: 2 }, { nome: '(Avulsa)', total: 1 }])
end
end
# NOT IN com NULL na subquery devolve zero linhas — por isso cada SELECT da
# união precisa filtrar nota_fiscal IS NOT NULL.
it 'protege o NOT IN contra nota_fiscal NULL' do
@@ -55,13 +92,14 @@ RSpec.describe Analytics::NotasForaOperacao do
it 'não quebra e informa vazio' do
expect(fora.total).to eq(0)
expect(fora.any?).to be(false)
expect(fora.por_plano).to eq([])
expect(fora.agrupamento.last).to eq([])
end
end
def row(nf:, status:, driver:, checkout: nil, planned: nil, title: nil, obs: nil)
def row(nf:, status:, driver:, sts: nil, checkout: nil, planned: nil, title: nil, notes: nil, obs: nil)
{ 'reference_id' => nf, 'status' => status, 'driver' => driver,
'checkout' => checkout, 'planned_date' => planned, 'observation' => obs,
'contact_name' => nil, 'title' => title, 'notes' => nil, 'comments' => nil, 'route_id' => nil }
'contact_name' => sts, 'title' => title, 'notes' => notes,
'comments' => nil, 'route_id' => nil }
end
end