Skip to content

Commit

Permalink
Feat: group sync with association membership and move cruds
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotheem committed Dec 14, 2024
1 parent c652755 commit 3b96865
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 90 deletions.
90 changes: 89 additions & 1 deletion app/core/cruds_core.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from collections.abc import Sequence
from datetime import UTC, date, datetime
from uuid import UUID

from sqlalchemy import delete, select
from sqlalchemy import delete, select, update
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession

from app.core import models_core
from app.core.groups.groups_type import AccountType
from app.modules.cdr import schemas_cdr
from app.types.membership import AvailableAssociationMembership


async def get_modules_by_user(
Expand Down Expand Up @@ -182,3 +186,87 @@ async def delete_core_data_crud(
),
)
await db.commit()


async def get_curents_memberships_by_name(
db: AsyncSession,
membership: AvailableAssociationMembership,
) -> Sequence[models_core.CoreAssociationMembership]:
result = await db.execute(
select(models_core.CoreAssociationMembership).where(
models_core.CoreAssociationMembership.membership == membership,
models_core.CoreAssociationMembership.end_date > datetime.now(UTC).date(),
models_core.CoreAssociationMembership.start_date < datetime.now(UTC).date(),
),
)
return result.scalars().all()


async def get_actual_memberships_by_user_id(
db: AsyncSession,
user_id: str,
) -> Sequence[models_core.CoreAssociationMembership]:
result = await db.execute(
select(models_core.CoreAssociationMembership).where(
models_core.CoreAssociationMembership.user_id == user_id,
models_core.CoreAssociationMembership.end_date
> date(datetime.now(UTC).year, 9, 5),
),
)
return result.scalars().all()


async def get_membership_by_user_id_and_membership_name(
db: AsyncSession,
user_id: str,
membership: AvailableAssociationMembership,
) -> models_core.CoreAssociationMembership | None:
result = await db.execute(
select(models_core.CoreAssociationMembership).where(
models_core.CoreAssociationMembership.user_id == user_id
and models_core.CoreAssociationMembership.membership == membership,
),
)
return result.scalars().first()


async def get_membership_by_id(
db: AsyncSession,
membership_id: UUID,
) -> models_core.CoreAssociationMembership | None:
result = await db.execute(
select(models_core.CoreAssociationMembership).where(
models_core.CoreAssociationMembership.id == membership_id,
),
)
return result.scalars().first()


def create_membership(
db: AsyncSession,
membership: models_core.CoreAssociationMembership,
):
db.add(membership)


async def delete_membership(
db: AsyncSession,
membership_id: UUID,
):
await db.execute(
delete(models_core.CoreAssociationMembership).where(
models_core.CoreAssociationMembership.id == membership_id,
),
)


async def update_membership(
db: AsyncSession,
membership_id: UUID,
membership: schemas_cdr.MembershipEdit,
):
await db.execute(
update(models_core.CoreAssociationMembership)
.where(models_core.CoreAssociationMembership.id == membership_id)
.values(**membership.model_dump(exclude_none=True)),
)
43 changes: 42 additions & 1 deletion app/core/groups/endpoints_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession

from app.core import models_core, schemas_core
from app.core import cruds_core, models_core, schemas_core
from app.core.groups import cruds_groups
from app.core.groups.groups_type import GroupType
from app.core.users import cruds_users
Expand All @@ -20,6 +20,7 @@
is_user_an_ecl_member,
is_user_in,
)
from app.types.membership import AvailableAssociationMembership

router = APIRouter(tags=["Groups"])

Expand Down Expand Up @@ -304,3 +305,43 @@ async def delete_group(

await cruds_groups.delete_membership_by_group_id(group_id=group_id, db=db)
await cruds_groups.delete_group(db=db, group_id=group_id)


@router.patch(
"/groups/{group_id}/sync/{association_membership}",
status_code=204,
)
async def synchronize_group_with_association(
group_id: str,
association_membership: AvailableAssociationMembership,
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_in(GroupType.admin)),
request_id: str = Depends(get_request_id),
):
"""
Synchronize a group with an association membership.
**This endpoint is only usable by administrators**
"""

group_db = await cruds_groups.get_group_by_id(group_id=group_id, db=db)
if group_db is None:
raise HTTPException(status_code=404, detail="Group not found")

hyperion_security_logger.warning(
f"Synchronize_group_with_association: Admin user {user.id} ({user.name}) synchronized group {group_db.id} ({group_db.name}) with association membership {association_membership.value} ({request_id})",
)
await cruds_groups.delete_membership_by_group_id(group_id=group_id, db=db)

members = await cruds_core.get_curents_memberships_by_name(
db=db,
membership=association_membership,
)

for member in members:
membership_db = models_core.CoreMembership(
user_id=member.user_id,
group_id=group_id,
description="",
)
await cruds_groups.create_membership(db=db, membership=membership_db)
73 changes: 1 addition & 72 deletions app/modules/cdr/cruds_cdr.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from collections.abc import Sequence
from datetime import UTC, date, datetime
from uuid import UUID

from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import noload, selectinload

from app.core.models_core import CoreAssociationMembership, CoreUser
from app.core.models_core import CoreUser
from app.modules.cdr import models_cdr, schemas_cdr
from app.types.membership import AvailableAssociationMembership


async def get_cdr_users_curriculum(
Expand Down Expand Up @@ -714,75 +712,6 @@ async def delete_payment(
)


async def get_actual_memberships_by_user_id(
db: AsyncSession,
user_id: str,
) -> Sequence[CoreAssociationMembership]:
result = await db.execute(
select(CoreAssociationMembership).where(
CoreAssociationMembership.user_id == user_id,
CoreAssociationMembership.end_date > date(datetime.now(UTC).year, 9, 5),
),
)
return result.scalars().all()


async def get_membership_by_user_id_and_membership_name(
db: AsyncSession,
user_id: str,
membership: AvailableAssociationMembership,
) -> CoreAssociationMembership | None:
result = await db.execute(
select(CoreAssociationMembership).where(
CoreAssociationMembership.user_id == user_id
and CoreAssociationMembership.membership == membership,
),
)
return result.scalars().first()


async def get_membership_by_id(
db: AsyncSession,
membership_id: UUID,
) -> CoreAssociationMembership | None:
result = await db.execute(
select(CoreAssociationMembership).where(
CoreAssociationMembership.id == membership_id,
),
)
return result.scalars().first()


def create_membership(
db: AsyncSession,
membership: CoreAssociationMembership,
):
db.add(membership)


async def delete_membership(
db: AsyncSession,
membership_id: UUID,
):
await db.execute(
delete(CoreAssociationMembership).where(
CoreAssociationMembership.id == membership_id,
),
)


async def update_membership(
db: AsyncSession,
membership_id: UUID,
membership: schemas_cdr.MembershipEdit,
):
await db.execute(
update(CoreAssociationMembership)
.where(CoreAssociationMembership.id == membership_id)
.values(**membership.model_dump(exclude_none=True)),
)


def create_action(
db: AsyncSession,
action: models_cdr.CdrAction,
Expand Down
32 changes: 16 additions & 16 deletions app/modules/cdr/endpoints_cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from fastapi.responses import FileResponse
from sqlalchemy.ext.asyncio import AsyncSession

from app.core import models_core, schemas_core
from app.core import cruds_core, models_core, schemas_core
from app.core.config import Settings
from app.core.groups import cruds_groups
from app.core.groups.groups_type import GroupType
Expand Down Expand Up @@ -1478,12 +1478,12 @@ async def remove_existing_membership(
- product_variant.related_membership_added_duration
<= existing_membership.start_date
):
await cruds_cdr.delete_membership(
await cruds_core.delete_membership(
db=db,
membership_id=existing_membership.id,
)
else:
await cruds_cdr.update_membership(
await cruds_core.update_membership(
db=db,
membership_id=existing_membership.id,
membership=schemas_cdr.MembershipEdit(
Expand All @@ -1506,7 +1506,7 @@ async def add_membership(
None,
)
if existing_membership:
await cruds_cdr.update_membership(
await cruds_core.update_membership(
db=db,
membership_id=existing_membership.id,
membership=schemas_cdr.MembershipEdit(
Expand All @@ -1523,7 +1523,7 @@ async def add_membership(
end_date=date(datetime.now(tz=UTC).date().year, 9, 1)
+ product_variant.related_membership_added_duration,
)
cruds_cdr.create_membership(db=db, membership=added_membership)
cruds_core.create_membership(db=db, membership=added_membership)


@module.router.patch(
Expand Down Expand Up @@ -1572,7 +1572,7 @@ async def mark_purchase_as_validated(
detail="Invalid product.",
)
if validated:
memberships = await cruds_cdr.get_actual_memberships_by_user_id(
memberships = await cruds_core.get_actual_memberships_by_user_id(
db=db,
user_id=user_id,
)
Expand Down Expand Up @@ -1632,7 +1632,7 @@ async def mark_purchase_as_validated(
cruds_cdr.create_ticket(db=db, ticket=ticket)
else:
if product.related_membership:
memberships = await cruds_cdr.get_actual_memberships_by_user_id(
memberships = await cruds_core.get_actual_memberships_by_user_id(
db=db,
user_id=user_id,
)
Expand Down Expand Up @@ -1692,12 +1692,12 @@ async def add_batch_membership(
if not m_user:
unknown_users.append(m)
continue
stored = await cruds_cdr.get_actual_memberships_by_user_id(
stored = await cruds_core.get_actual_memberships_by_user_id(
db=db,
user_id=m_user.id,
)
if membership_id not in [m.membership for m in stored]:
cruds_cdr.create_membership(
cruds_core.create_membership(
db=db,
membership=models_core.CoreAssociationMembership(
id=uuid4(),
Expand Down Expand Up @@ -1777,7 +1777,7 @@ async def delete_purchase(
)
if purchased_product:
if product in purchased_product.product_constraints:
memberships = await cruds_cdr.get_actual_memberships_by_user_id(
memberships = await cruds_core.get_actual_memberships_by_user_id(
db=db,
user_id=user_id,
)
Expand Down Expand Up @@ -2542,7 +2542,7 @@ async def get_memberships_by_user_id(
status_code=403,
detail="You're not allowed to see other users memberships.",
)
return await cruds_cdr.get_actual_memberships_by_user_id(db=db, user_id=user_id)
return await cruds_core.get_actual_memberships_by_user_id(db=db, user_id=user_id)


@module.router.post(
Expand All @@ -2564,7 +2564,7 @@ async def create_membership(
end_date=membership.end_date,
)
try:
cruds_cdr.create_membership(db, db_membership)
cruds_core.create_membership(db, db_membership)
await db.commit()
except Exception:
await db.rollback()
Expand All @@ -2584,7 +2584,7 @@ async def update_membership(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_in(GroupType.admin_cdr)),
):
db_membership = await cruds_cdr.get_membership_by_user_id_and_membership_name(
db_membership = await cruds_core.get_membership_by_user_id_and_membership_name(
user_id=user_id,
membership=membership,
db=db,
Expand All @@ -2595,7 +2595,7 @@ async def update_membership(
detail="This user doesn't have this membership",
)
try:
await cruds_cdr.update_membership(
await cruds_core.update_membership(
membership_id=db_membership.id,
membership=membership_edit,
db=db,
Expand All @@ -2616,7 +2616,7 @@ async def delete_membership(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_in(GroupType.admin_cdr)),
):
db_membership = await cruds_cdr.get_membership_by_id(
db_membership = await cruds_core.get_membership_by_id(
membership_id=membership_id,
db=db,
)
Expand All @@ -2626,7 +2626,7 @@ async def delete_membership(
detail="Invalid membership_id",
)
try:
await cruds_cdr.delete_membership(
await cruds_core.delete_membership(
membership_id=membership_id,
db=db,
)
Expand Down

0 comments on commit 3b96865

Please sign in to comment.