-
Notifications
You must be signed in to change notification settings - Fork 0
/
funders.ts
277 lines (266 loc) · 7.42 KB
/
funders.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
/**
* funders.ts implements the funder object handler for listing, creating, retrieving, updating and delete funder objects.
*/
import {
apiPort,
Dataset,
formDataToObject,
pathIdentifier,
renderPage,
} from "./deps.ts";
const ds = new Dataset(apiPort, "funders.ds");
/**
* FunderInterface
*/
export interface FunderInterface {
clfid: string;
include_in_feeds: boolean;
name: string;
description: string;
ror: string;
ofr: string;
doi: string;
grant_numbers: string[];
updated: string;
}
/**
* Funder class defines the data shape of the funder object managed by cold.
*/
export class Funder implements FunderInterface {
clfid: string = "";
include_in_feeds: boolean = false;
name: string = "";
description: string = "";
ror: string = "";
ofr: string = "";
doi: string = "";
grant_numbers: string[] = [];
updated: string = "";
migrateCsv(row: any): boolean {
if (row.hasOwnProperty("key")) {
this.include_in_feeds = true;
this.clfid = row.key;
} else {
return false;
}
if (row.hasOwnProperty("name")) {
this.name = row.name;
}
if (row.hasOwnProperty("grant_numbers") && row.grant_numbers !== "") {
this.grant_numbers = row.grant_numbers.trim().split(/;/g);
}
if (row.hasOwnProperty("description")) {
this.description = row.description;
}
if (row.hasOwnProperty("ror")) {
this.ror = row.ror;
}
if (row.hasOwnProperty("ofr")) {
this.ofr = row.ofr;
}
if (row.hasOwnProperty("doi")) {
this.doi = row.doi;
}
if (row.hasOwnProperty("updated")) {
this.updated = row.updated;
} else {
this.updated = new Date().toJSON().substring(0, 10);
}
return true;
}
/**
* asObject() returns a simple object version of a instantiated funder object.
*/
asObject(): Object {
return {
clfid: this.clfid,
include_in_feeds: this.include_in_feeds,
name: this.name,
grant_numbers: this.grant_numbers,
ror: this.ror,
ofr: this.ofr,
description: this.description,
updated: this.updated,
};
}
/**
* toJSON() returns a clean JSON representation of the funder object.
*/
toJSON(): string {
return JSON.stringify(this.asObject());
}
}
/**
* handleFunders provides the dataset collection UI for managing Funders.
* It is response for the following actions
*
* - list or search for funders
* - create a funder
* - view a funder
* - update a funder
* - remove a funder
*
* 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 funder handler
* @param {debug: boolean, htdocs: string} options holds options passed from ColdReadWriteHandlerr.
* @returns {Response}
*/
export async function handleFunders(
req: Request,
options: { debug: boolean; htdocs: string; apiUrl: string },
): Promise<Response> {
if (req.method === "GET") {
return await handleGetFunders(req, options);
}
if (req.method === "POST") {
return await handlePostFunders(req, options);
}
const body = `<html>${req.method} not supported</html>`;
return new Response(body, {
status: 405,
headers: { "content-type": "text/html" },
});
}
/**
* handleGetFunders handle GET actions on funder object(s).
*
* @param {Request} req holds the request to the funder handler
* @param {debug: boolean, htdocs: string} options holds options passed from
* handleFunder.
* @returns {Response}
*
* The expected paths are in the form
*
* - `/` list the funders by funder name (`?q=...` would perform a search by funder name)
* - `/{clfid}` indicates retrieving a single object by the Caltech Library funder id
*/
async function handleGetFunders(
req: Request,
options: { debug: boolean; htdocs: string; apiUrl: string },
): Promise<Response> {
/* parse the URL */
const url = new URL(req.url);
const clfid = pathIdentifier(req.url);
const params = url.searchParams;
let view = params.get("view");
let tmpl = "funder_list";
if (clfid !== undefined && clfid !== "") {
if (view !== undefined && view === "edit") {
tmpl = "funder_edit";
} else {
tmpl = "funder";
}
} else {
if (view !== "undefined" && view === "create") {
tmpl = "funder_edit";
}
}
if (tmpl === "funder_list") {
/* display a list of funders */
const funder_list = await ds.query("funder_names", [], {});
if (funder_list !== undefined) {
return renderPage(tmpl, {
base_path: "",
funder_list: funder_list,
});
} else {
return renderPage(tmpl, {
base_path: "",
funder_list: [],
});
}
} else {
/* decide if we are in display view or edit view and pick the right template */
/* retrieve a specific record */
const clfid = pathIdentifier(req.url);
const isCreateObject = clfid === "";
const obj = await ds.read(clfid);
console.log(`We have a GET for funder object ${clfid}, view = ${view}`);
return renderPage(tmpl, {
base_path: "",
isCreateObject: isCreateObject,
funder: obj,
debug_src: JSON.stringify(obj, null, 2),
});
}
}
/**
* handlePostFunder handle POST actions on funder object(s).
*
* @param {Request} req holds the request to the funder handler
* @param {debug: boolean, htdocs: string} options holds options passed from
* handleFunder.
* @returns {Response}
*/
async function handlePostFunders(
req: Request,
options: { debug: boolean; htdocs: string; apiUrl: string },
): Promise<Response> {
let clfid = pathIdentifier(req.url);
const isCreateObject = clfid === "";
if (req.body !== null) {
const form = await req.formData();
let obj = formDataToObject(form);
console.log(
`DEBUG form data after converting to object -> ${JSON.stringify(obj)}`,
);
if (!("clfid" in obj)) {
console.log("clfid missing", obj);
return new Response(`missing funder identifier`, {
status: 400,
headers: { "content-type": "text/html" },
});
}
if (isCreateObject) {
console.log("DEBUG detected create request");
clfid = obj.clfid as unknown as string;
}
if (obj.clfid !== clfid) {
return new Response(
`mismatched funder identifier ${clfid} != ${obj.clfid}`,
{
status: 400,
headers: { "content-type": "text/html" },
},
);
}
if (isCreateObject) {
console.log(`send to dataset create object ${clfid}`);
if (!(await ds.create(clfid, obj))) {
return new Response(
`<html>problem creating object ${clfid}, try again later`,
{
status: 500,
headers: { "content-type": "text/html" },
},
);
}
} else {
console.log(`send to dataset update object ${clfid}`);
if (!(await ds.update(clfid, obj))) {
return new Response(
`<html>problem updating object ${clfid}, try again later`,
{
status: 500,
headers: { "content-type": "text/html" },
},
);
}
}
return new Response(`<html>Redirect to ${clfid}</html>`, {
status: 303,
headers: { Location: `${clfid}` },
});
}
return new Response(`<html>problem creating funder data</html>`, {
status: 400,
headers: { "content-type": "text/html" },
});
}