fix(task1): block duplicate imports and duplicate records

This commit is contained in:
Atharv1705
2026-11-17 17:38:37 +05:30
parent b6465708f0
commit 041a4075dd
5 changed files with 330 additions and 6 deletions
+38 -5
View File
@@ -430,6 +430,12 @@ def process_contracts_import(
for v in vendors:
vendor_lookup[v.name.strip().lower()] = v
existing_contracts = {
(c.asset_id, (c.contract_type or "").upper(), c.start_date, c.end_date)
for c in db.query(Contract.asset_id, Contract.contract_type, Contract.start_date, Contract.end_date).all()
}
seen_contracts_in_file: dict[tuple[int, str, date, date], int] = {}
errors: list[dict[str, Any]] = []
preview_rows: list[dict[str, Any]] = []
valid_records: list[dict[str, Any]] = []
@@ -483,6 +489,16 @@ def process_contracts_import(
if start_date and end_date and end_date < start_date:
row_errors.append("end_date cannot be earlier than start_date")
if target_asset and contract_type_raw in ("AMC", "CMC", "OTHER") and start_date and end_date and end_date >= start_date:
key = (target_asset.id, contract_type_raw, start_date, end_date)
if key in seen_contracts_in_file:
row_errors.append(f"Duplicate of row {seen_contracts_in_file[key]} in this file.")
elif key in existing_contracts:
row_errors.append("Already exists in the system.")
seen_contracts_in_file[key] = row_idx
else:
seen_contracts_in_file[key] = row_idx
cost, c_err = _parse_optional_float(cost_val, "cost")
if c_err:
row_errors.append(c_err)
@@ -592,7 +608,7 @@ def process_calibration_import(
asset_lookup: dict[str, Asset] = {a.asset_code.strip().lower(): a for a in assets if a.asset_code}
existing_plan_asset_ids = {p.asset_id for p in db.query(CalibrationPlan.asset_id).all()}
seen_asset_ids: set[int] = set()
seen_asset_ids: dict[int, int] = {}
vendors = db.query(Vendor).filter(Vendor.is_active.is_(True)).all()
vendor_lookup: dict[str, Vendor] = {}
@@ -627,12 +643,13 @@ def process_calibration_import(
target_asset = asset_lookup.get(asset_code.lower())
if not target_asset:
row_errors.append(f"Asset with code '{asset_code}' not found")
elif target_asset.id in existing_plan_asset_ids:
row_errors.append(f"Calibration plan already exists for asset '{asset_code}'")
elif target_asset.id in seen_asset_ids:
row_errors.append(f"Duplicate calibration plan for asset '{asset_code}' in upload file")
row_errors.append(f"Duplicate of row {seen_asset_ids[target_asset.id]} in this file.")
elif target_asset.id in existing_plan_asset_ids:
row_errors.append("Asset already has a calibration plan.")
seen_asset_ids[target_asset.id] = row_idx
else:
seen_asset_ids.add(target_asset.id)
seen_asset_ids[target_asset.id] = row_idx
freq_months, f_err = _parse_positive_int(freq_val, "frequency_months")
if f_err:
@@ -777,6 +794,13 @@ def process_insurance_import(
for v in vendors:
vendor_lookup[v.name.strip().lower()] = v
existing_policies = {
p[0].strip().lower()
for p in db.query(InsurancePolicy.policy_no).all()
if p[0] and p[0].strip()
}
seen_policies_in_file: dict[str, int] = {}
errors: list[dict[str, Any]] = []
preview_rows: list[dict[str, Any]] = []
valid_records: list[dict[str, Any]] = []
@@ -820,6 +844,15 @@ def process_insurance_import(
if not policy_no:
row_errors.append("Missing required field 'policy_number'")
else:
pol_key = policy_no.strip().lower()
if pol_key in seen_policies_in_file:
row_errors.append(f"Duplicate of row {seen_policies_in_file[pol_key]} in this file.")
elif pol_key in existing_policies:
row_errors.append("Already exists in the system.")
seen_policies_in_file[pol_key] = row_idx
else:
seen_policies_in_file[pol_key] = row_idx
start_date, s_err = _parse_date(start_date_val, "start_date")
if s_err: