-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate-data.js
263 lines (240 loc) · 6.7 KB
/
generate-data.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const casual = require('casual')
const fs = require('fs')
const axios = require('axios')
const queryString = require('query-string')
const uniqid = require('uniqid')
const axiosClient = axios.create({
baseURL: 'https://mapi.sendo.vn/mob',
headers: {
'content-type': 'application/json',
},
paramsSerializer: (params) => queryString.stringify(params),
})
axiosClient.interceptors.response.use(
(response) => {
return response.data
},
(error) => {
console.log(error)
}
)
const searchProducts = async (queryParams) => {
const url = '/product/search'
const response = await axiosClient.get(url, { params: queryParams })
return response.data
}
const getProductDetail = async (productId) => {
const url = `/product/${productId}/detail/`
return await axiosClient.get(url)
}
// ---------------
// Random 50 posts data
const posts = []
Array.from(new Array(50).keys()).map(() => {
const post = {
id: uniqid(),
title: casual.title,
author: casual.full_name,
description: casual.words(50),
createdAt: Date.now(),
updatedAt: Date.now(),
imageUrl: `https://picsum.photos/id/${casual.integer(1, 1000)}/1368/400`,
}
posts.push(post)
})
const S3_IMAGE_URL = 'https://media3.scdn.vn'
const mapToProduct = (product) => {
return {
id: product.id,
name: product.name,
shortDescription: product.short_description,
description: product.description,
originalPrice: product.price,
salePrice: product.final_price,
isPromotion: product.is_promotion,
promotionPercent: product.promotion_percent,
images: product.images.map((url) => `${S3_IMAGE_URL}${url}`),
isFreeShip: product.is_free_ship,
createdAt: Date.now(),
updatedAt: Date.now(),
}
}
// https://techinsight.com.vn/tai-lieu-huong-dan-su-dung-api-vietnam-ai-hackathon
const categoryList = [
{
id: uniqid(),
name: 'Thời trang',
searchTerm: 'ao so mi nu',
createdAt: Date.now(),
updatedAt: Date.now(),
},
{
id: uniqid(),
name: 'Khẩu trang',
searchTerm: 'khau trang',
createdAt: Date.now(),
updatedAt: Date.now(),
},
{
id: uniqid(),
name: 'Làm đẹp',
searchTerm: 'lam dep',
createdAt: Date.now(),
updatedAt: Date.now(),
},
{
id: uniqid(),
name: 'Laptop',
searchTerm: 'macbook',
createdAt: Date.now(),
updatedAt: Date.now(),
},
{
id: uniqid(),
name: 'Ổ cứng',
searchTerm: 'o cung ssd',
createdAt: Date.now(),
updatedAt: Date.now(),
},
{
id: uniqid(),
name: 'Điện thoại',
searchTerm: 'iphone',
createdAt: Date.now(),
updatedAt: Date.now(),
},
]
const productList = []
const fetchProductList = async () => {
// Loop through categories
// Each cate, fetch list of product
// Slide the first 20 items
// Loop through each item and get detail
// Then map to our product
// Finally add to product list
for (const category of categoryList) {
const queryParams = {
p: 1,
q: category.searchTerm,
}
const productIdList = (await searchProducts(queryParams)).slice(0, 20).map((item) => item.id)
for (const productId of productIdList) {
const productData = await getProductDetail(productId)
const transformedProduct = mapToProduct(productData)
transformedProduct.categoryId = category.id
productList.push(transformedProduct)
}
console.log('Done adding category', category.name, productIdList.length)
}
}
const cityList = [
{
code: 'hcm',
name: 'Hồ Chí Minh',
},
{
code: 'hn',
name: 'Hà Nội',
},
{
code: 'dn',
name: 'Đà Nẵng',
},
{
code: 'pt',
name: 'Phan Thiết',
},
]
// Random 50 students data
const students = []
Array.from(new Array(50).keys()).map(() => {
const post = {
id: uniqid(),
name: casual.full_name,
age: casual.integer(18, 27),
mark: Number.parseFloat(casual.double(3, 10).toFixed(1)),
gender: ['male', 'female'][casual.integer(1, 100) % 2],
createdAt: Date.now(),
updatedAt: Date.now(),
city: ['hcm', 'hn', 'dn', 'pt'][casual.integer(1, 100) % 5],
}
students.push(post)
})
const tagList = [
'Design',
'Dashboard',
'User Experience',
'Computer Science',
'ReactJS',
'Frontend Development',
'NextJS',
]
// Generate works
const thumbnailList = [
'https://res.cloudinary.com/easy-frontend/image/upload/v1648712410/learn-nextjs/item1_cbidwn.jpg',
'https://res.cloudinary.com/easy-frontend/image/upload/v1648712410/learn-nextjs/item2_usidpx.jpg',
'https://res.cloudinary.com/easy-frontend/image/upload/v1648712410/learn-nextjs/item3_jlfuun.jpg',
]
const fullDescription =
'<h2>Easy Frontend</h2><p><br></p><p><img src="https://res.cloudinary.com/easy-frontend/image/upload/v1692522044/upload-learn-nextjs/tziuz7e5f4ime4xlce3f.jpg"></p><p><br></p><p>learn <strong>nextjs</strong> is <span style="color: rgb(255, 153, 0);">fun</span></p><p><br></p><blockquote>super cool</blockquote><p><br></p><p>it works wohoo</p>'
const workList = []
for (let i = 1; i <= 20; i++) {
const from = casual.integer(0, tagList.length - 1)
const to = casual.integer(from, tagList.length - 1)
console.log('tag list slice', from, to, tagList.slice(from, to))
const workItem = {
id: uniqid(),
title: casual.title,
createdAt: Date.now(),
updatedAt: Date.now(),
tagList: tagList.slice(from, to),
shortDescription:
'Amet minim mollit non deserunt ullamco est sit aliqua dolor do amet sint. Velit officia consequat duis enim velit mollit. Exercitation veniam consequat sunt nostrud amet.',
fullDescription,
thumbnailUrl: thumbnailList[i % 3],
}
workList.push(workItem)
}
// Generate tnxs
const transactions = []
for (let i = 0; i < 10; i++) {
const transaction = {
id: uniqid(),
date: casual.date('YYYY-MM-DD'),
amount: casual.double(0.01, 10000).toFixed(2),
description: casual.short_description,
sender: casual.full_name,
receiver: casual.full_name,
status: casual.random_element(['pending', 'processing', 'completed', 'cancelled']),
createdAt: Date.now(),
updatedAt: Date.now(),
}
transactions.push(transaction)
}
// --------------------
// --------------------
const main = async () => {
await fetchProductList()
// Setup db object
const db = {
posts,
categories: categoryList,
products: productList,
students,
cities: cityList,
works: workList,
tags: tagList,
transactions,
'public-profile': {
id: 'public-profile',
name: casual.full_name,
city: casual.city,
email: casual.email.toLowerCase(),
},
}
// Save posts array to db.json file
fs.writeFile('db.json', JSON.stringify(db), () => {
console.log(`Generate ${posts.length} sample post records and saved in db.json!!! =))`)
})
}
main()