database and services updated and tested
This commit is contained in:
0
api_events/events/identity/__init__.py
Normal file
0
api_events/events/identity/__init__.py
Normal file
219
api_events/events/identity/people.py
Normal file
219
api_events/events/identity/people.py
Normal file
@@ -0,0 +1,219 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import status
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from api_validations.validations_response.people import PeopleListResponse
|
||||
from databases import (
|
||||
Build,
|
||||
People,
|
||||
Users,
|
||||
Companies,
|
||||
)
|
||||
|
||||
from api_validations.validations_request import InsertPerson, UpdateUsers
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
from api_validations.core_response import AlchemyJsonResponse
|
||||
|
||||
|
||||
class PeopleListEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "SELECT"
|
||||
__event_keys__ = {
|
||||
"0a05f03c-6ed8-4230-a4ff-6e7cf886909b": "super_users_people_list",
|
||||
"b5612538-0445-4a4a-ab13-d2a06037f7a5": "sales_users_people_list",
|
||||
"25cbbaf8-117a-470f-a844-2cfc70f71dde": "human_resources_users_people_list",
|
||||
"cdf62f06-ec50-40de-b19e-adb3dd34bb95": "people_list_only_occupant_tenant_or_owner",
|
||||
}
|
||||
__event_validation__ = {
|
||||
"0a05f03c-6ed8-4230-a4ff-6e7cf886909b": PeopleListResponse,
|
||||
"b5612538-0445-4a4a-ab13-d2a06037f7a5": None,
|
||||
"25cbbaf8-117a-470f-a844-2cfc70f71dde": None,
|
||||
"cdf62f06-ec50-40de-b19e-adb3dd34bb95": None,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def super_users_people_list(
|
||||
cls, list_options, token_dict: Union[EmployeeTokenObject, OccupantTokenObject]
|
||||
):
|
||||
records = []
|
||||
if isinstance(token_dict, EmployeeTokenObject):
|
||||
People.pre_query = People.select_action(
|
||||
duty_id_list=[
|
||||
token_dict.selected_company.duty_id,
|
||||
token_dict.selected_company.bulk_duties_id,
|
||||
],
|
||||
)
|
||||
People.filter_attr = list_options
|
||||
records = People.filter_all()
|
||||
elif isinstance(token_dict, OccupantTokenObject):
|
||||
related_users = Users.filter_all(
|
||||
Users.related_company
|
||||
== token_dict.selected_occupant.responsible_company_id,
|
||||
).data
|
||||
People.pre_query = People.filter_all(
|
||||
People.id.in_([user.person_id for user in related_users]),
|
||||
).query
|
||||
People.filter_attr = list_options
|
||||
records = People.filter_all()
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
message="People are listed successfully",
|
||||
result=records,
|
||||
cls_object=People,
|
||||
filter_attributes=list_options,
|
||||
response_model=PeopleListResponse,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def sales_users_people_list(
|
||||
cls,
|
||||
list_options,
|
||||
token_dict: EmployeeTokenObject,
|
||||
):
|
||||
People.filter_attr = list_options
|
||||
records = People.filter_all().data
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
message="People are listed successfully",
|
||||
result=records,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def human_resources_users_people_list(
|
||||
cls,
|
||||
list_options,
|
||||
token_dict: EmployeeTokenObject,
|
||||
):
|
||||
if isinstance(token_dict, EmployeeTokenObject):
|
||||
People.filter_attr = list_options
|
||||
records = People.filter_all().data
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
message="People are listed successfully",
|
||||
result=records,
|
||||
)
|
||||
|
||||
|
||||
class PeopleCreateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "CREATE"
|
||||
__event_keys__ = {
|
||||
"2d1513f4-44ed-4fa3-84d1-dfbd0eadf9a1": "people_create",
|
||||
}
|
||||
__event_validation__ = {
|
||||
"2d1513f4-44ed-4fa3-84d1-dfbd0eadf9a1": InsertPerson,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def people_create(
|
||||
cls,
|
||||
data: InsertPerson,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
if isinstance(token_dict, EmployeeTokenObject):
|
||||
created_user = People.create_action(data=data, token=token_dict)
|
||||
People.save()
|
||||
elif isinstance(token_dict, OccupantTokenObject):
|
||||
created_user = People.create_action(data=data, token=token_dict)
|
||||
People.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Create User record",
|
||||
"data": created_user.get_dict(),
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class PeopleUpdateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "UPDATE"
|
||||
__event_keys__ = {
|
||||
"e05cf22c-16c4-450b-86c8-417896a26afc": "people_update",
|
||||
}
|
||||
__event_validation__ = {"e05cf22c-16c4-450b-86c8-417896a26afc": UpdateUsers}
|
||||
|
||||
@classmethod
|
||||
def people_update(
|
||||
cls,
|
||||
data: UpdateUsers,
|
||||
user_uu_id: str,
|
||||
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
data_dict = data.excluded_dump()
|
||||
if isinstance(token_dict, EmployeeTokenObject):
|
||||
find_one_user = Users.filter_one(
|
||||
Users.uu_id == user_uu_id,
|
||||
).data
|
||||
access_authorized_company = Companies.select_action(
|
||||
duty_id_list=[
|
||||
token_dict.selected_company.duty_id,
|
||||
token_dict.selected_company.bulk_duties_id,
|
||||
],
|
||||
)
|
||||
if access_authorized_company.count:
|
||||
updated_user = find_one_user.update(**data_dict)
|
||||
Users.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Update User record",
|
||||
"data": updated_user,
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
elif isinstance(token_dict, OccupantTokenObject):
|
||||
find_one_user = People.filter_one(
|
||||
People.uu_id == user_uu_id,
|
||||
).data
|
||||
access_authorized_company = Companies.select_action(
|
||||
duty_id_list=[getattr(token_dict, "duty_id")],
|
||||
filter_expr=[Companies.id == find_one_user.id],
|
||||
)
|
||||
if access_authorized_company.count:
|
||||
data_dict = data.excluded_dump()
|
||||
updated_user = find_one_user.update(**data_dict)
|
||||
People.save()
|
||||
return JSONResponse(
|
||||
content={"completed": True, "message": "Update User record", "data": {}},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class PeoplePatchEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "PATCH"
|
||||
__event_keys__ = {
|
||||
"3ae16d66-090b-4d27-b567-cce1b10a1c3b": "people_patch",
|
||||
}
|
||||
__event_validation__ = {"3ae16d66-090b-4d27-b567-cce1b10a1c3b": None}
|
||||
|
||||
@classmethod
|
||||
def people_patch(cls):
|
||||
return
|
||||
|
||||
|
||||
class PeopleDeleteEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "DELETE"
|
||||
__event_keys__ = {
|
||||
"7f84c7a2-a120-4867-90d4-6767a41320db": "people_delete",
|
||||
}
|
||||
__event_validation__ = {"7f84c7a2-a120-4867-90d4-6767a41320db": None}
|
||||
|
||||
|
||||
PeopleListEventMethod = PeopleListEventMethods(
|
||||
action=ActionsSchema(endpoint="/people/list")
|
||||
)
|
||||
PeopleCreateEventMethod = PeopleCreateEventMethods(
|
||||
action=ActionsSchema(endpoint="/people/create")
|
||||
)
|
||||
PeopleUpdateEventMethod = PeopleUpdateEventMethods(
|
||||
action=ActionsSchema(endpoint="/people/update")
|
||||
)
|
||||
PeoplePatchEventMethod = PeoplePatchEventMethods(
|
||||
action=ActionsSchema(endpoint="/people/patch")
|
||||
)
|
||||
217
api_events/events/identity/users.py
Normal file
217
api_events/events/identity/users.py
Normal file
@@ -0,0 +1,217 @@
|
||||
import typing
|
||||
|
||||
from fastapi import status
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from api_configs import ApiStatic
|
||||
from databases import MongoQueryIdentity, Users, Companies, People
|
||||
from databases.no_sql_models.validations import DomainViaUser
|
||||
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
from api_validations.core_response import AlchemyJsonResponse
|
||||
from api_services.email.service import send_email
|
||||
from api_services.templates.password_templates import change_your_password_template
|
||||
from api_validations.validations_request import (
|
||||
InsertUsers,
|
||||
UpdateUsers,
|
||||
PatchRecord,
|
||||
ListOptions,
|
||||
RegisterServices2Occupant,
|
||||
)
|
||||
|
||||
|
||||
class UserListEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "SELECT"
|
||||
__event_keys__ = {
|
||||
"1483a8a2-d244-4593-b9f8-f1b4bcbefcd5": "user_list",
|
||||
}
|
||||
__event_validation__ = {"1483a8a2-d244-4593-b9f8-f1b4bcbefcd5": None}
|
||||
|
||||
@classmethod
|
||||
def user_list(
|
||||
cls,
|
||||
list_options: ListOptions,
|
||||
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
if isinstance(token_dict, OccupantTokenObject):
|
||||
raise Users.raise_http_exception(
|
||||
status_code="HTTP_403_FORBIDDEN",
|
||||
message="Occupant object can not list users",
|
||||
error_case="NOTAUTHORIZED",
|
||||
data={},
|
||||
)
|
||||
if "user_uu_id_list" in list_options.query:
|
||||
people_ids = list_options.query.pop("user_uu_id_list")
|
||||
people_id_list = (
|
||||
user.person_id
|
||||
for user in Users.filter_all(Users.uu_id.in_(people_ids)).data
|
||||
)
|
||||
Users.filter_attr = list_options
|
||||
records = Users.filter_all(
|
||||
Users.person_id.in_(people_id_list),
|
||||
)
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
message="Users are listed successfully",
|
||||
result=records,
|
||||
)
|
||||
Users.filter_attr = list_options
|
||||
records = Users.filter_all()
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
message="Users are listed successfully",
|
||||
result=records,
|
||||
)
|
||||
|
||||
|
||||
class UserCreateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "CREATE"
|
||||
__event_keys__ = {
|
||||
"8eb50c24-4bdc-4309-9836-f7048daee409": "user_create",
|
||||
}
|
||||
__event_validation__ = {"8eb50c24-4bdc-4309-9836-f7048daee409": InsertUsers}
|
||||
|
||||
@classmethod
|
||||
def user_create(
|
||||
cls,
|
||||
data: InsertUsers,
|
||||
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
created_user = Users.create_action(create_user=data, token_dict=token_dict)
|
||||
domain_via_user = DomainViaUser(
|
||||
**{"user_uu_id": str(created_user.uu_id), "main_domain": "evyos.com.tr"}
|
||||
)
|
||||
created_user.save_and_confirm()
|
||||
mongo_query_identity = MongoQueryIdentity(
|
||||
company_uuid=created_user.related_company,
|
||||
)
|
||||
mongo_query_identity.create_domain_via_user(payload=domain_via_user)
|
||||
reset_password_token = created_user.reset_password_token(
|
||||
found_user=created_user
|
||||
)
|
||||
send_email_completed = send_email(
|
||||
subject=f"Dear {created_user.user_tag}, your password has been changed.",
|
||||
receivers=[str(created_user.email)],
|
||||
html=change_your_password_template(
|
||||
user_name=created_user.user_tag,
|
||||
forgot_link=ApiStatic.forgot_link(forgot_key=reset_password_token),
|
||||
),
|
||||
)
|
||||
if not send_email_completed:
|
||||
raise created_user.raise_http_exception(
|
||||
status_code=400, message="Email can not be sent. Try again later"
|
||||
)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Create User record",
|
||||
"data": created_user.get_dict(),
|
||||
"password_token": {
|
||||
"password_token": created_user.password_token,
|
||||
"password_expires_day": str(created_user.password_expires_day),
|
||||
"password_expiry_begins": str(created_user.password_expiry_begins),
|
||||
"hash_password": created_user.hash_password,
|
||||
"related_company": created_user.related_company,
|
||||
},
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class UserUpdateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "UPDATE"
|
||||
__event_keys__ = {
|
||||
"d08a9470-1eb0-4890-a9e8-b6686239d7e9": "user_update",
|
||||
}
|
||||
__event_validation__ = {"d08a9470-1eb0-4890-a9e8-b6686239d7e9": UpdateUsers}
|
||||
|
||||
@classmethod
|
||||
def user_update(
|
||||
cls,
|
||||
data: UpdateUsers,
|
||||
user_uu_id: str,
|
||||
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
find_one_user = Users.filter_one(
|
||||
Users.uu_id == user_uu_id,
|
||||
*Users.valid_record_args(Users),
|
||||
).data
|
||||
access_authorized_company = Companies.select_action(
|
||||
duty_id_list=[getattr(token_dict, "duty_id", 5)],
|
||||
filter_expr=[Companies.id == token_dict.get("")],
|
||||
)
|
||||
if access_authorized_company.count:
|
||||
data_dict = data.excluded_dump()
|
||||
updated_user = find_one_user.update(**data_dict)
|
||||
Users.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Update User record",
|
||||
"data": updated_user,
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
return JSONResponse(
|
||||
content={"completed": True, "message": "Update User record", "data": {}},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class UserPatchEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "PATCH"
|
||||
__event_keys__ = {
|
||||
"d26a1a3c-eaeb-4d01-b35b-a5ed714e29c0": "user_patch",
|
||||
}
|
||||
__event_validation__ = {"d26a1a3c-eaeb-4d01-b35b-a5ed714e29c0": None}
|
||||
|
||||
@classmethod
|
||||
def user_patch(cls, data: PatchRecord, user_uu_id: str, token_dict):
|
||||
find_one_user = Users.filter_one(
|
||||
Users.uu_id == user_uu_id,
|
||||
).data
|
||||
access_authorized_company = Companies.select_action(
|
||||
duty_id_list=[getattr(token_dict, "duty_id", 5)],
|
||||
filter_expr=[Companies.id == find_one_user.id],
|
||||
)
|
||||
if access_authorized_company.count:
|
||||
action = data.excluded_dump()
|
||||
find_one_user.active = bool(action.get("active", find_one_user.active))
|
||||
find_one_user.is_confirmed = bool(
|
||||
action.get("confirm", find_one_user.is_confirmed)
|
||||
)
|
||||
find_one_user.deleted = bool(action.get("delete", find_one_user.deleted))
|
||||
find_one_user.save()
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": True,
|
||||
"message": "Patch User record completed",
|
||||
"data": find_one_user.get_dict(),
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"completed": False,
|
||||
"message": "Patch User record failed",
|
||||
"data": {},
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
UserListEventMethod = UserListEventMethods(action=ActionsSchema(endpoint="/user/list"))
|
||||
UserCreateEventMethod = UserCreateEventMethods(
|
||||
action=ActionsSchema(endpoint="/user/create")
|
||||
)
|
||||
UserUpdateEventMethod = UserUpdateEventMethods(
|
||||
action=ActionsSchema(endpoint="/user/update")
|
||||
)
|
||||
UserPatchEventMethod = UserPatchEventMethods(
|
||||
action=ActionsSchema(endpoint="/user/patch")
|
||||
)
|
||||
Reference in New Issue
Block a user