-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-location.js
325 lines (288 loc) · 11.7 KB
/
upload-location.js
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
import { query, sparqlEscapeString, sparqlEscapeUri } from 'mu';
import { querySudo } from '@lblod/mu-auth-sudo';
const VISIT_REPORT_DIR = process.env.VISIT_REPORT_DIR || '/crm-development/visit-reports';
const INTERVENTION_REPORT_DIR = process.env.INTERVENTION_REPORT_DIR || '/crm-development/intervention-reports';
const OFFER_DIR = process.env.OFFER_DIR || '/crm-development/offers';
const ORDER_DIR = process.env.ORDER_DIR || '/crm-development/orders';
const DELIVERY_NOTE_DIR = process.env.DELIVERY_NOTE_DIR || '/crm-development/delivery-notes';
const INVOICE_DIR = process.env.INVOICE_DIR || '/crm-development/invoices';
const PRODUCTION_TICKET_TEMPLATES_DIR = process.env.PRODUCTION_TICKET_TEMPLATES_DIR || '/crm-development/production-ticket-templates';
const PRODUCTION_TICKETS_DIR = process.env.PRODUCTION_TICKETS_DIR || '/crm-development/production-tickets';
const CASE_ATTACHMENT_DIR = process.env.CASE_ATTACHMENT_DIR || '/crm-development/attachments';
const ACCOUNTANCY_EXPORT_DIR = process.env.ACCOUNTANCY_EXPORT_DIR || '/crm-development/winbooks';
export const FILE_TYPES = {
VISIT_REPORT: 'http://data.rollvolet.be/concepts/f5b9c371-a0ed-4476-90a1-3e73d5d4f09e',
INTERVENTION_REPORT: 'http://data.rollvolet.be/concepts/5d7f3d76-b78e-4481-ba66-89879ea1b3eb',
OFFER: 'http://data.rollvolet.be/concepts/51577f19-9d90-4abf-a0d2-187770f76fc9',
ORDER: 'http://data.rollvolet.be/concepts/6d080a6b-41f1-45f1-9698-7cbd3c846494',
DELIVERY_NOTE: 'http://data.rollvolet.be/concepts/dcf1aa80-6b1b-4423-8ce1-4df7ffe85684',
DEPOSIT_INVOICE: 'http://data.rollvolet.be/concepts/5c93373f-30f3-454c-8835-15140ff6d1d4',
INVOICE: 'http://data.rollvolet.be/concepts/3abc9905-29b9-47f2-a77d-e94a4025f8c3',
CASE_ATTACHMENT: 'http://data.rollvolet.be/concepts/44e7a6a6-b0e6-4a9c-ae4c-1f66275f730d',
PRODUCTION_TICKET_TEMPLATE: 'http://data.rollvolet.be/concepts/0b49fae8-3546-4211-9c1e-64f359993c82',
PRODUCTION_TICKET: 'http://data.rollvolet.be/concepts/bcc644a0-eff3-4cf6-a60a-9d4e490d47f0',
INVOICE_ACCOUNTANCY_EXPORT: 'http://data.rollvolet.be/concepts/6fbc15d2-11c0-4868-8b11-d15b8f1a3802',
CUSTOMER_ACCOUNTANCY_EXPORT: 'http://data.rollvolet.be/concepts/7afecda8-f128-4043-a69c-a68cbaaedac5'
};
class UploadLocationGenerator {
constructor(type, { optionsFn, pathFn }) {
this.types = Array.isArray(type) ? type : [type];
this.optionsFn = optionsFn || noop;
this.pathFn = pathFn || noop;
}
async getUploadLocations(opts) {
const pathOpts = await this.optionsFn(opts);
return this.pathFn(pathOpts);
}
async getDownloadLocation(opts) {
const pathOpts = await this.optionsFn(opts);
const locations = await this.pathFn(pathOpts);
return locations[0];
}
}
const UPLOAD_LOCATIONS = [
new UploadLocationGenerator(FILE_TYPES.CASE_ATTACHMENT, {
optionsFn: async (opts) => {
const result = await query(`
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT ?identifier
WHERE { ?case mu:uuid ${sparqlEscapeString(opts.case.id)} ; dct:identifier ?identifier . }
`);
return { identifier: result.results.bindings[0]?.['identifier'].value, fileName: opts.fileName };
},
pathFn: (opts) => [ { path: `${CASE_ATTACHMENT_DIR}/${opts.identifier}`, name: opts.fileName } ]
}),
new UploadLocationGenerator(FILE_TYPES.VISIT_REPORT, {
optionsFn: (opts) => {
return queryOne(`
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX schema: <http://schema.org/>
SELECT ?number ?year
WHERE {
${sparqlEscapeUri(opts.resource)} schema:identifier ?number ;
dct:issued ?date .
BIND (YEAR(?date) as ?year)
} LIMIT 1
`);
},
pathFn: (opts) => [ { path: `${VISIT_REPORT_DIR}/${opts.year}`, name: `AD${opts.number}.pdf`} ]
}),
new UploadLocationGenerator(FILE_TYPES.INTERVENTION_REPORT, {
optionsFn: (opts) => {
return queryOne(`
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX schema: <http://schema.org/>
SELECT ?number ?year
WHERE {
${sparqlEscapeUri(opts.resource)} schema:identifier ?number ;
dct:issued ?date .
BIND (YEAR(?date) as ?year)
} LIMIT 1
`);
},
pathFn: (opts) => [ { path: `${INTERVENTION_REPORT_DIR}/${opts.year}`, name: `IR${opts.number}.pdf`} ]
}),
new UploadLocationGenerator(FILE_TYPES.OFFER, {
optionsFn: (opts) => {
return queryOne(`
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX schema: <http://schema.org/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
SELECT ?number ?year ?version
WHERE {
?case ext:offer ${sparqlEscapeUri(opts.resource)} ;
ext:request ?request .
${sparqlEscapeUri(opts.resource)} dct:issued ?date .
OPTIONAL { ${sparqlEscapeUri(opts.resource)} owl:versionInfo ?version . }
?request schema:identifier ?number .
BIND (YEAR(?date) as ?year)
} LIMIT 1
`);
},
pathFn: (opts) => [{
path: `${OFFER_DIR}/${opts.year}`,
name: opts.version ? `AD${opts.number}_${opts.version}.pdf` : `AD${opts.number}.pdf`
}]
}),
new UploadLocationGenerator(FILE_TYPES.ORDER, {
optionsFn: (opts) => {
return queryOne(`
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
SELECT ?number ?year
WHERE {
?case ext:order ${sparqlEscapeUri(opts.resource)} ;
ext:request ?request .
${sparqlEscapeUri(opts.resource)} dct:issued ?date .
?request schema:identifier ?number .
BIND (YEAR(?date) as ?year)
} LIMIT 1
`);
},
pathFn: (opts) => [ { path: `${ORDER_DIR}/${opts.year}`, name: `AD${opts.number}.pdf`} ]
}),
new UploadLocationGenerator(FILE_TYPES.DELIVERY_NOTE, {
optionsFn: (opts) => {
return queryOne(`
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
SELECT ?number ?year
WHERE {
?case ext:order ${sparqlEscapeUri(opts.resource)} ;
ext:request ?request .
${sparqlEscapeUri(opts.resource)} dct:issued ?date .
?request schema:identifier ?number .
BIND (YEAR(?date) as ?year)
} LIMIT 1
`);
},
pathFn: (opts) => [ { path: `${DELIVERY_NOTE_DIR}/${opts.year}`, name: `AD${opts.number}.pdf`} ]
}),
new UploadLocationGenerator([FILE_TYPES.INVOICE, FILE_TYPES.DEPOSIT_INVOICE], {
optionsFn: async (opts) => {
const result = await queryOne(`
PREFIX p2poInvoice: <https://purl.org/p2p-o/invoice#>
SELECT ?number ?year
WHERE {
${sparqlEscapeUri(opts.resource)} p2poInvoice:invoiceNumber ?number ;
p2poInvoice:dateOfIssue ?date .
BIND (YEAR(?date) as ?year)
} LIMIT 1
`);
result['number'] = `${result['number']}`.padStart(7, 0); // normalize invoice number
return result;
},
pathFn: (opts) => [ { path: `${INVOICE_DIR}/${opts.year}`, name: `F${opts.number}.pdf`} ]
}),
new UploadLocationGenerator(FILE_TYPES.PRODUCTION_TICKET, {
optionsFn: async (opts) => {
let caseStatement;
if (opts.resource) {
caseStatement = `${sparqlEscapeUri(opts.resource)} `;
} else {
caseStatement = `?case mu:uuid ${sparqlEscapeString(opts.case.id)} ; `;
}
const result = await queryOne(`
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
SELECT ?number ?year ?customerName
WHERE {
${caseStatement} ext:request ?request ;
ext:order ?order ;
schema:customer ?customer .
?request schema:identifier ?number .
?order dct:issued ?date .
OPTIONAL { ?customer vcard:hasFamilyName ?customerName . }
BIND (YEAR(?date) as ?year)
} LIMIT 1
`);
result['customerName'] = result['customerName'] ? noSpecialChars(result['customerName']) : '';
return result;
},
pathFn: (opts) => [{
path: `${PRODUCTION_TICKETS_DIR}/${opts.year}`,
name: `AD${opts.number}_${opts.customerName}.pdf`,
search: `AD${opts.number}`
}]
}),
new UploadLocationGenerator(FILE_TYPES.PRODUCTION_TICKET_TEMPLATE, {
optionsFn: (opts) => {
return queryOne(`
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
SELECT ?number ?year
WHERE {
${sparqlEscapeUri(opts.resource)} ext:request ?request ; ext:order ?order .
?request schema:identifier ?number .
?order dct:issued ?date .
BIND (YEAR(?date) as ?year)
} LIMIT 1
`);
},
pathFn: (opts) => [ { path: `${PRODUCTION_TICKET_TEMPLATES_DIR}/${opts.year}`, name: `AD${opts.number}.pdf`} ]
}),
new UploadLocationGenerator(FILE_TYPES.INVOICE_ACCOUNTANCY_EXPORT, {
pathFn: () => [
{ path: ACCOUNTANCY_EXPORT_DIR, name: 'ACT.csv' },
{ path: ACCOUNTANCY_EXPORT_DIR, name: `${timestamp()}-ACT.csv` }
]
}),
new UploadLocationGenerator(FILE_TYPES.CUSTOMER_ACCOUNTANCY_EXPORT, {
pathFn: () => [
{ path: ACCOUNTANCY_EXPORT_DIR, name: 'CSF.csv' },
{ path: ACCOUNTANCY_EXPORT_DIR, name: `${timestamp()}-CSF.csv` }
]
}),
];
/**
* Returns one or more locations a file with a given type should be uploaded to.
* The location depends on the type of the file.
* Multiple locations may be returned. The first one is considered to be the 'main' location.
* Other locations are just copies of the same file and will not be tracked in the triplestore.
*/
export function getUploadLocations(type, opts) {
return findGenerator(type).getUploadLocations(opts);
}
export async function getUploadLocationsForFile(fileUri) {
const { type, resource } = await getFileType(fileUri);
return getUploadLocations(type, { resource });
}
export function getDownloadLocation(type, opts) {
return findGenerator(type).getDownloadLocation(opts);
}
/* Helper functions */
function findGenerator(type) {
const generator = UPLOAD_LOCATIONS.find((generator) => generator.types.includes(type));
if (generator) {
return generator;
} else {
throw new Error(`Upload location not yet implemented for file type '${type}'`);
}
}
function timestamp(date = new Date()) {
return date.toISOString()
.substr(0, 'yyyy-MM-DDTHH:mm:ss'.length)
.replaceAll(/[^0-9]/g, '');
}
async function queryOne(q) {
const { results } = await querySudo(q);
if (results.bindings.length) {
const binding = results.bindings[0];
const result = {};
for (let key of Object.keys(binding)) {
result[key] = binding[key]?.value;
}
return result;
} else {
return null;
}
}
async function getFileType(fileUri) {
return queryOne(`
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>
PREFIX nie: <http://www.semanticdesktop.org/ontologies/2007/01/19/nie#>
SELECT ?type ?resource
WHERE {
GRAPH ?g {
${sparqlEscapeUri(fileUri)} a nfo:FileDataObject ;
nie:dataSource ?virtualFile .
?virtualFile dct:type ?type .
OPTIONAL { ?virtualFile prov:wasDerivedFrom ?resource . }
}
} LIMIT 1
`);
}
function noSpecialChars(name) {
return name.replace(/\r|\n|\r\n|\t|\"/g, '');
}
function noop() { };