Fixed case importer errors
X-Lovable-Edit-ID: edt-cb46d824-f9e6-4837-aecc-79bf73fece1b Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
@@ -190,39 +190,72 @@ const IMPORTERS: ImporterConfig[] = [
|
||||
conflict: "external_id",
|
||||
required: ["case_number", "title", "client_id"],
|
||||
aliases: {
|
||||
id: "external_id", caseid: "external_id", externalid: "external_id",
|
||||
id: "external_id", caseid: "external_id", externalid: "external_id", mycaseid: "external_id",
|
||||
casenumber: "case_number", number: "case_number", mattercode: "case_number",
|
||||
caseno: "case_number", casenum: "case_number",
|
||||
name: "title", title: "title", mattername: "title",
|
||||
companyid: "_clientext", clientid: "_clientext", company: "_clientname", client: "_clientname",
|
||||
description: "description", summary: "case_summary", casesummary: "case_summary",
|
||||
casemattername: "title", casename: "title",
|
||||
companyid: "_clientext", clientid: "_clientext",
|
||||
clientno: "_clientext", clientnumber: "_clientext",
|
||||
company: "_clientname", client: "_clientname",
|
||||
communityassociation: "_clientname", associationname: "_clientname",
|
||||
managementcompany: "_clientname",
|
||||
description: "description", casedescription: "description",
|
||||
summary: "case_summary", casesummary: "case_summary",
|
||||
practicearea: "practice_area", area: "practice_area",
|
||||
status: "status",
|
||||
opened: "opened_at", openedat: "opened_at", dateopened: "opened_at",
|
||||
closed: "closed_at", closedat: "closed_at", dateclosed: "closed_at",
|
||||
caseinquirytype: "practice_area", representation: "practice_area",
|
||||
associationtype: "practice_area",
|
||||
status: "status", casestage: "status",
|
||||
opened: "opened_at", openedat: "opened_at", dateopened: "opened_at", opendate: "opened_at",
|
||||
closed: "closed_at", closedat: "closed_at", dateclosed: "closed_at", closeddate: "closed_at",
|
||||
caseclosed: "_closedflag",
|
||||
court: "court", judge: "judge", jurisdiction: "jurisdiction",
|
||||
district: "jurisdiction", county: "jurisdiction", countylocation: "jurisdiction",
|
||||
courtinformation: "court", primarycaselocation: "court",
|
||||
caseCaption: "case_caption", caption: "case_caption",
|
||||
courtcasenumber: "court_case_number",
|
||||
filingdate: "filing_date",
|
||||
nexthearing: "next_hearing_date", nexthearingdate: "next_hearing_date",
|
||||
pretrialconference: "next_hearing_date", trial: "next_hearing_date",
|
||||
opposingparty: "opposing_party",
|
||||
opposingcounsel: "opposing_counsel",
|
||||
defendants: "opposing_party", plaintiffs: "opposing_party",
|
||||
opposingcounsel: "opposing_counsel", opposingcounsels: "opposing_counsel",
|
||||
opposingcounselfirm: "opposing_counsel_firm",
|
||||
opposingcounselemail: "opposing_counsel_email",
|
||||
opposingcounselphone: "opposing_counsel_phone",
|
||||
leadattorney: "_assignedname", originatingattorney: "_originatingname",
|
||||
claimamount: "claim_amount", settlementamount: "settlement_amount",
|
||||
outstandingbalance: "claim_amount",
|
||||
casebalance: "claim_amount", ledgerbalance: "claim_amount",
|
||||
totaldue: "claim_amount", duetofirm: "claim_amount",
|
||||
hourlyrate: "default_hourly_rate", rate: "default_hourly_rate",
|
||||
flatfee: "flat_fee_amount",
|
||||
billingtype: "billing_method",
|
||||
soldate: "statute_of_limitations",
|
||||
archived: "_archived", isarchived: "_archived",
|
||||
},
|
||||
numeric: ["claim_amount", "settlement_amount", "default_hourly_rate"],
|
||||
numeric: ["claim_amount", "settlement_amount", "default_hourly_rate", "flat_fee_amount"],
|
||||
dateCols: ["opened_at", "closed_at", "filing_date", "next_hearing_date", "statute_of_limitations"],
|
||||
transform: (r, ctx) => {
|
||||
const cid = r._clientext ? ctx.clientByExt.get(String(r._clientext)) : null;
|
||||
const cidByName = r._clientname ? ctx.clientByName.get(String(r._clientname).toLowerCase()) : null;
|
||||
r.client_id = cid ?? cidByName ?? null;
|
||||
delete r._clientext; delete r._clientname;
|
||||
delete r._assignedname; delete r._originatingname;
|
||||
if (!r.client_id) return null;
|
||||
const closedFlag = String(r._closedflag ?? "").toLowerCase();
|
||||
delete r._closedflag;
|
||||
const s = String(r.status ?? "").toLowerCase();
|
||||
r.status = s.includes("close") ? "closed" : s.includes("hold") ? "on_hold" : s.includes("intake") ? "intake" : "active";
|
||||
if (closedFlag === "yes" || closedFlag === "true" || closedFlag === "1") {
|
||||
r.status = "closed";
|
||||
} else {
|
||||
r.status = s.includes("close") ? "closed" : s.includes("hold") ? "on_hold" : s.includes("intake") ? "intake" : "active";
|
||||
}
|
||||
// Normalise billing method
|
||||
if (r.billing_method) {
|
||||
const bm = String(r.billing_method).toLowerCase();
|
||||
r.billing_method = bm.includes("flat") ? "flat" : bm.includes("contingency") ? "contingency" : bm.includes("hour") ? "hourly" : null;
|
||||
}
|
||||
if (!r.case_number) r.case_number = `CASE-${Date.now()}-${Math.floor(Math.random() * 1000)}`;
|
||||
if (!r.title) r.title = r.case_number;
|
||||
return r;
|
||||
@@ -1063,15 +1096,34 @@ function ImportPage() {
|
||||
}
|
||||
}
|
||||
if (withoutConflict.length > 0) {
|
||||
const { data, error } = await supabase
|
||||
.from(cfg.table as any)
|
||||
.insert(withoutConflict as any)
|
||||
.select("*");
|
||||
if (error) {
|
||||
errors.push(`Batch ${i / chunkSize + 1} (insert): ${error.message}`);
|
||||
} else if (data) {
|
||||
inserted += data.length;
|
||||
inserted_rows.push(...data);
|
||||
// For cases without external_id, upsert on case_number to avoid duplicate-key errors on re-import
|
||||
if (cfg.table === "cases") {
|
||||
const withCaseNum = withoutConflict.filter((r) => r.case_number);
|
||||
const noCaseNum = withoutConflict.filter((r) => !r.case_number);
|
||||
if (withCaseNum.length > 0) {
|
||||
const { data, error } = await supabase
|
||||
.from("cases" as any)
|
||||
.upsert(withCaseNum as any, { onConflict: "case_number", ignoreDuplicates: false })
|
||||
.select("*");
|
||||
if (error) errors.push(`Batch ${i / chunkSize + 1} (upsert case_number): ${error.message}`);
|
||||
else if (data) { inserted += data.length; inserted_rows.push(...(data as Row[])); }
|
||||
}
|
||||
if (noCaseNum.length > 0) {
|
||||
const { data, error } = await supabase.from("cases" as any).insert(noCaseNum as any).select("*");
|
||||
if (error) errors.push(`Batch ${i / chunkSize + 1} (insert): ${error.message}`);
|
||||
else if (data) { inserted += data.length; inserted_rows.push(...(data as Row[])); }
|
||||
}
|
||||
} else {
|
||||
const { data, error } = await supabase
|
||||
.from(cfg.table as any)
|
||||
.insert(withoutConflict as any)
|
||||
.select("*");
|
||||
if (error) {
|
||||
errors.push(`Batch ${i / chunkSize + 1} (insert): ${error.message}`);
|
||||
} else if (data) {
|
||||
inserted += data.length;
|
||||
inserted_rows.push(...data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user