feat: decrement message counts on bulk message deletions

This commit is contained in:
rladenson 2024-11-25 02:09:40 -07:00
parent 71dd59b57d
commit 21a63a43cc
3 changed files with 30 additions and 6 deletions

View file

@ -111,21 +111,21 @@ public partial class ModelRepository
await _db.ExecuteQuery(query);
}
public async Task UpdateMemberForDeletedMessage(MemberId id, int msgCount)
public async Task UpdateMemberForDeletedMessage(MemberId id, int msgCount, int numberToDecrement = 1)
{
if (msgCount <= 0) return;
var query = new Query("members")
.When(msgCount > 1,
// when there are plenty of messages
.When(msgCount > numberToDecrement,
// when message count is higher than number we're removing
q => q.AsUpdate(new
{
message_count = new UnsafeLiteral("message_count - 1")
message_count = new UnsafeLiteral($"message_count - {numberToDecrement}")
}),
// when this is the only message (if the db for some reason thinks there are no messages we already returned)
// when this is the only/last message(s) (if the db for some reason thinks there are no messages we already returned)
q => q.AsUpdate(new
{
message_count = new UnsafeLiteral("message_count - 1"),
message_count = 0,
last_message_timestamp = new UnsafeLiteral("null")
})
)