forked from baron/baron-sso
test: add unit and e2e tests for bulk user creation and schema validation
This commit is contained in:
40
adminfront/src/features/users/utils/csvParser.ts
Normal file
40
adminfront/src/features/users/utils/csvParser.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { type BulkUserItem } from "../../../lib/adminApi";
|
||||
|
||||
export function parseUserCSV(text: string): BulkUserItem[] {
|
||||
const lines = text.split(/\r?\n/);
|
||||
if (lines.length < 2) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const headers = lines[0].split(",").map((h) => h.trim().toLowerCase());
|
||||
const data: BulkUserItem[] = [];
|
||||
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
if (!lines[i].trim()) continue;
|
||||
|
||||
const values = lines[i].split(",").map((v) => v.trim());
|
||||
const item: any = { metadata: {} };
|
||||
|
||||
headers.forEach((header, index) => {
|
||||
const value = values[index];
|
||||
if (value === undefined || value === "") return;
|
||||
|
||||
if (
|
||||
["email", "name", "phone", "role", "companycode", "department"].includes(
|
||||
header,
|
||||
)
|
||||
) {
|
||||
const key = header === "companycode" ? "companyCode" : header;
|
||||
item[key] = value;
|
||||
} else {
|
||||
item.metadata[header] = value;
|
||||
}
|
||||
});
|
||||
|
||||
if (item.email && item.name) {
|
||||
data.push(item as BulkUserItem);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user