use of io.apiman.manager.api.beans.idm.UserDto in project apiman by apiman.
the class DefaultTemplatesTest method contract_approval_response_txt.
@Test
public void contract_approval_response_txt() throws IOException {
var appDeveloper = new UserDto().setFullName("John Smith Appdev").setUsername("JohnSmith123").setEmail("foo@apiman.io").setLocale(Locale.ENGLISH);
ContractApprovalEvent approvalEvent = ContractApprovalEvent.builder().setHeaders(ApimanEventHeaders.builder().setId("Event123").setSource(URI.create("https://example.org")).setSubject("Hello").setEventVersion(1L).setTime(OffsetDateTime.now()).setType("X").build()).setApiOrgId("ApiOrg").setApiId("CoolApi").setApiVersion("1.0").setClientOrgId("MobileKompany").setClientId("MobileApp").setClientVersion("2.0").setContractId("1234").setPlanId("Gold").setPlanVersion("1.3").setApprover(appDeveloper).setApproved(true).build();
UserDto recipient = new UserDto().setEmail("approver@apiman.io").setUsername("ApproverPerson").setFullName("David Approver").setLocale(Locale.ENGLISH);
NotificationDto<IVersionedApimanEvent> notificationDto = new NotificationDto<>().setId(123L).setCategory(NotificationCategory.API_ADMINISTRATION).setReason("whatever").setReasonMessage("hi").setStatus(NotificationStatus.OPEN).setCreatedOn(OffsetDateTime.now()).setModifiedOn(OffsetDateTime.now()).setRecipient(recipient).setSource("adsadsaad").setPayload(approvalEvent);
QuteTemplateEngine engine = new QuteTemplateEngine(CONFIG);
engine.applyTemplate(Files.readString(CONFIG.getConfigDirectory().resolve("notifications/email/tpl/en/apiman.client.contract.approval.granted.txt")), EmailNotificationDispatcher.createDefaultTemplateMap(notificationDto, CONFIG));
}
use of io.apiman.manager.api.beans.idm.UserDto in project apiman by apiman.
the class ContractService method fireContractApprovalRequest.
private void fireContractApprovalRequest(String requesterId, ContractBean contract) {
LOGGER.debug("Firing contract approval request from requester {0} on contract {1}", requesterId, contract);
UserDto requester = UserMapper.INSTANCE.toDto(tryAction(() -> storage.getUser(requesterId)));
ApimanEventHeaders headers = ApimanEventHeaders.builder().setId(UUID.randomUUID().toString()).setSource(URI.create("/apiman/events/contracts/approvals")).setSubject("request").build();
PlanVersionBean plan = contract.getPlan();
ClientVersionBean cvb = contract.getClient();
ApiVersionBean avb = contract.getApi();
OrganizationBean orgA = avb.getApi().getOrganization();
OrganizationBean orgC = cvb.getClient().getOrganization();
var approvalRequestEvent = ContractCreatedEvent.builder().setHeaders(headers).setUser(requester).setClientOrgId(orgC.getId()).setClientId(cvb.getClient().getId()).setClientVersion(cvb.getVersion()).setApiOrgId(orgA.getId()).setApiId(avb.getApi().getId()).setApiVersion(avb.getVersion()).setContractId(String.valueOf(contract.getId())).setPlanId(plan.getPlan().getId()).setPlanVersion(plan.getVersion()).setApprovalRequired(true).build();
LOGGER.debug("Sending approval request event {0}", approvalRequestEvent);
eventService.fireEvent(approvalRequestEvent);
}
use of io.apiman.manager.api.beans.idm.UserDto in project apiman by apiman.
the class NotificationService method sendNotification.
/**
* Send a new notification to a specified recipient (userId)
*
* @param newNotification the new notification.
*/
public void sendNotification(@NotNull CreateNotificationDto newNotification) {
if (!config.isEnabled()) {
LOGGER.trace("Notifications subsystem is disabled. Notification {0} will not be sent", newNotification);
return;
}
LOGGER.debug("Creating new notification(s): {0}", newNotification);
List<UserDto> resolvedRecipients = calculateRecipients(newNotification.getRecipient());
for (UserDto resolvedRecipient : resolvedRecipients) {
NotificationEntity notificationEntity = new NotificationEntity().setCategory(newNotification.getCategory()).setReason(newNotification.getReason()).setReasonMessage(newNotification.getReasonMessage()).setStatus(NotificationStatus.OPEN).setRecipient(resolvedRecipient.getUsername()).setSource(newNotification.getSource()).setPayload(JsonUtil.toJsonTree(newNotification.getPayload()));
tryAction(() -> {
// 1. Save notification into notifications table.
LOGGER.trace("Creating notification entity in repository layer: {0}", notificationEntity);
notificationRepository.create(notificationEntity);
// Avoiding serializing and deserializing the payload immediately!
NotificationDto<?> dto = toDto(notificationEntity, newNotification.getPayload(), resolvedRecipient);
// 2. Emit notification onto notification bus.
LOGGER.trace("Firing notification: {0}", dto);
notificationDispatcher.fireAsync(dto).whenCompleteAsync((res, throwable) -> {
if (throwable != null) {
LOGGER.error(throwable, "An error occurred in the asynchronous part of the notification subsystem. " + "This error likely did not return directly to the requester: {0}", throwable.getMessage());
}
});
});
}
}
use of io.apiman.manager.api.beans.idm.UserDto in project apiman by apiman.
the class ContractApprovalRequestEmailNotification method handle.
@Override
public void handle(NotificationDto<ContractCreatedEvent> notification, Map<String, Object> defaultTemplateMap) {
ContractCreatedEvent event = notification.getPayload();
if (event.isApprovalRequired()) {
UserDto recipient = notification.getRecipient();
mailNotificationService.findTemplateFor(notification.getReason(), recipient.getLocale()).ifPresentOrElse(template -> send(recipient, template, defaultTemplateMap), () -> warnOnce(recipient, notification));
}
}
use of io.apiman.manager.api.beans.idm.UserDto in project apiman by apiman.
the class ContractApprovalResponseEmailNotification method handle.
@Override
public void handle(NotificationDto<ContractApprovalEvent> notification, Map<String, Object> defaultTemplateMap) {
UserDto recipient = notification.getRecipient();
mailNotificationService.findTemplateFor(notification.getReason(), recipient.getLocale()).ifPresentOrElse(template -> send(recipient, template, defaultTemplateMap), () -> warnOnce(recipient, notification));
}
Aggregations