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);
}
}
Aggregations