Untitled

mail@pastecode.io avatar
unknown
plain_text
6 days ago
9.2 kB
3
Indexable
Never
package pl.santander.bancassurance.comparison.document.service.mail.apk;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pl.santander.bancassurance.comparison.document.docsend.integration.api.SendMailService;
import pl.santander.bancassurance.comparison.document.docsend.integration.api.dto.DocSendResponseDto;
import pl.santander.bancassurance.comparison.document.domain.mailrequest.MailRequest;
import pl.santander.bancassurance.comparison.document.domain.mailrequest.MailRequestRepository;
import pl.santander.bancassurance.comparison.document.domain.mailrequest.MailRequestTemplateType;
import pl.santander.bancassurance.comparison.document.service.mail.MailDocumentType;

import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
@RequiredArgsConstructor
@EnableConfigurationProperties(DocSendApkDocumentProperties.class)
public class SignedApkDocumentMailService {

    private final SendMailService sendMailService;
    private final DocSendApkDocumentProperties properties;
    private final MailRequestRepository mailRequestRepository;

    @Transactional
    public void send(ApkDocumentMailRequestDto mailRequestDto,
                     String apkNodeId,
                     UUID applicationNumber,
                     UUID offerNumber) {
        final var docSendRequestDto =
                new DocSendApkDocumentDtoConverter(
                        properties,
                        Map.of(MailDocumentType.APK_DOCUMENT, apkNodeId),
                        mailRequestDto
                ).convert();
        final var response = sendMailService.sendMailRequest(docSendRequestDto);
        storeRequest(response, mailRequestDto.email(), applicationNumber, offerNumber);
    }

    private void storeRequest(DocSendResponseDto responseDto,
                              String email,
                              UUID applicationNumber,
                              UUID offerNumber) {
        final var mailRequest = MailRequest.builder()
                .applicationNumber(applicationNumber)
                .offerNumber(offerNumber)
                .email(email)
                .deliveryId(responseDto.getDeliveryId())
                .templateType(MailRequestTemplateType.SIGNED_APK_DOCUMENT)
                .documentSentContent(responseDto.getRequestContent())
                .build();
        mailRequestRepository.save(mailRequest);
    }

}


package pl.santander.bancassurance.comparison.document.service.mail.apk;

public record ApkDocumentMailRequestDto(String cif,
                                        String email,
                                        String pesel,
                                        String registrationNumber) {
}


package pl.santander.bancassurance.comparison.document.docsend.integration.api.dto;


import lombok.*;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
public class DocSendResponseDto {
    private String deliveryId;
    private String requestContent;
}

package pl.santander.bancassurance.comparison.document.domain.mailrequest;

import jakarta.persistence.*;
import lombok.*;

import java.io.Serializable;
import java.util.UUID;

@Entity
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class MailRequest implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "application_number", nullable = false)
    private UUID applicationNumber;

    @Column(name = "offer_number")
    private UUID offerNumber;

    @Column(name = "policy_number")
    private String policyNumber;

    @Column(name = "email", nullable = false)
    private String email;

    @Column(name = "delivery_id", nullable = false)
    private String deliveryId;

    @Enumerated(EnumType.STRING)
    @Column(name = "template_type", nullable = false)
    private MailRequestTemplateType templateType;

    @Column(name = "document_sent_content", nullable = false)
    private String documentSentContent;

}

package pl.santander.bancassurance.comparison.document.domain.mailrequest;

public enum MailRequestTemplateType {

    SIGNED_APK_DOCUMENT,
    SIGNED_POLICY_DOCUMENT

}


package pl.santander.bancassurance.comparison.document.service.mail.apk;

import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import pl.santander.bancassurance.comparison.document.docsend.integration.api.dto.DocSendRequestDto;
import pl.santander.bancassurance.comparison.document.docsend.integration.api.dto.Document;
import pl.santander.bancassurance.comparison.document.docsend.integration.api.dto.ProtectionMethod;
import pl.santander.bancassurance.comparison.document.service.mail.MailDocumentType;
import pl.santander.bancassurance.comparison.document.service.mail.PasswordGenerator;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
class DocSendApkDocumentDtoConverter {

    private final DocSendApkDocumentProperties properties;
    private final Map<MailDocumentType, String> documents;
    private final ApkDocumentMailRequestDto mailRequestDto;

    DocSendRequestDto convert() {
        return new DocSendRequestDto(
                properties.getBusinessProcessId(),
                properties.getWfd(),
                properties.getTemplateType(),
                properties.getInsuranceType(),
                properties.getPasswordDescription(),
                properties.getAttachments(),
                properties.getContactMethods(),
                properties.isProtectDocuments(),
                ProtectionMethod.valueOf(properties.getProtectionMethod()),
                mailRequestDto.cif(),
                documents(),
                properties.getDeliveryType(),
                mailRequestDto.email(),
                password(),
                properties.getSubject(),
                properties.getCustomerSegment()
        );
    }

    private String password() {
        return new PasswordGenerator(mailRequestDto.pesel(), mailRequestDto.registrationNumber()).generate();
    }

    private List<Document> documents() {
        if (!ObjectUtils.isEmpty(documents) && ObjectUtils.isEmpty(properties.getDocuments())) {
            throw new IllegalStateException("No configurations for documents");
        }

        return documents.entrySet()
                .stream()
                .map(entry -> {
                    final var nodeId = properties.getDocuments().get(entry.getKey());
                    if (nodeId == null) {
                        throw new IllegalStateException("No configurations for document type: " + entry.getKey());
                    }

                    return new Document(entry.getValue(), nodeId);
                }).toList();
    }

}
package pl.santander.bancassurance.comparison.document.service.mail;

public enum MailDocumentType {
    APK_DOCUMENT
}


package pl.santander.bancassurance.comparison.document.service.mail.apk;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import pl.santander.bancassurance.comparison.document.service.mail.DocSendBasicProperties;

@EqualsAndHashCode(callSuper = true)
@Data
@ConfigurationProperties(prefix = "doc-send.signed-apk-document")
public class DocSendApkDocumentProperties extends DocSendBasicProperties {
}


package pl.santander.bancassurance.comparison.document.service.mail;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;
import java.util.Map;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class DocSendBasicProperties {
    private String customerSegment;
    private String businessProcessId;
    private String wfd;
    private String templateType;
    private String insuranceType;
    private List<String> attachments;
    private List<String> contactMethods;
    private boolean protectDocuments;
    private String protectionMethod;
    private Map<MailDocumentType, String> documents;
    private String deliveryType;
    private String passwordDescription;
    private String subject;
}


package pl.santander.bancassurance.comparison.document.service.mail;

import jakarta.validation.constraints.NotNull;

public class PasswordGenerator {
    private final String pesel;
    private final String registrationNumber;

    public PasswordGenerator(@NotNull String pesel, @NotNull String registrationNumber) {
        this.pesel = pesel;
        this.registrationNumber = registrationNumber;
    }

    public String generate() {
        return pesel + registrationNumber.substring(registrationNumber.length() - 3);
    }

}
Leave a Comment