diff --git a/backend/ResQueue/ResQueue/Dtos/ArchiveMessagesDto.cs b/backend/ResQueue/ResQueue/Dtos/ArchiveMessagesDto.cs index 877e96b..52de16c 100644 --- a/backend/ResQueue/ResQueue/Dtos/ArchiveMessagesDto.cs +++ b/backend/ResQueue/ResQueue/Dtos/ArchiveMessagesDto.cs @@ -1,3 +1,3 @@ namespace ResQueue.Dtos; -public record ArchiveMessagesDto(string[] Ids); +public record ArchiveMessagesDto(string QueueId, string[] Ids); \ No newline at end of file diff --git a/backend/ResQueue/ResQueue/Features/Messages/ArchiveMessages/ArchiveMessagesFeature.cs b/backend/ResQueue/ResQueue/Features/Messages/ArchiveMessages/ArchiveMessagesFeature.cs index e776cc9..6809981 100644 --- a/backend/ResQueue/ResQueue/Features/Messages/ArchiveMessages/ArchiveMessagesFeature.cs +++ b/backend/ResQueue/ResQueue/Features/Messages/ArchiveMessages/ArchiveMessagesFeature.cs @@ -14,7 +14,9 @@ public record ArchiveMessagesFeatureResponse(); public class ArchiveMessagesFeature( UserManager userManager, - IMongoCollection messagesCollection + IMongoCollection messagesCollection, + IMongoCollection queuesCollection, + IMongoClient mongoClient ) : IArchiveMessagesFeature { public async Task> ExecuteAsync( @@ -35,12 +37,21 @@ public async Task> ExecuteAsync( var filter = Builders.Filter.And( Builders.Filter.In(m => m.Id, request.Dto.Ids.Select(ObjectId.Parse)), + Builders.Filter.Eq(m => m.QueueId, ObjectId.Parse(request.Dto.QueueId)), Builders.Filter.Eq(m => m.UserId, user.Id) ); var update = Builders.Update.Set(m => m.DeletedAt, dt); - await messagesCollection.UpdateManyAsync(filter, update); + using var session = await mongoClient.StartSessionAsync(); + session.StartTransaction(); + + var result = await messagesCollection.UpdateManyAsync(filter, update); + + await queuesCollection.UpdateOneAsync(session, x => x.Id == ObjectId.Parse(request.Dto.QueueId), + Builders.Update.Inc(x => x.TotalMessages, -result.ModifiedCount)); + + await session.CommitTransactionAsync(); return OperationResult.Success(new ArchiveMessagesFeatureResponse()); } diff --git a/frontend/src/api/messages/archiveMessagesMutation.ts b/frontend/src/api/messages/archiveMessagesMutation.ts index 68fe5de..af13cac 100644 --- a/frontend/src/api/messages/archiveMessagesMutation.ts +++ b/frontend/src/api/messages/archiveMessagesMutation.ts @@ -6,16 +6,16 @@ export function useArchiveMessagesMutation() { const queryClient = useQueryClient() return useMutation({ - mutationFn: (ids: string[]) => + mutationFn: (data: { ids: string[]; queueId: string }) => axios.delete(`${API_URL}/messages`, { - data: { - ids - }, + data, withCredentials: true }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['messages'] }) queryClient.invalidateQueries({ queryKey: ['message'] }) + queryClient.invalidateQueries({ queryKey: ['queues'] }) + queryClient.invalidateQueries({ queryKey: ['queue'] }) } }) } diff --git a/frontend/src/features/message/MessageActions.vue b/frontend/src/features/message/MessageActions.vue index d8b80b8..2c951ff 100644 --- a/frontend/src/features/message/MessageActions.vue +++ b/frontend/src/features/message/MessageActions.vue @@ -149,7 +149,10 @@ const archiveMessages = () => { severity: 'danger' }, accept: () => { - archiveMessagesAsync(props.selectedMessageIds).then(() => { + archiveMessagesAsync({ + ids: props.selectedMessageIds, + queueId: props.rabbitMqQueue.id + }).then(() => { emit('archive:message') toast.add({