| | 1 | | using System.Diagnostics; |
| | 2 | | using Syki.Back.Features.Academic.CallWebhooks; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Features.Academic.CreateStudent; |
| | 5 | |
|
| | 6 | | [CommandDescription("Criar chamada de webhook para o evento 'Aluno criado'")] |
| | 7 | | public record CreateStudentCreatedWebhookCallCommand(DomainEventId EventId, Guid WebhookId, Guid UserId) : ICommand; |
| | 8 | |
|
| 0 | 9 | | public class CreateStudentCreatedWebhookCallCommandHandler(SykiDbContext ctx) : ICommandHandler<CreateStudentCreatedWebh |
| | 10 | | { |
| | 11 | | public async Task Handle(CommandId commandId, CreateStudentCreatedWebhookCallCommand command) |
| | 12 | | { |
| 0 | 13 | | var activityId = Activity.Current?.Id; |
| | 14 | |
|
| 0 | 15 | | var student = await ctx.Students.AsNoTracking() |
| 0 | 16 | | .Include(x => x.User) |
| 0 | 17 | | .Where(x => x.Id == command.UserId) |
| 0 | 18 | | .Select(x => new { x.Id, x.Name, x.User.Email }) |
| 0 | 19 | | .FirstAsync(); |
| | 20 | |
|
| 0 | 21 | | var webhook = await ctx.Webhooks.AsNoTracking() |
| 0 | 22 | | .Include(x => x.Authentication) |
| 0 | 23 | | .Where(x => x.Id == command.WebhookId) |
| 0 | 24 | | .Select(x => new { x.InstitutionId, x.Authentication.ApiKey }) |
| 0 | 25 | | .FirstAsync(); |
| | 26 | |
|
| 0 | 27 | | var payload = new StudentCreatedWebhookPayload(student.Id, student.Name, student.Email); |
| 0 | 28 | | var call = new WebhookCall( |
| 0 | 29 | | webhook.InstitutionId, |
| 0 | 30 | | command.WebhookId, |
| 0 | 31 | | payload, |
| 0 | 32 | | command.EventId, |
| 0 | 33 | | WebhookEventType.StudentCreated |
| 0 | 34 | | ); |
| 0 | 35 | | ctx.Add(call); |
| 0 | 36 | | } |
| | 37 | | } |