Repository navigation
WA.Auto: incluir processos encerrados com movimentação comprovada #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
58f51ea
681b0a0
42337e1
5430ad2
bad063f
09b482f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import {describe,expect,it} from 'vitest'; | ||
| import {isStatusRequest,normalizePhone,prepareDailyReturn,type ReturnCase} from './wa-daily-return-policy'; | ||
| import {prepareMovementAlert} from './wa-movement-builder'; | ||
| const base:ReturnCase={ | ||
| id:1,empresa_id:'11111111-1111-1111-1111-111111111111', | ||
| cliente:'Maria de Souza',telefone:'(11) 99999-4321', | ||
|
|
@@ -43,6 +44,16 @@ describe('retorno inteligente DataJud/DJEN',()=>{ | |
| expect(prepareDailyReturn({...base,ultimo_retorno:null},{mode:'due',today:'2026-10-09'}).reason).toBe('missing_return'); | ||
| expect(prepareDailyReturn({...base,status:'ENCERRADO'},{mode:'due',today:'2026-10-09'}).reason).toBe('closed'); | ||
| }); | ||
| it('includes closed cases only in verified movement campaigns, not routine due returns',()=>{ | ||
| const closed={...base,status:'ENCERRADO'}; | ||
| const allowed=prepareMovementAlert(closed); | ||
| expect(allowed.reason).toBe('ok'); | ||
|
Comment on lines
+49
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This new Useful? React with 👍 / 👎. |
||
| expect(allowed.alert?.message).toContain('Setor Processual'); | ||
| expect(allowed.alert?.message).not.toMatch(/seu processo foi encerrado|seu processo foi extinto/i); | ||
| expect(prepareDailyReturn(closed,{mode:'due',today:'2026-10-09'}).reason).toBe('closed'); | ||
| expect(prepareMovementAlert({...closed,ultimo_retorno:'2026-10-08'}).reason).toBe('no_new_movement'); | ||
| expect(prepareMovementAlert({...closed,dados:{whatsapp_opt_in:true,whatsapp_opt_out:true}}).reason).toBe('blocked'); | ||
| }); | ||
| it('requires explicit opt-in and honors opt-out',()=>{ | ||
| expect(prepareDailyReturn({...base,dados:{}},{mode:'due',today:'2026-10-09'}).reason).toBe('no_consent'); | ||
| expect(prepareDailyReturn({...base,dados:{whatsapp_opt_in:true,nao_contatar:true}},{mode:'due',today:'2026-10-09'}).reason).toBe('blocked'); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,10 +64,13 @@ export function isStatusRequest(input:string):boolean { | |
| * A date-only last-return record cannot distinguish two events on the same day. | ||
| */ | ||
| export function prepareDailyReturn(row:ReturnCase,opts:{ | ||
| mode:ReturnMode;today?:string;intervalDays?:number;consentAttested?:boolean; | ||
| mode:ReturnMode;today?:string;intervalDays?:number;consentAttested?:boolean;includeClosed?:boolean; | ||
| }):ReturnDecision { | ||
| const meta=row.dados||{}; | ||
| if(isCasoEncerrado(row))return {ready:null,reason:'closed'}; | ||
| // Movimento posterior confirmado pode ocorrer em uma carteira marcada como encerrada. | ||
| // Apenas os avisos de movimentação da carteira optam por incluir esses casos; | ||
| // um status interno nunca é prova de encerramento definitivo no tribunal. | ||
| if(isCasoEncerrado(row) && !opts.includeClosed)return {ready:null,reason:'closed'}; | ||
|
Comment on lines
+70
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an already-closed case has a fresh official terminal event, this new bypass lets it reach the source checks, but the unchanged guards at lines 103-104 and 115-117 still return Useful? React with 👍 / 👎. |
||
| const hasOptIn=['whatsapp_opt_in','consentimento_whatsapp','whatsapp_autorizado'].some(k=>affirmative(meta[k])); | ||
| if(['nao_contatar','não_contatar','whatsapp_opt_out','optOut','optout','bloquear_whatsapp','naoEnviarWhatsapp'].some(k=>blockedValue(meta[k])) || ['whatsapp_opt_in','consentimento_whatsapp','whatsapp_autorizado'].some(k=>denied(meta[k]))) | ||
| return {ready:null,reason:'blocked'}; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import { persistWhatsAppMessage } from '@/lib/whatsapp-persist'; | |
| import { prepareMovementAlert, type SourceRow, type Alert } from '@/lib/wa-movement-builder'; | ||
| import {getDailyReturnSettings,upsertDailyReturnSettings} from '@/lib/wa-daily-return-service'; | ||
| import {brazilToday} from '@/lib/wa-daily-return-policy'; | ||
| import {isCasoEncerrado} from '@/lib/status-encerrado'; | ||
|
|
||
|
|
||
| async function requireManager() { | ||
|
|
@@ -28,7 +29,7 @@ async function collectPortfolio(empresaId: string, kind:CampaignKind='movement', | |
| const db = await getSupabaseAdmin(); | ||
| const entries: Alert[] = []; | ||
| const counts = { scanned:0, withoutPhone:0, withoutEvent:0, blocked:0, samePhone:0, | ||
| alreadyClosed:0, consentMissing:0, needsReview:0, alreadyNotified:0, missingReturn:0, noNewMovement:0 }; | ||
| alreadyClosed:0, closedEligible:0, consentMissing:0, needsReview:0, alreadyNotified:0, missingReturn:0, noNewMovement:0 }; | ||
| let after=0; | ||
| for (;;) { | ||
| const {data,error}=await db.rpc('wa_notice_portfolio_rows',{p_empresa:empresaId,p_after:after,p_limit:3000}); | ||
|
|
@@ -46,7 +47,10 @@ async function collectPortfolio(empresaId: string, kind:CampaignKind='movement', | |
| else if(reason==='needs_source_review')counts.needsReview++; | ||
| else if(reason==='no_new_movement')counts.noNewMovement++; | ||
| else counts.withoutEvent++; | ||
| } else entries.push(alert); | ||
| } else { | ||
| entries.push(alert); | ||
| if(isCasoEncerrado(row))counts.closedEligible++; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Incrementing Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
| if(rows.length<3000)break; | ||
| after=Number(rows[rows.length-1].id); | ||
|
|
@@ -99,7 +103,7 @@ export async function createMovementCampaign(confirmed: boolean,kind:CampaignKin | |
| const {entries}=await collectPortfolio(ctx.empresa_id,kind,confirmed||settings.consentAttested); | ||
| if (!entries.length) throw new Error( | ||
| kind === 'publication' | ||
| ? 'Nenhum aviso elegível: confira telefone, data do último retorno e novidade em processos abertos.' | ||
| ? 'Nenhum aviso elegível: confira telefone, último retorno e nova publicação verificada na carteira.' | ||
| : 'Nenhum processo com telefone válido e movimentação identificada.' | ||
| ); | ||
| const {data: campaign,error: createErr} = await db.from('wa_movement_campaigns') | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updated description tells managers that closed processes are included, but the same confirmation dialog still states at lines 256-259 that the system excludes closed processes. Since the backend now includes them, an operator can start a customer-facing bulk campaign based on a false exclusion summary; the final notice should accurately describe the recipients before enabling the send.
Useful? React with 👍 / 👎.