63 lines
2.1 KiB
Python
63 lines
2.1 KiB
Python
from app.repositories.scheme_groups import list_scheme_version_groups
|
|
from app.repositories.scheme_seats import (
|
|
list_scheme_version_seats,
|
|
repair_orphan_group_refs,
|
|
repair_orphan_sector_refs,
|
|
)
|
|
from app.repositories.scheme_sectors import list_scheme_version_sectors
|
|
|
|
|
|
async def repair_structure_references(
|
|
*,
|
|
scheme_version_id: str,
|
|
) -> dict:
|
|
sectors = await list_scheme_version_sectors(scheme_version_id)
|
|
groups = await list_scheme_version_groups(scheme_version_id)
|
|
seats = await list_scheme_version_seats(scheme_version_id)
|
|
|
|
valid_sector_ids = {item.sector_id for item in sectors if item.sector_id}
|
|
valid_group_ids = {item.group_id for item in groups if item.group_id}
|
|
|
|
orphan_sector_values = sorted(
|
|
{
|
|
row.sector_id
|
|
for row in seats
|
|
if row.sector_id and row.sector_id not in valid_sector_ids
|
|
}
|
|
)
|
|
orphan_group_values = sorted(
|
|
{
|
|
row.group_id
|
|
for row in seats
|
|
if row.group_id and row.group_id not in valid_group_ids
|
|
}
|
|
)
|
|
|
|
repaired_sector_refs_count = 0
|
|
repaired_group_refs_count = 0
|
|
|
|
if len(valid_sector_ids) == 1 and orphan_sector_values:
|
|
repaired_sector_refs_count = await repair_orphan_sector_refs(
|
|
scheme_version_id=scheme_version_id,
|
|
new_sector_id=next(iter(valid_sector_ids)),
|
|
orphan_values=orphan_sector_values,
|
|
)
|
|
|
|
if len(valid_group_ids) == 1 and orphan_group_values:
|
|
repaired_group_refs_count = await repair_orphan_group_refs(
|
|
scheme_version_id=scheme_version_id,
|
|
new_group_id=next(iter(valid_group_ids)),
|
|
orphan_values=orphan_group_values,
|
|
)
|
|
|
|
return {
|
|
"repaired_sector_refs_count": repaired_sector_refs_count,
|
|
"repaired_group_refs_count": repaired_group_refs_count,
|
|
"details": {
|
|
"valid_sector_ids": sorted(valid_sector_ids),
|
|
"valid_group_ids": sorted(valid_group_ids),
|
|
"orphan_sector_values": orphan_sector_values,
|
|
"orphan_group_values": orphan_group_values,
|
|
},
|
|
}
|