Skip to content

Commit

Permalink
Feat: add users to school on school creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotheem committed Nov 19, 2024
1 parent ccf6688 commit 501d212
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions app/core/schools/endpoints_schools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
"""

import logging
import re
import uuid

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession

from app.core import models_core, schemas_core
from app.core.groups.groups_type import GroupType
from app.core.groups.groups_type import AccountType, GroupType
from app.core.schools import cruds_schools
from app.core.schools.schools_type import SchoolType
from app.core.users import cruds_users
from app.dependencies import (
get_db,
is_user_a_member_of,
Expand Down Expand Up @@ -72,7 +74,7 @@ async def read_school(
async def create_school(
school: schemas_core.CoreSchoolBase,
db: AsyncSession = Depends(get_db),
user=Depends(is_user_a_member_of(GroupType.admin)),
user: schemas_core.CoreUser = Depends(is_user_a_member_of(GroupType.admin)),
):
"""
Create a new school.
Expand All @@ -94,9 +96,25 @@ async def create_school(
name=school.name,
email_regex=school.email_regex,
)
return await cruds_schools.create_school(school=db_school, db=db)
new_school = await cruds_schools.create_school(school=db_school, db=db)
users = await cruds_users.get_users(
db=db,
included_account_types=[AccountType.external],
)
for db_user in users:
if re.match(db_school.email_regex, db_user.email):
await cruds_users.update_user(
db,
db_user.id,
schemas_core.CoreUserUpdateAdmin(
school_id=db_school.id,
account_type=AccountType.other_school_student,
),
)
except ValueError as error:
raise HTTPException(status_code=422, detail=str(error))
else:
return new_school


@router.patch(
Expand Down

0 comments on commit 501d212

Please sign in to comment.