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