refactor: extract server-side CRM contact update

This commit is contained in:
2026-11-06 09:58:02 +00:00
parent ea5e90943d
commit 71de8a2d14
7 changed files with 190 additions and 147 deletions
+3 -27
View File
@@ -10,15 +10,9 @@
* 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/";
import { updateCrmContact } from "../../../lib/server/updateCrmContact";
export default async function ApiProxy(req, res) {
const contactId = req.query.contactId;
@@ -40,26 +34,8 @@ export default async function ApiProxy(req, res) {
}
try {
const token = await getToken();
const data = JSON.stringify(req.body);
const queryUrl = "contacts(" + contactId + ")";
const config = {
method: "patch",
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
};
const response = await axios(config);
return respondSuccess(res, response.data);
const data = await updateCrmContact(contactId, req.body);
return respondSuccess(res, data);
} catch (error) {
consoleLogger(error);
return respondError(res, {
@@ -1,4 +1,4 @@
import { updateAccount } from "../../../actions/services/accountService";
import { updateCrmContact } from "../../../lib/server/updateCrmContact";
import { hashAPIPath } from "../../../actions/core/hash";
import { consoleLogger } from "../../../actions/core/logger";
import {
@@ -59,10 +59,7 @@ ApiProxy.get(async (req, res) => {
});
}
if (
!isValidContainer(containerName) ||
!isValidTempCaseRef(tempCaseRef)
) {
if (!isValidContainer(containerName) || !isValidTempCaseRef(tempCaseRef)) {
return respondError(res, {
status: 400,
code: "INVALID_REQUEST",
@@ -158,13 +155,9 @@ ApiProxy.get(async (req, res) => {
if (typeofinvolvement != 846040000) {
void (async () => {
try {
await updateAccount(
contactId[1],
{
pinswg_typeofinvolvement: 846040001
},
true
);
await updateCrmContact(contactId[1], {
pinswg_typeofinvolvement: 846040001
});
} catch (error) {
consoleLogger(error);
}