|
2 | 2 |
|
3 | 3 | import {BaseImportModel} from "@base_import/import_model"; |
4 | 4 |
|
5 | | -const originalExecuteImport = BaseImportModel.prototype.executeImport; |
6 | | - |
7 | 5 | BaseImportModel.prototype.executeImport = async function (isTest = false, totalSteps, importProgress) { |
8 | | - const result = await originalExecuteImport.call(this, isTest, totalSteps, importProgress); |
9 | | - |
10 | | - // Replace "Resume" warning with a custom warning for test imports |
11 | | - if (isTest && result.res && result.res.nextrow) { |
12 | | - // Remove the default resume warning message |
13 | | - this.importMessages = this.importMessages.filter( |
14 | | - (msg) => !(msg.type === "warning" && msg.lines.some((line) => line.includes("Resume"))) |
15 | | - ); |
16 | | - |
17 | | - // Add a custom warning with the number of records tested |
18 | | - const testedCount = result.res.ids.length; |
19 | | - this._addMessage("info", [`Completed test import of ${testedCount} records. No data was imported.`]); |
20 | | - result.res.nextrow = false; // Prevent further "Resume" prompts |
21 | | - console.log(result); |
22 | | - this.setOption("skip", 0); |
| 6 | + this.handleInterruption = false; |
| 7 | + this._updateComments(); |
| 8 | + this.importMessages = []; |
| 9 | + |
| 10 | + const startRow = this.importOptions.skip; |
| 11 | + const importRes = { |
| 12 | + ids: [], |
| 13 | + fields: this.columns.map((e) => Boolean(e.fieldInfo) && e.fieldInfo.fieldPath), |
| 14 | + columns: this.columns.map((e) => e.name.trim().toLowerCase()), |
| 15 | + hasError: false, |
| 16 | + }; |
| 17 | + |
| 18 | + // We need to get the total number of records to import to calculate the last batch size. |
| 19 | + // The most reliable way is to derive it from the totalSteps and batchSize passed by the ImportAction component. |
| 20 | + const totalToImport = |
| 21 | + (totalSteps - 1) * this.importOptions.limit + |
| 22 | + (this.importOptions.file_length % this.importOptions.limit || this.importOptions.limit); |
| 23 | + console.log("Total to import:", totalToImport); |
| 24 | + const batchSize = this.importOptions.limit || 2000; |
| 25 | + |
| 26 | + for (let i = 1; i <= totalSteps + 1; i++) { |
| 27 | + if (this.handleInterruption) { |
| 28 | + if (importRes.hasError || isTest) { |
| 29 | + importRes.nextrow = startRow; |
| 30 | + this.setOption("skip", startRow); |
| 31 | + } |
| 32 | + break; |
| 33 | + } |
| 34 | + |
| 35 | + // Calculate the correct limit for each step. |
| 36 | + // This is the key change to ensure the last step processes the remainder. |
| 37 | + if (i === totalSteps && totalToImport % batchSize !== 0) { |
| 38 | + // Last step: process the remainder of records. |
| 39 | + this.importOptionsValues.limit.value = totalToImport % batchSize; |
| 40 | + } else { |
| 41 | + // For all other steps, use the full batch size. |
| 42 | + this.importOptionsValues.limit.value = batchSize; |
| 43 | + } |
| 44 | + |
| 45 | + const error = await this._executeImportStep(isTest, importRes); |
| 46 | + if (error) { |
| 47 | + const errorData = error.data || {}; |
| 48 | + const message = |
| 49 | + (errorData.arguments && (errorData.arguments[1] || errorData.arguments[0])) || |
| 50 | + "An unknown issue occurred during import. Please retry or try to split the file."; |
| 51 | + |
| 52 | + // Restore original batch size on error |
| 53 | + this.importOptionsValues.limit.value = batchSize; |
| 54 | + |
| 55 | + if (error.message) { |
| 56 | + this._addMessage("danger", [error.message, message]); |
| 57 | + } else { |
| 58 | + this._addMessage("danger", [message]); |
| 59 | + } |
| 60 | + |
| 61 | + importRes.hasError = true; |
| 62 | + break; |
| 63 | + } |
| 64 | + |
| 65 | + if (importProgress) { |
| 66 | + importProgress.step = i; |
| 67 | + // Update progress value based on the number of records processed so far |
| 68 | + const processedCount = Math.min(i * batchSize, totalToImport); |
| 69 | + importProgress.value = Math.round((100 * processedCount) / totalToImport); |
| 70 | + } |
| 71 | + console.log(`Completed step ${i}/${totalSteps}`); |
| 72 | + console.log("Next row to process:", importRes.nextrow); |
| 73 | + if (i === totalSteps) { |
| 74 | + console.log("Final step completed."); |
| 75 | + importRes.nextrow = startRow; |
| 76 | + this.setOption("skip", 0); |
| 77 | + } |
23 | 78 | } |
| 79 | + console.log("Next row!:", importRes.nextrow); |
| 80 | + // Restore original batch size after completion |
| 81 | + this.importOptionsValues.limit.value = batchSize; |
24 | 82 |
|
25 | | - return result; |
| 83 | + if (!importRes.hasError) { |
| 84 | + if (importRes.nextrow) { |
| 85 | + this._addMessage("warning", [ |
| 86 | + `Click 'Resume' to proceed with the import, resuming at line ${importRes.nextrow + 1}.`, |
| 87 | + "You can test or reload your file before resuming the import.", |
| 88 | + ]); |
| 89 | + } |
| 90 | + if (isTest) { |
| 91 | + this._addMessage("info", ["Everything seems valid."]); |
| 92 | + } |
| 93 | + } else { |
| 94 | + importRes.nextrow = startRow; |
| 95 | + } |
| 96 | + console.log("Import result:", importRes); |
| 97 | + return {res: importRes}; |
26 | 98 | }; |
0 commit comments