Merged PR 2273: check for existing record and not insert if exists

check for existing record and not insert if exists

Related work items: #22441
This commit is contained in:
Robert Bond
2026-04-24 15:56:54 +00:00
parent d406d97ddf
commit 138f03912b
+56 -25
View File
@@ -42,51 +42,82 @@ export default async function ApiProxy(req, res) {
const queryUrl = "pinswg_contactinvolvements"; const queryUrl = "pinswg_contactinvolvements";
const userInfo = await getPersonalAccount(contactid); const userInfo = await getPersonalAccount(contactid);
const crmUrl = "https://" + process.env.CRMURL;
const crmVersion = process.env.CRMURL_VERSION;
var typeofinvolvement = var typeofinvolvement =
{ {
"appellant": 846040001, appellant: 846040001,
"interestedparty": 846040061, interestedparty: 846040061,
"agent": 846040000, agent: 846040000,
"LPA": 846040012 LPA: 846040012
}[req.body.involvement] || 846040061; }[req.body.involvement] || 846040061;
const data = { const existingQuery =
"pinswg_email": userInfo.emailaddress1, queryUrl +
"pinswg_lastname": userInfo.lastname, "?$select=pinswg_contactinvolvementid" +
"pinswg_firstname": userInfo.firstname, "&$filter=_pinswg_contact_value eq " +
"pinswg_typeofinvolvement": typeofinvolvement, contactid +
"pinswg_Case@odata.bind": "/incidents(" + incidentid + ")", " and _pinswg_case_value eq " +
"pinswg_Contact@odata.bind": "/contacts(" + contactid + ")" incidentid +
}; " and pinswg_typeofinvolvement eq " +
typeofinvolvement;
const config = { const existingConfig = {
method: "post", method: "get",
url: WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), url: WEBAPI_URL + existingQuery + hashAPIPath(existingQuery),
headers: { headers: {
"OData-MaxVersion": "4.0", "OData-MaxVersion": "4.0",
"OData-Version": "4.0", "OData-Version": "4.0",
"Accept": "application/json", Accept: "application/json",
"If-None-Match": "null", Authorization: "Bearer " + token.access_token,
"Prefer": 'odata.include-annotations="*",return=representation',
"Authorization": "Bearer " + token.access_token,
"Content-Type": "application/json" "Content-Type": "application/json"
}, }
data: data
}; };
try { try {
const { data: existingResponse } = await axios(existingConfig);
if (existingResponse?.value?.length > 0) {
return respondSuccess(res, {
record: "exists",
pinswg_contactinvolvementid:
existingResponse.value[0].pinswg_contactinvolvementid
});
}
const data = {
pinswg_email: userInfo.emailaddress1,
pinswg_lastname: userInfo.lastname,
pinswg_firstname: userInfo.firstname,
pinswg_typeofinvolvement: typeofinvolvement,
"pinswg_Case@odata.bind": "/incidents(" + incidentid + ")",
"pinswg_Contact@odata.bind": "/contacts(" + contactid + ")"
};
const config = {
method: "post",
url: WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
headers: {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
Accept: "application/json",
Prefer: 'odata.include-annotations="*",return=representation',
Authorization: "Bearer " + token.access_token,
"Content-Type": "application/json"
},
data: data
};
const { data: responseData } = await axios(config); const { data: responseData } = await axios(config);
return respondSuccess(res, responseData); return respondSuccess(res, responseData);
} catch (error) { } catch (error) {
const errorStatus = error?.status || error?.response?.status; const errorStatus = error?.status || error?.response?.status;
if (errorStatus == 412) { if (errorStatus == 412) {
return respondSuccess(res, { record: "exists" }); return respondSuccess(res, { record: "exists" });
} }
consoleLogger(Object.assign(error, data)); consoleLogger(error);
return respondError(res, { return respondError(res, {
status: 400, status: 400,
code: "CREATE_REP_INVOLVEMENT_FAILED", code: "CREATE_REP_INVOLVEMENT_FAILED",