/
get-form-json.ts
85 lines (83 loc) · 2.55 KB
/
get-form-json.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import {
IGetItemInfoOptions,
IItem,
getItemInfo,
} from "@esri/arcgis-rest-portal";
import { getFormInfoJson } from "./get-form-info-json";
import { isSurvey123Connect } from "./is-survey123-connect";
import { decodeForm } from "./decode-form";
/**
* Given a Survey Item, return the form json, if it exists.
* @param {IItem} the survey item
* @param {IGetItemInfoOptions} requestOptions
* @returns {Promise}
*/
export const getFormJson = async (
item: IItem,
requestOptions: IGetItemInfoOptions
) => {
const { name } = await getFormInfoJson(item.id, requestOptions);
let promise;
if (isSurvey123Connect(item)) {
// survey123 connect does not have a <name>.json file, instead
// that configuration is split between <name>.webform & <name>.info
promise = Promise.all([
getItemInfo(item.id, {
fileName: `${name}.webform`,
readAs: "json",
...requestOptions,
}),
getItemInfo(item.id, {
fileName: `${name}.info`,
readAs: "json",
...requestOptions,
}),
]).then(([webform, info]) => {
// webform contains questions
const { surveyFormJson = {} } = webform;
// other settings come from info
// const { webformInfo: { settings } } = info;
const {
// webformInfo only exists if settings have been
// managed/changed from the s123 web application.
// Disabling survey submissions from the ArcGISSurvey123Connect
// desktop application does not disable survey submissions from
// the web UI. Therefore, just default to open when the survey settings
// have not been changed from the s123 web application.
webformInfo = {
settings: {
openStatusInfo: {
status: "open",
schedule: {
end: null,
status: null,
},
},
multiSubmissionInfo: {
maxAllowed: 0,
},
},
},
} = info;
const { settings } = webformInfo;
// combine relevant parts
return { ...surveyFormJson, settings };
});
} else {
promise = getItemInfo(item.id, {
fileName: `${name}.json`,
readAs: "json",
...requestOptions,
});
}
return promise.then((form) => {
// response may be null... so check...
if (form) {
// form's have some properties which may contain html, these will
// be run thru encodeURIComponent, so we decode it here for use
// in our template
form = decodeForm(form);
}
return form;
});
};