-
Notifications
You must be signed in to change notification settings - Fork 13
/
create-site.ts
138 lines (131 loc) · 4 KB
/
create-site.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
IModel,
IHubRequestOptions,
serializeModel,
cloneObject,
interpolateItemId,
uploadResourcesFromUrl,
getProp,
addSiteDomains,
} from "@esri/hub-common";
import { ensureRequiredSiteProperties } from "./ensure-required-site-properties";
import {
createItem,
protectItem,
updateItem,
shareItemWithGroup,
} from "@esri/arcgis-rest-portal";
import { updateInitiativeSiteId } from "@esri/hub-initiatives";
/**
* Create a New Site
* Creates and protects the site item
* Uploads any assets/thumbnails passed in via options.assets array
* If not portal..,
* - register the site as an application, w/ needed redirect uris
* - register the domains with the Hub Domain Service
* @param {Object} model Site Model to create
* @param {Object} options options hash. Key prop is assets
* @param {IHubRequestOptions} hubRequestOptions
*/
export function createSite(
model: IModel,
options: any,
hubRequestOptions: IHubRequestOptions
): Promise<IModel> {
// ensure properties
model = ensureRequiredSiteProperties(
model,
hubRequestOptions.authentication.username,
hubRequestOptions.isPortal
);
// create the item
return createItem({
item: serializeModel(model),
owner: model.item.owner,
authentication: hubRequestOptions.authentication,
})
.then((createResponse) => {
// hold onto the Id so we can return a complete model
model.item.id = createResponse.id;
// protect it
return protectItem({
id: model.item.id,
owner: model.item.owner,
authentication: hubRequestOptions.authentication,
});
})
.then((protectResponse) => {
// get the clientId out of the addSiteDomains call
return addSiteDomains(model, hubRequestOptions);
})
.then((domainResponses) => {
// client id will be the same for all domain resonses so we can just grab the first one
model.data.values.clientId = domainResponses[0].clientKey;
// If we have a dcat section, hoist it out as it may contain complex adlib
// templates that are needed at run-time
// If we have data.values.dcatConfig, yank it off b/c that may have adlib template stuff in it
const dcatConfig = cloneObject(model.data.values.dcatConfig);
delete model.data.values.dcatConfig;
// with the id of the actual item
model = interpolateItemId(model);
// re-attach if we got anything...
if (dcatConfig) {
model.data.values.dcatConfig = dcatConfig;
}
return updateItem({
item: serializeModel(model),
authentication: hubRequestOptions.authentication,
});
})
.then((updateResponse) => {
// upload resources from url
return uploadResourcesFromUrl(
model,
options.assets || [],
hubRequestOptions
);
})
.then((uploadResponses) => {
// default to a success response
let sharePrms: Promise<any> = Promise.resolve({ success: true });
// share it to the collab team if that got created
const collabGroupId = getProp(
model,
"item.properties.collaborationGroupId"
);
if (collabGroupId) {
sharePrms = shareItemWithGroup({
id: model.item.id,
groupId: collabGroupId,
authentication: hubRequestOptions.authentication,
confirmItemControl: true,
});
}
return sharePrms;
})
.then((resp) => {
// if we created an initiative, ensure we inject the site Id into it
const initiativeItemId = getProp(
model,
"item.properties.parentInitiativeId"
);
if (initiativeItemId) {
// get the item and update it
return updateInitiativeSiteId(
initiativeItemId,
model.item.id,
hubRequestOptions
);
} else {
return Promise.resolve(true);
}
})
.then((resp) => {
return model;
})
.catch((err) => {
throw Error(
`site-utils::createSite - Error creating site ${JSON.stringify(err)}`
);
});
}