fix(task1): block duplicate imports and duplicate records
This commit is contained in:
@@ -243,6 +243,22 @@ def create_contract(
|
||||
if not db.query(Vendor.id).filter(Vendor.id == payload.provider_id).first():
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Provider vendor not found.")
|
||||
|
||||
existing_contract = (
|
||||
db.query(Contract.id)
|
||||
.filter(
|
||||
Contract.asset_id == payload.asset_id,
|
||||
Contract.contract_type == payload.contract_type,
|
||||
Contract.start_date == payload.start_date,
|
||||
Contract.end_date == payload.end_date,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if existing_contract:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="A contract with the same asset, type and dates already exists.",
|
||||
)
|
||||
|
||||
contract = Contract(
|
||||
asset_id=payload.asset_id,
|
||||
contract_type=payload.contract_type,
|
||||
|
||||
@@ -2,7 +2,7 @@ from datetime import date
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, Query, Response, UploadFile, status
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy import func, or_
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
|
||||
from ..core.db import get_db
|
||||
@@ -200,6 +200,17 @@ def create_insurance_policy(
|
||||
detail="policy_no cannot be blank.",
|
||||
)
|
||||
|
||||
existing_policy = (
|
||||
db.query(InsurancePolicy.id)
|
||||
.filter(func.lower(func.trim(InsurancePolicy.policy_no)) == clean_policy_no.lower())
|
||||
.first()
|
||||
)
|
||||
if existing_policy:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Policy number already exists.",
|
||||
)
|
||||
|
||||
asset.insurance_applicable = True
|
||||
pol = InsurancePolicy(
|
||||
asset_id=payload.asset_id,
|
||||
|
||||
Reference in New Issue
Block a user