124 lines
5.3 KiB
Python
124 lines
5.3 KiB
Python
import ulid
|
|
import sqlalchemy
|
|
from app.core.database.db_session import SessionLocal, engine_crm, engine_commerce
|
|
from app.models.BrandDeviceTypeModel import BrandDeviceType
|
|
from app.models.DeviceCatalogModel import ServiceType, RepairService, RepairVariant, DeviceModel
|
|
|
|
def seed_crm():
|
|
db = SessionLocal()
|
|
|
|
try:
|
|
print("Checking existing ServiceTypes...")
|
|
service_types_data = [
|
|
{"name": "Screen Replacement", "slug": "screen-replacement", "description": "Display & Touchscreen glass replacement with warranty"},
|
|
{"name": "Battery Replacement", "slug": "battery-replacement", "description": "High health battery replacement with quick charging support"},
|
|
{"name": "Camera Repair", "slug": "camera-repair", "description": "Front/Rear camera module lens and sensor replacement"},
|
|
{"name": "Charging Port Repair", "slug": "charging-port-repair", "description": "USB-C / Lightning port connector replacement"},
|
|
{"name": "Speaker Repair", "slug": "speaker-repair", "description": "Earpiece and loudspeaker audio restoration"},
|
|
{"name": "Back Glass Replacement", "slug": "back-glass-replacement", "description": "Rear glass panel restoration"}
|
|
]
|
|
|
|
st_map = {}
|
|
for st_info in service_types_data:
|
|
existing = db.execute(
|
|
sqlalchemy.select(ServiceType).where(ServiceType.slug == st_info["slug"])
|
|
).scalar_one_or_none()
|
|
|
|
if not existing:
|
|
st_id = str(ulid.ULID())
|
|
st = ServiceType(
|
|
service_type_id=st_id,
|
|
name=st_info["name"],
|
|
slug=st_info["slug"],
|
|
description=st_info["description"],
|
|
is_active=True
|
|
)
|
|
db.add(st)
|
|
st_map[st_info["slug"]] = st_id
|
|
print(f"Created ServiceType: {st_info['name']}")
|
|
else:
|
|
st_map[st_info["slug"]] = existing.service_type_id
|
|
|
|
db.commit()
|
|
|
|
# Fetch all models from Commerce DB
|
|
models = db.execute(sqlalchemy.select(DeviceModel)).scalars().all()
|
|
print(f"Found {len(models)} device models in commerce catalog.")
|
|
|
|
for m in models:
|
|
for st_slug, st_id in st_map.items():
|
|
# Check if RepairService exists
|
|
existing_rs = db.execute(
|
|
sqlalchemy.select(RepairService).where(
|
|
RepairService.model_id == m.model_id,
|
|
RepairService.service_type_id == st_id
|
|
)
|
|
).scalar_one_or_none()
|
|
|
|
if not existing_rs:
|
|
rs_id = str(ulid.ULID())
|
|
rs = RepairService(
|
|
repair_service_id=rs_id,
|
|
model_id=m.model_id,
|
|
service_type_id=st_id,
|
|
slug=f"{m.slug}-{st_slug}",
|
|
full_path=f"{m.full_path}/{st_slug}",
|
|
description=f"{st_slug.replace('-', ' ').title()} for {m.name}"
|
|
)
|
|
db.add(rs)
|
|
db.flush()
|
|
|
|
# Add Variants
|
|
if st_slug == "screen-replacement":
|
|
v1 = RepairVariant(
|
|
variant_id=str(ulid.ULID()),
|
|
repair_service_id=rs_id,
|
|
name="Original OLED",
|
|
price=12000.00,
|
|
cost=8000.00,
|
|
duration_minutes=180,
|
|
warranty_days=90
|
|
)
|
|
v2 = RepairVariant(
|
|
variant_id=str(ulid.ULID()),
|
|
repair_service_id=rs_id,
|
|
name="Premium Display",
|
|
price=7500.00,
|
|
cost=4500.00,
|
|
duration_minutes=120,
|
|
warranty_days=60
|
|
)
|
|
db.add_all([v1, v2])
|
|
elif st_slug == "battery-replacement":
|
|
v1 = RepairVariant(
|
|
variant_id=str(ulid.ULID()),
|
|
repair_service_id=rs_id,
|
|
name="Original High Capacity",
|
|
price=3500.00,
|
|
cost=2000.00,
|
|
duration_minutes=60,
|
|
warranty_days=180
|
|
)
|
|
db.add(v1)
|
|
else:
|
|
v1 = RepairVariant(
|
|
variant_id=str(ulid.ULID()),
|
|
repair_service_id=rs_id,
|
|
name="Standard Service",
|
|
price=2500.00,
|
|
cost=1200.00,
|
|
duration_minutes=60,
|
|
warranty_days=30
|
|
)
|
|
db.add(v1)
|
|
|
|
db.commit()
|
|
print("CRM Catalog seeding complete!")
|
|
except Exception as e:
|
|
db.rollback()
|
|
print("Error seeding CRM catalog:", e)
|
|
finally:
|
|
db.close()
|
|
|
|
if __name__ == "__main__":
|
|
seed_crm()
|