--
This commit is contained in:
@@ -56,15 +56,17 @@ class Headquarters(Base, UUIDMixin, TimestampMixin):
|
||||
class AMHeadquarters(Base, UUIDMixin, TimestampMixin):
|
||||
"""Which Area Manager currently manages which headquarters.
|
||||
|
||||
`effective_to IS NULL` means "current". A partial unique index guarantees at most
|
||||
one active Area Manager per headquarters at any time.
|
||||
`effective_to IS NULL` means "current". A headquarters may have several Area Managers
|
||||
and an Area Manager several headquarters; the partial unique index only stops the same
|
||||
pair being active twice.
|
||||
"""
|
||||
|
||||
__tablename__ = "am_headquarters"
|
||||
__table_args__ = (
|
||||
Index(
|
||||
"uq_active_am_per_hq",
|
||||
"uq_active_am_hq_pair",
|
||||
"headquarters_id",
|
||||
"area_manager_user_id",
|
||||
unique=True,
|
||||
postgresql_where=text("effective_to IS NULL"),
|
||||
),
|
||||
|
||||
@@ -81,9 +81,8 @@ class OrganizationService:
|
||||
) -> list[AMHeadquarters]:
|
||||
"""Make `headquarters_ids` exactly the set of headquarters this AM manages.
|
||||
|
||||
Headquarters missing from the list are released, new ones are added. If a new
|
||||
headquarters already has another AM, that mapping is closed first (one active AM
|
||||
per headquarters stays true). An AM may manage any number of headquarters.
|
||||
Headquarters missing from the list are released, new ones are added. Other Area
|
||||
Managers of the same headquarters keep it: a headquarters may have several.
|
||||
"""
|
||||
manager = await self.db.get(User, am_user_id)
|
||||
if manager is None or manager.company_id != actor.company_id:
|
||||
@@ -127,16 +126,6 @@ class OrganizationService:
|
||||
for hq_id in released:
|
||||
mine[hq_id].effective_to = start
|
||||
|
||||
if added:
|
||||
for row in (
|
||||
await self.db.execute(
|
||||
select(AMHeadquarters).where(
|
||||
AMHeadquarters.headquarters_id.in_(added),
|
||||
AMHeadquarters.effective_to.is_(None),
|
||||
)
|
||||
)
|
||||
).scalars():
|
||||
row.effective_to = start
|
||||
await self.db.flush()
|
||||
|
||||
for hq_id in added:
|
||||
@@ -176,10 +165,9 @@ class OrganizationService:
|
||||
actor: User,
|
||||
ip: str | None = None,
|
||||
) -> AMHeadquarters:
|
||||
"""Assign an AM to an HQ, closing any existing active mapping first.
|
||||
"""Add an Area Manager to a headquarters, beside any it already has.
|
||||
|
||||
The partial unique index `uq_active_am_per_hq` is the real guarantee that only
|
||||
one Area Manager is active per headquarters at a time.
|
||||
Assigning the same pair twice changes nothing (`uq_active_am_hq_pair`).
|
||||
"""
|
||||
hq = await self.db.get(Headquarters, headquarters_id)
|
||||
if hq is None:
|
||||
@@ -207,8 +195,6 @@ class OrganizationService:
|
||||
for row in existing:
|
||||
if row.area_manager_user_id == am_user_id:
|
||||
return row
|
||||
row.effective_to = start
|
||||
await self.db.flush()
|
||||
|
||||
mapping = AMHeadquarters(
|
||||
area_manager_user_id=am_user_id,
|
||||
|
||||
Reference in New Issue
Block a user