Possible fix for pledge updates

This commit is contained in:
Kwoth
2022-06-15 10:24:09 +02:00
parent 65995bdca4
commit 5fbe93d898

View File

@@ -234,23 +234,33 @@ public sealed class PatronageService
|| dbPatron.UserId != subscriber.UserId) // if user updated user id) || dbPatron.UserId != subscriber.UserId) // if user updated user id)
{ {
// the user updated the pledge or changed the connected discord account // the user updated the pledge or changed the connected discord account
var newData = await ctx.GetTable<PatronUser>() var count = await ctx.GetTable<PatronUser>()
.Where(x => x.UniquePlatformUserId == subscriber.UniquePlatformUserId .Where(x => x.UniquePlatformUserId == subscriber.UniquePlatformUserId
&& x.LastCharge < lastChargeUtc) && x.LastCharge < lastChargeUtc)
.UpdateWithOutputAsync(old => new() .UpdateAsync(old => new()
{ {
UserId = subscriber.UserId, UserId = subscriber.UserId,
AmountCents = subscriber.Cents, AmountCents = subscriber.Cents,
LastCharge = lastChargeUtc, LastCharge = lastChargeUtc,
ValidThru = old.ValidThru, ValidThru = old.ValidThru,
}); });
await tran.CommitAsync();
// this should never happen if (count == 0)
if (newData.Length == 0) {
await tran.RollbackAsync();
continue; continue;
}
var newData = await ctx.GetTable<PatronUser>()
.FirstAsync(x => x.UniquePlatformUserId
== subscriber.UniquePlatformUserId);
await tran.CommitAsync();
await OnPatronUpdated(
PatronUserToPatron(dbPatron),
PatronUserToPatron(newData));
await OnPatronUpdated(PatronUserToPatron(dbPatron), PatronUserToPatron(newData[0].Inserted));
} }
} }
} }
@@ -262,22 +272,26 @@ public sealed class PatronageService
} }
} }
var expiredDate = DateTime.MinValue;
foreach (var patron in subscribers.Where(x => x.ChargeStatus == SubscriptionChargeStatus.Refunded)) foreach (var patron in subscribers.Where(x => x.ChargeStatus == SubscriptionChargeStatus.Refunded))
{ {
var expiredDate = DateTime.MinValue;
// if the subscription is refunded, Disable user's valid thru // if the subscription is refunded, Disable user's valid thru
var output = await ctx.GetTable<PatronUser>() var changedCount = await ctx.GetTable<PatronUser>()
.Where(x => x.UniquePlatformUserId == patron.UniquePlatformUserId .Where(x => x.UniquePlatformUserId == patron.UniquePlatformUserId
&& x.ValidThru != expiredDate) && x.ValidThru != expiredDate)
.UpdateWithOutputAsync(old => new() .UpdateAsync(old => new()
{ {
ValidThru = expiredDate ValidThru = expiredDate
}); });
if (output.Length == 0) if (changedCount == 0)
continue; continue;
await OnPatronRefunded(PatronUserToPatron(output[0].Inserted)); var updated = await ctx.GetTable<PatronUser>()
.Where(x => x.UniquePlatformUserId == patron.UniquePlatformUserId)
.FirstAsync();
await OnPatronRefunded(PatronUserToPatron(updated));
} }
} }