127 lines
3.8 KiB
JavaScript
127 lines
3.8 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 { relayGetData } from "../middleware/relayForwarding";
|
|
|
|
const WEBAPI_URL =
|
|
process.env.RELAY_ROOT ||
|
|
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
|
|
|
|
const recordExists = async (incidentId, contactId, accessToken) => {
|
|
const filter = `$filter=pinswg_WatchedCase/incidentid eq ${incidentId} and pinswg_Contact/contactid eq ${contactId}`;
|
|
const queryUrl = `pinswg_watchlists?${filter}`;
|
|
|
|
try {
|
|
const response = await relayGetData({
|
|
queryUrl,
|
|
accessToken,
|
|
requestOptionsBuilder: (token) => ({
|
|
headers: {
|
|
Authorization: "Bearer " + token,
|
|
Accept: "application/json"
|
|
}
|
|
})
|
|
});
|
|
|
|
if (response.data.value && response.data.value.length > 0) {
|
|
return response.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.access_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"
|
|
});
|
|
}
|
|
}
|