diff --git a/apps/api/src/app/inbox/usecases/mark-notifications-as-seen/mark-notifications-as-seen.usecase.ts b/apps/api/src/app/inbox/usecases/mark-notifications-as-seen/mark-notifications-as-seen.usecase.ts index 344dfc26757..297b65a99f3 100644 --- a/apps/api/src/app/inbox/usecases/mark-notifications-as-seen/mark-notifications-as-seen.usecase.ts +++ b/apps/api/src/app/inbox/usecases/mark-notifications-as-seen/mark-notifications-as-seen.usecase.ts @@ -73,7 +73,7 @@ export class MarkNotificationsAsSeen { const updatedMessages: MessageEntity[] = []; // If notificationIds are provided, use them; otherwise use filters if (notificationIds && notificationIds.length > 0) { - const BATCH_SIZE = 50; + const BATCH_SIZE = 500; const notificationIdChunks = this.chunkArray(notificationIds, BATCH_SIZE); for (const idChunk of notificationIdChunks) { diff --git a/libs/dal/src/repositories/base-repository.ts b/libs/dal/src/repositories/base-repository.ts index d760cd993f2..c43347a35c3 100644 --- a/libs/dal/src/repositories/base-repository.ts +++ b/libs/dal/src/repositories/base-repository.ts @@ -452,8 +452,12 @@ export class BaseRepository { }); } - async bulkWrite(bulkOperations: any, ordered = false): Promise { - return await this.MongooseModel.bulkWrite(bulkOperations, { ordered }); + async bulkWrite( + bulkOperations: any, + ordered = false, + options?: Pick + ): Promise { + return await this.MongooseModel.bulkWrite(bulkOperations, { ordered, ...options }); } protected mapEntity(data: TData): TData extends null ? null : T_MappedEntity { diff --git a/libs/dal/src/repositories/message/message.repository.ts b/libs/dal/src/repositories/message/message.repository.ts index e30e1592466..8e10c8228d7 100644 --- a/libs/dal/src/repositories/message/message.repository.ts +++ b/libs/dal/src/repositories/message/message.repository.ts @@ -82,6 +82,8 @@ function mergeTagsMongoFragment { private static readonly BATCH_SIZE = 100; + /** Larger chunks for status updates: pairs of updateMany are merged via bulkWrite per chunk. */ + private static readonly STATUS_UPDATE_CHUNK_SIZE = 1000; private feedRepository = new FeedRepository(); constructor() { super(Message, MessageEntity); @@ -618,8 +620,7 @@ export class MessageRepository extends BaseRepository doc._id); - // Perform the update using document IDs in batches - const chunks = this.chunkArray(documentIds); + const chunks = this.chunkArray(documentIds, MessageRepository.STATUS_UPDATE_CHUNK_SIZE); for (const chunk of chunks) { await this.update( @@ -934,17 +935,28 @@ export class MessageRepository extends BaseRepository