1 | import * as wmill from "windmill-client" |
2 | import axios from "axios" |
3 |
|
4 | type Nextcloud = { |
5 | baseUrl: string, |
6 | password: string, |
7 | username: string |
8 | }; |
9 |
|
10 | export async function main( |
11 | ncResource: Nextcloud, |
12 | userId: string|null = null, |
13 | templateFileId: number, |
14 | formFieldValue: string, |
15 | destination: string, |
16 | fields: { [index: string]: string }, |
17 | convertToPdf: boolean = false, |
18 | useAppApiAuth: boolean = false, |
19 | ) { |
20 |
|
21 | let data: any = { fields: {}, destination }; |
22 |
|
23 | const selectors: { [index: string]: string } = { |
24 | id: 'ById', |
25 | title: 'ByAlias', |
26 | index: 'ByIndex', |
27 | tag: 'ByTag', |
28 | } |
29 | const selector: string = selectors[formFieldValue] ?? 'ById' |
30 | console.debug('SELECTOR ----', selector) |
31 | data.fields = Object.keys(fields).reduce((carry: { [index: string]: object }, key: string) => { |
32 | carry['ContentControls.' + selector + '.' + key] = { content: fields[key] } |
33 | return carry |
34 | }, {}); |
35 | |
36 | if (convertToPdf) { |
37 | data.convert = 'pdf' |
38 | } |
39 | |
40 | console.debug('data', data) |
41 |
|
42 | const url = ncResource.baseUrl + '/ocs/v2.php/apps/richdocuments/api/v1/template/fields/fill/' + templateFileId |
43 | const config = { |
44 | ...(!useAppApiAuth && ({ |
45 | auth: { |
46 | username: ncResource.username, |
47 | password: ncResource.password, |
48 | }, |
49 | })), |
50 | headers: { |
51 | 'content-type': 'application/json', |
52 | 'ocs-apirequest': true, |
53 | ...(useAppApiAuth && ({ |
54 | "AA-VERSION": ncResource.aa_version, |
55 | "EX-APP-ID": ncResource.app_id, |
56 | "EX-APP-VERSION": ncResource.app_version, |
57 | "AUTHORIZATION-APP-API": btoa( |
58 | `${userId || ncResource.username}:${ncResource.password}`, |
59 | ), |
60 | })), |
61 | }, |
62 | } |
63 | console.debug('config', config) |
64 | try { |
65 | const resp = await axios.post(url, data, config) |
66 | console.debug('RESPONSE', resp.data) |
67 | return { |
68 | data: resp.data, |
69 | } |
70 | } catch(e) { |
71 | console.debug('error', e) |
72 | } |
73 | |
74 | |
75 | return {} |
76 | } |