Search in sources :

Example 1 with UserDto

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));
}
Also used : IVersionedApimanEvent(io.apiman.manager.api.beans.events.IVersionedApimanEvent) NotificationDto(io.apiman.manager.api.beans.notifications.dto.NotificationDto) QuteTemplateEngine(io.apiman.manager.api.notifications.email.QuteTemplateEngine) UserDto(io.apiman.manager.api.beans.idm.UserDto) ContractApprovalEvent(io.apiman.manager.api.beans.events.ContractApprovalEvent) Test(org.junit.Test)

Example 2 with UserDto

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);
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) UserDto(io.apiman.manager.api.beans.idm.UserDto) ApimanEventHeaders(io.apiman.manager.api.beans.events.ApimanEventHeaders) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean)

Example 3 with UserDto

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());
                }
            });
        });
    }
}
Also used : UserDto(io.apiman.manager.api.beans.idm.UserDto) NotificationEntity(io.apiman.manager.api.beans.notifications.NotificationEntity)

Example 4 with UserDto

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));
    }
}
Also used : ContractCreatedEvent(io.apiman.manager.api.beans.events.ContractCreatedEvent) UserDto(io.apiman.manager.api.beans.idm.UserDto)

Example 5 with UserDto

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));
}
Also used : UserDto(io.apiman.manager.api.beans.idm.UserDto)

Aggregations

UserDto (io.apiman.manager.api.beans.idm.UserDto)11 ContractCreatedEvent (io.apiman.manager.api.beans.events.ContractCreatedEvent)4 IVersionedApimanEvent (io.apiman.manager.api.beans.events.IVersionedApimanEvent)4 NotificationDto (io.apiman.manager.api.beans.notifications.dto.NotificationDto)4 QuteTemplateEngine (io.apiman.manager.api.notifications.email.QuteTemplateEngine)4 Test (org.junit.Test)4 ContractApprovalEvent (io.apiman.manager.api.beans.events.ContractApprovalEvent)2 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)1 ClientVersionBean (io.apiman.manager.api.beans.clients.ClientVersionBean)1 ApimanEventHeaders (io.apiman.manager.api.beans.events.ApimanEventHeaders)1 NotificationEntity (io.apiman.manager.api.beans.notifications.NotificationEntity)1 OrganizationBean (io.apiman.manager.api.beans.orgs.OrganizationBean)1 PlanVersionBean (io.apiman.manager.api.beans.plans.PlanVersionBean)1 Before (org.junit.Before)1