-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissn.ts
324 lines (311 loc) · 9.18 KB
/
issn.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
/**
* issn.ts implements the issn object handler for listing, creating, retrieving, updating and delete issn objects.
*/
import { apiPort, Dataset, pathIdentifier, renderPage } from "./deps.ts";
import { timeStamp } from "./utils.ts";
const ds = new Dataset(apiPort, "issn.ds");
/**
* ISSNInterface
*/
export interface ISSNInterface {
/* ISSN is the primary identifier for record */
issn: string;
/* Name of Journal */
name: string;
/* Other Journal names */
alternate_names: string[];
/* Publisher Name */
publisher_name: string;
/* Publisher Address */
publisher_address: string;
/* Publisher Country, State/Province/Region of Journal */
publisher_location: string;
/* Decription of Journal */
description: string;
/* Internal Notes */
internal_notes: string;
/* Date record was updated */
updated: string;
}
/**
* ISSN class defines the data shape of the issn object managed by cold.
*/
export class ISSN implements ISSNInterface {
issn: string = "";
name: string = "";
alternate_names: string[] = [];
publisher_name: string = "";
publisher_location: string = "";
publisher_address: string = "";
description: string = "";
internal_notes: string = "";
updated: string = "";
migrateCsv(row: any): boolean {
if (row.hasOwnProperty("ISSN") && row.issn !== "") {
this.issn = row.ISSN;
} else {
return false;
}
if (row.hasOwnProperty("name")) {
this.name = row.name;
}
if (row.hasOwnProperty("Journal Name")) {
this.name = row["Journal Name"];
}
if (row.hasOwnProperty("Publisher")) {
this.publisher_name = row.Publisher;
}
if (row.hasOwnProperty("Internal Notes")) {
this.internal_notes = row.internal_notes;
}
if (row.hasOwnProperty("alternate_names")) {
this.alternate_names = row.alernate_name.split(/;/g);
}
if (row.hasOwnProperty("publisher_name")) {
this.publisher_name = row.publisher_name;
}
if (row.hasOwnProperty("publisher_location")) {
this.publisher_location = row.publisher_location;
}
if (row.hasOwnProperty("publisher_address")) {
this.publisher_address = row.publisher_address;
}
if (row.hasOwnProperty("description")) {
this.description = row.description;
}
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 issn object.
*/
asObject(): Object {
return {
issn: this.issn,
name: this.name,
alternate_names: this.alternate_names,
publisher_name: this.publisher_name,
publisher_location: this.publisher_location,
publisher_address: this.publisher_address,
description: this.description,
internal_notes: this.internal_notes,
updated: this.updated,
};
}
/**
* toJSON() returns a clean JSON representation of the issn object.
*/
toJSON(): string {
return JSON.stringify(this.asObject());
}
}
/**
* formDataToJournal turn the form data into a ISSN object.
* The difference from the utils.ts forDataToObject is that the
* alternative names fields needs to be converted from form text to an
* array of string, one per line of form text.
*
* @param {FormData} form data the form object to process
* @returns {Object}
*/
export function formDataToJournal(form: FormData): object {
const obj: { [k: string]: string | string[] | boolean } = {};
for (const v of form.entries()) {
const key: string = v[0];
if (key !== "submit") {
const val: any = v[1];
if (key === "alternative_names") {
const alt_names: string = (v[1] as any as string).trim();
if (alt_names != "") {
obj[key] = alt_names.split(/\n/g) as string[];
} else {
obj[key] = [];
}
} else if (val === "true" || val === "on") {
obj[key] = true;
} else if (val === "false" || val === "off") {
obj[key] = false;
} else {
obj[key] = val;
}
}
}
/* NOTE: Make sure we update obj.updated */
obj["updated"] = timeStamp(new Date());
return obj;
}
/**
* handleISSN provides the dataset collection UI for managing ISSN.
* It is response for the following actions
*
* - list or search for issn
* - create a issn
* - view a issn
* - update a issn
* - remove a issn
*
* 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 issn handler
* @param {debug: boolean, htdocs: string} options holds options passed from ColdReadWriteHandlerr.
* @returns {Response}
*/
export async function handleISSN(
req: Request,
options: { debug: boolean; htdocs: string; apiUrl: string },
): Promise<Response> {
if (req.method === "GET") {
return await handleGetISSN(req, options);
}
if (req.method === "POST") {
return await handlePostISSN(req, options);
}
const body = `<html>${req.method} not supported</html>`;
return new Response(body, {
status: 405,
headers: { "content-type": "text/html" },
});
}
/**
* handleGetISSN handle GET actions on issn object(s).
*
* @param {Request} req holds the request to the issn handler
* @param {debug: boolean, htdocs: string} options holds options passed from
* handleISSN.
* @returns {Response}
*
* The expected paths are in the form
*
* - `/` list the issn by issn name (`?q=...` would perform a search by issn name)
* - `/{issn}` indicates retrieving a single object by the Caltech Library issn id
*/
async function handleGetISSN(
req: Request,
options: { debug: boolean; htdocs: string; apiUrl: string },
): Promise<Response> {
/* parse the URL */
const url = new URL(req.url);
const issn = pathIdentifier(req.url);
const params = url.searchParams;
let view = params.get("view");
let tmpl = "issn_list";
if (issn !== undefined && issn !== "") {
if (view !== undefined && view === "edit") {
tmpl = "issn_edit";
} else {
tmpl = "issn";
}
} else {
if (view !== "undefined" && view === "create") {
tmpl = "issn_edit";
}
}
if (tmpl === "issn_list") {
/* display a list of issn */
const issn_list = await ds.query("issn_names", [], {});
if (issn_list !== undefined) {
return renderPage(tmpl, {
base_path: "",
issn_list: issn_list,
});
} else {
return renderPage(tmpl, {
base_path: "",
issn_list: [],
});
}
} else {
/* decide if we are in display view or edit view and pick the right template */
/* retrieve a specific record */
const issn = pathIdentifier(req.url);
const isCreateObject = issn === "";
const obj = await ds.read(issn);
console.log(`We have a GET for issn object ${issn}, view = ${view}`);
return renderPage(tmpl, {
base_path: "",
isCreateObject: isCreateObject,
issn: obj,
debug_src: JSON.stringify(obj, null, 2),
});
}
}
/**
* handlePostISSN handle POST actions on issn object(s).
*
* @param {Request} req holds the request to the issn handler
* @param {debug: boolean, htdocs: string} options holds options passed from
* handleISSN.
* @returns {Response}
*/
async function handlePostISSN(
req: Request,
options: { debug: boolean; htdocs: string; apiUrl: string },
): Promise<Response> {
let issn = pathIdentifier(req.url);
const isCreateObject = issn === "";
if (req.body !== null) {
const form = await req.formData();
let obj = formDataToJournal(form);
console.log(
`DEBUG form data after converting to object -> ${JSON.stringify(obj)}`,
);
if (!("issn" in obj)) {
console.log("issn missing", obj);
return new Response(`missing issn identifier`, {
status: 400,
headers: { "content-type": "text/html" },
});
}
if (isCreateObject) {
console.log("DEBUG detected create request");
issn = obj.issn as unknown as string;
}
if (obj.issn !== issn) {
return new Response(`mismatched issn identifier ${issn} != ${obj.issn}`, {
status: 400,
headers: { "content-type": "text/html" },
});
}
if (isCreateObject) {
console.log(`send to dataset create object ${issn}`);
if (!(await ds.create(issn, obj))) {
return new Response(
`<html>problem creating object ${issn}, try again later`,
{
status: 500,
headers: { "content-type": "text/html" },
},
);
}
} else {
console.log(`send to dataset update object ${issn}`);
if (!(await ds.update(issn, obj))) {
return new Response(
`<html>problem updating object ${issn}, try again later`,
{
status: 500,
headers: { "content-type": "text/html" },
},
);
}
}
return new Response(`<html>Redirect to ${issn}</html>`, {
status: 303,
headers: { Location: `${issn}` },
});
}
return new Response(`<html>problem creating issn data</html>`, {
status: 400,
headers: { "content-type": "text/html" },
});
}