-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (91 loc) · 3.19 KB
/
index.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
import 'dotenv/config';
import { env } from 'node:process';
import { HttpClient } from './HttpClient.js';
import { FieldFactory } from './Field.js';
import { FieldResponseFactory } from './FieldResponse.js';
async function main() {
const formBuilder = new FormBuilderClient(
env['123_FORM_BUILDER_URL'],
env['123_FORM_BUILDER_USERNAME'],
env['123_FORM_BUILDER_PASSWORD'],
);
const formPixOrga = 50313;
const formSubmissions = await formBuilder.getFormSubmissions(formPixOrga);
formSubmissions.forEach((submission) => {
// eslint-disable-next-line no-console
console.log(submission.id);
// eslint-disable-next-line no-console
console.log(submission.fieldResponses);
});
}
class FormBuilderClient {
#httpClient;
#baseUrl;
#username;
#password;
#token;
constructor(url, username, password, httpClient = new HttpClient()) {
this.#baseUrl = url;
this.#username = username;
this.#password = password;
this.#httpClient = httpClient;
}
async getToken() {
if (this.#token) {
return this.#token;
}
const tokenUrl = `${this.#baseUrl}/token`;
const response = await this.#httpClient.post(tokenUrl, {
username: this.#username,
password: this.#password,
}, {});
this.#token = response.token;
return this.#token;
}
async getList() {
const token = await this.getToken();
const formsUrl = `${this.#baseUrl}/forms`;
return this.#httpClient.get(formsUrl, {
Authorization: `Bearer ${token}`,
});
}
async getFormDetails(formId) {
const token = await this.getToken();
const formUrl = `${this.#baseUrl}/forms/${formId}`;
return this.#httpClient.get(formUrl, {
Authorization: `Bearer ${token}`,
});
}
async getFormFields(formId) {
const token = await this.getToken();
const formUrl = `${this.#baseUrl}/forms/${formId}`;
const result = await this.#httpClient.get(`${formUrl}/fields`, {
Authorization: `Bearer ${token}`,
});
return result.data.controls.data.map(field => FieldFactory.createField(field));
}
async getFormSubmissions(formId) {
const token = await this.getToken();
const fields = await this.getFormFields(formId);
const formUrl = `${this.#baseUrl}/forms/${formId}/submissions`;
const result = await this.#httpClient.get(formUrl, {
Authorization: `Bearer ${token}`,
});
return result.data.map((submission) => {
const fieldResponses = fields.map(field => FieldResponseFactory.createFieldResponse(field));
const fieldsData = submission.content.fields.field;
fieldsData.forEach((field) => {
const foundFieldResponse = fieldResponses.find(f => field.fieldid.includes(f.id));
if (!foundFieldResponse) {
console.error(`Field with id ${field.fieldid} not found`);
}
else {
foundFieldResponse.insertResponse(field);
}
});
const filteredResponses = fieldResponses.filter(fieldResponse => fieldResponse.formattedResponse.responses !== undefined).map(({ formattedResponse }) => formattedResponse);
return { id: submission.id, refid: submission.content.refid, fieldResponses: filteredResponses };
});
}
}
main().then().catch(console.error);