438 lines
14 KiB
JavaScript
438 lines
14 KiB
JavaScript
import jsonpath from "jsonpath";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
import data from "../../data/collections.json";
|
|
import {
|
|
getBasicSearchDetails,
|
|
getBasicSearchDetailsPaged,
|
|
getBasicPartSavedDetails,
|
|
} from "../../actions";
|
|
|
|
import xpath from "xpath";
|
|
|
|
/*
|
|
* Get the lookup collection by the appeal type entity
|
|
* @param String formtype - string from appeal entity name
|
|
*/
|
|
export const getFormCollection = (formtype) => {
|
|
const collectionName = jsonpath.query(
|
|
data,
|
|
"$..[?(@.LogicalName=='" + formtype + "')]"
|
|
);
|
|
return collectionName[0];
|
|
};
|
|
|
|
export const getFormCollectionByID = (appealTypeID) => {
|
|
const collectionName = jsonpath.query(
|
|
data,
|
|
"$..[?(@.appealTypeID =='" + appealTypeID + "')]"
|
|
);
|
|
return collectionName[0];
|
|
};
|
|
|
|
export const getPickListLabel = (data, picklistType, picklistID) => {
|
|
const pickListData = jsonpath.query(
|
|
data,
|
|
"$..[?(@.LogicalName=='" + picklistType + "')]"
|
|
);
|
|
|
|
const pickListLabel = jsonpath.query(
|
|
pickListData,
|
|
"$..[?(@.Value=='" + picklistID + "')].Label.LocalizedLabels..Label"
|
|
);
|
|
return pickListLabel[0];
|
|
};
|
|
|
|
export const getRadioLabel = (data, radiotListName, radioListID) => {
|
|
const radioListData = jsonpath.query(
|
|
data,
|
|
"$..[?(@.LogicalName=='" + radiotListName + "')]"
|
|
);
|
|
|
|
const pickListLabel = jsonpath.query(
|
|
radioListData,
|
|
"$..[?(@.Value=='" + radioListID + "')].Label.LocalizedLabels..Label"
|
|
);
|
|
return pickListLabel[0];
|
|
};
|
|
|
|
/*
|
|
* Get the details of a case from the appeal type
|
|
* @param object searchResultsObj
|
|
*/
|
|
export const getSearchDetails = (searchResultsObj) => {
|
|
//console.log(searchResultsObj);
|
|
|
|
let detailsArr = [];
|
|
searchResultsObj = searchResultsObj.value;
|
|
const detailsObj = searchResultsObj.map((searchDetail, index) => {
|
|
if (searchDetail.pinswg_appealcasetype == null) {
|
|
//console.log(searchDetail.title);
|
|
detailsArr.push(
|
|
getBasicSearchDetails(
|
|
"pinswg_dnses",
|
|
searchDetail.title,
|
|
"pinswg_dnsid",
|
|
searchDetail.incidentid
|
|
)
|
|
);
|
|
} else {
|
|
let formMeta = getFormCollectionByID(
|
|
searchDetail.pinswg_appealcasetype
|
|
);
|
|
//console.log(formMeta);
|
|
detailsArr.push(
|
|
getBasicSearchDetails(
|
|
formMeta.LogicalCollectionName,
|
|
searchDetail.title,
|
|
formMeta.PrimaryIdAttribute,
|
|
searchDetail.incidentid
|
|
)
|
|
);
|
|
}
|
|
});
|
|
//console.log(detailsArr);
|
|
return Promise.all(detailsArr);
|
|
};
|
|
|
|
/*
|
|
* Get the partially saved details of a case from the appeal type
|
|
* @param object searchResultsObj
|
|
*/
|
|
export const getPartSavedDetails = (searchResultsObj) => {
|
|
//console.log(searchResultsObj);
|
|
|
|
let detailsArr = [];
|
|
searchResultsObj = searchResultsObj.value;
|
|
const detailsObj = searchResultsObj.map((searchDetail, index) => {
|
|
if (searchDetail.pinswg_appealcasetype == null) {
|
|
console.log(searchDetail.title);
|
|
} else {
|
|
let formMeta = getFormCollectionByID(
|
|
searchDetail.pinswg_appealcasetype
|
|
);
|
|
console.log(formMeta);
|
|
detailsArr.push(
|
|
getBasicPartSavedDetails(
|
|
formMeta.LogicalCollectionName,
|
|
searchDetail.title,
|
|
formMeta.PrimaryIdAttribute,
|
|
searchDetail.incidentid
|
|
)
|
|
);
|
|
}
|
|
});
|
|
//console.log(detailsArr);
|
|
return Promise.all(detailsArr);
|
|
};
|
|
|
|
/*
|
|
* Get the details of a case from the appeal type
|
|
* @param object searchResultsObj
|
|
*/
|
|
export const getSearchDetailsPaged = (searchResultsObj) => {
|
|
//console.log(searchResultsObj);
|
|
|
|
let detailsArr = [];
|
|
//console.log("search results", searchResultsObj);
|
|
searchResultsObj = searchResultsObj.value;
|
|
const detailsObj = searchResultsObj.map((searchDetail, index) => {
|
|
//console.log(searchDetail);
|
|
if (searchDetail.pinswg_appealcasetype == null) {
|
|
console.log(searchDetail.title);
|
|
} else {
|
|
let formMeta = getFormCollectionByID(
|
|
searchDetail.pinswg_appealcasetype
|
|
);
|
|
|
|
detailsArr.push(
|
|
getBasicSearchDetailsPaged(
|
|
formMeta.LogicalCollectionName,
|
|
searchDetail.title,
|
|
formMeta.PrimaryIdAttribute,
|
|
searchDetail.incidentid
|
|
)
|
|
);
|
|
}
|
|
});
|
|
// console.log(detailsArr);
|
|
return Promise.all(detailsArr);
|
|
};
|
|
|
|
// export const absoluteUrl = (req, setLocalhost) => {
|
|
// var protocol = "https:";
|
|
// var host = req
|
|
// ? req.headers["x-forwarded-host"] || req.headers["host"]
|
|
// : window.location.host;
|
|
|
|
// if (host.indexOf("localhost") > -1) {
|
|
// if (setLocalhost) host = setLocalhost;
|
|
// protocol = "http:";
|
|
// }
|
|
|
|
// return {
|
|
// protocol: protocol,
|
|
// host: host,
|
|
// origin: protocol + "//" + host,
|
|
// };
|
|
// };
|
|
|
|
export const getProgressObj = (
|
|
formObjXML,
|
|
titleList,
|
|
mandatoryFieldsData,
|
|
props
|
|
) => {
|
|
const parser = new DOMParser();
|
|
const BuildFormObj = (formObjXML) => {
|
|
var doc = parser.parseFromString(formObjXML, "text/xml");
|
|
|
|
var tabs = xpath.select("//form/tabs/tab[*]", doc);
|
|
|
|
const formObj = Object.keys(tabs).map((key, index) => {
|
|
var sectionXML = xpath.select(
|
|
"//tabs/tab[" +
|
|
(index + 1) +
|
|
"]//rows/row/control[not(@parentField)]/..",
|
|
doc
|
|
);
|
|
|
|
return BuildRowObj(sectionXML, titleList[key].value);
|
|
});
|
|
|
|
const progressObj = Object.keys(formObj).map((key, index) => {
|
|
let fieldObj = formObj[key].fieldsrowObj;
|
|
|
|
let rowCount = 0;
|
|
|
|
!props.dirty &&
|
|
typeof props.initialValues != "undefined" &&
|
|
Object.keys(fieldObj).map((key, index) => {
|
|
fieldObj[key].datafieldname in props.initialValues &&
|
|
props.initialValues[fieldObj[key].datafieldname] !=
|
|
null &&
|
|
rowCount++;
|
|
});
|
|
props.dirty &&
|
|
Object.keys(fieldObj).map((key, index) => {
|
|
fieldObj[key].datafieldname in
|
|
props.props.form["appealForm"].values &&
|
|
props.props.form["appealForm"].values[
|
|
fieldObj[key].datafieldname
|
|
] != null &&
|
|
rowCount++;
|
|
});
|
|
|
|
return {
|
|
"title": titleList[key].value,
|
|
rowCount,
|
|
"totalFields": formObj[key].fieldsTotal,
|
|
"allFieldsComplete":
|
|
titleList[key].value == "Upload documents"
|
|
? _.has(props.props.appealType.fileList, "value") &&
|
|
props.props.appealType.fileList.value.length > 0
|
|
: rowCount == formObj[key].fieldsTotal,
|
|
};
|
|
});
|
|
|
|
return progressObj;
|
|
};
|
|
|
|
const BuildRowObj = (rowXML, titleList) => {
|
|
let rowObj = Object.keys(rowXML).map((key, index) => {
|
|
var doc = parser.parseFromString(rowXML[key].outerHTML, "text/xml");
|
|
if (_.isEmpty(rowXML[key].innerHTML)) {
|
|
("");
|
|
} else {
|
|
var rows = xpath.select("//label/@description", doc);
|
|
var fieldtype = xpath.select("//@classid", doc);
|
|
var validation = xpath.select("//@validation", doc);
|
|
var datafieldname = xpath.select("//@datafieldname", doc);
|
|
var datafieldcontent = xpath.select("//@datafieldcontent", doc);
|
|
var parentField = xpath.select("//@parentField", doc);
|
|
|
|
var parentFieldShowOnValue = xpath.select(
|
|
"//@parentFieldShowOnValue",
|
|
doc
|
|
);
|
|
|
|
var requiredDocumentValue = xpath.select(
|
|
"//@requiredDocumentValue",
|
|
doc
|
|
);
|
|
|
|
var requiredDocumentLabel = xpath.select(
|
|
"//@requiredDocumentLabel",
|
|
doc
|
|
);
|
|
|
|
var hint = xpath.select("//label/@hint", doc);
|
|
var dateStart = xpath.select("//@dateStart", doc);
|
|
var dateEnd = xpath.select("//@dateEnd", doc);
|
|
|
|
hint = !_.isEmpty(hint) && hint[0].value;
|
|
|
|
validation = !_.isEmpty(validation) ? validation[0].value : [];
|
|
|
|
parentField = !_.isEmpty(parentField) && parentField[0].value;
|
|
|
|
parentFieldShowOnValue =
|
|
!_.isEmpty(parentFieldShowOnValue) &&
|
|
parentFieldShowOnValue[0].value;
|
|
|
|
datafieldcontent =
|
|
!_.isEmpty(datafieldcontent) && datafieldcontent[0].value;
|
|
|
|
requiredDocumentValue =
|
|
!_.isEmpty(requiredDocumentValue) &&
|
|
requiredDocumentValue[0].value;
|
|
|
|
requiredDocumentLabel =
|
|
!_.isEmpty(requiredDocumentLabel) &&
|
|
requiredDocumentLabel[0].value;
|
|
|
|
dateStart = !_.isEmpty(dateStart) && dateStart[0].value;
|
|
|
|
dateEnd = !_.isEmpty(dateEnd) && dateEnd[0].value;
|
|
|
|
const isRequiredField = jsonpath.query(
|
|
mandatoryFieldsData,
|
|
"$..value[?(@.LogicalName=='" +
|
|
datafieldname[0].value +
|
|
"')]"
|
|
);
|
|
|
|
if (datafieldname[0].value == "pinswg_fileUpload") {
|
|
return {
|
|
"fieldType": fieldtype[0].value,
|
|
"label": rows[0].value,
|
|
"datafieldname": datafieldname[0].value,
|
|
"key": key,
|
|
"picklistData": props.props.formData.pickListData,
|
|
"mandatoryFieldsData": mandatoryFieldsData,
|
|
};
|
|
} else {
|
|
if (typeof isRequiredField[0] != "undefined") {
|
|
if (isRequiredField[0].RequiredLevel.Value != "None") {
|
|
//console.log(datafieldname[0].value, validation);
|
|
return {
|
|
"fieldType": fieldtype[0].value,
|
|
"label": rows[0].value,
|
|
"datafieldname": datafieldname[0].value,
|
|
"datafieldcontent": datafieldcontent,
|
|
"validation": validation,
|
|
};
|
|
} else {
|
|
return null;
|
|
}
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
rowObj = rowObj.filter(function (el) {
|
|
return el != null;
|
|
});
|
|
|
|
const filterUnwanted = (rowObj) => {
|
|
const required = rowObj.filter((el) => {
|
|
return !_.isEmpty(el.validation);
|
|
});
|
|
return required;
|
|
};
|
|
|
|
return {
|
|
"title": titleList,
|
|
"validationFieldsTotal": filterUnwanted(rowObj).length,
|
|
"fieldsTotal": rowObj.length,
|
|
"fieldsrowObj": rowObj,
|
|
};
|
|
};
|
|
|
|
const progObj = BuildFormObj(formObjXML);
|
|
//console.log(progObj);
|
|
return progObj;
|
|
};
|
|
|
|
export const getTempCaseRef = () => {
|
|
function randomString(length) {
|
|
return Math.random().toString(36).substr(2, length);
|
|
}
|
|
|
|
const randomGUID = uuidv4();
|
|
|
|
return (
|
|
"TMP-" +
|
|
randomString(5).toUpperCase() +
|
|
"-" +
|
|
randomString(6).toUpperCase()
|
|
);
|
|
};
|
|
|
|
export const getThumbnailIconByExtension = (blobName) => {
|
|
let fileType = blobName.slice(blobName.lastIndexOf(".") + 1);
|
|
|
|
switch (fileType) {
|
|
case "html":
|
|
return "/assets/images/documenttypes/html.png";
|
|
break;
|
|
case "txt":
|
|
return "/assets/images/documenttypes/txt.png";
|
|
break;
|
|
case "doc":
|
|
return "/assets/images/documenttypes/doc.png";
|
|
break;
|
|
case "pdf":
|
|
return "/assets/images/documenttypes/pdf.png";
|
|
break;
|
|
case "docx":
|
|
return "/assets/images/documenttypes/docx.png";
|
|
break;
|
|
case "csv":
|
|
return "/assets/images/documenttypes/csv.png";
|
|
break;
|
|
case "xlsx":
|
|
return "/assets/images/documenttypes/xlsx.png";
|
|
break;
|
|
|
|
case "zip":
|
|
return "/assets/images/documenttypes/zip.png";
|
|
break;
|
|
case "jpeg":
|
|
case "jpg":
|
|
return "/assets/images/documenttypes/jpg.png";
|
|
case "png":
|
|
return "/assets/images/documenttypes/png.png";
|
|
break;
|
|
default:
|
|
return "/assets/images/documenttypes/txt.png";
|
|
break;
|
|
}
|
|
};
|
|
|
|
export const bytesToSize = (bytes) => {
|
|
var sizes = ["Bytes", "KB", "MB", "GB", "TB"];
|
|
if (bytes == 0) return "0 Byte";
|
|
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
|
|
return Math.round(bytes / Math.pow(1024, i), 2) + " " + sizes[i];
|
|
};
|
|
|
|
export const formatDates = (dateObj, showTime) => {
|
|
var dateObj = new Date(dateObj);
|
|
var dd = String(dateObj.getDate()).padStart(2, "0");
|
|
var mm = String(dateObj.getMonth() + 1).padStart(2, "0"); //January is 0!
|
|
var yyyy = dateObj.getFullYear();
|
|
var hh = dateObj.getHours();
|
|
hh = ("0" + hh).slice(-2);
|
|
var mins = dateObj.getMinutes();
|
|
mins = ("0" + mins).slice(-2);
|
|
|
|
const dateStr = showTime
|
|
? hh == 0 && mins == 0
|
|
? ""
|
|
: hh + ":" + mins
|
|
: dd + "/" + mm + "/" + yyyy + " ";
|
|
return dateStr;
|
|
};
|