All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.foodtechlab.lib.auth.app.config.sender.SmsConfirmationCodeSender Maven / Gradle / Ivy

The newest version!
package ru.foodtechlab.lib.auth.app.config.sender;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import ru.foodtechlab.lib.auth.service.domain.confirmationCode.entity.ConfirmationCodeEntity;
import ru.foodtechlab.lib.auth.service.domain.confirmationCode.port.ConfirmationCodeSender;

@Slf4j
@RequiredArgsConstructor
@Component
public class SmsConfirmationCodeSender implements ConfirmationCodeSender {

    private final RestTemplate restTemplate = new RestTemplate();

    @Override
    public void send(ConfirmationCodeEntity confirmationCodeEntity) {
        var url = confirmationCodeEntity.getWebhookProviderUrl();

        if (url == null)
            return;

        var requestBody = Request.builder()
                .id(confirmationCodeEntity.getId())
                .message(confirmationCodeEntity.getMessage())
                .phoneNumber(confirmationCodeEntity.getAuthSession().getLoginDetails().getPhoneNumber().getValue())
                .isoTwoLetterCountryCode(confirmationCodeEntity.getAuthSession().getLoginDetails().getPhoneNumber().getIsoTwoLetterCountryCode())
                .header(confirmationCodeEntity.getHeader())
                .applicationDetails(EmailConfirmationCodeSender.Request.ApplicationDetails.builder()
                        .name(confirmationCodeEntity.getAuthSession().getClientInfo().getApplication())
                        .platform(confirmationCodeEntity.getAuthSession().getClientInfo().getPlatform())
                        .versionName(confirmationCodeEntity.getAuthSession().getClientInfo().getVersionName())
                        .build())
                .build();
        HttpEntity request = new HttpEntity<>(requestBody);
        Response foo = restTemplate.postForObject(url, request, Response.class);
        log.info("Confirmation code sent to SMS webhook " + url);
    }

    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @Data
    public static class Request {
        private String id;
        private String phoneNumber;
        private String isoTwoLetterCountryCode;
        private String header;
        private String message;
        private EmailConfirmationCodeSender.Request.ApplicationDetails applicationDetails;

        @AllArgsConstructor
        @NoArgsConstructor
        @Builder
        @Data
        public static class ApplicationDetails {
            private String name;
            private String platform;
            private String versionName;
        }
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    private static class Response {
        String result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy