|
| 1 | +/* eslint no-console: off */ |
1 | 2 | import fetch from 'node-fetch';
|
| 3 | +import _ from 'lodash'; |
2 | 4 |
|
3 |
| -const hcBackendUrl = 'http://localhost:3030' |
4 |
| -const bundestagGraphqlUrl = 'http://localhost:3000/graphql' |
5 |
| -const query = '{"query": "{procedures(type: PREPARATION) { title abstract }}"}'; |
| 5 | +const hcBackendUrl = 'http://localhost:3030'; |
| 6 | +const bundestagGraphqlUrl = 'http://localhost:3000/graphql'; |
| 7 | +const query = `{ |
| 8 | + procedures(type: PREPARATION) { |
| 9 | + title |
| 10 | + abstract |
| 11 | + } |
| 12 | +}`; |
6 | 13 |
|
7 | 14 | class User {
|
8 | 15 | login(email, password) {
|
9 | 16 | const formData = {
|
10 |
| - email: email, |
11 |
| - password: password, |
| 17 | + email, |
| 18 | + password, |
12 | 19 | strategy: 'local',
|
13 |
| - } |
| 20 | + }; |
14 | 21 | return fetch(`${hcBackendUrl}/authentication`, {
|
15 | 22 | method: 'post',
|
16 | 23 | body: JSON.stringify(formData),
|
17 |
| - headers: { 'Content-Type': 'application/json' } |
18 |
| - }).then((response) => { |
19 |
| - return response.json() |
20 |
| - }).then((json) => { |
| 24 | + headers: { 'Content-Type': 'application/json' }, |
| 25 | + }).then(response => response.json()).then((json) => { |
21 | 26 | this.accessToken = json.accessToken;
|
22 | 27 | return true;
|
23 | 28 | });
|
24 | 29 | }
|
25 | 30 |
|
26 |
| - contribute(contribution){ |
| 31 | + contribute(contribution) { |
27 | 32 | return fetch(`${hcBackendUrl}/contributions`, {
|
28 | 33 | method: 'post',
|
29 |
| - body: JSON.stringify(contribution), |
30 |
| - headers: { |
31 |
| - 'Authorization': `Bearer ${this.accessToken}`, |
32 |
| - 'Content-Type': 'application/json' |
33 |
| - } |
34 |
| - }).then((response) => { |
35 |
| - return response.json(); |
36 |
| - }) |
| 34 | + body: JSON.stringify(contribution), |
| 35 | + headers: { |
| 36 | + Authorization: `Bearer ${this.accessToken}`, |
| 37 | + 'Content-Type': 'application/json', |
| 38 | + }, |
| 39 | + }).then(response => response.json()); |
37 | 40 | }
|
38 | 41 | }
|
39 | 42 |
|
40 |
| -async function bundestagProcedures(){ |
41 |
| - return await fetch(bundestagGraphqlUrl, { |
42 |
| - method: 'post', |
43 |
| - body: query, |
44 |
| - headers: { 'Content-Type': 'application/json' } |
45 |
| - }).then((response) => { |
46 |
| - return response.json() |
47 |
| - }).then((json) => { |
48 |
| - return json.data.procedures; |
49 |
| - }); |
| 43 | +async function bundestagProcedures() { |
| 44 | + return fetch(bundestagGraphqlUrl, { |
| 45 | + method: 'post', |
| 46 | + body: JSON.stringify({ query }), |
| 47 | + headers: { 'Content-Type': 'application/json' }, |
| 48 | + }).then(response => response.json()).then(json => json.data.procedures); |
50 | 49 | }
|
51 | 50 |
|
52 | 51 |
|
53 |
| - |
54 |
| - |
55 | 52 | async function main() {
|
56 |
| - let user = new User(); |
| 53 | + const user = new User(); |
57 | 54 | await user.login('[email protected]', '1234');
|
58 |
| - const procedures = await bundestagProcedures(); |
59 |
| - if (procedures.length > 0){ |
| 55 | + let procedures = await bundestagProcedures(); |
| 56 | + procedures = procedures.filter((p) => { return ! _.isNil(p.abstract) }); |
| 57 | + |
| 58 | + |
| 59 | + if (_.isEmpty(procedures)) { |
| 60 | + console.log('No procedures found!'); |
| 61 | + } else { |
60 | 62 | const contribution = {
|
61 | 63 | title: procedures[0].title,
|
62 | 64 | content: procedures[0].abstract,
|
63 | 65 | contentExcerpt: procedures[0].abstract,
|
64 |
| - type: "post", |
65 |
| - language: "de", |
66 |
| - categoryIds: ["5ac7768f8d655d2ee6d48fe4"] // democracy-politics |
67 |
| - } |
| 66 | + type: 'post', |
| 67 | + language: 'de', |
| 68 | + categoryIds: ['5ac7768f8d655d2ee6d48fe4'], // democracy-politics |
| 69 | + }; |
68 | 70 | const response = await user.contribute(contribution);
|
69 | 71 | console.log(response);
|
70 |
| - } else { |
71 |
| - console.log("No procedures found!"); |
72 | 72 | }
|
73 | 73 | }
|
74 | 74 |
|
|
0 commit comments