Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
3
Indexable
Never
public class SaleOrderShareReportComponent {
    private static final Long TIMEOUT_PLATAFORM = 25_000L;
    
    @Inject
    private SaleOrderShareReportRepository orderShareReportRepository;
    
    @Inject
    private ProductImageBlobService productImageBlobService;
    
    @Inject
    private PersonalizationStub personalizationStub;
    
    @Inject
    private TranslationHubApi translation;
    
    public void createPurchaseOrderShareReport(OrderReport orderReport, String reportTicket) {
        if(EnumSet.of(EnumTypeOrderReport.V1, EnumTypeOrderReport.V2).contains(orderReport.getType())) {
            var orderShareReportEntity = new SaleOrderShareReportEntity();
            orderShareReportEntity.setOrderReportType(orderReport.getType());
            orderShareReportEntity.setReportTicket(reportTicket);
            orderShareReportEntity.setPhoneNumber(ConvertUtils.extractDigits(orderReport.getPhoneNumber()));
            orderShareReportEntity.setEmail(orderReport.getEmail());
            orderShareReportEntity.setE120ped(orderReport.getOrderId().stream().findFirst().map(UUID::fromString).orElse(null));
            orderShareReportRepository.save(orderShareReportEntity);
        }
    }
    
    public List<ParameterValue> getParams(String id) {
        List<ParameterValue> params = new ArrayList<>();

        params.add(new ParameterValue("pedido_id", id));
        List<ProductFile> productFiles = productImageBlobService.retrieveProductFilesFromBlobService(UUID.fromString(id));
        if(CollectionUtils.isNotEmpty(productFiles)) {
            params.add(new ParameterValue("productFiles", productFiles));
        }
        
        String logoURIImage = this.getLogoURIImage();

        if (nonNull(logoURIImage)) {
            params.add(new ParameterValue("logo-image", logoURIImage));
        }
            
        return params;
    }

    private String getLogoURIImage() {
        try {
            log.info("Buscando dados de personlização");

            var obtainPersonalizationSettings = personalizationStub.obtainPersonalizationSettings(
                    new ObtainPersonalizationSettingsInput(ServiceContext.get().getCurrentTenant()), TIMEOUT_PLATAFORM);

            log.info("Dados retornados, validando URI de logo {}", obtainPersonalizationSettings.logoImageURL);

            var urlValidator = new UrlValidator();
            return urlValidator.isValid(obtainPersonalizationSettings.logoImageURL)
                    ? obtainPersonalizationSettings.logoImageURL
                    : null;
        } catch (Exception e) {
            log.error("erro ao recuperar logo: ", e);
            throw new ServiceException(ErrorCategory.BAD_REQUEST, this.translation
                    .getFormattedMessage(TranslationConstants.PERSONALIZATION_DATA_ERROR, e.getMessage()));
        }
    }
}