Search in sources :

Example 6 with PricePlan

use of io.codekvast.common.customer.PricePlan in project codekvast by crispab.

the class CustomerServiceImpl method changePlanForExternalId.

@Override
@Transactional(rollbackFor = Exception.class)
@CacheEvict(value = CUSTOMERS_CACHE, allEntries = true)
public void changePlanForExternalId(String source, @NonNull String externalId, @NonNull String newPlanName) {
    CustomerData customerData = getCustomerDataByExternalId(source, externalId);
    PricePlan oldEffectivePricePlan = customerData.getPricePlan();
    String oldPlanName = oldEffectivePricePlan.getName();
    if (newPlanName.equals(oldPlanName)) {
        logger.info("{} is already on plan '{}'", customerData, newPlanName);
        return;
    }
    int count = jdbcTemplate.update("UPDATE customers SET plan = ? WHERE source = ? AND externalId = ? ORDER BY id ", newPlanName, source, externalId);
    if (count == 0) {
        logger.error("Failed to change plan for customer {} from '{}' to '{}'", customerData.getDisplayName(), oldPlanName, newPlanName);
        return;
    }
    String logMessage = String.format("Changed plan for customer %s from '%s' to '%s'", customerData.getDisplayName(), oldPlanName, newPlanName);
    logger.info(logMessage);
    slackService.sendNotification(logMessage, SlackService.Channel.BUSINESS_EVENTS);
    eventService.send(PlanChangedEvent.builder().customerId(customerData.getCustomerId()).oldPlan(oldPlanName).newPlan(newPlanName).build());
    count = jdbcTemplate.update("DELETE FROM price_plan_overrides WHERE customerId = ?", customerData.getCustomerId());
    if (count > 0) {
        PricePlan newEffectivePricePlan = PricePlan.of(PricePlanDefaults.ofDatabaseName(newPlanName));
        eventService.send(PlanOverridesDeletedEvent.builder().customerId(customerData.getCustomerId()).oldEffectivePlan(oldEffectivePricePlan).newEffectivePlan(newEffectivePricePlan).build());
        String pricePlanOverridesMessage = String.format("Removed price plan overrides for customer %s, effective price plan changed from `%s` to `%s`", customerData.getDisplayName(), oldEffectivePricePlan, newEffectivePricePlan);
        slackService.sendNotification(pricePlanOverridesMessage, SlackService.Channel.BUSINESS_EVENTS);
        logger.warn(pricePlanOverridesMessage);
    }
}
Also used : CustomerData(io.codekvast.common.customer.CustomerData) PricePlan(io.codekvast.common.customer.PricePlan) CacheEvict(org.springframework.cache.annotation.CacheEvict) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PricePlan (io.codekvast.common.customer.PricePlan)6 Transactional (org.springframework.transaction.annotation.Transactional)5 CustomerData (io.codekvast.common.customer.CustomerData)3 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)2 LicenseViolationException (io.codekvast.common.customer.LicenseViolationException)1 MethodDescriptor2 (io.codekvast.dashboard.dashboard.model.methods.MethodDescriptor2)1 AgentDescriptor (io.codekvast.dashboard.dashboard.model.status.AgentDescriptor)1 ApplicationDescriptor2 (io.codekvast.dashboard.dashboard.model.status.ApplicationDescriptor2)1 EnvironmentStatusDescriptor (io.codekvast.dashboard.dashboard.model.status.EnvironmentStatusDescriptor)1 SignatureStatus2 (io.codekvast.javaagent.model.v2.SignatureStatus2)1 Timestamp (java.sql.Timestamp)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1