Created
August 7, 2022 16:31
-
-
Save samuelkarani/499698017e9fa0f1e42715748cda982e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
UpdateCounterPartyRisk, | |
UpdateAssetCounterPartyRisk, | |
UpdateCounterPartyRiskWithAlerts, | |
UpdateAssetCounterPartyRiskWithAlerts, | |
UpdateDDCounterPartyRisk, | |
UpdateHitStatus, | |
UpdateBatchStatus, | |
} = require("@schemas/grid"); | |
const axios = require("axios"); | |
const { key: apikey } = require("@config/global"); | |
function send(res, query, selector) { | |
axios({ | |
url: `${process.env.HOST}/graphql`, | |
method: "post", | |
data: { query }, | |
headers: { apikey }, | |
}) | |
.then(({ data }) => | |
res.status(200).send({ | |
status: "success", | |
data: selector(data), | |
}) | |
) | |
.catch((err) => | |
res.status(500).send({ | |
status: "error", | |
data: err, | |
}) | |
); | |
} | |
function helper(entities, gridInquiries) { | |
return entities.map((entity, index) => { | |
let data = { | |
...entity, | |
ddCountryRisk: entity.risk, | |
}; | |
if (gridInquiries[index].hit <= 0) { | |
data.typeDDRisk = null; | |
if ((entity.risk || "").toLowerCase().includes("high")) { | |
data.typeDDRisk = "Country"; | |
data.risk = "High Risk"; | |
} else { | |
data.risk = "Low Risk"; | |
} | |
} else { | |
if ((entity.type || "").toLowerCase().includes("person")) { | |
data.typeDDRisk = "PEP"; | |
} else { | |
data.typeDDRisk = "Sanction"; | |
} | |
if ((entity.risk || "").toLowerCase().includes("high")) { | |
data.typeDDRisk = `Country & ${data.typeDDRisk}`; | |
} | |
data.risk = "High Risk"; | |
} | |
return data; | |
}); | |
} | |
const generateCounterPartyRisk = (req, res) => { | |
const { entities, batchResult, ddId } = req.body; | |
const { id: batchId, gridInquiries } = batchResult; | |
const allEntities = helper(entities); | |
const mutation = | |
gridInquiries.length === 0 | |
? UpdateCounterPartyRisk(ddId, allEntities, batchId) | |
: UpdateCounterPartyRiskWithAlerts( | |
ddId, | |
allEntities, | |
batchId, | |
gridInquiries | |
); | |
send( | |
res, | |
mutation, | |
(data) => | |
data.data.updateDueDiligences.dueDiligences[0] | |
.riskCounterPartyCounterpartyrisk | |
); | |
}; | |
const generateAssetCounterPartyRisk = (req, res) => { | |
const { entities, batchResult, ddId } = req.body; | |
const { id: batchId, gridInquiries } = batchResult; | |
const allEntities = helper(entities); | |
const mutation = | |
gridInquiries.length === 0 | |
? UpdateAssetCounterPartyRisk(ddId, allEntities, batchId) | |
: UpdateAssetCounterPartyRiskWithAlerts( | |
ddId, | |
allEntities, | |
batchId, | |
gridInquiries | |
); | |
send( | |
res, | |
mutation, | |
(data) => | |
data.data.updateDueDiligences.dueDiligences[0] | |
.riskCounterPartyCounterpartyrisk | |
); | |
}; | |
const updateEntityMatch = (req, res) => { | |
const { entityId, status, reasonCode } = req.body; | |
const mutation = UpdateHitStatus(entityId, reasonCode, status); | |
send(res, mutation, (data) => data.data.updateHits.hits[0]); | |
}; | |
const updateCounterPartyRisk = (req, res) => { | |
const { entities, ddId } = req.body; | |
let matchEntities = entities.filter( | |
(entity) => | |
entity.status !== "NOMATCH" && | |
entity.status !== "NOALERT" && | |
entity.status !== "DECISIONED_ALERT" | |
); | |
if (matchEntities.length <= 0) { | |
return res.status(200).send({ | |
status: "success", | |
data: {}, | |
}); | |
} | |
matchEntities = matchEntities.map((entity) => { | |
let data = { | |
id: entity.riskId, | |
inquiry: entity.inquiry, | |
risk: (entity.risk || "").toLowerCase().includes("high") | |
? entity.risk | |
: "Low Risk", | |
type: "Country", | |
}; | |
return data; | |
}); | |
const mutation = UpdateDDCounterPartyRisk(ddId, matchEntities); | |
send(res, mutation, (data) => data.data.updateDueDiligences.dueDiligences[0]); | |
}; | |
const updateCounterPartyRiskBatch = (req, res) => { | |
const batchId = req.params.id; | |
const mutation = UpdateBatchStatus(batchId); | |
send( | |
res, | |
mutation, | |
(data) => data.data.updateCounterPartyRisks.counterPartyRisks[0] | |
); | |
}; | |
module.exports = { | |
generateCounterPartyRisk, | |
generateAssetCounterPartyRisk, | |
updateEntityMatch, | |
updateCounterPartyRisk, | |
updateCounterPartyRiskBatch, | |
}; | |
/* | |
1 - generate alert for each entity with hits | |
a - organisation: | |
- alertStatus = Pending | |
- problemType = Sanction | |
- dueDiligenceIdConcern = ddID | |
- statusCounterPartyRisk = High Risk | |
b - person | |
- alertStatus = Pending | |
- problemType = PEP | |
- dueDiligenceIdConcern = ddID | |
- statusCounterPartyRisk = High Risk | |
if one alert is generated, set high risk on due diligence | |
if no alert are generated, set low risk on due diligence | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment