forked from ELEVATE-Project/mentoring-mobile-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformsManager.js
85 lines (77 loc) · 2.25 KB
/
formsManager.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
const axios = require("axios");
const { error } = require("console");
const fs = require("fs");
const path = require("path");
const authToken = process.env.AUTH_TOKEN;
const apiUrl = process.env.API_URL;
const args = process.argv.slice(2);
const actionsApplicable = ["skip", "update","create"]
const formsFilePath = path.resolve(__dirname, "./forms.json");
let forms = require(formsFilePath);
const createForm = async (form) => {
try {
const response = await axios.post(
`${apiUrl}/mentoring/v1/form/create`,
form,
{ headers: { 'X-auth-token': `bearer ${authToken}` } }
);
if(response){
console.log("Form created successfully:", response.data);
form.action = "skip";
}
} catch (error) {
console.error("Error creating form:", error);
}
};
const updateForm = async (form) => {
try {
const response = await axios.put(
`${apiUrl}/mentoring/v1/form/update`,
form,
{ headers: { 'X-auth-token': `bearer ${authToken}` } }
);
if(response){
console.log("Form updated successfully:", response.data);
form.action = "skip";
}
} catch (error) {
console.error("Error updating form:", error);
}
};
const saveFormsToFile = () => {
fs.writeFile(formsFilePath, JSON.stringify(forms, null, 2), (err) => {
if (err) {
console.error("Error saving forms to file:", err);
} else {
console.log("Forms saved to file successfully.");
}
});
};
const createOrUpdateForms = async () => {
for (const form of forms) {
if(!!args[0] && actionsApplicable.includes(args[0])){
form.action = args[0];
}else if(!!args[0] && !actionsApplicable.includes(args[0])){
throw error("Invalid argument applied => ",args[0])
}
try {
switch (form.action) {
case "skip":
console.log("Skipping form with type", form.type );
break;
case "update":
await updateForm(form);
break;
case "create":
await createForm(form);
break;
default:
console.log("Unknown action for form with type", form.type);
}
} catch (error) {
console.error("Error processing form with type", form.type, ":", error);
}
}
saveFormsToFile();
};
createOrUpdateForms();