Untitled
unknown
plain_text
a year ago
1.9 kB
14
Indexable
@Service
public class WebhookService {
private static final Logger log = LoggerFactory.getLogger(WebhookService.class);
private final WebClient webClient = CustomWebClientBuilder.createWebClientBuilderWithDisabledSSLVerification().build();
private final WebhookConfig webhookConfig;
public WebhookService(WebhookConfig webhookConfig) {
this.webhookConfig = webhookConfig;
}
public Mono<Void> notifyWebhooks(String event, Object data) {
List<String> webhookUrls = this.webhookConfig.getUrls();
return Flux.fromIterable(webhookUrls).flatMap((url) -> {
return this.sendNotification(url, event, data);
}).then();
}
private Mono<Void> sendNotification(String url, String event, Object data) {
WebhookTr payload = new WebhookTr(event, data);
return ((WebClient.RequestBodySpec)this.webClient.post().uri(url, new Object[0])).bodyValue(payload).retrieve().bodyToMono(Void.class).onErrorResume((error) -> {
log.error("Failed to send webhook to {} {}: ", url, error.getMessage());
return Mono.empty();
});
}
}
public Mono<UserTr> saveRoleAndPermissionForUser(String userName, String roleName, Long initiativeId) {
UserRolePermissionTr roleAndPermission = UserRolePermissionTr.builder().userId(userName).roleId(roleName).initiativeId(initiativeId).insertedAt(LocalDateTime.now()).build();
return this.userPersistencePort.saveRoleAndPermissionForUser(roleAndPermission).doOnSuccess((userTr) -> {
this.webhookService.notifyWebhooks("user.creation", userTr);
});
}
public Mono<Void> delete(@NotNull String userName) {
return this.userPersistencePort.delete(userName).doOnSuccess((tr) -> {
this.webhookService.notifyWebhooks("user.deletion", userName);
});
}Editor is loading...
Leave a Comment