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