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:
@@ -42,22 +42,52 @@ export default async function ApiProxy(req, res) {
|
||||
const queryUrl = "pinswg_contactinvolvements";
|
||||
const userInfo = await getPersonalAccount(contactid);
|
||||
|
||||
const crmUrl = "https://" + process.env.CRMURL;
|
||||
const crmVersion = process.env.CRMURL_VERSION;
|
||||
|
||||
var typeofinvolvement =
|
||||
{
|
||||
"appellant": 846040001,
|
||||
"interestedparty": 846040061,
|
||||
"agent": 846040000,
|
||||
"LPA": 846040012
|
||||
appellant: 846040001,
|
||||
interestedparty: 846040061,
|
||||
agent: 846040000,
|
||||
LPA: 846040012
|
||||
}[req.body.involvement] || 846040061;
|
||||
|
||||
const existingQuery =
|
||||
queryUrl +
|
||||
"?$select=pinswg_contactinvolvementid" +
|
||||
"&$filter=_pinswg_contact_value eq " +
|
||||
contactid +
|
||||
" and _pinswg_case_value eq " +
|
||||
incidentid +
|
||||
" and pinswg_typeofinvolvement eq " +
|
||||
typeofinvolvement;
|
||||
|
||||
const existingConfig = {
|
||||
method: "get",
|
||||
url: WEBAPI_URL + existingQuery + hashAPIPath(existingQuery),
|
||||
headers: {
|
||||
"OData-MaxVersion": "4.0",
|
||||
"OData-Version": "4.0",
|
||||
Accept: "application/json",
|
||||
Authorization: "Bearer " + token.access_token,
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
};
|
||||
|
||||
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_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 + ")"
|
||||
};
|
||||
@@ -68,25 +98,26 @@ export default async function ApiProxy(req, res) {
|
||||
headers: {
|
||||
"OData-MaxVersion": "4.0",
|
||||
"OData-Version": "4.0",
|
||||
"Accept": "application/json",
|
||||
"If-None-Match": "null",
|
||||
"Prefer": 'odata.include-annotations="*",return=representation',
|
||||
"Authorization": "Bearer " + token.access_token,
|
||||
Accept: "application/json",
|
||||
Prefer: 'odata.include-annotations="*",return=representation',
|
||||
Authorization: "Bearer " + token.access_token,
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
data: data
|
||||
};
|
||||
|
||||
try {
|
||||
const { data: responseData } = await axios(config);
|
||||
|
||||
return respondSuccess(res, responseData);
|
||||
} catch (error) {
|
||||
const errorStatus = error?.status || error?.response?.status;
|
||||
|
||||
if (errorStatus == 412) {
|
||||
return respondSuccess(res, { record: "exists" });
|
||||
}
|
||||
|
||||
consoleLogger(Object.assign(error, data));
|
||||
consoleLogger(error);
|
||||
|
||||
return respondError(res, {
|
||||
status: 400,
|
||||
code: "CREATE_REP_INVOLVEMENT_FAILED",
|
||||
|
||||
Reference in New Issue
Block a user