Files
pedwfrontend/pages/api/endpoint/createwatchedcases_api.js
T

119 lines
3.6 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";
const WEBAPI_URL =
process.env.RELAY_ROOT ||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
const recordExists = async (incidentId, contactId, token) => {
const filter = `$filter=pinswg_WatchedCase/incidentid eq ${incidentId} and pinswg_Contact/contactid eq ${contactId}`;
const queryUrl = `pinswg_watchlists?${filter}`;
const url = WEBAPI_URL + queryUrl + hashAPIPath(queryUrl);
try {
const res = await axios.get(url, {
headers: {
Authorization: "Bearer " + token.access_token,
Accept: "application/json"
}
});
if (res.data.value && res.data.value.length > 0) {
return res.data.value[0]; // return the existing record
}
return null;
} catch (err) {
throw err;
}
};
export default async function ApiProxy(req, res) {
const data = req.body;
const watchedCaseBind = data?.["pinswg_WatchedCase@odata.bind"];
const contactBind = data?.["pinswg_Contact@odata.bind"];
if (
typeof watchedCaseBind !== "string" ||
watchedCaseBind.length === 0 ||
typeof contactBind !== "string" ||
contactBind.length === 0
) {
return respondError(res, {
status: 400,
code: "WATCHED_CASE_BINDINGS_REQUIRED",
message:
"pinswg_WatchedCase and pinswg_Contact bindings are required"
});
}
const watchedCaseMatch = watchedCaseBind.match(/\(([^)]+)\)/);
const contactMatch = contactBind.match(/\(([^)]+)\)/);
if (!watchedCaseMatch || !contactMatch) {
return respondError(res, {
status: 400,
code: "INVALID_BINDING_FORMAT",
message: "Invalid odata.bind format for watched case or contact"
});
}
try {
const token = await getToken();
const incidentId = watchedCaseMatch[1];
const contactId = contactMatch[1];
const existingRecord = await recordExists(incidentId, contactId, token);
let method, queryUrl;
if (existingRecord) {
method = "patch";
queryUrl = `pinswg_watchlists(${existingRecord.pinswg_watchlistid})`;
} else {
method = "post";
queryUrl = "pinswg_watchlists";
}
const config = {
method: method,
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: JSON.stringify(data)
};
const result = await axios(config);
return respondSuccess(res, result.data);
} catch (err) {
consoleLogger(err);
return respondError(res, {
status: 400,
code: "WATCHED_CASE_UPSERT_FAILED",
message: "Failed to create or update watched case"
});
}
}