@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.js CHANGED
@@ -170,6 +170,27 @@ var settings = metaWhatsAppSchema.table("settings", {
170
170
 
171
171
  // src/repositories/SessionRepository.ts
172
172
  var DEFAULT_LIMIT = 20;
173
+ var conversationSummaryProjection = {
174
+ lastContent: sql2`(
175
+ select m.content from ${messages} m
176
+ where m.company_id = ${sessions}.company_id and m.session_id = ${sessions}.id
177
+ order by m.created_at desc limit 1
178
+ )`,
179
+ lastDirection: sql2`(
180
+ select m.direction from ${messages} m
181
+ where m.company_id = ${sessions}.company_id and m.session_id = ${sessions}.id
182
+ order by m.created_at desc limit 1
183
+ )`,
184
+ // Entradas do cliente depois da última leitura do atendente. Sessão nunca lida conta
185
+ // tudo — é o comportamento esperado de uma conversa que ninguém abriu ainda.
186
+ unread: sql2`(
187
+ select count(*)::int from ${messages} m
188
+ where m.company_id = ${sessions}.company_id
189
+ and m.session_id = ${sessions}.id
190
+ and m.direction = 'inbound'
191
+ and (${sessions}.last_agent_read_at is null or m.created_at > ${sessions}.last_agent_read_at)
192
+ )`
193
+ };
173
194
  var SessionRepository = class {
174
195
  static {
175
196
  __name(this, "SessionRepository");
@@ -285,17 +306,28 @@ var SessionRepository = class {
285
306
  currentState: sessions.currentState,
286
307
  lastActivity: sessions.lastActivity,
287
308
  lastInboundAt: sessions.lastInboundAt,
288
- humanRequestedAt: sessions.humanRequestedAt
309
+ humanRequestedAt: sessions.humanRequestedAt,
310
+ // Prévia e contagem saem de subquery correlacionada em vez de N+1 na volta: uma inbox
311
+ // lista dezenas de conversas por página, e uma query por linha é o gargalo clássico
312
+ // dessa tela. Ambos os campos são dados do próprio módulo — deixá-los para o host
313
+ // obrigaria todo consumidor a reescrever o mesmo join contra tabelas que não são dele.
314
+ ...conversationSummaryProjection
289
315
  }).from(sessions).where(and(...conditions)).orderBy(desc(sessions.lastActivity)).limit(limit).offset(offset);
290
316
  return rows.map((row) => ({
291
317
  id: row.id,
292
318
  whatsappNumber: row.whatsappNumber,
319
+ ...row.lastContent !== null ? {
320
+ lastContent: row.lastContent
321
+ } : {},
322
+ ...row.lastDirection !== null ? {
323
+ lastDirection: row.lastDirection
324
+ } : {},
293
325
  lastAt: row.lastActivity.toISOString(),
294
326
  lastInboundAt: row.lastInboundAt?.toISOString() ?? null,
295
327
  mode: row.mode,
296
328
  assignedUserId: row.assignedUserId,
297
329
  waitingHuman: row.humanRequestedAt !== null,
298
- unread: 0,
330
+ unread: Number(row.unread),
299
331
  currentState: row.currentState
300
332
  }));
301
333
  }