@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.cjs CHANGED
@@ -141,9 +141,14 @@ var messages = metaWhatsAppSchema.table("messages", {
141
141
  length: 12
142
142
  }).notNull(),
143
143
  agentUserId: (0, import_pg_core.uuid)("agent_user_id"),
144
- type: (0, import_pg_core.varchar)("type", {
145
- length: 16
146
- }).notNull().default("text"),
144
+ // 32, não 16: os tipos da própria Meta são curtos ('text', 'interactive'), mas o host rotula
145
+ type: (
146
+ // a saída com o subtipo que enviou ('interactive_buttons' já tem 19). Apertar aqui só
147
+ // transfere para o consumidor a escolha entre truncar o rótulo e estourar o insert.
148
+ (0, import_pg_core.varchar)("type", {
149
+ length: 32
150
+ }).notNull().default("text")
151
+ ),
147
152
  content: (0, import_pg_core.text)("content"),
148
153
  // T5.4 — nunca base64 aqui; mídia vive no ObjectStorageInterface do host, referenciada por
149
154
  // uploadId dentro deste jsonb.
@@ -336,17 +341,46 @@ var SessionRepository = class {
336
341
  currentState: sessions.currentState,
337
342
  lastActivity: sessions.lastActivity,
338
343
  lastInboundAt: sessions.lastInboundAt,
339
- humanRequestedAt: sessions.humanRequestedAt
344
+ humanRequestedAt: sessions.humanRequestedAt,
345
+ // Prévia e contagem saem de subquery correlacionada em vez de N+1 na volta: uma inbox
346
+ // lista dezenas de conversas por página, e uma query por linha é o gargalo clássico
347
+ // dessa tela. Ambos os campos são dados do próprio módulo — deixá-los para o host
348
+ // obrigaria todo consumidor a reescrever o mesmo join contra tabelas que não são dele.
349
+ lastContent: import_drizzle_orm2.sql`(
350
+ select m.content from ${messages} m
351
+ where m.company_id = ${sessions.companyId} and m.session_id = ${sessions.id}
352
+ order by m.created_at desc limit 1
353
+ )`,
354
+ lastDirection: import_drizzle_orm2.sql`(
355
+ select m.direction from ${messages} m
356
+ where m.company_id = ${sessions.companyId} and m.session_id = ${sessions.id}
357
+ order by m.created_at desc limit 1
358
+ )`,
359
+ // Entradas do cliente depois da última leitura do atendente. Sessão nunca lida conta
360
+ // tudo — é o comportamento esperado de uma conversa que ninguém abriu ainda.
361
+ unread: import_drizzle_orm2.sql`(
362
+ select count(*)::int from ${messages} m
363
+ where m.company_id = ${sessions.companyId}
364
+ and m.session_id = ${sessions.id}
365
+ and m.direction = 'inbound'
366
+ and (${sessions.lastAgentReadAt} is null or m.created_at > ${sessions.lastAgentReadAt})
367
+ )`
340
368
  }).from(sessions).where((0, import_drizzle_orm2.and)(...conditions)).orderBy((0, import_drizzle_orm2.desc)(sessions.lastActivity)).limit(limit).offset(offset);
341
369
  return rows.map((row) => ({
342
370
  id: row.id,
343
371
  whatsappNumber: row.whatsappNumber,
372
+ ...row.lastContent !== null ? {
373
+ lastContent: row.lastContent
374
+ } : {},
375
+ ...row.lastDirection !== null ? {
376
+ lastDirection: row.lastDirection
377
+ } : {},
344
378
  lastAt: row.lastActivity.toISOString(),
345
379
  lastInboundAt: row.lastInboundAt?.toISOString() ?? null,
346
380
  mode: row.mode,
347
381
  assignedUserId: row.assignedUserId,
348
382
  waitingHuman: row.humanRequestedAt !== null,
349
- unread: 0,
383
+ unread: Number(row.unread),
350
384
  currentState: row.currentState
351
385
  }));
352
386
  }