fix(notas): notas persisten entre etapas via clienteClave
- Campo clienteClave en RecordatorioVendedor (hash nombre+telefono) - Al crear nota: se calcula clienteClave del lead/cliente - Al buscar notas: busca por leadId O clienteClave (no por nombre suelto) - Mantiene compatibilidad: notas viejas sin clienteClave siguen por leadId - Migración SQL aplicada
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE "RecordatorioVendedor" ADD COLUMN "clienteClave" TEXT;
|
||||
CREATE INDEX IF NOT EXISTS "RecordatorioVendedor_clienteClave_idx" ON "RecordatorioVendedor"("clienteClave");
|
||||
@@ -420,6 +420,7 @@ model RecordatorioVendedor {
|
||||
id String @id @default(uuid())
|
||||
usuarioId String
|
||||
leadId String?
|
||||
clienteClave String?
|
||||
titulo String
|
||||
descripcion String?
|
||||
fechaRecordar DateTime
|
||||
|
||||
@@ -9,6 +9,7 @@ const crearSchema = z.object({
|
||||
titulo: z.string().min(1),
|
||||
descripcion: z.string().optional(),
|
||||
leadId: z.string().optional(),
|
||||
clienteClave: z.string().optional(),
|
||||
fechaRecordar: z.string().min(1),
|
||||
})
|
||||
|
||||
@@ -24,6 +25,7 @@ export async function crearRecordatorioEndpoint(req: Request, res: Response): Pr
|
||||
titulo: body.titulo,
|
||||
descripcion: body.descripcion ?? null,
|
||||
leadId: body.leadId ?? null,
|
||||
clienteClave: body.clienteClave ?? null,
|
||||
fechaRecordar: body.fechaRecordar,
|
||||
})
|
||||
res.status(201).json(recordatorio)
|
||||
@@ -38,9 +40,10 @@ export async function listarRecordatoriosEndpoint(req: Request, res: Response):
|
||||
if (!usuarioId) throw new Error('Auth requerida')
|
||||
|
||||
const completadosStr = req.query['completados'] as string | undefined
|
||||
const opts: { completados?: boolean; desde?: string | null; hasta?: string | null } = {
|
||||
const opts: { completados?: boolean; desde?: string | null; hasta?: string | null; clienteClave?: string | null } = {
|
||||
desde: (req.query['desde'] as string) ?? null,
|
||||
hasta: (req.query['hasta'] as string) ?? null,
|
||||
clienteClave: (req.query['clienteClave'] as string) ?? null,
|
||||
}
|
||||
if (completadosStr === 'true') opts.completados = true
|
||||
else if (completadosStr === 'false') opts.completados = false
|
||||
@@ -78,6 +81,7 @@ export function crearController(svc: RecordatorioVendedorService) {
|
||||
titulo: body.titulo,
|
||||
descripcion: body.descripcion ?? null,
|
||||
leadId: body.leadId ?? null,
|
||||
clienteClave: body.clienteClave ?? null,
|
||||
fechaRecordar: body.fechaRecordar,
|
||||
})
|
||||
res.status(201).json(r)
|
||||
@@ -92,6 +96,7 @@ export function crearController(svc: RecordatorioVendedorService) {
|
||||
}
|
||||
if (completadosStr === 'true') opts.completados = true
|
||||
else if (completadosStr === 'false') opts.completados = false
|
||||
opts.clienteClave = (req.query['clienteClave'] as string) ?? null
|
||||
const items = await svc.listar(usuarioId, opts)
|
||||
res.json(items)
|
||||
},
|
||||
|
||||
@@ -7,10 +7,18 @@ import { NotFoundError } from '../utils/errors.js'
|
||||
* muestra en "Mis recordatorios" de Mi día.
|
||||
*/
|
||||
|
||||
/** Calcula una clave estable del cliente: nombre normalizado + últimos 10 dígitos del teléfono */
|
||||
export function calcularClienteClave(nombre?: string | null, telefono?: string | null): string {
|
||||
const n = (nombre ?? '').trim().toLowerCase()
|
||||
const t = (telefono ?? '').replace(/\s+/g, '').slice(-10)
|
||||
return `${n}|${t}`
|
||||
}
|
||||
|
||||
export interface DatosCrearRecordatorio {
|
||||
titulo: string
|
||||
descripcion?: string | null
|
||||
leadId?: string | null
|
||||
clienteClave?: string | null
|
||||
fechaRecordar: string // ISO o YYYY-MM-DDTHH:mm
|
||||
}
|
||||
|
||||
@@ -18,6 +26,7 @@ export interface OpcionesListarRecordatorio {
|
||||
completados?: boolean
|
||||
desde?: string | null // ISO
|
||||
hasta?: string | null // ISO
|
||||
clienteClave?: string | null
|
||||
}
|
||||
|
||||
export class RecordatorioVendedorService {
|
||||
@@ -28,6 +37,7 @@ export class RecordatorioVendedorService {
|
||||
titulo: data.titulo.trim(),
|
||||
descripcion: data.descripcion?.trim() ?? null,
|
||||
leadId: data.leadId ?? null,
|
||||
clienteClave: data.clienteClave ?? null,
|
||||
fechaRecordar: new Date(data.fechaRecordar),
|
||||
},
|
||||
})
|
||||
@@ -36,6 +46,9 @@ export class RecordatorioVendedorService {
|
||||
async listar(usuarioId: string, opciones: OpcionesListarRecordatorio = {}) {
|
||||
const where: Record<string, unknown> = { usuarioId }
|
||||
|
||||
if (opciones.clienteClave) {
|
||||
where['clienteClave'] = opciones.clienteClave
|
||||
}
|
||||
if (opciones.completados !== undefined) {
|
||||
where['completado'] = opciones.completados
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user