Updated the batch users to work with entities
This commit is contained in:
@@ -10,6 +10,10 @@ class DemographicInfo(BaseModel):
|
|||||||
passport_id: Optional[str] = None
|
passport_id: Optional[str] = None
|
||||||
country: Optional[str] = None
|
country: Optional[str] = None
|
||||||
|
|
||||||
|
class Entity(BaseModel):
|
||||||
|
id: str
|
||||||
|
role: str
|
||||||
|
|
||||||
|
|
||||||
class UserDTO(BaseModel):
|
class UserDTO(BaseModel):
|
||||||
id: uuid.UUID = Field(default_factory=uuid.uuid4)
|
id: uuid.UUID = Field(default_factory=uuid.uuid4)
|
||||||
@@ -24,6 +28,7 @@ class UserDTO(BaseModel):
|
|||||||
studentID: Optional[str] = None
|
studentID: Optional[str] = None
|
||||||
expiryDate: Optional[str] = None
|
expiryDate: Optional[str] = None
|
||||||
demographicInformation: Optional[DemographicInfo] = None
|
demographicInformation: Optional[DemographicInfo] = None
|
||||||
|
entities: list[Entity]
|
||||||
|
|
||||||
|
|
||||||
class BatchUsersDTO(BaseModel):
|
class BatchUsersDTO(BaseModel):
|
||||||
|
|||||||
@@ -128,12 +128,6 @@ class BatchUsers:
|
|||||||
self._insert_new_user(user)
|
self._insert_new_user(user)
|
||||||
code = self._create_code(user, maker_id)
|
code = self._create_code(user, maker_id)
|
||||||
|
|
||||||
if user.type == "corporate":
|
|
||||||
self._set_corporate_default_groups(user)
|
|
||||||
|
|
||||||
if user.corporate:
|
|
||||||
self._assign_corporate_to_user(user, code)
|
|
||||||
|
|
||||||
if user.groupName and len(user.groupName.strip()) > 0:
|
if user.groupName and len(user.groupName.strip()) > 0:
|
||||||
self._assign_user_to_group_by_name(user, maker_id)
|
self._assign_user_to_group_by_name(user, maker_id)
|
||||||
|
|
||||||
@@ -153,7 +147,8 @@ class BatchUsers:
|
|||||||
'isFirstLogin': False,
|
'isFirstLogin': False,
|
||||||
'isVerified': True,
|
'isVerified': True,
|
||||||
'registrationDate': datetime.now(),
|
'registrationDate': datetime.now(),
|
||||||
'subscriptionExpirationDate': user.expiryDate
|
'subscriptionExpirationDate': user.expiryDate,
|
||||||
|
'entities': user.entities
|
||||||
}
|
}
|
||||||
self._db.users.insert_one(new_user)
|
self._db.users.insert_one(new_user)
|
||||||
|
|
||||||
@@ -173,81 +168,13 @@ class BatchUsers:
|
|||||||
})
|
})
|
||||||
return code
|
return code
|
||||||
|
|
||||||
def _set_corporate_default_groups(self, user: UserDTO):
|
|
||||||
user_id = str(user.id)
|
|
||||||
default_groups = [
|
|
||||||
{
|
|
||||||
'admin': user_id,
|
|
||||||
'id': str(uuid.uuid4()),
|
|
||||||
'name': "Teachers",
|
|
||||||
'participants': [],
|
|
||||||
'disableEditing': True,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'admin': user_id,
|
|
||||||
'id': str(uuid.uuid4()),
|
|
||||||
'name': "Students",
|
|
||||||
'participants': [],
|
|
||||||
'disableEditing': True,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'admin': user_id,
|
|
||||||
'id': str(uuid.uuid4()),
|
|
||||||
'name': "Corporate",
|
|
||||||
'participants': [],
|
|
||||||
'disableEditing': True,
|
|
||||||
}
|
|
||||||
]
|
|
||||||
for group in default_groups:
|
|
||||||
self._db.groups.insert_one(group)
|
|
||||||
|
|
||||||
def _assign_corporate_to_user(self, user: UserDTO, code: str):
|
|
||||||
user_id = str(user.id)
|
|
||||||
corporate_user = self._db.users.find_one(
|
|
||||||
{"email": user.corporate}
|
|
||||||
)
|
|
||||||
if corporate_user:
|
|
||||||
self._db.codes.update_one(
|
|
||||||
{"id": code},
|
|
||||||
{"$set": {"creator": corporate_user.id}},
|
|
||||||
upsert=True
|
|
||||||
)
|
|
||||||
group_type = "Students" if user.type == "student" else "Teachers"
|
|
||||||
|
|
||||||
group = self._db.groups.find_one(
|
|
||||||
{
|
|
||||||
"admin": corporate_user.id,
|
|
||||||
"name": group_type
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
if group:
|
|
||||||
participants = group['participants']
|
|
||||||
if user_id not in participants:
|
|
||||||
participants.append(user_id)
|
|
||||||
self._db.groups.update_one(
|
|
||||||
{"id": group.id},
|
|
||||||
{"$set": {"participants": participants}}
|
|
||||||
)
|
|
||||||
|
|
||||||
else:
|
|
||||||
group = {
|
|
||||||
'admin': corporate_user.id,
|
|
||||||
'id': str(uuid.uuid4()),
|
|
||||||
'name': group_type,
|
|
||||||
'participants': [user_id],
|
|
||||||
'disableEditing': True,
|
|
||||||
}
|
|
||||||
|
|
||||||
self._db.groups.insert_one(group)
|
|
||||||
|
|
||||||
def _assign_user_to_group_by_name(self, user: UserDTO, maker_id: str):
|
def _assign_user_to_group_by_name(self, user: UserDTO, maker_id: str):
|
||||||
user_id = str(user.id)
|
user_id = str(user.id)
|
||||||
|
|
||||||
group = self._db.groups.find_one(
|
group = self._db.groups.find_one(
|
||||||
{
|
{
|
||||||
"admin": maker_id,
|
"admin": maker_id,
|
||||||
"name": user.group_name.strip()
|
"name": user.groupName.strip()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user