| | | 1 | | using Estud.Back.Domain.Webhooks; |
| | | 2 | | |
| | | 3 | | namespace Estud.Back.Features.Webhooks.CreateWebhookSubscription; |
| | | 4 | | |
| | 36 | 5 | | public class CreateWebhookSubscriptionService(EstudDbContext ctx) : IEstudService |
| | | 6 | | { |
| | | 7 | | private class Validator : AbstractValidator<CreateWebhookSubscriptionIn> |
| | | 8 | | { |
| | 2 | 9 | | public Validator() |
| | | 10 | | { |
| | 2 | 11 | | RuleFor(x => x.Name).NotEmpty().WithError(InvalidWebhookName.I); |
| | 2 | 12 | | RuleFor(x => x.Name).MaximumLength(100).WithError(InvalidWebhookName.I); |
| | | 13 | | |
| | 2 | 14 | | RuleFor(x => x.Url).NotEmpty().WithError(InvalidWebhookUrl.I); |
| | 38 | 15 | | RuleFor(x => x.Url).Must(x => Uri.TryCreate(x, UriKind.Absolute, out _)).WithError(InvalidWebhookUrl.I); |
| | | 16 | | |
| | 38 | 17 | | RuleFor(x => x.Events).Must(x => x != null && x.Count > 0).WithError(InvalidWebhookEvents.I); |
| | | 18 | | |
| | 2 | 19 | | RuleFor(x => x.CustomHeaders).Must(WebhookCustomHeaders.IsValid).WithError(InvalidWebhookCustomHeaders.I); |
| | 2 | 20 | | } |
| | | 21 | | } |
| | 2 | 22 | | private static readonly Validator V = new(); |
| | | 23 | | |
| | | 24 | | public async Task<OneOf<CreateWebhookSubscriptionOut, EstudError>> Create(CreateWebhookSubscriptionIn data) |
| | | 25 | | { |
| | 48 | 26 | | if (V.Run(data, out var error)) return error; |
| | | 27 | | |
| | 24 | 28 | | var subscription = new WebhookSubscription(ctx.RequestUser.InstitutionId, data.Name, data.Url, data.Events, data |
| | 24 | 29 | | await ctx.SaveChangesAsync(subscription); |
| | | 30 | | |
| | 24 | 31 | | return new CreateWebhookSubscriptionOut { Id = subscription.Id }; |
| | 36 | 32 | | } |
| | | 33 | | } |