added automatic deletion of polls

This commit is contained in:
Sarah Faey 2023-02-13 23:12:05 +01:00
parent b6dd3ff9c4
commit ac3cc69b68
7 changed files with 846 additions and 7 deletions

View file

@ -46,7 +46,7 @@ namespace Lieb.Data
.Where(p => p.RaidId == raidId).ToList();
}
public async Task<int> CreatePoll(string question, List<string> options, int raidId)
public async Task<int> CreatePoll(string question, List<string> options, int raidId, bool isAutoPoll = false)
{
Poll poll = new Poll()
{
@ -62,10 +62,10 @@ namespace Lieb.Data
Name = option
});
}
return await CreatePoll(poll, raidId);
return await CreatePoll(poll, raidId, isAutoPoll);
}
public async Task<int> CreatePoll(Poll poll, int raidId)
public async Task<int> CreatePoll(Poll poll, int raidId, bool isAutoPoll = false)
{
using var context = _contextFactory.CreateDbContext();
Raid? raid = context.Raids
@ -74,12 +74,13 @@ namespace Lieb.Data
if (raid == null) return 0;
HashSet<ulong> users = raid.SignUps.Where(s => s.LiebUserId != null && s.IsMessageSignUp).Select(s => (ulong)s.LiebUserId).ToHashSet();
return await CreatePoll(poll, users, raidId);
return await CreatePoll(poll, users, raidId, isAutoPoll);
}
public async Task<int> CreatePoll(Poll poll, HashSet<ulong> users, int? raidId = null)
public async Task<int> CreatePoll(Poll poll, HashSet<ulong> users, int? raidId = null, bool isAutoPoll = false)
{
poll.RaidId = raidId;
poll.IsAutoPoll = isAutoPoll;
foreach(ulong user in users)
{
@ -170,5 +171,19 @@ namespace Lieb.Data
poll.Answers.Remove(answer);
await context.SaveChangesAsync();
}
public async Task DeleteAutoPolls()
{
using var context = _contextFactory.CreateDbContext();
List<Poll> polls = context.Polls.ToList();
foreach(Poll poll in polls)
{
if((poll.IsAutoPoll && poll.CreatedAt < DateTime.UtcNow.AddDays(-7)))
{
await DeletePoll(poll.PollId);
}
}
}
}
}

View file

@ -704,7 +704,7 @@ namespace Lieb.Data
{
raid.MinUserPollId = await _pollService.CreatePoll(
"The raid has not the required users, do you want to raid anyway?",
new List<string>() {Constants.Polls.YES, Constants.Polls.NO }, raid.RaidId);
new List<string>() {Constants.Polls.YES, Constants.Polls.NO }, raid.RaidId, true);
await context.SaveChangesAsync();
await _discordService.PostRaidMessage(raid.RaidId);
}

View file

@ -73,6 +73,10 @@ namespace Lieb.Data
scope.ServiceProvider
.GetRequiredService<UserService>();
await userService.DeleteInactiveUsers();
var pollService =
scope.ServiceProvider
.GetRequiredService<PollService>();
await pollService.DeleteAutoPolls();
}
}