check for existing record and not insert if exists Related work items: #22441
128 lines
4.0 KiB
JavaScript
128 lines
4.0 KiB
JavaScript
/**
|
|
* @swagger
|
|
* /api/endpoint/createmywatchedcases_api:
|
|
* post:
|
|
* tags: [Portal,Case]
|
|
* summary: Phase 2
|
|
* description: Create my watched cases
|
|
* responses:
|
|
* 200:
|
|
* description: Success
|
|
*/
|
|
|
|
import axios from "axios";
|
|
import { consoleLogger } from "../../../actions/core/logger";
|
|
import { getToken } from "../../../actions/core/token";
|
|
import { hashAPIPath } from "../../../actions/core/hash";
|
|
import { respondError, respondSuccess } from "../middleware/apiResponse";
|
|
import { getPersonalAccount } from "../../../actions";
|
|
|
|
const WEBAPI_URL =
|
|
process.env.RELAY_ROOT ||
|
|
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
|
|
|
|
export default async function ApiProxy(req, res) {
|
|
if (
|
|
!req.body ||
|
|
typeof req.body.contactid === "undefined" ||
|
|
req.body.contactid.length === 0 ||
|
|
typeof req.body.incidentid === "undefined" ||
|
|
req.body.incidentid.length === 0
|
|
) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "MISSING_REQUIRED_BODY",
|
|
message: "contactid and incidentid are required"
|
|
});
|
|
}
|
|
|
|
const token = await getToken();
|
|
const contactid = req.body.contactid;
|
|
const incidentid = req.body.incidentid;
|
|
const queryUrl = "pinswg_contactinvolvements";
|
|
const userInfo = await getPersonalAccount(contactid);
|
|
|
|
var typeofinvolvement =
|
|
{
|
|
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_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(error);
|
|
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "CREATE_REP_INVOLVEMENT_FAILED",
|
|
message: "Failed to create representation involvement"
|
|
});
|
|
}
|
|
}
|