Coverage for apps/inners/use_cases/managements/account_management.py: 85%
41 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-22 19:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-22 19:03 +0000
1import uuid
2from typing import List
3from uuid import UUID
5import bcrypt
6from starlette.datastructures import State
8from apps.inners.models.daos.account import Account
9from apps.inners.models.dtos.contracts.requests.managements.accounts.create_one_body import CreateOneBody
10from apps.inners.models.dtos.contracts.requests.managements.accounts.patch_one_body import PatchOneBody
11from apps.outers.repositories.account_repository import AccountRepository
14class AccountManagement:
15 def __init__(
16 self,
17 account_repository: AccountRepository,
18 ):
19 self.account_repository: AccountRepository = account_repository
21 async def find_many_with_authorization_and_pagination(self, state: State, page_position: int, page_size: int) -> \
22 List[
23 Account]:
24 found_accounts: List[Account] = await self.account_repository.find_many_by_account_id_with_pagination(
25 session=state.session,
26 account_id=state.authorized_session.account_id,
27 page_position=page_position,
28 page_size=page_size
29 )
31 return found_accounts
33 async def find_one_by_id_with_authorization(self, state: State, id: UUID) -> Account:
34 found_account: Account = await self.account_repository.find_one_by_id_and_account_id(
35 session=state.session,
36 id=id,
37 account_id=state.authorized_session.account_id
38 )
40 return found_account
42 async def find_one_by_email(self, state: State, email: str) -> Account:
43 found_account: Account = await self.account_repository.find_one_by_email(
44 session=state.session,
45 email=email
46 )
48 return found_account
50 async def create_one(self, state: State, body: CreateOneBody) -> Account:
51 account_creator: Account = Account(**body.dict())
52 account_creator.id = uuid.uuid4()
53 account_creator.password = bcrypt.hashpw(account_creator.password.encode(), bcrypt.gensalt()).decode()
54 created_account: Account = self.create_one_raw(
55 state=state,
56 account_creator=account_creator
57 )
59 return created_account
61 def create_one_raw(self, state: State, account_creator: Account) -> Account:
62 created_account: Account = self.account_repository.create_one(
63 session=state.session,
64 account_creator=account_creator
65 )
67 return created_account
69 async def patch_one_by_id_with_authorization(self, state: State, id: UUID, body: PatchOneBody) -> Account:
70 account_patcher: Account = Account(**body.dict())
71 account_patcher.password = bcrypt.hashpw(account_patcher.password.encode(), bcrypt.gensalt()).decode()
72 patched_account: Account = await self.patch_one_by_id_raw_with_authorization(
73 state=state,
74 id=id,
75 account_patcher=account_patcher
76 )
78 return patched_account
80 async def patch_one_by_id_raw_with_authorization(self, state: State, id: UUID, account_patcher: Account) -> Account:
81 patched_account: Account = await self.account_repository.patch_one_by_id_and_account_id(
82 session=state.session,
83 id=id,
84 account_id=state.authorized_session.account_id,
85 account_patcher=account_patcher
86 )
88 return patched_account
90 async def delete_one_by_id_with_authorization(self, state: State, id: UUID) -> Account:
91 deleted_account: Account = await self.account_repository.delete_one_by_id_and_account_id(
92 session=state.session,
93 id=id,
94 account_id=state.authorized_session.account_id
95 )
97 return deleted_account