@mixd-id/web-scaffold 0.1.240411086 → 0.1.240411088

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mixd-id/web-scaffold",
3
3
  "private": false,
4
- "version": "0.1.240411086",
4
+ "version": "0.1.240411088",
5
5
  "scripts": {
6
6
  "dev": "vite serve",
7
7
  "build": "vite build",
@@ -179,6 +179,8 @@ export default{
179
179
  data
180
180
  })
181
181
  .then(({ data:res }) => {
182
+ if(!res?.keys)
183
+ return this.alert('File gagal di analisa, pastikan file sesuai dengan format yang ditentukan.')
182
184
 
183
185
  this.step = 2
184
186
  this.importData = res
@@ -195,7 +197,7 @@ export default{
195
197
  })
196
198
  })
197
199
  .catch((e) => {
198
- this.alert(e.response.data)
200
+ this.alert(e)
199
201
  })
200
202
  .finally(() => {
201
203
  this.isLoading = false
@@ -59,7 +59,6 @@ const analyseRequest = async(req) => {
59
59
  xlsxFile = process.env.ROOT_PATH + '/storage/files/temp/' + folderName + '/' + filename
60
60
  }
61
61
 
62
-
63
62
  const workbook = new Exceljs.Workbook();
64
63
  switch(xlsxFileType){
65
64
 
@@ -76,30 +75,40 @@ const analyseRequest = async(req) => {
76
75
  break
77
76
  }
78
77
 
79
- if(workbook.worksheets.length > 1)
80
- throw new Error('Number of sheet allowed within file is 1')
81
- else if(workbook.worksheets.length < 1)
82
- throw new Error('Unable to read worksheet')
78
+ if(workbook.worksheets.length < 1)
79
+ throw new Error('Unable to read worksheet, please check your file and try again')
83
80
 
84
- const worksheet = workbook.worksheets[0]
81
+ const docs = []
82
+ let index = 0
83
+ for(const worksheet of workbook.worksheets){
85
84
 
86
- const columns = []
87
- const header = worksheet.getRow(1)
85
+ const columns = []
86
+ const header = worksheet.getRow(1)
88
87
 
89
- if(!header)
90
- throw new Error('Invalid file')
91
- header.eachCell((cell) => {
92
- if(cell.isMerged){
93
- const sub = worksheet.getCell(cell.address.replace(/\d/g, '') + '2').value
94
- columns.push(cell.value + '>' + sub)
95
- }
96
- else{
97
- columns.push(cell.value)
98
- }
99
- })
88
+ header.eachCell((cell) => {
89
+ if(cell.isMerged){
90
+ const sub = worksheet.getCell(cell.address.replace(/\d/g, '') + '2').value
91
+ columns.push(cell.value + '>' + sub)
92
+ }
93
+ else{
94
+ columns.push(cell.value)
95
+ }
96
+ })
97
+
98
+ docs.push({
99
+ name: `${worksheet.name ?? ''}`.trim(),
100
+ columns,
101
+ index
102
+ })
103
+
104
+ index++
105
+ }
106
+
107
+ const columns = docs[0].columns
100
108
 
101
109
  return {
102
110
  columns,
111
+ docs,
103
112
  folderName,
104
113
  xlsxFileType,
105
114
  xlsxFile
@@ -108,14 +117,13 @@ const analyseRequest = async(req) => {
108
117
 
109
118
  const importRequest = async(req) => {
110
119
 
111
- const { columns, folderName, xlsxFileType, xlsxFile, keys } = req.body
120
+ const { columns, docs, folderName, xlsxFileType, xlsxFile } = req.body
112
121
 
113
- keys.forEach((key) => {
122
+ req.body.keys.forEach((key) => {
114
123
  if (key.required && !key.value)
115
124
  throw new Error({name: ['Kolom ' + key.text + ' harus diisi']})
116
125
  })
117
126
 
118
- let rows = []
119
127
  const workbook = new Exceljs.Workbook();
120
128
  switch(xlsxFileType){
121
129
 
@@ -132,63 +140,85 @@ const importRequest = async(req) => {
132
140
  break
133
141
  }
134
142
 
135
- const worksheet = workbook.worksheets[0]
143
+ for(const worksheet of workbook.worksheets){
144
+ const doc = docs.find(_ => _.name === worksheet.name)
136
145
 
137
- const row = worksheet.getRow(2)
138
- let isInfoRow = false
139
- row.eachCell(cell => {
140
- if((cell.value ?? '').toString().trim().startsWith('***')){
141
- isInfoRow = true
146
+ let rows = []
147
+ const row = worksheet.getRow(2)
148
+ let isInfoRow = false
149
+ row.eachCell(cell => {
150
+ if((cell.value ?? '').toString().trim().startsWith('***')){
151
+ isInfoRow = true
152
+ }
153
+ })
154
+
155
+ let rowStart = isInfoRow ? 3 : 2
156
+ const header = worksheet.getRow(1)
157
+ const columnAddress = {}
158
+ header.eachCell((cell, index) => {
159
+ if(cell.isMerged){
160
+ const sub = worksheet.getCell(cell.address.replace(/\d/g, '') + '2').value
161
+ columnAddress[cell.value + '>' + sub] = index
162
+ rowStart = 3
163
+ }
164
+ else{
165
+ columnAddress[cell.value] = index
166
+ }
167
+ })
168
+
169
+ let keys = doc.index === 0 ? req.body.keys : null
170
+ if(!keys){
171
+ keys = Object.keys(columnAddress).map(key => ({
172
+ key: key,
173
+ value: key
174
+ }))
142
175
  }
143
- })
144
176
 
145
- let rowStart = isInfoRow ? 3 : 2
146
- const header = worksheet.getRow(1)
147
- const columnAddress = {}
148
- header.eachCell((cell, index) => {
149
- if(cell.isMerged){
150
- const sub = worksheet.getCell(cell.address.replace(/\d/g, '') + '2').value
151
- columnAddress[cell.value + '>' + sub] = index
152
- rowStart = 3
153
- }
154
- else{
155
- columnAddress[cell.value] = index
156
- }
157
- })
177
+ worksheet.eachRow((row, index) => {
178
+ if(index >= rowStart){
179
+ const obj = {}
180
+
181
+ // Check if cells in row 2 and 3 is notes in italic
182
+ if(index <= 3){
183
+ let isItalic = false
184
+ for(let idx in keys){
185
+ const key = keys[idx]
186
+ if(key.value && columnAddress[key.value]){
187
+ const cell = row.getCell(columnAddress[key.value])
188
+ if(cell.font && cell.font.italic){
189
+ isItalic = true
190
+ break
191
+ }
192
+ }
193
+ }
194
+
195
+ if(isItalic)
196
+ return
197
+ }
158
198
 
159
- worksheet.eachRow((row, index) => {
160
- if(index >= rowStart){
161
- const obj = {}
162
-
163
- // Check if cells in row 2 and 3 is notes in italic
164
- if(index <= 3){
165
- let isItalic = false
166
- for(let idx in keys){
167
- const key = keys[idx]
168
- if(key.value && columnAddress[key.value]){
169
- const cell = row.getCell(columnAddress[key.value])
170
- if(cell.font && cell.font.italic){
171
- isItalic = true
172
- break
199
+ if(keys){
200
+ for(let idx in keys){
201
+ const key = keys[idx]
202
+ if(key.value && columnAddress[key.value]){
203
+ const cell = row.getCell(columnAddress[key.value])
204
+ obj[key.key] = getCellText(cell)
173
205
  }
174
206
  }
175
207
  }
208
+ else{
209
+ //Object.assign(obj, row)
210
+ }
176
211
 
177
- if(isItalic)
178
- return
212
+ rows.push(unflatten(obj))
179
213
  }
214
+ })
180
215
 
181
- for(let idx in keys){
182
- const key = keys[idx]
183
- if(key.value && columnAddress[key.value]){
184
- const cell = row.getCell(columnAddress[key.value])
185
- obj[key.key] = getCellText(cell)
186
- }
187
- }
216
+ Object.assign(doc, {
217
+ rows
218
+ })
219
+ }
188
220
 
189
- rows.push(unflatten(obj))
190
- }
191
- })
221
+ const rows = docs[0].rows
192
222
 
193
223
  const images = glob.sync(process.env.ROOT_PATH + '/storage/files/temp/' + folderName +
194
224
  '/**/*(*.jpg|*.jpeg|*.png|*.webp)')
@@ -196,9 +226,10 @@ const importRequest = async(req) => {
196
226
  return {
197
227
  ...req.body,
198
228
  rows,
229
+ docs,
199
230
  images,
200
231
  columns,
201
- keys
232
+ keys: req.body.keys
202
233
  }
203
234
  }
204
235