@rempays/shared-core 1.0.2-beta.11 → 1.0.2-beta.12

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.
@@ -46,12 +46,9 @@ export declare class ChatService {
46
46
  listChatsByBusiness(businessId: string, status: string, params?: PaginationParams): Promise<PaginatedResult<Chat>>;
47
47
  /**
48
48
  * Lista todos los chats de un business (todos los status)
49
+ * Nota: Hace múltiples queries, una por cada status común
49
50
  */
50
51
  listAllChatsByBusiness(businessId: string, params?: PaginationParams): Promise<PaginatedResult<Chat>>;
51
- /**
52
- * Incrementa el contador de mensajes no leídos
53
- */
54
- incrementUnreadCount(chatId: string): Promise<void>;
55
52
  /**
56
53
  * Marca todos los mensajes como leídos
57
54
  */
@@ -242,48 +242,33 @@ class ChatService {
242
242
  }
243
243
  /**
244
244
  * Lista todos los chats de un business (todos los status)
245
+ * Nota: Hace múltiples queries, una por cada status común
245
246
  */
246
247
  async listAllChatsByBusiness(businessId, params = {}) {
247
- const { limit = 20, lastEvaluatedKey } = params;
248
- const result = await this.client.send(new client_dynamodb_1.QueryCommand({
249
- TableName: this.tableName,
250
- IndexName: 'GSI1',
251
- KeyConditionExpression: 'begins_with(GSI1PK, :prefix)',
252
- ExpressionAttributeValues: (0, util_dynamodb_1.marshall)({
253
- ':prefix': `BUSINESS#${businessId}#`,
254
- }),
255
- ScanIndexForward: false,
256
- Limit: limit,
257
- ExclusiveStartKey: lastEvaluatedKey ? (0, util_dynamodb_1.marshall)(lastEvaluatedKey) : undefined,
258
- }));
259
- const items = result.Items?.map((item) => {
260
- const data = (0, util_dynamodb_1.unmarshall)(item);
261
- const { PK, SK, GSI1PK, GSI1SK, ...chat } = data;
262
- return chat;
263
- }) || [];
248
+ const { limit = 20 } = params;
249
+ const statuses = ['active', 'closed', 'pending', 'archived'];
250
+ const allChats = [];
251
+ // Query para cada status
252
+ for (const status of statuses) {
253
+ const result = await this.listChatsByBusiness(businessId, status, {
254
+ limit: Math.ceil(limit / statuses.length),
255
+ });
256
+ allChats.push(...result.items);
257
+ // Si ya tenemos suficientes, parar
258
+ if (allChats.length >= limit) {
259
+ break;
260
+ }
261
+ }
262
+ // Ordenar por lastMessageTimestamp descendente
263
+ allChats.sort((a, b) => b.lastMessageTimestamp - a.lastMessageTimestamp);
264
+ // Limitar resultados
265
+ const items = allChats.slice(0, limit);
264
266
  return {
265
267
  items,
266
- lastEvaluatedKey: result.LastEvaluatedKey ? (0, util_dynamodb_1.unmarshall)(result.LastEvaluatedKey) : undefined,
267
- hasMore: !!result.LastEvaluatedKey,
268
+ lastEvaluatedKey: undefined,
269
+ hasMore: allChats.length > limit,
268
270
  };
269
271
  }
270
- /**
271
- * Incrementa el contador de mensajes no leídos
272
- */
273
- async incrementUnreadCount(chatId) {
274
- await this.client.send(new client_dynamodb_1.UpdateItemCommand({
275
- TableName: this.tableName,
276
- Key: (0, util_dynamodb_1.marshall)({
277
- PK: `CHAT#${chatId}`,
278
- SK: 'METADATA',
279
- }),
280
- UpdateExpression: 'SET unreadCount = if_not_exists(unreadCount, :zero) + :inc',
281
- ExpressionAttributeValues: (0, util_dynamodb_1.marshall)({
282
- ':inc': 1,
283
- ':zero': 0,
284
- }),
285
- }));
286
- }
287
272
  /**
288
273
  * Marca todos los mensajes como leídos
289
274
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rempays/shared-core",
3
- "version": "1.0.2-beta.11",
3
+ "version": "1.0.2-beta.12",
4
4
  "description": "Core utilities layer for RemPays platform with AWS services integration (Cognito, S3, Secrets Manager, Textract, Facebook API, DynamoDB)",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -38,7 +38,8 @@
38
38
  "build": "tsc",
39
39
  "build:watch": "tsc --watch",
40
40
  "prepublishOnly": "npm run build",
41
- "dev": "tsc --watch"
41
+ "dev": "tsc --watch",
42
+ "test:chat": "tsx examples/test-chat-service.ts"
42
43
  },
43
44
  "dependencies": {
44
45
  "@aws-sdk/client-cognito-identity-provider": "^3.454.0",
@@ -50,6 +51,8 @@
50
51
  },
51
52
  "devDependencies": {
52
53
  "@types/node": "^20.11.0",
54
+ "dotenv": "^17.3.1",
55
+ "tsx": "^4.7.0",
53
56
  "typescript": "^5.3.0"
54
57
  },
55
58
  "files": [