| Line 7... |
Line 7... |
| 7 |
import in.shop2020.crm.ActivityType;
|
7 |
import in.shop2020.crm.ActivityType;
|
| 8 |
import in.shop2020.crm.Agent;
|
8 |
import in.shop2020.crm.Agent;
|
| 9 |
import in.shop2020.crm.CRMService.Iface;
|
9 |
import in.shop2020.crm.CRMService.Iface;
|
| 10 |
import in.shop2020.crm.SearchFilter;
|
10 |
import in.shop2020.crm.SearchFilter;
|
| 11 |
import in.shop2020.crm.Ticket;
|
11 |
import in.shop2020.crm.Ticket;
|
| 12 |
import in.shop2020.crm.TicketCategory;
|
- |
|
| 13 |
import in.shop2020.crm.TicketPriority;
|
- |
|
| 14 |
import in.shop2020.crm.TicketStatus;
|
- |
|
| 15 |
import in.shop2020.crm.handler.ActivityHandler;
|
12 |
import in.shop2020.crm.handler.ActivityHandler;
|
| 16 |
import in.shop2020.crm.handler.AgentHandler;
|
13 |
import in.shop2020.crm.handler.AgentHandler;
|
| 17 |
import in.shop2020.crm.handler.TicketHandler;
|
14 |
import in.shop2020.crm.handler.TicketHandler;
|
| 18 |
import in.shop2020.model.v1.order.Order;
|
- |
|
| 19 |
import in.shop2020.model.v1.order.OrderStatus;
|
15 |
import in.shop2020.crm.util.DelayedOrderProcessorTask;
|
| 20 |
import in.shop2020.model.v1.order.Transaction;
|
16 |
import in.shop2020.crm.util.PaymentProcessorTask;
|
| 21 |
import in.shop2020.model.v1.order.TransactionService.Client;
|
- |
|
| 22 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
- |
|
| 23 |
import in.shop2020.model.v1.user.User;
|
- |
|
| 24 |
import in.shop2020.model.v1.user.UserContextException;
|
- |
|
| 25 |
import in.shop2020.thrift.clients.TransactionClient;
|
17 |
import in.shop2020.crm.util.CODTransactionProcessorTask;
|
| 26 |
import in.shop2020.thrift.clients.UserClient;
|
- |
|
| 27 |
|
18 |
|
| 28 |
import java.text.ParseException;
|
19 |
import java.text.ParseException;
|
| 29 |
import java.util.ArrayList;
|
20 |
import java.util.ArrayList;
|
| 30 |
import java.util.Date;
|
21 |
import java.util.Date;
|
| 31 |
import java.util.List;
|
22 |
import java.util.List;
|
| - |
|
23 |
import java.util.concurrent.Executors;
|
| - |
|
24 |
import java.util.concurrent.ScheduledExecutorService;
|
| - |
|
25 |
import java.util.concurrent.TimeUnit;
|
| 32 |
|
26 |
|
| 33 |
import org.apache.commons.logging.Log;
|
27 |
import org.apache.commons.logging.Log;
|
| 34 |
import org.apache.commons.logging.LogFactory;
|
28 |
import org.apache.commons.logging.LogFactory;
|
| 35 |
import org.apache.thrift.TException;
|
29 |
import org.apache.thrift.TException;
|
| 36 |
import org.springframework.context.ApplicationContext;
|
30 |
import org.springframework.context.ApplicationContext;
|
| Line 43... |
Line 37... |
| 43 |
*
|
37 |
*
|
| 44 |
* @author mandeep
|
38 |
* @author mandeep
|
| 45 |
*/
|
39 |
*/
|
| 46 |
@Service
|
40 |
@Service
|
| 47 |
public class CRMServiceHandler implements Iface {
|
41 |
public class CRMServiceHandler implements Iface {
|
| 48 |
public static final long ADMIN_AGENT_ID = 1;
|
- |
|
| 49 |
public static final long OUTBOUND_DEFAULT_ASSIGNEE_ID = 12;
|
- |
|
| 50 |
ApplicationContext context = new ClassPathXmlApplicationContext(
|
- |
|
| 51 |
"context.xml");
|
- |
|
| 52 |
TicketHandler ticketHandler = context.getBean(TicketHandler.class);
|
- |
|
| 53 |
ActivityHandler activityHandler = context.getBean(ActivityHandler.class);
|
- |
|
| 54 |
AgentHandler agentHandler = context.getBean(AgentHandler.class);
|
- |
|
| 55 |
|
- |
|
| 56 |
private static final Log log = LogFactory.getLog(CRMServiceHandler.class);
|
42 |
private static final Log log = LogFactory.getLog(CRMServiceHandler.class);
|
| - |
|
43 |
|
| - |
|
44 |
private ScheduledExecutorService scheduler;
|
| - |
|
45 |
private TicketHandler ticketHandler;
|
| 57 |
private static final double TRUST_THRESHOLD_LEVEL = 4.5;
|
46 |
private ActivityHandler activityHandler;
|
| - |
|
47 |
private AgentHandler agentHandler;
|
| - |
|
48 |
|
| - |
|
49 |
public CRMServiceHandler() {
|
| - |
|
50 |
log.info("Creating context");
|
| - |
|
51 |
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
|
| - |
|
52 |
ticketHandler = context.getBean(TicketHandler.class);
|
| - |
|
53 |
activityHandler = context.getBean(ActivityHandler.class);
|
| - |
|
54 |
agentHandler = context.getBean(AgentHandler.class);
|
| - |
|
55 |
scheduler = Executors.newSingleThreadScheduledExecutor();
|
| - |
|
56 |
|
| - |
|
57 |
log.info("Scheduling tasks");
|
| - |
|
58 |
scheduler.scheduleAtFixedRate(new CODTransactionProcessorTask(ticketHandler, activityHandler), 2, 5, TimeUnit.MINUTES);
|
| - |
|
59 |
scheduler.scheduleAtFixedRate(new PaymentProcessorTask(ticketHandler, activityHandler), 4, 5, TimeUnit.MINUTES);
|
| - |
|
60 |
scheduler.scheduleAtFixedRate(new DelayedOrderProcessorTask(ticketHandler, activityHandler), 6, 5, TimeUnit.MINUTES);
|
| - |
|
61 |
}
|
| 58 |
|
62 |
|
| 59 |
public List<Ticket> getTickets(SearchFilter searchFilter) throws TException {
|
63 |
public List<Ticket> getTickets(SearchFilter searchFilter) throws TException {
|
| 60 |
List<Ticket> ttickets = new ArrayList<Ticket>();
|
64 |
List<Ticket> ttickets = new ArrayList<Ticket>();
|
| 61 |
for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
|
65 |
for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
|
| 62 |
.getTickets(in.shop2020.crm.domain.SearchFilter
|
66 |
.getTickets(in.shop2020.crm.domain.SearchFilter
|
| Line 189... |
Line 193... |
| 189 |
|
193 |
|
| 190 |
public void closeSession() throws TException {
|
194 |
public void closeSession() throws TException {
|
| 191 |
// TODO Auto-generated method stub
|
195 |
// TODO Auto-generated method stub
|
| 192 |
|
196 |
|
| 193 |
}
|
197 |
}
|
| 194 |
|
- |
|
| 195 |
/**
|
- |
|
| 196 |
* Async method that creates a ticket if necessary for COD verification
|
- |
|
| 197 |
*/
|
- |
|
| 198 |
public void processCODTxn(long transactionId) {
|
- |
|
| 199 |
try {
|
- |
|
| 200 |
log.info("Processing txn id: " + transactionId);
|
- |
|
| 201 |
Client client = new TransactionClient().getClient();
|
- |
|
| 202 |
Transaction transaction = client.getTransaction(transactionId);
|
- |
|
| 203 |
in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
|
- |
|
| 204 |
User user = userClient.getUserById(transaction.getCustomer_id());
|
- |
|
| 205 |
if (user.getTrustLevel() - TRUST_THRESHOLD_LEVEL < 1e-4) {
|
- |
|
| 206 |
for (Order order : transaction.getOrders()) {
|
- |
|
| 207 |
log.info("Processing order: " + order.getId());
|
- |
|
| 208 |
if (order.isCod() && OrderStatus.INIT.equals(order.getStatus())) {
|
- |
|
| 209 |
log.info("Processing COD order: " + order.getId());
|
- |
|
| 210 |
SearchFilter searchFilter = new SearchFilter();
|
- |
|
| 211 |
searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
|
- |
|
| 212 |
searchFilter.getTicketStatuses().add(TicketStatus.OPEN);
|
- |
|
| 213 |
searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
|
- |
|
| 214 |
searchFilter.setTicketCategory(TicketCategory.COD_VERIFICATION);
|
- |
|
| 215 |
searchFilter.setCustomerId(transaction.getCustomer_id());
|
- |
|
| 216 |
|
- |
|
| 217 |
// No need to create a ticket if there exists one for the customer!
|
- |
|
| 218 |
if (this.getTickets(searchFilter).isEmpty()) {
|
- |
|
| 219 |
log.info("Logging ticket");
|
- |
|
| 220 |
Ticket ticket = new Ticket();
|
- |
|
| 221 |
ticket.setCategory(TicketCategory.COD_VERIFICATION);
|
- |
|
| 222 |
ticket.setCreatorId(ADMIN_AGENT_ID);
|
- |
|
| 223 |
ticket.setCustomerId(transaction.getCustomer_id());
|
- |
|
| 224 |
ticket.setDescription("Requires COD verification");
|
- |
|
| 225 |
ticket.setPriority(TicketPriority.HIGH);
|
- |
|
| 226 |
ticket.setStatus(TicketStatus.OPEN);
|
- |
|
| 227 |
ticket.setAssigneeId(OUTBOUND_DEFAULT_ASSIGNEE_ID);
|
- |
|
| 228 |
|
- |
|
| 229 |
Activity activity = new Activity();
|
- |
|
| 230 |
activity.setCreatorId(ticket.getCreatorId());
|
- |
|
| 231 |
activity.setCustomerId(ticket.getCustomerId());
|
- |
|
| 232 |
activity.setDescription("Creating ticket");
|
- |
|
| 233 |
activity.setTicketCategory(ticket.getCategory());
|
- |
|
| 234 |
activity.setTicketDescription(ticket.getDescription());
|
- |
|
| 235 |
activity.setTicketPriority(ticket.getPriority());
|
- |
|
| 236 |
activity.setTicketStatus(ticket.getStatus());
|
- |
|
| 237 |
activity.setTicketAssigneeId(ticket.getAssigneeId());
|
- |
|
| 238 |
activity.setType(ActivityType.OTHER);
|
- |
|
| 239 |
|
- |
|
| 240 |
this.insertTicket(ticket, activity);
|
- |
|
| 241 |
break;
|
- |
|
| 242 |
}
|
- |
|
| 243 |
}
|
- |
|
| 244 |
}
|
- |
|
| 245 |
}
|
- |
|
| 246 |
else {
|
- |
|
| 247 |
log.info("Autoverifying orders");
|
- |
|
| 248 |
// Submit order for processing
|
- |
|
| 249 |
for (Order order : transaction.getOrders()) {
|
- |
|
| 250 |
log.info("Processing order: " + order.getId());
|
- |
|
| 251 |
client.changeOrderStatus(order.getId(), OrderStatus.SUBMITTED_FOR_PROCESSING, "In Process");
|
- |
|
| 252 |
}
|
- |
|
| 253 |
}
|
- |
|
| 254 |
} catch (TransactionServiceException e) {
|
- |
|
| 255 |
log.error("Could not fetch transaction details", e);
|
- |
|
| 256 |
} catch (UserContextException e) {
|
- |
|
| 257 |
log.error("Could not fetch user details", e);
|
- |
|
| 258 |
} catch (TException e) {
|
- |
|
| 259 |
log.info("Error processing txn id: " + transactionId);
|
- |
|
| 260 |
} catch (Exception e) {
|
- |
|
| 261 |
log.info("Exception processing txn id: " + transactionId);
|
- |
|
| 262 |
}
|
- |
|
| 263 |
}
|
- |
|
| 264 |
|
- |
|
| 265 |
/**
|
- |
|
| 266 |
* Async method to create tickets for failed payments.
|
- |
|
| 267 |
*/
|
- |
|
| 268 |
public void processPaymentFailure(long customerId) {
|
- |
|
| 269 |
try {
|
- |
|
| 270 |
log.info("Processing Payment failure for customerId: " + customerId);
|
- |
|
| 271 |
SearchFilter searchFilter = new SearchFilter();
|
- |
|
| 272 |
searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
|
- |
|
| 273 |
searchFilter.getTicketStatuses().add(TicketStatus.OPEN);
|
- |
|
| 274 |
searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
|
- |
|
| 275 |
searchFilter.setTicketCategory(TicketCategory.FAILED_PAYMENTS);
|
- |
|
| 276 |
searchFilter.setCustomerId(customerId);
|
- |
|
| 277 |
|
- |
|
| 278 |
// No need to create a ticket if there exists one for the customer!
|
- |
|
| 279 |
if (this.getTickets(searchFilter).isEmpty()) {
|
- |
|
| 280 |
Ticket ticket = new Ticket();
|
- |
|
| 281 |
ticket.setCategory(TicketCategory.FAILED_PAYMENTS);
|
- |
|
| 282 |
ticket.setCreatorId(ADMIN_AGENT_ID);
|
- |
|
| 283 |
ticket.setCustomerId(customerId);
|
- |
|
| 284 |
ticket.setDescription("Requires payment failures' follow-up!");
|
- |
|
| 285 |
ticket.setPriority(TicketPriority.HIGH);
|
- |
|
| 286 |
ticket.setStatus(TicketStatus.OPEN);
|
- |
|
| 287 |
ticket.setAssigneeId(OUTBOUND_DEFAULT_ASSIGNEE_ID);
|
- |
|
| 288 |
|
- |
|
| 289 |
Activity activity = new Activity();
|
- |
|
| 290 |
activity.setCreatorId(ticket.getCreatorId());
|
- |
|
| 291 |
activity.setCustomerId(ticket.getCustomerId());
|
- |
|
| 292 |
activity.setDescription("Creating ticket");
|
- |
|
| 293 |
activity.setTicketCategory(ticket.getCategory());
|
- |
|
| 294 |
activity.setTicketDescription(ticket.getDescription());
|
- |
|
| 295 |
activity.setTicketPriority(ticket.getPriority());
|
- |
|
| 296 |
activity.setTicketStatus(ticket.getStatus());
|
- |
|
| 297 |
activity.setType(ActivityType.OTHER);
|
- |
|
| 298 |
activity.setTicketAssigneeId(ticket.getAssigneeId());
|
- |
|
| 299 |
|
- |
|
| 300 |
this.insertTicket(ticket, activity);
|
- |
|
| 301 |
}
|
- |
|
| 302 |
} catch (TException e) {
|
- |
|
| 303 |
log.error("Error fetching/inserting tickets for customer Id: " + customerId, e);
|
- |
|
| 304 |
} catch (Exception e) {
|
- |
|
| 305 |
log.error("Exception processing payment failrue for customer Id: " + customerId, e);
|
- |
|
| 306 |
}
|
- |
|
| 307 |
}
|
- |
|
| 308 |
}
|
198 |
}
|