@adatechnology/meta-whatsapp-module 0.2.0-rc.5 → 0.2.0-rc.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -285,17 +285,46 @@ var SessionRepository = class {
285
285
  currentState: sessions.currentState,
286
286
  lastActivity: sessions.lastActivity,
287
287
  lastInboundAt: sessions.lastInboundAt,
288
- humanRequestedAt: sessions.humanRequestedAt
288
+ humanRequestedAt: sessions.humanRequestedAt,
289
+ // Prévia e contagem saem de subquery correlacionada em vez de N+1 na volta: uma inbox
290
+ // lista dezenas de conversas por página, e uma query por linha é o gargalo clássico
291
+ // dessa tela. Ambos os campos são dados do próprio módulo — deixá-los para o host
292
+ // obrigaria todo consumidor a reescrever o mesmo join contra tabelas que não são dele.
293
+ lastContent: sql2`(
294
+ select m.content from ${messages} m
295
+ where m.company_id = ${sessions.companyId} and m.session_id = ${sessions.id}
296
+ order by m.created_at desc limit 1
297
+ )`,
298
+ lastDirection: sql2`(
299
+ select m.direction from ${messages} m
300
+ where m.company_id = ${sessions.companyId} and m.session_id = ${sessions.id}
301
+ order by m.created_at desc limit 1
302
+ )`,
303
+ // Entradas do cliente depois da última leitura do atendente. Sessão nunca lida conta
304
+ // tudo — é o comportamento esperado de uma conversa que ninguém abriu ainda.
305
+ unread: sql2`(
306
+ select count(*)::int from ${messages} m
307
+ where m.company_id = ${sessions.companyId}
308
+ and m.session_id = ${sessions.id}
309
+ and m.direction = 'inbound'
310
+ and (${sessions.lastAgentReadAt} is null or m.created_at > ${sessions.lastAgentReadAt})
311
+ )`
289
312
  }).from(sessions).where(and(...conditions)).orderBy(desc(sessions.lastActivity)).limit(limit).offset(offset);
290
313
  return rows.map((row) => ({
291
314
  id: row.id,
292
315
  whatsappNumber: row.whatsappNumber,
316
+ ...row.lastContent !== null ? {
317
+ lastContent: row.lastContent
318
+ } : {},
319
+ ...row.lastDirection !== null ? {
320
+ lastDirection: row.lastDirection
321
+ } : {},
293
322
  lastAt: row.lastActivity.toISOString(),
294
323
  lastInboundAt: row.lastInboundAt?.toISOString() ?? null,
295
324
  mode: row.mode,
296
325
  assignedUserId: row.assignedUserId,
297
326
  waitingHuman: row.humanRequestedAt !== null,
298
- unread: 0,
327
+ unread: Number(row.unread),
299
328
  currentState: row.currentState
300
329
  }));
301
330
  }