151 lines
4.5 KiB
JavaScript
151 lines
4.5 KiB
JavaScript
/**
|
|
* @swagger
|
|
* /api/endpoint/createcase_api:
|
|
* post:
|
|
* tags: [Case]
|
|
* description: Create case
|
|
* summary: Phase 2
|
|
* parameters:
|
|
* - name: body
|
|
* in: requestbody
|
|
* required: true
|
|
* example: {}
|
|
* schema:
|
|
* type: object
|
|
* - name: contactid
|
|
* in: query
|
|
* required: true
|
|
* example: pinswg_planningappeals78s
|
|
* schema:
|
|
* type: string
|
|
* - name: appealTypeId
|
|
* in: query
|
|
* required: true
|
|
* example: pinswg_planningappeals78id
|
|
* schema:
|
|
* type: string
|
|
* - name: containername
|
|
* in: query
|
|
* required: true
|
|
* example: pinswg_planningappeals78id
|
|
* schema:
|
|
* type: string
|
|
* - name: lpaID
|
|
* in: query
|
|
* required: true
|
|
* example: pinswg_planningappeals78id
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: Success
|
|
*/
|
|
|
|
import axios from "axios";
|
|
import { consoleLogger } from "../../../actions/core/logger";
|
|
import { getToken } from "../../../actions/core/token";
|
|
import { getCaseBlob } from "../../../actions/azurestorage";
|
|
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/";
|
|
|
|
export default async function ApiProxy(req, res) {
|
|
const createCaseBody = req.body;
|
|
const contactid = req.query.contactid;
|
|
const appealTypeId = req.query.appealTypeId;
|
|
const containerName = req.query.containername;
|
|
const lpaID = req.query.lpaID;
|
|
const queryUrl = "incidents";
|
|
|
|
if (!createCaseBody || typeof createCaseBody !== "object") {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "CASE_PAYLOAD_REQUIRED",
|
|
message: "Case payload is required"
|
|
});
|
|
}
|
|
if (typeof contactid !== "string" || contactid.trim().length === 0) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "CONTACT_ID_REQUIRED",
|
|
message: "contactid is required"
|
|
});
|
|
}
|
|
if (typeof appealTypeId !== "string" || appealTypeId.trim().length === 0) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "APPEAL_TYPE_ID_REQUIRED",
|
|
message: "appealTypeId is required"
|
|
});
|
|
}
|
|
if (
|
|
typeof containerName !== "string" ||
|
|
containerName.trim().length === 0
|
|
) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "CONTAINER_NAME_REQUIRED",
|
|
message: "containername is required"
|
|
});
|
|
}
|
|
if (typeof lpaID !== "string" || lpaID.trim().length === 0) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "LPA_ID_REQUIRED",
|
|
message: "lpaID is required"
|
|
});
|
|
}
|
|
|
|
try {
|
|
const token = await getToken();
|
|
|
|
const data = {
|
|
"title": "insertion test case",
|
|
"caseorigincode": 3,
|
|
"servicestage": 1,
|
|
"customerid_contact@odata.bind": "/contacts(" + contactid + ")",
|
|
"pinswg_appealcasetype": appealTypeId,
|
|
"pinswg_AssociatedLPA@odata.bind": "/accounts(" + lpaID + ")"
|
|
};
|
|
const newData = Object.assign(data, createCaseBody);
|
|
|
|
delete newData.lpaTypes;
|
|
delete newData.appealTypes;
|
|
|
|
console.log("/////Create Case:\n", newData, "\n//////////////");
|
|
|
|
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: JSON.stringify(newData)
|
|
};
|
|
|
|
const { data: responseData } = await axios(config);
|
|
await getCaseBlob(
|
|
containerName,
|
|
responseData.ticketnumber,
|
|
createCaseBody
|
|
);
|
|
|
|
return respondSuccess(res, responseData);
|
|
} catch (error) {
|
|
consoleLogger(error);
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "CASE_CREATE_FAILED",
|
|
message: "Failed to create case"
|
|
});
|
|
}
|
|
}
|