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