TASK22211: normalize delete and watched-case endpoint contracts
This commit is contained in:
@@ -11,10 +11,10 @@
|
||||
// */
|
||||
|
||||
import axios from "axios";
|
||||
import CryptoJS from "crypto-js";
|
||||
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 ||
|
||||
@@ -83,51 +83,65 @@ export default async function ApiProxy(req, res) {
|
||||
typeof contactBind !== "string" ||
|
||||
contactBind.length === 0
|
||||
) {
|
||||
return res.status(400).json();
|
||||
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 res.status(400).json();
|
||||
}
|
||||
|
||||
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})`; // adjust with your primary key logical name
|
||||
} 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)
|
||||
};
|
||||
|
||||
return axios(config)
|
||||
.then(({ data }) => res.status(200).json(data))
|
||||
.catch((err) => {
|
||||
consoleLogger(err);
|
||||
res.status(400).json(err.response?.data || err);
|
||||
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"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user