From db1cc8ddf0acc0df67a19655fb5ffe9ed1f60d29 Mon Sep 17 00:00:00 2001 From: DongwookKim0823 Date: Tue, 18 Jun 2024 18:59:39 +0900 Subject: [PATCH] =?UTF-8?q?Feat=20:=20=EC=9C=A0=EC=A0=80=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80=20#9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 콜백 로직에 유저 등록 로직 추가 Close #9 --- config/django/base.py | 4 +++ mung_manager/authentications/apis/apis.py | 13 ++++++++ mung_manager/customers/selectors/abstracts.py | 7 +++++ mung_manager/customers/selectors/customers.py | 11 +++++++ mung_manager/customers/services/abstracts.py | 8 +++++ mung_manager/customers/services/customers.py | 31 +++++++++++++++++++ 6 files changed, 74 insertions(+) diff --git a/config/django/base.py b/config/django/base.py index 2f040e7..8150cd4 100644 --- a/config/django/base.py +++ b/config/django/base.py @@ -19,6 +19,10 @@ "mung_manager.commons.apps.CommonConfig", "mung_manager.schemas.apps.SchemasConfig", "mung_manager.authentications.apps.AuthenticationConfig", + "mung_manager.customers.apps.CustomersConfig", + "mung_manager.pet_kindergardens.apps.PetKindergardensConfig", + "mung_manager.reservations.apps.ReservationsConfig", + "mung_manager.tickets.apps.TicketsConfig", ] THIRD_PARTY_APPS = [ diff --git a/mung_manager/authentications/apis/apis.py b/mung_manager/authentications/apis/apis.py index 157c7be..28d751d 100644 --- a/mung_manager/authentications/apis/apis.py +++ b/mung_manager/authentications/apis/apis.py @@ -10,6 +10,7 @@ from mung_manager.authentications.containers import AuthenticationContainer from mung_manager.authentications.enums import UserProvider from mung_manager.commons.base.serializers import BaseSerializer +from mung_manager.customers.containers import CustomerContainer from mung_manager.errors.exceptions import ( AuthenticationFailedException, InvalidTokenException, @@ -31,6 +32,8 @@ def __init__(self, *args, **kwargs): self._auth_service = AuthenticationContainer.auth_service() self._kakao_login_flow_service = AuthenticationContainer.kakao_login_flow_service() self._user_service = AuthenticationContainer.user_service() + self._customer_selector = CustomerContainer.customer_selector() + self._customer_service = CustomerContainer.customer_service() def get(self, request: Request) -> Response: input_serializer = self.InputSerializer(data=request.GET) @@ -73,6 +76,16 @@ def get(self, request: Request) -> Response: has_phone_number=has_phone_number, ) + # 유저 등록 + customer_queryset = self._customer_selector.get_queryset_by_phone_number_and_user_id_is_null( + phone_number=phone_number + ) + for customer in customer_queryset: + self._customer_service.register_customer( + user=user, + customer_id=customer.id, + ) + # 유저 토큰 발급 self._auth_service.authenticate_user(user) refresh_token, access_token = self._auth_service.generate_token(user) diff --git a/mung_manager/customers/selectors/abstracts.py b/mung_manager/customers/selectors/abstracts.py index 5c2610d..eab48fd 100644 --- a/mung_manager/customers/selectors/abstracts.py +++ b/mung_manager/customers/selectors/abstracts.py @@ -46,6 +46,13 @@ def get_by_id_and_customer_pet_id_for_undeleted_customer_pets( ) -> Optional[Customer]: raise NotImplementedException() + @abstractmethod + def get_queryset_by_phone_number_and_user_id_is_null( + self, + phone_number: str, + ) -> QuerySet[Customer]: + raise NotImplementedException() + class AbstractCustomerTicketSelector(ABC): @abstractmethod diff --git a/mung_manager/customers/selectors/customers.py b/mung_manager/customers/selectors/customers.py index dd00567..f0a84df 100644 --- a/mung_manager/customers/selectors/customers.py +++ b/mung_manager/customers/selectors/customers.py @@ -147,3 +147,14 @@ def get_by_id_and_customer_pet_id_for_undeleted_customer_pets( ) except Customer.DoesNotExist: return None + + def get_queryset_by_phone_number_and_user_id_is_null(self, phone_number: str) -> QuerySet[Customer]: + """이 함수는 전화번호로 user_id가 NULL인 고객 정보를 조회합니다. + + Args: + phone_number (str): 고객 전화번호 + + Returns: + QuerySet[Customer]: 고객 리스트 쿼리셋이며 존재하지 않으면 빈 쿼리셋을 반환 + """ + return Customer.objects.filter(phone_number=phone_number, user_id__isnull=True) diff --git a/mung_manager/customers/services/abstracts.py b/mung_manager/customers/services/abstracts.py index 3544f3e..2e221da 100644 --- a/mung_manager/customers/services/abstracts.py +++ b/mung_manager/customers/services/abstracts.py @@ -41,6 +41,14 @@ def update_customer( ) -> Optional[Customer]: raise NotImplementedException() + @abstractmethod + def register_customer( + self, + user, + customer_id: int, + ) -> Customer: + raise NotImplementedException() + class AbstractCustomerTicketService(ABC): @abstractmethod diff --git a/mung_manager/customers/services/customers.py b/mung_manager/customers/services/customers.py index 79eb96b..4474ecb 100644 --- a/mung_manager/customers/services/customers.py +++ b/mung_manager/customers/services/customers.py @@ -293,3 +293,34 @@ def update_customer( customer = self._customer_selector.get_with_undeleted_customer_pet_by_id(customer_id=customer_id) return customer + + @transaction.atomic + def register_customer( + self, + user, + customer_id: int, + ) -> Customer: + """ + 이 함수는 고객 정보를 업데이트합니다. + + Args: + user: 유저 객체 + customer_id (int): 고객 아이디 + + Returns: + Customer: 고객 객체 + """ + # 고객이 존재하는지 검증 + customer = get_object_or_not_found( + self._customer_selector.get_by_id(customer_id=customer_id), + msg=SYSTEM_CODE.message("NOT_FOUND_CUSTOMER"), + code=SYSTEM_CODE.code("NOT_FOUND_CUSTOMER"), + ) + + # 유저 id 업데이트 + fields = ["user"] + data = {"user": user} + + customer, has_updated = update_model(instance=customer, fields=fields, data=data) + + return customer