fix: added more notifications

This commit is contained in:
Damien Ostler 2024-02-22 21:34:03 -05:00
parent de95f333aa
commit 80502aa2d6
2 changed files with 96 additions and 6 deletions

View File

@ -5,6 +5,8 @@ using comissions.app.database.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Novu;
using Novu.DTO.Events;
namespace comissions.app.api.Controllers;
@ -15,9 +17,10 @@ public class AdminArtistRequestsController : Controller
{
private readonly ApplicationDbContext _dbContext;
private readonly IPaymentService _paymentService;
public AdminArtistRequestsController(ApplicationDbContext dbContext, IPaymentService paymentService)
private readonly NovuClient _client;
public AdminArtistRequestsController(NovuClient client, ApplicationDbContext dbContext, IPaymentService paymentService)
{
_client = client;
_paymentService = paymentService;
_dbContext = dbContext;
}
@ -126,6 +129,16 @@ public class AdminArtistRequestsController : Controller
request = _dbContext.ArtistRequests.Update(request).Entity;
await _dbContext.SaveChangesAsync();
var result = request.ToModel();
var newTriggerModel = new EventCreateData()
{
EventName = "artistrequestaccepted",
To =
{
SubscriberId = userId,
},
Payload = { }
};
await _client.Event.Trigger(newTriggerModel);
return Ok(result);
}
@ -143,6 +156,16 @@ public class AdminArtistRequestsController : Controller
_dbContext.ArtistRequests.Remove(request);
await _dbContext.SaveChangesAsync();
var newTriggerModel = new EventCreateData()
{
EventName = "artistrequestdenied",
To =
{
SubscriberId = userId,
},
Payload = { }
};
await _client.Event.Trigger(newTriggerModel);
return Ok();
}
}

View File

@ -9,6 +9,8 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Stripe;
using Novu;
using Novu.DTO.Events;
using Stripe.Checkout;
namespace comissions.app.api.Controllers;
@ -20,11 +22,12 @@ public class RequestsController : Controller
private readonly ApplicationDbContext _dbContext;
private readonly IStorageService _storageService;
private readonly IPaymentService _paymentService;
private readonly NovuClient _client;
private readonly string _webHookSecret;
public RequestsController(ApplicationDbContext dbContext, IPaymentService paymentService, IStorageService storageService, IConfiguration configuration)
public RequestsController(ApplicationDbContext dbContext, NovuClient client, IPaymentService paymentService, IStorageService storageService, IConfiguration configuration)
{
_client = client;
_webHookSecret = configuration.GetValue<string>("Stripe:WebHookSecret");
_paymentService = paymentService;
_storageService = storageService;
@ -746,6 +749,16 @@ public class RequestsController : Controller
await _dbContext.SaveChangesAsync();
var result = request.ToModel();
var newTriggerModel = new EventCreateData()
{
EventName = "requestcompleted",
To =
{
SubscriberId = request.UserId
},
Payload = { }
};
await _client.Event.Trigger(newTriggerModel);
return Ok(result);
}
@ -777,6 +790,26 @@ public class RequestsController : Controller
request.AcceptedDate = DateTime.UtcNow;
_dbContext.Entry(request).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
var newTriggerModel = new EventCreateData()
{
EventName = "requestacceptedbuyer",
To =
{
SubscriberId = request.UserId
},
Payload = { }
};
await _client.Event.Trigger(newTriggerModel);
var newTriggerArtistModel = new EventCreateData()
{
EventName = "requestacceptedartist",
To =
{
SubscriberId = request.Artist.UserId
},
Payload = { }
};
await _client.Event.Trigger(newTriggerModel);
var result = request.ToModel();
return Ok(result);
@ -809,6 +842,16 @@ public class RequestsController : Controller
_dbContext.Entry(request).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
var result = request.ToModel();
var newTriggerModel = new EventCreateData()
{
EventName = "requestdenied",
To =
{
SubscriberId = request.UserId
},
Payload = { }
};
await _client.Event.Trigger(newTriggerModel);
return Ok(result);
}
@ -821,15 +864,19 @@ public class RequestsController : Controller
.Where(x=>x.UserId==User.GetUserId())
.CountAsync();
var artist = await _dbContext.UserArtists.FirstOrDefaultAsync(x=>x.Id==model.ArtistId);
if(artist==null)
return NotFound("Artist not found.");
if(openRequests>3)
return BadRequest("You can only have 3 open requests at a time.");
var userId = User.GetUserId();
var request = new Request()
{
Amount = model.Amount,
Message = model.Message,
RequestDate = DateTime.UtcNow,
UserId = User.GetUserId(),
UserId = userId,
ArtistId = model.ArtistId,
Accepted = false,
AcceptedDate = null,
@ -840,6 +887,26 @@ public class RequestsController : Controller
};
_dbContext.Requests.Add(request);
await _dbContext.SaveChangesAsync();
var newArtistTriggerModel = new EventCreateData()
{
EventName = "requestcreatedbuyer",
To =
{
SubscriberId = userId,
},
Payload = { }
};
await _client.Event.Trigger(newArtistTriggerModel);
var newTriggerModel = new EventCreateData()
{
EventName = "requestcreatedartist",
To =
{
SubscriberId = artist.UserId,
},
Payload = { }
};
await _client.Event.Trigger(newTriggerModel);
return Ok(request.ToModel());
}
}