Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 6s
Build & Deploy / 🧪 QA (push) Successful in 4m25s
Build & Deploy / 🏗️ Build (push) Failing after 17s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Post-Deploy Verification (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 1s
169 lines
5.2 KiB
TypeScript
169 lines
5.2 KiB
TypeScript
import fs from "node:fs";
|
|
import * as xlsxImport from "xlsx";
|
|
const xlsx = (xlsxImport as any).default || xlsxImport;
|
|
import { getPayload } from "payload";
|
|
import configPromise from "../payload.config";
|
|
|
|
async function run() {
|
|
try {
|
|
console.log("Initializing Payload...");
|
|
const payload = await getPayload({ config: configPromise });
|
|
|
|
const filePath = "/Users/marcmintel/Downloads/Akquise_Branchen.xlsx";
|
|
if (!fs.existsSync(filePath)) {
|
|
console.error("File not found:", filePath);
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`Reading Excel file: ${filePath}`);
|
|
const wb = xlsx.readFile(filePath);
|
|
|
|
let accountsCreated = 0;
|
|
let contactsCreated = 0;
|
|
|
|
for (const sheetName of wb.SheetNames) {
|
|
if (
|
|
sheetName === "Weitere Kundenideen" ||
|
|
sheetName.includes("BKF Firmen")
|
|
)
|
|
continue;
|
|
|
|
let industry = sheetName
|
|
.replace(/^\d+_/, "")
|
|
.replace(/^\d+\.\s*/, "")
|
|
.replace(/_/g, " ");
|
|
console.log(
|
|
`\n--- Importing Sheet: ${sheetName} -> Industry: ${industry} ---`,
|
|
);
|
|
const rows = xlsx.utils.sheet_to_json(wb.Sheets[sheetName]);
|
|
|
|
for (const row of rows) {
|
|
const companyName = row["Unternehmen"]?.trim();
|
|
const website = row["Webseitenlink"]?.trim();
|
|
let email = row["Emailadresse"]?.trim();
|
|
const contactName = row["Ansprechpartner"]?.trim();
|
|
const position = row["Position"]?.trim();
|
|
const statusRaw = row["Webseiten-Status (alt/gut/schlecht)"]
|
|
?.trim()
|
|
?.toLowerCase();
|
|
const notes = row["Notizen"]?.trim();
|
|
|
|
if (!companyName) continue;
|
|
|
|
let websiteStatus = "unknown";
|
|
if (statusRaw === "gut") websiteStatus = "gut";
|
|
else if (statusRaw === "ok" || statusRaw === "okay")
|
|
websiteStatus = "ok";
|
|
else if (
|
|
statusRaw === "schlecht" ||
|
|
statusRaw === "alt" ||
|
|
statusRaw === "veraltet"
|
|
)
|
|
websiteStatus = "schlecht";
|
|
|
|
// Find or create account
|
|
let accountId;
|
|
const whereClause = website
|
|
? { website: { equals: website } }
|
|
: { name: { equals: companyName } };
|
|
|
|
const existingAccounts = await payload.find({
|
|
collection: "crm-accounts",
|
|
where: whereClause,
|
|
});
|
|
|
|
if (existingAccounts.docs.length > 0) {
|
|
accountId = existingAccounts.docs[0].id;
|
|
console.log(`[SKIP] Account exists: ${companyName}`);
|
|
} else {
|
|
try {
|
|
const newAccount = await payload.create({
|
|
collection: "crm-accounts",
|
|
data: {
|
|
name: companyName,
|
|
website: website || "",
|
|
status: "lead",
|
|
leadTemperature: "cold",
|
|
industry,
|
|
websiteStatus,
|
|
notes,
|
|
} as any,
|
|
});
|
|
accountId = newAccount.id;
|
|
accountsCreated++;
|
|
console.log(`[OK] Created account: ${companyName}`);
|
|
} catch (err: any) {
|
|
console.error(
|
|
`[ERROR] Failed to create account ${companyName}:`,
|
|
err.message,
|
|
);
|
|
continue; // Skip contact creation if account failed
|
|
}
|
|
}
|
|
|
|
// Handle contact
|
|
if (email) {
|
|
// Some rows have multiple emails or contacts. Let's just pick the first email if there are commas.
|
|
if (email.includes(",")) email = email.split(",")[0].trim();
|
|
|
|
const existingContacts = await payload.find({
|
|
collection: "crm-contacts",
|
|
where: { email: { equals: email } },
|
|
});
|
|
|
|
if (existingContacts.docs.length === 0) {
|
|
let firstName = "Team";
|
|
let lastName = companyName; // fallback
|
|
|
|
if (contactName) {
|
|
// If multiple contacts are listed, just take the first one
|
|
const firstContact = contactName.split(",")[0].trim();
|
|
const parts = firstContact.split(" ");
|
|
if (parts.length > 1) {
|
|
lastName = parts.pop();
|
|
firstName = parts.join(" ");
|
|
} else {
|
|
firstName = firstContact;
|
|
lastName = "Contact";
|
|
}
|
|
}
|
|
|
|
try {
|
|
await payload.create({
|
|
collection: "crm-contacts",
|
|
data: {
|
|
email,
|
|
firstName,
|
|
lastName,
|
|
role: position,
|
|
account: accountId as any,
|
|
},
|
|
});
|
|
contactsCreated++;
|
|
console.log(` -> [OK] Created contact: ${email}`);
|
|
} catch (err: any) {
|
|
console.error(
|
|
` -> [ERROR] Failed to create contact ${email}:`,
|
|
err.message,
|
|
);
|
|
}
|
|
} else {
|
|
console.log(` -> [SKIP] Contact exists: ${email}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`\nMigration completed successfully!`);
|
|
console.log(
|
|
`Created ${accountsCreated} Accounts and ${contactsCreated} Contacts.`,
|
|
);
|
|
process.exit(0);
|
|
} catch (e) {
|
|
console.error("Migration failed:", e);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
run();
|