backend: enhance deleteMany method to allow non-failure on empty results in RoomMemberRepository

This commit is contained in:
juancarmore 2026-01-21 19:35:17 +01:00
parent 8d255bd051
commit 086f60d60a
2 changed files with 19 additions and 10 deletions

View File

@ -228,20 +228,29 @@ export abstract class BaseRepository<TDomain, TDocument extends Document> {
/**
* Deletes multiple documents matching the given filter.
* @param filter - MongoDB query filter
* @throws Error if no documents were found or deleted
* @param failIfEmpty - Whether to throw error if no documents are found (default: true)
* @throws Error if no documents were found or deleted (only when failIfEmpty is true)
*/
protected async deleteMany(filter: FilterQuery<TDocument> = {}): Promise<void> {
protected async deleteMany(filter: FilterQuery<TDocument> = {}, failIfEmpty = true): Promise<void> {
try {
const result = await this.model.deleteMany(filter).exec();
const deletedCount = result.deletedCount || 0;
if (deletedCount === 0) {
this.logger.error('No documents found to delete with filter:', filter);
throw new Error('No documents found for deletion');
if (failIfEmpty) {
this.logger.error('No documents found to delete with filter:', filter);
throw new Error('No documents found for deletion');
} else {
this.logger.debug('No documents found to delete with filter:', filter);
}
} else {
this.logger.debug(`Deleted ${deletedCount} documents`);
}
} catch (error) {
if (error instanceof Error && error.message === 'No documents found for deletion') {
throw error;
}
this.logger.debug(`Deleted ${deletedCount} documents`);
} catch (error) {
this.logger.error('Error deleting documents:', error);
throw error;
}

View File

@ -267,23 +267,23 @@ export class RoomMemberRepository extends BaseRepository<MeetRoomMember, MeetRoo
/**
* Removes all members from a room.
* Does not fail if no members are found.
*
* @param roomId - The ID of the room
* @throws Error if members could not be deleted
*/
async deleteAllByRoomId(roomId: string): Promise<void> {
await this.deleteMany({ roomId });
await this.deleteMany({ roomId }, false);
}
/**
* Removes all room memberships for a specific member across all rooms.
* This is useful when deleting a user account.
* Does not fail if no memberships are found.
*
* @param memberId - The ID of the member whose memberships should be deleted
* @throws Error if members could not be deleted
*/
async deleteAllByMemberId(memberId: string): Promise<void> {
await this.deleteMany({ memberId });
await this.deleteMany({ memberId }, false);
}
// ==========================================