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 | destination: string, |
15 | fields: object, |
16 | convertToPdf: boolean = false, |
17 | useAppApiAuth: boolean = false, |
18 | ) { |
19 |
|
20 | const data = { |
21 | fields: Object.keys(fields).reduce((carry: object, key: string) => { |
22 | carry['ContentControls.ByIndex.' + key] = { content: fields[key] } |
23 | return carry |
24 | }, {}), |
25 | destination, |
26 | } |
27 | if (convertToPdf) { |
28 | data.convert = 'pdf' |
29 | } |
30 | |
31 | console.debug('data', data) |
32 |
|
33 | const url = ncResource.baseUrl + '/ocs/v2.php/apps/richdocuments/api/v1/template/fields/fill/' + templateFileId |
34 | const config = { |
35 | ...(!useAppApiAuth && ({ |
36 | auth: { |
37 | username: ncResource.username, |
38 | password: ncResource.password, |
39 | }, |
40 | })), |
41 | headers: { |
42 | 'content-type': 'application/json', |
43 | 'ocs-apirequest': true, |
44 | ...(useAppApiAuth && ({ |
45 | "AA-VERSION": ncResource.aa_version, |
46 | "EX-APP-ID": ncResource.app_id, |
47 | "EX-APP-VERSION": ncResource.app_version, |
48 | "AUTHORIZATION-APP-API": btoa( |
49 | `${userId || ncResource.username}:${ncResource.password}`, |
50 | ), |
51 | })), |
52 | }, |
53 | } |
54 | console.debug('config', config) |
55 | try { |
56 | const resp = await axios.post(url, data, config) |
57 | console.debug('RESPONSE', resp.data) |
58 | return { |
59 | data: resp.data, |
60 | } |
61 | } catch(e) { |
62 | console.debug('error', e) |
63 | } |
64 | |
65 | |
66 | return {} |
67 | } |