-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathimport-controller.ts
More file actions
123 lines (116 loc) · 3.48 KB
/
import-controller.ts
File metadata and controls
123 lines (116 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { TranslateFunction , DataProvider, Identifier } from "ra-core";
import { processCsvFile } from "./csv-extractor";
import { SimpleLogger } from "./SimpleLogger";
import { ValidateRowFunction } from "./config.interface";
function makeLogger(logging: boolean) {
const logger = new SimpleLogger("import-controller", true);
logger.setEnabled(logging);
return logger;
}
export async function GetIdsColliding(
logging: boolean,
translate: TranslateFunction,
dataProvider: DataProvider,
csvValues: any[],
resourceName: string,
disableGetMany: boolean | undefined,
): Promise<Identifier[]> {
const logger = makeLogger(logging);
const hasIds = csvValues.some((v) => v.id);
if (!hasIds) {
return [];
}
try {
const csvIds: Identifier[] = csvValues.filter(v => !!v.id).map((v) => v.id);
const recordsIdsColliding = await (disableGetMany
? GetIdsCollidingGetSingle( logging, translate, dataProvider, csvIds, resourceName )
: GetIdsCollidingGetMany( logging, translate, dataProvider, csvIds, resourceName )
);
return recordsIdsColliding;
} catch (error) {
logger.error("GetIdsColliding", { csvValues }, error);
throw translate("csv.parsing.collidingIds");
}
}
export async function GetIdsCollidingGetSingle(
logging: boolean,
translate: TranslateFunction,
dataProvider: DataProvider,
csvIds: Identifier[],
resourceName: string,
): Promise<Identifier[]> {
const logger = makeLogger(logging);
try {
const recordsColliding = await Promise.all(csvIds.map(id => IsIdColliding(
dataProvider,
id,
resourceName,
)));
const recordIdsColliding = recordsColliding.filter(Boolean) as Identifier[];
return recordIdsColliding;
} catch (error) {
logger.error("GetIdsCollidingGetSingle", { csvIds }, error);
throw translate("csv.parsing.collidingIds");
}
}
export async function IsIdColliding(
dataProvider: DataProvider,
id: Identifier,
resourceName: string,
) {
return dataProvider.getOne(resourceName, {id})
.then(_ => id)
.catch(_ => undefined);
}
export async function GetIdsCollidingGetMany(
logging: boolean,
translate: TranslateFunction,
dataProvider: DataProvider,
csvIds: Identifier[],
resourceName: string,
): Promise<Identifier[]> {
const logger = makeLogger(logging);
try {
const recordsColliding = await dataProvider.getMany(resourceName, {
ids: csvIds,
});
const recordIdsColliding = recordsColliding.data.map((r) => r.id);
return recordIdsColliding;
} catch (error) {
logger.error("GetIdsCollidingGetMany", { csvIds }, error);
throw translate("csv.parsing.collidingIds");
}
}
export async function CheckCSVValidation(
logging: boolean,
translate: TranslateFunction,
csvValues: any[],
validateRow?: ValidateRowFunction
): Promise<void> {
const logger = makeLogger(logging);
if (!validateRow) {
return;
}
try {
await Promise.all(csvValues.map(validateRow));
} catch (error) {
logger.error("CheckCSVValidation", { csvValues }, error);
throw translate("csv.parsing.failedValidateRow");
}
}
export async function GetCSVItems(
logging: boolean,
translate: TranslateFunction,
file: File,
parseConfig: any,
): Promise<any[]> {
const logger = makeLogger(logging);
let csvValues: any[] | undefined;
try {
csvValues = await processCsvFile(file, parseConfig);
return csvValues || [];
} catch (error) {
logger.error("GetCSVItems", { csvValues }, error);
throw translate("csv.parsing.invalidCsvDocument");
}
}