mirror of
				https://github.com/D4M13N-D3V/comissions-app-core-api.git
				synced 2025-10-31 01:25:27 +00:00 
			
		
		
		
	feat: redid the ban/suspension system
This commit is contained in:
		
							parent
							
								
									850288258a
								
							
						
					
					
						commit
						a3f137cb12
					
				| @ -49,5 +49,7 @@ public class ApplicationDbContext:DbContext | ||||
|     public DbSet<RequestReference> RequestReferences { get; set; }= null!; | ||||
|     public DbSet<RequestAsset> RequestAssets { get; set; }= null!; | ||||
|     public DbSet<ArtistRequestMessage> ArtistRequestMessages { get; set; }= null!; | ||||
|     public DbSet<Ban> Bans { get; set; }= null!; | ||||
|     public DbSet<Suspension> Suspensions { get; set; }= null!; | ||||
|     #endregion | ||||
| } | ||||
| @ -50,50 +50,6 @@ public class AdminArtistsController:ControllerBase | ||||
|     } | ||||
|      | ||||
|      | ||||
|     [HttpPut("{sellerId:int}/Suspend")] | ||||
|     public async Task<IActionResult> SuspendArtist(int sellerId, [FromQuery]string reason, [FromQuery]int days) | ||||
|     { | ||||
|         var seller = _dbContext.UserArtists.FirstOrDefault(x=>x.Id==sellerId); | ||||
|          | ||||
|         if (seller == null) | ||||
|             return NotFound(); | ||||
| 
 | ||||
|         if (seller.Suspended) | ||||
|             return BadRequest(); | ||||
|          | ||||
|         seller.Suspended = true; | ||||
|         seller.SuspendedDate = DateTime.UtcNow; | ||||
|         seller.UnsuspendDate = DateTime.UtcNow.AddDays(days); | ||||
|         seller.SuspendedReason = reason; | ||||
|         seller.SuspendAdminId = User.GetUserId(); | ||||
|         _dbContext.UserArtists.Update(seller); | ||||
|          | ||||
|         await _dbContext.SaveChangesAsync(); | ||||
|         return Ok(); | ||||
|     } | ||||
|      | ||||
|     [HttpPut("{sellerId:int}/Unsuspend")] | ||||
|     public async Task<IActionResult> UnsuspendArtist(int sellerId) | ||||
|     { | ||||
|         var seller = _dbContext.UserArtists.FirstOrDefault(x=>x.Id==sellerId); | ||||
|          | ||||
|         if (seller == null) | ||||
|             return NotFound(); | ||||
| 
 | ||||
|         if (!seller.Suspended) | ||||
|             return BadRequest(); | ||||
|          | ||||
|         seller.Suspended = false; | ||||
|         seller.SuspendedDate = null; | ||||
|         seller.UnsuspendDate = null; | ||||
|         seller.SuspendedReason = null; | ||||
|         seller.SuspendAdminId = null; | ||||
|         _dbContext.UserArtists.Update(seller); | ||||
|          | ||||
|         await _dbContext.SaveChangesAsync(); | ||||
|         return Ok(); | ||||
|     } | ||||
|      | ||||
|     [HttpPut("{sellerId:int}/Terminate")] | ||||
|     public async Task<IActionResult> TerminateArtist(int sellerId) | ||||
|     { | ||||
| @ -102,9 +58,6 @@ public class AdminArtistsController:ControllerBase | ||||
|         if (seller == null) | ||||
|             return NotFound(); | ||||
| 
 | ||||
|         if (!seller.Suspended) | ||||
|             return BadRequest(); | ||||
| 
 | ||||
|         _dbContext.UserArtists.Remove(seller); | ||||
|         await _dbContext.SaveChangesAsync(); | ||||
|         return Ok(); | ||||
| @ -118,9 +71,6 @@ public class AdminArtistsController:ControllerBase | ||||
|         if (seller == null) | ||||
|             return NotFound(); | ||||
| 
 | ||||
|         if (!seller.Suspended) | ||||
|             return BadRequest(); | ||||
| 
 | ||||
|         seller.Description = biography; | ||||
|         _dbContext.UserArtists.Update(seller); | ||||
|         await _dbContext.SaveChangesAsync(); | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| using comissions.app.api.Extensions; | ||||
| using comissions.app.database; | ||||
| using comissions.app.database.Entities; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| @ -56,12 +57,16 @@ public class AdminUsersController:ControllerBase | ||||
|         if (user == null) | ||||
|             return NotFound(); | ||||
|          | ||||
|         user.Suspended = true; | ||||
|         user.SuspendedDate = DateTime.UtcNow; | ||||
|         user.SuspendedReason = reason; | ||||
|         user.SuspendAdminId = User.GetUserId(); | ||||
|         user.UnsuspendDate = DateTime.UtcNow.AddDays(days); | ||||
|         _dbContext.Users.Update(user); | ||||
|         var newSuspension = new Suspension() | ||||
|         { | ||||
|             UserId = userId, | ||||
|             Reason = reason, | ||||
|             AdminId = User.GetUserId(), | ||||
|             SuspensionDate = DateTime.UtcNow, | ||||
|             UnsuspensionDate = DateTime.UtcNow.AddDays(days), | ||||
|             Voided = false | ||||
|         }; | ||||
|         _dbContext.Suspensions.Add(newSuspension); | ||||
|         await _dbContext.SaveChangesAsync(); | ||||
|         return Ok(); | ||||
|     } | ||||
| @ -73,13 +78,13 @@ public class AdminUsersController:ControllerBase | ||||
|          | ||||
|         if (user == null) | ||||
|             return NotFound(); | ||||
|         var suspension = await _dbContext.Suspensions.FirstOrDefaultAsync(x=>x.UserId==userId && x.UnsuspensionDate>DateTime.UtcNow); | ||||
| 
 | ||||
|         if (suspension == null) | ||||
|             return BadRequest(); | ||||
|          | ||||
|         user.Suspended = false; | ||||
|         user.SuspendedDate = null; | ||||
|         user.SuspendedReason = null; | ||||
|         user.SuspendAdminId = null; | ||||
|         user.UnsuspendDate = null; | ||||
|         _dbContext.Users.Update(user); | ||||
|         suspension.Voided = true; | ||||
|         _dbContext.Suspensions.Update(suspension); | ||||
|         await _dbContext.SaveChangesAsync(); | ||||
|         return Ok(); | ||||
|     } | ||||
| @ -92,12 +97,16 @@ public class AdminUsersController:ControllerBase | ||||
|         if (user == null) | ||||
|             return NotFound(); | ||||
|          | ||||
|         user.Banned = true; | ||||
|         user.BannedDate = DateTime.UtcNow; | ||||
|         user.BannedReason = reason; | ||||
|         user.BanAdminId = User.GetUserId(); | ||||
|         user.UnbanDate = DateTime.UtcNow.AddDays(days); | ||||
|         _dbContext.Users.Update(user); | ||||
|         var ban = new Ban() | ||||
|         { | ||||
|             UserId = userId, | ||||
|             Reason = reason, | ||||
|             AdminId = User.GetUserId(), | ||||
|             BanDate = DateTime.UtcNow, | ||||
|             UnbanDate = DateTime.UtcNow.AddDays(days), | ||||
|             Voided = false | ||||
|         }; | ||||
|         _dbContext.Bans.Add(ban); | ||||
|         await _dbContext.SaveChangesAsync(); | ||||
|         return Ok(); | ||||
|     } | ||||
| @ -110,11 +119,13 @@ public class AdminUsersController:ControllerBase | ||||
|         if (user == null) | ||||
|             return NotFound(); | ||||
|          | ||||
|         user.Banned = false; | ||||
|         user.BannedDate = null; | ||||
|         user.BannedReason = null; | ||||
|         user.BanAdminId = null; | ||||
|         user.UnbanDate = null; | ||||
|         var ban = await _dbContext.Bans.FirstOrDefaultAsync(x=>x.UserId==userId && x.UnbanDate>DateTime.UtcNow); | ||||
| 
 | ||||
|         if (ban == null) | ||||
|             return BadRequest(); | ||||
|          | ||||
|         ban.Voided = true; | ||||
|         _dbContext.Bans.Update(ban); | ||||
|         _dbContext.Users.Update(user); | ||||
|         await _dbContext.SaveChangesAsync(); | ||||
|         return Ok(); | ||||
|  | ||||
| @ -73,8 +73,6 @@ public class ArtistPaymentController:Controller | ||||
|             return BadRequest(); | ||||
|         } | ||||
|          | ||||
|         if(existingArtist.Suspended) | ||||
|             return BadRequest(); | ||||
|         var result = _paymentService.ArtistAccountIsOnboarded(existingArtist.StripeAccountId); | ||||
|         return Ok(new ArtistOnboardStatusModel(){ Onboarded= result }); | ||||
|     } | ||||
| @ -93,8 +91,7 @@ public class ArtistPaymentController:Controller | ||||
|                 return BadRequest(); | ||||
|             return Unauthorized(); | ||||
|         } | ||||
|         if(existingArtist.Suspended) | ||||
|             return BadRequest(); | ||||
|          | ||||
|         if(existingArtist.StripeAccountId==null) | ||||
|             return BadRequest(); | ||||
| 
 | ||||
|  | ||||
| @ -43,8 +43,6 @@ public class ArtistPortfolioController: Controller | ||||
|                 return BadRequest(); | ||||
|             return Unauthorized(); | ||||
|         } | ||||
|         if(existingArtist.Suspended) | ||||
|             return BadRequest(); | ||||
| 
 | ||||
|         var portfolio = await _dbContext.ArtistPortfolioPieces | ||||
|             .FirstAsync(x => x.ArtistId == existingArtist.Id && x.Id==portfolioId); | ||||
| @ -66,8 +64,7 @@ public class ArtistPortfolioController: Controller | ||||
|                 return BadRequest(); | ||||
|             return Unauthorized(); | ||||
|         } | ||||
|         if(existingArtist.Suspended) | ||||
|             return BadRequest(); | ||||
|          | ||||
|         var portfolio = await _dbContext.ArtistPortfolioPieces.Where(x=>x.ArtistId==existingArtist.Id).ToListAsync(); | ||||
|         var result = portfolio.Select(x=>x.ToModel()).ToList(); | ||||
|         return Ok(result); | ||||
| @ -85,8 +82,6 @@ public class ArtistPortfolioController: Controller | ||||
|             return BadRequest(); | ||||
|         } | ||||
| 
 | ||||
|         if(existingArtist.Suspended) | ||||
|             return BadRequest(); | ||||
|         var url = await _storageService.UploadImageAsync(HttpContext.Request.Body, Guid.NewGuid().ToString()); | ||||
|         var portfolio = new ArtistPortfolioPiece() | ||||
|         { | ||||
| @ -114,8 +109,7 @@ public class ArtistPortfolioController: Controller | ||||
|                 return BadRequest(); | ||||
|             return Unauthorized(); | ||||
|         } | ||||
|         if(existingArtist.Suspended) | ||||
|             return BadRequest(); | ||||
|          | ||||
|         var portfolio = await _dbContext.ArtistPortfolioPieces.FirstOrDefaultAsync(x=>x.Id==portfolioId); | ||||
|         if(portfolio==null) | ||||
|             return NotFound(); | ||||
|  | ||||
							
								
								
									
										14
									
								
								src/comissions.app.api/Entities/Ban.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/comissions.app.api/Entities/Ban.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| namespace comissions.app.database.Entities; | ||||
| 
 | ||||
| public class Ban | ||||
| { | ||||
|     public int Id { get; set; } | ||||
|     public string UserId { get; set; } | ||||
|     public DateTime BanDate { get; set; } | ||||
|     public DateTime UnbanDate { get; set; } | ||||
|     public bool Voided { get; set; } = false; | ||||
|     public string Reason { get; set; } | ||||
|     public string AdminId { get; set; } | ||||
|     public virtual User Admin { get; set; } | ||||
|     public virtual User User { get; set; } | ||||
| } | ||||
							
								
								
									
										14
									
								
								src/comissions.app.api/Entities/Suspension.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/comissions.app.api/Entities/Suspension.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| namespace comissions.app.database.Entities; | ||||
| 
 | ||||
| public class Suspension | ||||
| { | ||||
|     public int Id { get; set; } | ||||
|     public string UserId { get; set; } | ||||
|     public DateTime SuspensionDate { get; set; } | ||||
|     public DateTime UnsuspensionDate { get; set; } | ||||
|     public bool Voided { get; set; } = false; | ||||
|     public string Reason { get; set; } | ||||
|     public string AdminId { get; set; } | ||||
|     public virtual User Admin { get; set; } | ||||
|     public virtual User User { get; set; } | ||||
| } | ||||
| @ -10,19 +10,10 @@ public record User | ||||
|     public string DisplayName { get; set; } = null!; | ||||
|     public string Biography { get; set; } = null!; | ||||
|     public string Email { get; set; } = null!; | ||||
|      | ||||
|     public int? UserArtistId { get; set; } | ||||
|      | ||||
|     public bool Banned { get; set; } = false; | ||||
|     public DateTime? BannedDate { get; set; } | ||||
|     public DateTime? UnbanDate { get; set; } | ||||
|     public string? BannedReason { get; set; } | ||||
|     public string? BanAdminId { get; set; } | ||||
|      | ||||
|     public bool Suspended { get; set; } = false; | ||||
|     public DateTime? SuspendedDate { get; set; } | ||||
|     public DateTime? UnsuspendDate { get; set; } | ||||
|     public string? SuspendedReason { get; set; } | ||||
|     public string? SuspendAdminId { get; set; } | ||||
|      | ||||
|     [JsonIgnore] public virtual UserArtist? UserArtist { get; set; } | ||||
|     [JsonIgnore] public virtual ICollection<Request> Requests { get; set; } = new List<Request>(); | ||||
|     [JsonIgnore] public virtual ICollection<Suspension> Suspensions { get; set; } = new List<Suspension>(); | ||||
|     [JsonIgnore] public virtual ICollection<Ban> Bans { get; set; } = new List<Ban>(); | ||||
| } | ||||
| @ -17,11 +17,6 @@ public record UserArtist | ||||
|     public bool AgeRestricted { get; set; } | ||||
|     public string? StripeAccountId { get; set; } | ||||
|     public bool PrepaymentRequired { get; set; } = false; | ||||
|     public bool Suspended { get; set; } = false; | ||||
|     public DateTime? SuspendedDate { get; set; } | ||||
|     public DateTime? UnsuspendDate { get; set; } | ||||
|     public string? SuspendedReason { get; set; } | ||||
|     public string? SuspendAdminId { get; set; } | ||||
|     public virtual User User { get; set; } = null!; | ||||
|      | ||||
|     public int ArtistPageSettingsId { get; set; } | ||||
|  | ||||
| @ -27,7 +27,10 @@ public class UserMiddleware | ||||
|         { | ||||
|             var userId = context.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; | ||||
| 
 | ||||
|             var user = await dbContext.Users.Include(x=>x.UserArtist).FirstOrDefaultAsync(x=>x.Id==userId); | ||||
|             var user = await dbContext.Users.Include(x=>x.UserArtist) | ||||
|                 .Include(x=>x.Bans).ThenInclude(x=>x.Admin) | ||||
|                 .Include(x=>x.Suspensions).ThenInclude(x=>x.Admin) | ||||
|                 .FirstOrDefaultAsync(x=>x.Id==userId); | ||||
| 
 | ||||
|             if (user == null) | ||||
|             { | ||||
| @ -59,71 +62,24 @@ public class UserMiddleware | ||||
|                 Email = user.Email | ||||
|             }; | ||||
|             var subscriber = await _client.Subscriber.Create(newSubscriberDto); | ||||
|              | ||||
|             if (user.Suspended) | ||||
|             var suspension = user.Suspensions.FirstOrDefault(x => x.UnsuspensionDate > DateTime.UtcNow && x.Voided==false); | ||||
|             if (suspension!=null) | ||||
|             { | ||||
|                 if (user.UnsuspendDate < DateTime.UtcNow) | ||||
|                 { | ||||
|                     user.Suspended = false; | ||||
|                     user.SuspendedDate = null; | ||||
|                     user.UnsuspendDate = null; | ||||
|                     user.SuspendedReason = null; | ||||
|                     user.SuspendAdminId = null; | ||||
|                     dbContext.Users.Update(user); | ||||
|                     await dbContext.SaveChangesAsync(); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     var suspendDate = user.SuspendedDate.Value.ToString("MM/dd/yyyy"); | ||||
|                     var unsuspendDate = user.UnsuspendDate.Value.ToString("MM/dd/yyyy"); | ||||
|                     await context.Response.WriteAsync($"Suspended on {suspendDate} until {unsuspendDate} for {user.SuspendedReason} by {user.SuspendAdminId}."); | ||||
|                     context.Response.StatusCode = StatusCodes.Status403Forbidden; | ||||
|                     return; | ||||
|                 } | ||||
|                 var suspendDate = suspension.SuspensionDate.ToString("MM/dd/yyyy"); | ||||
|                 var unsuspendDate = suspension.UnsuspensionDate.ToString("MM/dd/yyyy"); | ||||
|                 await context.Response.WriteAsync($"Suspended on {suspendDate} until {unsuspendDate} for {suspension.Reason} by {suspension.Admin.DisplayName}."); | ||||
|                 context.Response.StatusCode = StatusCodes.Status403Forbidden; | ||||
|                 return; | ||||
|             } | ||||
| 
 | ||||
|             if (user.Banned) | ||||
|             var ban = user.Bans.FirstOrDefault(x => x.UnbanDate > DateTime.UtcNow && x.Voided==false); | ||||
|             if (ban!=null) | ||||
|             { | ||||
|                 if (user.UnsuspendDate < DateTime.UtcNow) | ||||
|                 { | ||||
|                     user.Banned = false; | ||||
|                     user.BannedDate = null; | ||||
|                     user.BannedDate = null; | ||||
|                     user.BannedReason = null; | ||||
|                     user.BanAdminId = null; | ||||
|                     dbContext.Users.Update(user); | ||||
|                     await dbContext.SaveChangesAsync(); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     var suspendDate = user.BannedDate.Value.ToString("MM/dd/yyyy"); | ||||
|                     var unsuspendDate = user.UnbanDate.Value.ToString("MM/dd/yyyy"); | ||||
|                     await context.Response.WriteAsync($"Banned on {suspendDate} until {unsuspendDate} for {user.BannedReason} by {user.BanAdminId}."); | ||||
|                     context.Response.StatusCode = StatusCodes.Status403Forbidden; | ||||
|                     return; | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             if (user.UserArtist != null && user.UserArtist.Suspended) | ||||
|             { | ||||
|                 if (user.UserArtist.UnsuspendDate < DateTime.UtcNow) | ||||
|                 { | ||||
|                     user.UserArtist.Suspended = false; | ||||
|                     user.UserArtist.SuspendedDate = null; | ||||
|                     user.UserArtist.UnsuspendDate = null; | ||||
|                     user.UserArtist.SuspendedReason = null; | ||||
|                     user.UserArtist.SuspendAdminId = null; | ||||
|                     dbContext.Users.Update(user); | ||||
|                     await dbContext.SaveChangesAsync(); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     var suspendDate = user.UserArtist.SuspendedDate.Value.ToString("MM/dd/yyyy"); | ||||
|                     var unsuspendDate = user.UserArtist.UnsuspendDate.Value.ToString("MM/dd/yyyy"); | ||||
|                     await context.Response.WriteAsync($"Banned on {suspendDate} until {unsuspendDate} for {user.UserArtist.SuspendedReason} by {user.UserArtist.SuspendAdminId}."); | ||||
|                     context.Response.StatusCode = StatusCodes.Status403Forbidden; | ||||
|                     return; | ||||
|                 } | ||||
|                 var suspendDate = ban.BanDate.ToString("MM/dd/yyyy"); | ||||
|                 var unsuspendDate = ban.UnbanDate.ToString("MM/dd/yyyy"); | ||||
|                 await context.Response.WriteAsync($"Banned on {suspendDate} until {unsuspendDate} for {ban.Reason} by {ban.Admin.DisplayName}."); | ||||
|                 context.Response.StatusCode = StatusCodes.Status403Forbidden; | ||||
|                 return; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Damien Ostler
						Damien Ostler