@mixd-id/web-scaffold 0.1.230406020 → 0.1.230406021
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/utils/helpers.js +31 -1
- package/src/utils/importer.js +30 -26
package/package.json
CHANGED
package/src/utils/helpers.js
CHANGED
|
@@ -228,6 +228,35 @@ const getPresetSortWhereParams = (order, afterItem) => {
|
|
|
228
228
|
return sortWhere
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
+
const unflatten = (flatObject) => {
|
|
232
|
+
const nestedObject = {};
|
|
233
|
+
|
|
234
|
+
for (const key in flatObject) {
|
|
235
|
+
if (Object.prototype.hasOwnProperty.call(flatObject, key)) {
|
|
236
|
+
const value = flatObject[key];
|
|
237
|
+
const keys = key.split('.');
|
|
238
|
+
|
|
239
|
+
let currentObj = nestedObject;
|
|
240
|
+
for (let i = 0; i < keys.length; i++) {
|
|
241
|
+
const currentKey = keys[i];
|
|
242
|
+
|
|
243
|
+
if (!currentObj[currentKey]) {
|
|
244
|
+
currentObj[currentKey] = {};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (i === keys.length - 1) {
|
|
248
|
+
currentObj[currentKey] = value;
|
|
249
|
+
} else {
|
|
250
|
+
currentObj = currentObj[currentKey];
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return nestedObject;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
|
|
231
260
|
module.exports = {
|
|
232
261
|
ceil,
|
|
233
262
|
floor,
|
|
@@ -244,5 +273,6 @@ module.exports = {
|
|
|
244
273
|
writeStorage,
|
|
245
274
|
bufferToStream,
|
|
246
275
|
sequelizeChunk,
|
|
247
|
-
getPresetSortWhereParams
|
|
276
|
+
getPresetSortWhereParams,
|
|
277
|
+
unflatten
|
|
248
278
|
}
|
package/src/utils/importer.js
CHANGED
|
@@ -3,7 +3,7 @@ const fs = require("fs");
|
|
|
3
3
|
const AdmZip = require("adm-zip");
|
|
4
4
|
const glob = require("glob");
|
|
5
5
|
const Exceljs = require("exceljs")
|
|
6
|
-
const { saveBuffer } = require('./helpers.js')
|
|
6
|
+
const { saveBuffer, unflatten } = require('./helpers.js')
|
|
7
7
|
|
|
8
8
|
const analyseRequest = async(req) => {
|
|
9
9
|
|
|
@@ -64,10 +64,17 @@ const analyseRequest = async(req) => {
|
|
|
64
64
|
|
|
65
65
|
const columns = []
|
|
66
66
|
const header = worksheet.getRow(1)
|
|
67
|
+
|
|
67
68
|
if(!header)
|
|
68
69
|
throw new Error('Invalid file')
|
|
69
70
|
header.eachCell((cell) => {
|
|
70
|
-
|
|
71
|
+
if(cell.isMerged){
|
|
72
|
+
const sub = worksheet.getCell(cell.address.replace(/\d/g, '') + '2').value
|
|
73
|
+
columns.push(cell.value + '>' + sub)
|
|
74
|
+
}
|
|
75
|
+
else{
|
|
76
|
+
columns.push(cell.value)
|
|
77
|
+
}
|
|
71
78
|
})
|
|
72
79
|
|
|
73
80
|
return {
|
|
@@ -87,8 +94,7 @@ const importRequest = async(req) => {
|
|
|
87
94
|
throw new Error({name: ['Kolom ' + key.text + ' harus diisi']})
|
|
88
95
|
})
|
|
89
96
|
|
|
90
|
-
|
|
91
|
-
const cols = {}
|
|
97
|
+
let rows = []
|
|
92
98
|
const workbook = new Exceljs.Workbook();
|
|
93
99
|
switch(xlsxFileType){
|
|
94
100
|
|
|
@@ -106,37 +112,35 @@ const importRequest = async(req) => {
|
|
|
106
112
|
}
|
|
107
113
|
|
|
108
114
|
const worksheet = workbook.worksheets[0]
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
if(!mapped){
|
|
122
|
-
cols[cell.value] = index
|
|
123
|
-
}
|
|
124
|
-
})
|
|
115
|
+
|
|
116
|
+
let rowStart = 2
|
|
117
|
+
const header = worksheet.getRow(1)
|
|
118
|
+
const columnAddress = {}
|
|
119
|
+
header.eachCell((cell, index) => {
|
|
120
|
+
if(cell.isMerged){
|
|
121
|
+
const sub = worksheet.getCell(cell.address.replace(/\d/g, '') + '2').value
|
|
122
|
+
columnAddress[cell.value + '>' + sub] = index
|
|
123
|
+
rowStart = 3
|
|
125
124
|
}
|
|
126
125
|
else{
|
|
126
|
+
columnAddress[cell.value] = index
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
worksheet.eachRow((row, index) => {
|
|
131
|
+
if(index >= rowStart){
|
|
127
132
|
const obj = {}
|
|
128
133
|
|
|
129
|
-
for(let
|
|
130
|
-
const
|
|
131
|
-
|
|
134
|
+
for(let idx in keys){
|
|
135
|
+
const key = keys[idx]
|
|
136
|
+
const cell = row.getCell(columnAddress[key.value])
|
|
137
|
+
obj[key.key] = cell.formulaType === 1 ? cell.result : cell.value
|
|
132
138
|
}
|
|
133
139
|
|
|
134
|
-
rows.push(obj)
|
|
140
|
+
rows.push(unflatten(obj))
|
|
135
141
|
}
|
|
136
142
|
})
|
|
137
143
|
|
|
138
|
-
//console.log(JSON.stringify(rows, null, 2))
|
|
139
|
-
|
|
140
144
|
const images = glob.sync(process.env.ROOT_PATH + '/storage/files/temp/' + folderName +
|
|
141
145
|
'/**/*(*.jpg|*.jpeg|*.png|*.webp)')
|
|
142
146
|
|