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

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.cjs CHANGED
@@ -226,6 +226,27 @@ var settings = metaWhatsAppSchema.table("settings", {
226
226
 
227
227
  // src/repositories/SessionRepository.ts
228
228
  var DEFAULT_LIMIT = 20;
229
+ var conversationSummaryProjection = {
230
+ lastContent: import_drizzle_orm2.sql`(
231
+ select m.content from ${messages} m
232
+ where m.company_id = ${sessions}.company_id and m.session_id = ${sessions}.id
233
+ order by m.created_at desc limit 1
234
+ )`,
235
+ lastDirection: import_drizzle_orm2.sql`(
236
+ select m.direction from ${messages} m
237
+ where m.company_id = ${sessions}.company_id and m.session_id = ${sessions}.id
238
+ order by m.created_at desc limit 1
239
+ )`,
240
+ // Entradas do cliente depois da última leitura do atendente. Sessão nunca lida conta
241
+ // tudo — é o comportamento esperado de uma conversa que ninguém abriu ainda.
242
+ unread: import_drizzle_orm2.sql`(
243
+ select count(*)::int from ${messages} m
244
+ where m.company_id = ${sessions}.company_id
245
+ and m.session_id = ${sessions}.id
246
+ and m.direction = 'inbound'
247
+ and (${sessions}.last_agent_read_at is null or m.created_at > ${sessions}.last_agent_read_at)
248
+ )`
249
+ };
229
250
  var SessionRepository = class {
230
251
  static {
231
252
  __name(this, "SessionRepository");
@@ -341,17 +362,28 @@ var SessionRepository = class {
341
362
  currentState: sessions.currentState,
342
363
  lastActivity: sessions.lastActivity,
343
364
  lastInboundAt: sessions.lastInboundAt,
344
- humanRequestedAt: sessions.humanRequestedAt
365
+ humanRequestedAt: sessions.humanRequestedAt,
366
+ // Prévia e contagem saem de subquery correlacionada em vez de N+1 na volta: uma inbox
367
+ // lista dezenas de conversas por página, e uma query por linha é o gargalo clássico
368
+ // dessa tela. Ambos os campos são dados do próprio módulo — deixá-los para o host
369
+ // obrigaria todo consumidor a reescrever o mesmo join contra tabelas que não são dele.
370
+ ...conversationSummaryProjection
345
371
  }).from(sessions).where((0, import_drizzle_orm2.and)(...conditions)).orderBy((0, import_drizzle_orm2.desc)(sessions.lastActivity)).limit(limit).offset(offset);
346
372
  return rows.map((row) => ({
347
373
  id: row.id,
348
374
  whatsappNumber: row.whatsappNumber,
375
+ ...row.lastContent !== null ? {
376
+ lastContent: row.lastContent
377
+ } : {},
378
+ ...row.lastDirection !== null ? {
379
+ lastDirection: row.lastDirection
380
+ } : {},
349
381
  lastAt: row.lastActivity.toISOString(),
350
382
  lastInboundAt: row.lastInboundAt?.toISOString() ?? null,
351
383
  mode: row.mode,
352
384
  assignedUserId: row.assignedUserId,
353
385
  waitingHuman: row.humanRequestedAt !== null,
354
- unread: 0,
386
+ unread: Number(row.unread),
355
387
  currentState: row.currentState
356
388
  }));
357
389
  }