-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.ts
390 lines (377 loc) · 10.4 KB
/
groups.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* groups.ts implements the groups object handler for listing, creating, retrieving, updating and delete group objects.
*/
import {
apiPort,
Dataset,
formDataToObject,
matchType,
pathIdentifier,
renderPage,
} from "./deps.ts";
import { timeStamp } from "./utils.ts";
const ds = new Dataset(apiPort, "groups.ds");
/**
* GroupInterface
*/
export interface GroupInterface {
clgid: string;
include_in_feeds: boolean;
name: string;
alternative: string[];
email: string;
date: string;
description: string;
start_date: string;
is_approx_start: boolean;
end_date: string;
is_approx_end: boolean;
//FIXME: should activity be called status? In the people record status seems similar.
activity: string;
website: string;
pi: string;
parent: string;
/* prefix refers to the DOI prefix that some groups on campus have. */
prefix: string;
grid: string;
isni: string;
ringold: string;
viaf: string;
ror: string;
updated: string;
Scope: string;
/* authors_id */
authors_id: string;
/* thesis_id */
thesis_id: string;
/* internal notes */
internal_notes: string;
}
/**
* Group class defines the data shape of the group object managed by cold.
*/
export class Group implements GroupInterface {
clgid: string = "";
include_in_feeds: boolean = false;
name: string = "";
alternative: string[] = [];
email: string = "";
date: string = "";
description: string = "";
start_date: string = "";
is_approx_start: boolean = false;
end_date: string = "";
is_approx_end: boolean = false;
//FIXME: should activity be called status? In the people record status seems similar.
activity: string = "";
website: string = "";
pi: string = "";
parent: string = "";
/* prefix refers to the DOI prefix that some groups on campus have. */
prefix: string = "";
grid: string = "";
isni: string = "";
ringold: string = "";
viaf: string = "";
ror: string = "";
updated: string = "";
Scope: string = "";
/* authors_id */
authors_id: string = "";
/* thesis_id */
thesis_id: string = "";
/* internal_notes */
internal_notes: string = "";
migrateCsv(row: any): boolean {
if (row.hasOwnProperty("key")) {
this.include_in_feeds = true;
this.clgid = row.key;
} else {
return false;
}
if (row.hasOwnProperty("name")) {
this.name = row.name;
}
if (row.hasOwnProperty("alternative") && row.alternative !== "") {
this.alternative = row.alternative.trim().split(/;/g);
}
if (row.hasOwnProperty("email")) {
this.email = row.email;
}
if (row.hasOwnProperty("date")) {
this.date = row.date;
}
if (row.hasOwnProperty("description")) {
this.description = row.description;
}
if (row.hasOwnProperty("start")) {
this.start_date = row.start;
}
if (row.hasOwnProperty("approx_start")) {
this.is_approx_start = matchType(this.is_approx_start, row.approx_start);
} else {
this.is_approx_start = false;
}
if (row.hasOwnProperty("end")) {
this.end_date = row.end;
}
if (row.hasOwnProperty("approx_end")) {
this.is_approx_end = matchType(this.is_approx_end, row.approx_end);
} else {
this.is_approx_end = false;
}
if (row.hasOwnProperty("activity")) {
this.activity = matchType(this.activity, row.activity);
}
if (row.hasOwnProperty("website")) {
this.website = row.website;
}
if (row.hasOwnProperty("pi")) {
this.pi = row.pi;
}
if (row.hasOwnProperty("parent")) {
this.parent = row.parent;
}
if (row.hasOwnProperty("prefix")) {
this.prefix = row.prefix;
}
if (row.hasOwnProperty("grid")) {
this.grid = row.grid;
}
if (row.hasOwnProperty("isni")) {
this.isni = row.isni;
}
if (row.hasOwnProperty("ringold")) {
this.ringold = row.ringold;
}
if (row.hasOwnProperty("viaf")) {
this.viaf = row.viaf;
}
if (row.hasOwnProperty("ror")) {
this.ror = row.ror;
}
if (row.hasOwnProperty("internal_notes")) {
this.internal_notes = row.internal_notes;
}
if (row.hasOwnProperty("updated")) {
this.updated = row.updated;
} else {
this.updated = timeStamp(new Date());
}
if (row.hasOwnProperty("Scope")) {
this.Scope = row.Scope;
}
return true;
}
/**
* asObject() returns a simple object version of a instantiated Group object.
*/
asObject(): Object {
return {
clgid: this.clgid,
include_in_feeds: this.include_in_feeds,
name: this.name,
alternative: this.alternative,
email: this.email,
date: this.date,
description: this.description,
start_date: this.start_date,
is_approx_start: this.is_approx_start,
end_date: this.end_date,
is_approx_end: this.is_approx_end,
activity: this.activity,
website: this.website,
pi: this.pi,
parent: this.parent,
prefix: this.prefix,
grid: this.grid,
isni: this.isni,
ringold: this.ringold,
viaf: this.viaf,
ror: this.ror,
updated: this.updated,
Scope: this.Scope,
internal_notes: this.internal_notes,
};
}
/**
* toJSON() returns a clean JSON representation of the group object.
*/
toJSON(): string {
return JSON.stringify(this.asObject());
}
}
/**
* handleGroups provides the dataset collection UI for managing Groups.
* It is response for the following actions
*
* - list or search for groups
* - create a group
* - view a group
* - update a group
* - remove a group
*
* http methods and their interpretation
*
* - `GET /` list objects, use `?q=...` for search
* - `POST /` creates an object
* - `GET /{id}` retrieve an object
* - `PUT /{id}` update an object
* - `DELETE /{id}` delete an object
*
* @param {Request} req holds the request to the group handler
* @param {debug: boolean, htdocs: string} options holds options passed from ColdReadWriteHandlerr.
* @returns {Response}
*/
export async function handleGroups(
req: Request,
options: { debug: boolean; htdocs: string; apiUrl: string },
): Promise<Response> {
if (req.method === "GET") {
return await handleGetGroups(req, options);
}
if (req.method === "POST") {
return await handlePostGroups(req, options);
}
const body = `<html>${req.method} not supported</html>`;
return new Response(body, {
status: 405,
headers: { "content-type": "text/html" },
});
}
/**
* handleGetGroups handle GET actions on group object(s).
*
* @param {Request} req holds the request to the group handler
* @param {debug: boolean, htdocs: string} options holds options passed from
* handleGroups.
* @returns {Response}
*
* The expected paths are in the form
*
* - `/` list the groups by group name (`?q=...` would perform a search by group name)
* - `/{clgid}` indicates retrieving a single object by the Caltech Library group id
*/
async function handleGetGroups(
req: Request,
options: { debug: boolean; htdocs: string; apiUrl: string },
): Promise<Response> {
/* parse the URL */
const url = new URL(req.url);
const clgid = pathIdentifier(req.url);
const params = url.searchParams;
let view = params.get("view");
let tmpl = "group_list";
if (clgid !== undefined && clgid !== "") {
if (view !== undefined && view === "edit") {
tmpl = "group_edit";
} else {
tmpl = "group";
}
} else {
if (view !== "undefined" && view === "create") {
tmpl = "group_edit";
}
}
if (tmpl === "group_list") {
/* display a list of groups */
const group_list = await ds.query("group_names", [], {});
if (group_list !== undefined) {
return renderPage(tmpl, {
base_path: "",
group_list: group_list,
});
} else {
return renderPage(tmpl, {
base_path: "",
group_list: [],
});
}
} else {
/* decide if we are in display view or edit view and pick the right template */
/* retrieve a specific record */
const clgid = pathIdentifier(req.url);
const isCreateObject = clgid === "";
const obj = await ds.read(clgid);
console.log(`We have a GET for group object ${clgid}, view = ${view}`);
return renderPage(tmpl, {
base_path: "",
isCreateObject: isCreateObject,
group: obj,
debug_src: JSON.stringify(obj, null, 2),
});
}
}
/**
* handlePostGroups handle POST actions on group object(s).
*
* @param {Request} req holds the request to the group handler
* @param {debug: boolean, htdocs: string} options holds options passed from
* handleGroups.
* @returns {Response}
*/
async function handlePostGroups(
req: Request,
options: { debug: boolean; htdocs: string; apiUrl: string },
): Promise<Response> {
let clgid = pathIdentifier(req.url);
const isCreateObject = clgid === "";
if (req.body !== null) {
const form = await req.formData();
let obj: Object = formDataToObject(form);
console.log(
`DEBUG form data after converting to object -> ${JSON.stringify(obj)}`,
);
if (!("clgid" in obj)) {
console.log("clgid missing", obj);
return new Response(`missing group identifier`, {
status: 400,
headers: { "content-type": "text/html" },
});
}
if (isCreateObject) {
console.log("DEBUG detected create request");
clgid = obj.clgid as unknown as string;
}
if (obj.clgid !== clgid) {
return new Response(
`mismatched group identifier ${clgid} != ${obj.clgid}`,
{
status: 400,
headers: { "content-type": "text/html" },
},
);
}
if (isCreateObject) {
console.log(`send to dataset create object ${clgid}`);
if (!(await ds.create(clgid, obj))) {
return new Response(
`<html>problem creating object ${clgid}, try again later`,
{
status: 500,
headers: { "content-type": "text/html" },
},
);
}
} else {
console.log(`send to dataset update object ${clgid}`);
if (!(await ds.update(clgid, obj))) {
return new Response(
`<html>problem updating object ${clgid}, try again later`,
{
status: 500,
headers: { "content-type": "text/html" },
},
);
}
}
return new Response(`<html>Redirect to ${clgid}</html>`, {
status: 303,
headers: { Location: `${clgid}` },
});
}
return new Response(`<html>problem creating group data</html>`, {
status: 400,
headers: { "content-type": "text/html" },
});
}