From 5d48b55a07c38b936d18abd66ab6ad3c4ee1670b Mon Sep 17 00:00:00 2001
From: Afrisal Yodi Purnama <afrisalyp@gmail.com>
Date: Tue, 5 Nov 2019 13:40:27 +0700
Subject: [PATCH] Copilot payment retry.

---
 services/CopilotPaymentService.js | 11 ++++++-----
 services/EventService.js          |  7 +++++++
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/services/CopilotPaymentService.js b/services/CopilotPaymentService.js
index 1441e77..8b7c149 100644
--- a/services/CopilotPaymentService.js
+++ b/services/CopilotPaymentService.js
@@ -142,7 +142,8 @@ async function _checkAndReSchedule(event, payment) {
     // reschedule
     setTimeout(async () => {
       const kafka = require('../utils/kafka'); // eslint-disable-line
-      await kafka.send(JSON.stringify(event));
+      const eventToHandle = _.omit(event, ['project']);
+      await kafka.send(JSON.stringify(eventToHandle));
       logger.debug('The event is scheduled for retry');
     }, config.RETRY_INTERVAL);
     return true;
@@ -218,11 +219,11 @@ async function handlePaymentAdd(event, payment) {
 
       logger.debug(`challenge ${challengeId} has been activated!`);
     } catch (ex) {
-      await dbHelper.remove(models.CopilotPayment, {
-        id: {eq: payment.id}
+      await dbHelper.update(models.CopilotPayment, payment.id, {
+        status: 'challenge_creation_retried'
       });
-
-      await eventService.handleEventGracefully(event, payment, ex);
+      const eventToHandle = _.omit(event, ['project']);
+      await eventService.handleEventGracefully(eventToHandle, payment, ex);
     }
   }
 }
diff --git a/services/EventService.js b/services/EventService.js
index d18918e..93ff003 100644
--- a/services/EventService.js
+++ b/services/EventService.js
@@ -13,6 +13,8 @@ const _ = require('lodash');
 const logger = require('../utils/logger');
 const gitHubService = require('./GithubService');
 const gitlabService = require('./GitlabService');
+const models = require('../models');
+const dbHelper = require('../utils/db-helper');
 
 const timeoutMapper = {};
 
@@ -80,6 +82,11 @@ async function handleEventGracefully(event, data, err) {
       } else if (event.event === 'copilotPayment.add') {
         // comment for copilot payment challenge create failed
         comment = 'The copilot payment challenge creation on the Topcoder platform failed.  Please contact support to try again';
+        await dbHelper.remove(models.CopilotPayment, {
+          id: {eq: data.id}
+        });
+        // we dont need to put comment for copilot payment
+        return;
       }
       // notify error in git host
       if (event.provider === 'github') {