@adatechnology/meta-whatsapp-module 0.2.0-rc.4 → 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.d.cts CHANGED
@@ -431,7 +431,7 @@ declare const messages: drizzle_orm_pg_core.PgTableWithColumns<{
431
431
  identity: undefined;
432
432
  generated: undefined;
433
433
  }, {}, {
434
- length: 16;
434
+ length: 32;
435
435
  }>;
436
436
  content: drizzle_orm_pg_core.PgColumn<{
437
437
  name: "content";
package/dist/index.d.ts CHANGED
@@ -431,7 +431,7 @@ declare const messages: drizzle_orm_pg_core.PgTableWithColumns<{
431
431
  identity: undefined;
432
432
  generated: undefined;
433
433
  }, {}, {
434
- length: 16;
434
+ length: 32;
435
435
  }>;
436
436
  content: drizzle_orm_pg_core.PgColumn<{
437
437
  name: "content";
package/dist/index.js CHANGED
@@ -85,9 +85,14 @@ var messages = metaWhatsAppSchema.table("messages", {
85
85
  length: 12
86
86
  }).notNull(),
87
87
  agentUserId: uuid("agent_user_id"),
88
- type: varchar("type", {
89
- length: 16
90
- }).notNull().default("text"),
88
+ // 32, não 16: os tipos da própria Meta são curtos ('text', 'interactive'), mas o host rotula
89
+ type: (
90
+ // a saída com o subtipo que enviou ('interactive_buttons' já tem 19). Apertar aqui só
91
+ // transfere para o consumidor a escolha entre truncar o rótulo e estourar o insert.
92
+ varchar("type", {
93
+ length: 32
94
+ }).notNull().default("text")
95
+ ),
91
96
  content: text("content"),
92
97
  // T5.4 — nunca base64 aqui; mídia vive no ObjectStorageInterface do host, referenciada por
93
98
  // uploadId dentro deste jsonb.
@@ -280,17 +285,46 @@ var SessionRepository = class {
280
285
  currentState: sessions.currentState,
281
286
  lastActivity: sessions.lastActivity,
282
287
  lastInboundAt: sessions.lastInboundAt,
283
- 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
+ )`
284
312
  }).from(sessions).where(and(...conditions)).orderBy(desc(sessions.lastActivity)).limit(limit).offset(offset);
285
313
  return rows.map((row) => ({
286
314
  id: row.id,
287
315
  whatsappNumber: row.whatsappNumber,
316
+ ...row.lastContent !== null ? {
317
+ lastContent: row.lastContent
318
+ } : {},
319
+ ...row.lastDirection !== null ? {
320
+ lastDirection: row.lastDirection
321
+ } : {},
288
322
  lastAt: row.lastActivity.toISOString(),
289
323
  lastInboundAt: row.lastInboundAt?.toISOString() ?? null,
290
324
  mode: row.mode,
291
325
  assignedUserId: row.assignedUserId,
292
326
  waitingHuman: row.humanRequestedAt !== null,
293
- unread: 0,
327
+ unread: Number(row.unread),
294
328
  currentState: row.currentState
295
329
  }));
296
330
  }