Skip to content

Commit ac85389

Browse files
author
Rakib Ansary
committed
fix(ps-241): bring back commits lost in revert pr #717
1 parent 2be872d commit ac85389

File tree

4 files changed

+168
-3
lines changed

4 files changed

+168
-3
lines changed

config/default.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ module.exports = {
9292
UPDATE: process.env.SCOPE_CHALLENGES_UPDATE || "update:challenges",
9393
DELETE: process.env.SCOPE_CHALLENGES_DELETE || "delete:challenges",
9494
ALL: process.env.SCOPE_CHALLENGES_ALL || "all:challenges",
95+
PAYMENT: process.env.SCOPE_PAYMENT || "create:payments",
9596
},
9697

9798
DEFAULT_CONFIDENTIALITY_TYPE: process.env.DEFAULT_CONFIDENTIALITY_TYPE || "public",
@@ -129,7 +130,8 @@ module.exports = {
129130
GRPC_CHALLENGE_SERVER_HOST: process.env.GRPC_DOMAIN_CHALLENGE_SERVER_HOST || "localhost",
130131
GRPC_CHALLENGE_SERVER_PORT: process.env.GRPC_DOMAIN_CHALLENGE_SERVER_PORT || 8888,
131132
GRPC_ACL_SERVER_HOST: process.env.GRPC_ACL_SERVER_HOST || "localhost",
132-
GRPC_ACL_SERVER_PORT: process.env.GRPC_ACL_SERVER_PORT || 8889,
133+
GRPC_ACL_SERVER_PORT: process.env.GRPC_ACL_SERVER_PORT || 40020,
133134

134-
SKIP_PROJECT_ID_BY_TIMLINE_TEMPLATE_ID: process.env.SKIP_PROJECT_ID_BY_TIMLINE_TEMPLATE_ID || '517e76b0-8824-4e72-9b48-a1ebde1793a8'
135+
SKIP_PROJECT_ID_BY_TIMLINE_TEMPLATE_ID:
136+
process.env.SKIP_PROJECT_ID_BY_TIMLINE_TEMPLATE_ID || "517e76b0-8824-4e72-9b48-a1ebde1793a8",
135137
};

src/controllers/ChallengeController.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ async function updateChallenge(req, res) {
104104
res.send(result);
105105
}
106106

107+
/**
108+
* Update Legacy Payout (Updates informixoltp:payment_detail)
109+
* This has no effect other than to keep DW in sync for looker with
110+
* Updates that happen in Wallet
111+
*/
112+
async function updateLegacyPayout(req, res) {
113+
logger.debug(
114+
`updateLegacyPayout User: ${JSON.stringify(req.authUser)} - ChallengeID: ${
115+
req.params.challengeId
116+
} - Body: ${JSON.stringify(req.body)}`
117+
);
118+
const result = await service.updateLegacyPayout(
119+
req,
120+
req.authUser,
121+
req.params.challengeId,
122+
req.body
123+
);
124+
res.send(result);
125+
}
126+
107127
/**
108128
* Delete challenge
109129
* @param {Object} req the request
@@ -152,6 +172,7 @@ module.exports = {
152172
createChallenge,
153173
getChallenge,
154174
updateChallenge,
175+
updateLegacyPayout,
155176
deleteChallenge,
156177
getChallengeStatistics,
157178
sendNotifications,

src/routes.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
const constants = require("../app-constants");
66
const {
7-
SCOPES: { READ, CREATE, UPDATE, DELETE, ALL },
7+
SCOPES: { PAYMENT, READ, CREATE, UPDATE, DELETE, ALL },
88
} = require("config");
99

1010
module.exports = {
@@ -112,6 +112,14 @@ module.exports = {
112112
scopes: [UPDATE, ALL],
113113
},
114114
},
115+
"/challenges/:challengeId/legacy-payment": {
116+
patch: {
117+
controller: "ChallengeController",
118+
method: "updateLegacyPayout",
119+
auth: "jwt",
120+
scopes: [PAYMENT],
121+
},
122+
},
115123
"/challenges/:challengeId/statistics": {
116124
get: {
117125
controller: "ChallengeController",

src/services/ChallengeService.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,13 @@ updateChallenge.schema = {
21722172
)
21732173
.optional(),
21742174
overview: Joi.any().forbidden(),
2175+
v5Payout: Joi.object().keys({
2176+
userId: Joi.number().integer().positive().required(),
2177+
amount: Joi.number().allow(null),
2178+
status: Joi.string().allow(null),
2179+
datePaid: Joi.string().allow(null),
2180+
releaseDate: Joi.string().allow(null),
2181+
}),
21752182
})
21762183
.unknown(true)
21772184
.required(),
@@ -2498,6 +2505,132 @@ async function indexChallengeAndPostToKafka(updatedChallenge, track, type) {
24982505
});
24992506
}
25002507

2508+
async function updateLegacyPayout(currentUser, challengeId, data) {
2509+
const challenge = await challengeDomain.lookup(getLookupCriteria("id", challengeId));
2510+
const { v5Payout } = data;
2511+
2512+
// SQL qurey to fetch the payment and payment_detail record
2513+
let sql = `SELECT * FROM informixoltp:payment p
2514+
INNER JOIN informixoltp:payment_detail pd ON p.most_recent_detail_id = pd.payment_detail_id
2515+
WHERE p.user_id = ${v5Payout.userId} AND`;
2516+
2517+
if (challenge.legacyId != null) {
2518+
sql += ` pd.component_project_id = ${challenge.legacyId}`;
2519+
} else {
2520+
sql += ` pd.jira_issue_id = \'${challengeId}\'`;
2521+
}
2522+
2523+
sql += " ORDER BY pd.payment_detail_id ASC";
2524+
2525+
console.log("Fetch legacy payment detail: ", sql);
2526+
2527+
const result = await aclQueryDomain.rawQuery({ sql });
2528+
let updateClauses = [`date_modified = current`];
2529+
2530+
const statusMap = {
2531+
Paid: 53,
2532+
OnHold: 55,
2533+
OnHoldAdmin: 55,
2534+
Owed: 56,
2535+
Cancelled: 65,
2536+
EnteredIntoPaymentSystem: 70,
2537+
};
2538+
2539+
if (v5Payout.status != null) {
2540+
updateClauses.push(`payment_status_id = ${statusMap[v5Payout.status]}`);
2541+
if (v5Payout.status === "Paid") {
2542+
updateClauses.push(`date_paid = '${v5Payout.datePaid}'`);
2543+
} else {
2544+
updateClauses.push("date_paid = null");
2545+
}
2546+
}
2547+
2548+
if (v5Payout.releaseDate != null) {
2549+
updateClauses.push(`date_due = '${v5Payout.releaseDate}'`);
2550+
}
2551+
2552+
const paymentDetailIds = result.rows.map(
2553+
(row) => row.fields.find((field) => field.key === "payment_detail_id").value
2554+
);
2555+
2556+
if (v5Payout.amount != null) {
2557+
updateClauses.push(`total_amount = ${v5Payout.amount}`);
2558+
if (paymentDetailIds.length === 1) {
2559+
updateClauses.push(`net_amount = ${v5Payout.amount}`);
2560+
updateClauses.push(`gross_amount = ${v5Payout.amount}`);
2561+
}
2562+
}
2563+
2564+
if (paymentDetailIds.length === 0) {
2565+
return {
2566+
success: false,
2567+
message: "No payment detail record found",
2568+
};
2569+
}
2570+
2571+
const whereClause = [`payment_detail_id IN (${paymentDetailIds.join(",")})`];
2572+
2573+
const updateQuery = `UPDATE informixoltp:payment_detail SET ${updateClauses.join(
2574+
", "
2575+
)} WHERE ${whereClause.join(" AND ")}`;
2576+
2577+
console.log("Update Clauses", updateClauses);
2578+
console.log("Update Query", updateQuery);
2579+
2580+
await aclQueryDomain.rawQuery({ sql: updateQuery });
2581+
2582+
if (v5Payout.amount != null) {
2583+
if (paymentDetailIds.length > 1) {
2584+
const amountInCents = v5Payout.amount * 100;
2585+
2586+
const split1Cents = Math.round(amountInCents * 0.75);
2587+
const split2Cents = amountInCents - split1Cents;
2588+
2589+
const split1Dollars = Number((split1Cents / 100).toFixed(2));
2590+
const split2Dollars = Number((split2Cents / 100).toFixed(2));
2591+
2592+
const paymentUpdateQueries = paymentDetailIds.map((paymentDetailId, index) => {
2593+
let amt = 0;
2594+
if (index === 0) {
2595+
amt = split1Dollars;
2596+
}
2597+
if (index === 1) {
2598+
amt = split2Dollars;
2599+
}
2600+
2601+
return `UPDATE informixoltp:payment_detail SET date_modified = CURRENT, net_amount = ${amt}, gross_amount = ${amt} WHERE payment_detail_id = ${paymentDetailId}`;
2602+
});
2603+
2604+
console.log("Payment Update Queries", paymentUpdateQueries);
2605+
2606+
await Promise.all(
2607+
paymentUpdateQueries.map((query) => aclQueryDomain.rawQuery({ sql: query }))
2608+
);
2609+
}
2610+
}
2611+
2612+
return {
2613+
success: true,
2614+
message: "Successfully updated legacy payout",
2615+
};
2616+
}
2617+
updateLegacyPayout.schema = {
2618+
currentUser: Joi.any(),
2619+
challengeId: Joi.id(),
2620+
data: Joi.object()
2621+
.keys({
2622+
v5Payout: Joi.object().keys({
2623+
userId: Joi.number().integer().positive().required(),
2624+
amount: Joi.number().allow(null),
2625+
status: Joi.string().allow(null),
2626+
datePaid: Joi.string().allow(null),
2627+
releaseDate: Joi.string().allow(null),
2628+
}),
2629+
})
2630+
.unknown(true)
2631+
.required(),
2632+
};
2633+
25012634
/**
25022635
* Get SRM Schedule
25032636
* @param {Object} criteria the criteria
@@ -2562,6 +2695,7 @@ module.exports = {
25622695
getChallenge,
25632696
updateChallenge,
25642697
deleteChallenge,
2698+
updateLegacyPayout,
25652699
getChallengeStatistics,
25662700
sendNotifications,
25672701
advancePhase,

0 commit comments

Comments
 (0)