|
@ -137,6 +137,20 @@ export class ImportActivitiesDialog implements OnDestroy { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public onFilesDropped({ |
|
|
|
|
|
files, |
|
|
|
|
|
stepper |
|
|
|
|
|
}: { |
|
|
|
|
|
files: FileList; |
|
|
|
|
|
stepper: MatStepper; |
|
|
|
|
|
}): void { |
|
|
|
|
|
if (files.length === 0) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.handleFile({ stepper, file: files[0] }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public onImportStepChange(event: StepperSelectionEvent) { |
|
|
public onImportStepChange(event: StepperSelectionEvent) { |
|
|
if (event.selectedIndex === ImportStep.UPLOAD_FILE) { |
|
|
if (event.selectedIndex === ImportStep.UPLOAD_FILE) { |
|
|
this.importStep = ImportStep.UPLOAD_FILE; |
|
|
this.importStep = ImportStep.UPLOAD_FILE; |
|
@ -175,111 +189,120 @@ export class ImportActivitiesDialog implements OnDestroy { |
|
|
aStepper.reset(); |
|
|
aStepper.reset(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public onSelectFile(aStepper: MatStepper) { |
|
|
public onSelectFile(stepper: MatStepper) { |
|
|
const input = document.createElement('input'); |
|
|
const input = document.createElement('input'); |
|
|
input.accept = 'application/JSON, .csv'; |
|
|
input.accept = 'application/JSON, .csv'; |
|
|
input.type = 'file'; |
|
|
input.type = 'file'; |
|
|
|
|
|
|
|
|
input.onchange = (event) => { |
|
|
input.onchange = (event) => { |
|
|
this.snackBar.open('⏳ ' + $localize`Validating data...`); |
|
|
|
|
|
|
|
|
|
|
|
// Getting the file reference
|
|
|
// Getting the file reference
|
|
|
const file = (event.target as HTMLInputElement).files[0]; |
|
|
const file = (event.target as HTMLInputElement).files[0]; |
|
|
|
|
|
this.handleFile({ file, stepper }); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
// Setting up the reader
|
|
|
input.click(); |
|
|
const reader = new FileReader(); |
|
|
} |
|
|
reader.readAsText(file, 'UTF-8'); |
|
|
|
|
|
|
|
|
public updateSelection(activities: Activity[]) { |
|
|
|
|
|
this.selectedActivities = activities.filter(({ error }) => { |
|
|
|
|
|
return !error; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
reader.onload = async (readerEvent) => { |
|
|
public ngOnDestroy() { |
|
|
const fileContent = readerEvent.target.result as string; |
|
|
this.unsubscribeSubject.next(); |
|
|
|
|
|
this.unsubscribeSubject.complete(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
try { |
|
|
private async handleFile({ |
|
|
if (file.name.endsWith('.json')) { |
|
|
file, |
|
|
const content = JSON.parse(fileContent); |
|
|
stepper |
|
|
|
|
|
}: { |
|
|
|
|
|
file: File; |
|
|
|
|
|
stepper: MatStepper; |
|
|
|
|
|
}): Promise<void> { |
|
|
|
|
|
this.snackBar.open('⏳ ' + $localize`Validating data...`); |
|
|
|
|
|
|
|
|
this.accounts = content.accounts; |
|
|
// Setting up the reader
|
|
|
|
|
|
const reader = new FileReader(); |
|
|
|
|
|
reader.readAsText(file, 'UTF-8'); |
|
|
|
|
|
|
|
|
if (!isArray(content.activities)) { |
|
|
reader.onload = async (readerEvent) => { |
|
|
if (isArray(content.orders)) { |
|
|
const fileContent = readerEvent.target.result as string; |
|
|
this.handleImportError({ |
|
|
|
|
|
activities: [], |
|
|
|
|
|
error: { |
|
|
|
|
|
error: { |
|
|
|
|
|
message: [`orders needs to be renamed to activities`] |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} else { |
|
|
|
|
|
throw new Error(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
const { activities } = |
|
|
if (file.name.endsWith('.json')) { |
|
|
await this.importActivitiesService.importJson({ |
|
|
const content = JSON.parse(fileContent); |
|
|
accounts: content.accounts, |
|
|
|
|
|
activities: content.activities, |
|
|
|
|
|
isDryRun: true |
|
|
|
|
|
}); |
|
|
|
|
|
this.activities = activities; |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error(error); |
|
|
|
|
|
this.handleImportError({ error, activities: content.activities }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
this.accounts = content.accounts; |
|
|
} else if (file.name.endsWith('.csv')) { |
|
|
|
|
|
try { |
|
|
if (!isArray(content.activities)) { |
|
|
const data = await this.importActivitiesService.importCsv({ |
|
|
if (isArray(content.orders)) { |
|
|
fileContent, |
|
|
|
|
|
isDryRun: true, |
|
|
|
|
|
userAccounts: this.data.user.accounts |
|
|
|
|
|
}); |
|
|
|
|
|
this.activities = data.activities; |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error(error); |
|
|
|
|
|
this.handleImportError({ |
|
|
this.handleImportError({ |
|
|
activities: error?.activities ?? [], |
|
|
activities: [], |
|
|
error: { |
|
|
error: { |
|
|
error: { message: error?.error?.message ?? [error?.message] } |
|
|
error: { |
|
|
|
|
|
message: [`orders needs to be renamed to activities`] |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} else { |
|
|
|
|
|
throw new Error(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
throw new Error(); |
|
|
try { |
|
|
} catch (error) { |
|
|
const { activities } = |
|
|
console.error(error); |
|
|
await this.importActivitiesService.importJson({ |
|
|
this.handleImportError({ |
|
|
accounts: content.accounts, |
|
|
activities: [], |
|
|
activities: content.activities, |
|
|
error: { error: { message: ['Unexpected format'] } } |
|
|
isDryRun: true |
|
|
}); |
|
|
}); |
|
|
} finally { |
|
|
this.activities = activities; |
|
|
this.importStep = ImportStep.SELECT_ACTIVITIES; |
|
|
} catch (error) { |
|
|
this.snackBar.dismiss(); |
|
|
console.error(error); |
|
|
|
|
|
this.handleImportError({ error, activities: content.activities }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
aStepper.next(); |
|
|
return; |
|
|
|
|
|
} else if (file.name.endsWith('.csv')) { |
|
|
|
|
|
try { |
|
|
|
|
|
const data = await this.importActivitiesService.importCsv({ |
|
|
|
|
|
fileContent, |
|
|
|
|
|
isDryRun: true, |
|
|
|
|
|
userAccounts: this.data.user.accounts |
|
|
|
|
|
}); |
|
|
|
|
|
this.activities = data.activities; |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error(error); |
|
|
|
|
|
this.handleImportError({ |
|
|
|
|
|
activities: error?.activities ?? [], |
|
|
|
|
|
error: { |
|
|
|
|
|
error: { message: error?.error?.message ?? [error?.message] } |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.changeDetectorRef.markForCheck(); |
|
|
return; |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
input.click(); |
|
|
throw new Error(); |
|
|
} |
|
|
} catch (error) { |
|
|
|
|
|
console.error(error); |
|
|
|
|
|
this.handleImportError({ |
|
|
|
|
|
activities: [], |
|
|
|
|
|
error: { error: { message: ['Unexpected format'] } } |
|
|
|
|
|
}); |
|
|
|
|
|
} finally { |
|
|
|
|
|
this.importStep = ImportStep.SELECT_ACTIVITIES; |
|
|
|
|
|
this.snackBar.dismiss(); |
|
|
|
|
|
|
|
|
public updateSelection(activities: Activity[]) { |
|
|
stepper.next(); |
|
|
this.selectedActivities = activities.filter(({ error }) => { |
|
|
|
|
|
return !error; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public ngOnDestroy() { |
|
|
this.changeDetectorRef.markForCheck(); |
|
|
this.unsubscribeSubject.next(); |
|
|
} |
|
|
this.unsubscribeSubject.complete(); |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private handleImportError({ |
|
|
private handleImportError({ |
|
|