Skip to content

Commit

Permalink
Feat : 유저 등록 로직 추가 #9
Browse files Browse the repository at this point in the history
- 콜백 로직에 유저 등록 로직 추가
Close #9
  • Loading branch information
DongwookKim0823 committed Jun 18, 2024
1 parent 3ab4f2c commit db1cc8d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/django/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
13 changes: 13 additions & 0 deletions mung_manager/authentications/apis/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions mung_manager/customers/selectors/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions mung_manager/customers/selectors/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 8 additions & 0 deletions mung_manager/customers/services/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions mung_manager/customers/services/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit db1cc8d

Please sign in to comment.