-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
107 lines (93 loc) · 2.76 KB
/
seed.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
const http = require('http');
const baseURL = 'http://localhost:3000'; // Replace with your API base URL
const registerUsers = async () => {
try {
for (let i = 1; i <= 10; i++) {
const userData = {
username: `user${i}`,
email: `user${i}@example.com`,
password: 'userpassword',
};
await fetchRequest(`${baseURL}/register`, 'POST', userData);
console.log(`User ${i} registered successfully`);
}
} catch (error) {
console.error('Error registering users:', error);
}
};
const createForm = async (token, userNumber, formNumber) => {
try {
const formDetails = {
title: `Form ${formNumber} by user${userNumber}`,
description: `Description for Form ${formNumber} by user${userNumber}`,
inputs: [
{
type: 'small',
label: 'Full Name',
description: 'Enter your full name',
},
{
type: 'email',
label: 'Email Address',
},
{
type: 'long',
label: 'Feedback',
description: 'Enter your feedback',
},
// Add more input objects as needed with different types
],
};
const response = await fetchRequest(`${baseURL}/forms`, 'POST', formDetails, token);
const responseData = await response.json();
// check response code in range 200-299
if (responseData.statusCode >= 200 && responseData.statusCode < 300) {
console.log(`Form ${formNumber} created successfully for user${userNumber}`);
}else{
console.log(`Form ${formNumber} not created for user${userNumber}`);
}
} catch (error) {
console.error(`Error creating form ${formNumber} for user${userNumber}:`, error);
}
};
const loginAndCreateForms = async () => {
try {
for (let i = 1; i <= 10; i++) {
const loginData = {
loginID: `user${i}@email.com`,
password: 'userpassword',
};
const loginResponse = await fetchRequest(`${baseURL}/login`, 'POST', loginData);
const tokenData = await loginResponse.json();
const token = tokenData.token;
for (let j = 1; j <= 10; j++) {
await createForm(token, i, j);
}
}
} catch (error) {
console.error('Error logging in or creating forms:', error);
}
};
const fetchRequest = async (url, method, data, token = null) => {
const options = {
method,
headers: {
'Content-Type': 'application/json',
},
};
if (data) {
options.body = JSON.stringify(data);
}
if (token) {
options.headers.Authorization = `Bearer ${token}`;
}
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response;
};
(async () => {
await registerUsers();
await loginAndCreateForms();
})();