diff --git a/pages/api/file/createrepinvolvement_api.js b/pages/api/file/createrepinvolvement_api.js index a48534fa..d05138bb 100644 --- a/pages/api/file/createrepinvolvement_api.js +++ b/pages/api/file/createrepinvolvement_api.js @@ -42,51 +42,82 @@ 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 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 existingQuery = + queryUrl + + "?$select=pinswg_contactinvolvementid" + + "&$filter=_pinswg_contact_value eq " + + contactid + + " and _pinswg_case_value eq " + + incidentid + + " and pinswg_typeofinvolvement eq " + + typeofinvolvement; - const config = { - method: "post", - url: WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), + const existingConfig = { + method: "get", + url: WEBAPI_URL + existingQuery + hashAPIPath(existingQuery), 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", + Authorization: "Bearer " + token.access_token, "Content-Type": "application/json" - }, - data: data + } }; 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); + 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",