Skip to content

Commit

Permalink
- Resource "likes" merged with resource "contacts"
Browse files Browse the repository at this point in the history
  • Loading branch information
onstabb committed Dec 25, 2023
1 parent b18f044 commit 6b0e2b7
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 63 deletions.
2 changes: 0 additions & 2 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from contacts.routers import router as contact_router
from events.routers import router as event_router
from events.routers import user_router as user_events_router
from likes.routers import router as likes_router
from notifications.routers import router as notifications_router
from photos.routers import router as photos_router
from reports.routers import router as reports_router
Expand All @@ -23,7 +22,6 @@
authenticated_router.include_router(profile_router, tags=["Profile"], prefix="/profile")
authenticated_router.include_router(photos_router, tags=["Photos"], prefix="/photos")
authenticated_router.include_router(candidates_router, tags=["Candidates"], prefix="/candidates")
authenticated_router.include_router(likes_router, tags=["Likes"], prefix="/likes")
authenticated_router.include_router(contact_router, tags=["Contacts"], prefix="/contacts")
authenticated_router.include_router(user_events_router, tags=["Events"], prefix="/events")
authenticated_router.include_router(notifications_router, tags=["Notifications"], prefix="/notifications")
Expand Down
1 change: 0 additions & 1 deletion src/authorization/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def create_admin() -> User:
User(
phone_number=config.ADMIN_PHONE_NUMBER,
password=hash_password(password),
is_active=False,
role=UserRole.ADMIN
)
)
Expand Down
6 changes: 6 additions & 0 deletions src/contacts/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ class ContactUpdateAction(enum.StrEnum):
REFUSE = "refuse"
BLOCK = "block"
SEEN = "seen"


@enum.unique
class ContactType(enum.StrEnum):
LIKE = "likes"
DIALOG = "dialogs"
15 changes: 10 additions & 5 deletions src/contacts/routers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import APIRouter, HTTPException, status, Query

from contacts import config, service
from contacts.enums import ContactState, ContactUpdateAction
from contacts.enums import ContactState, ContactUpdateAction, ContactType
from contacts.models import Contact
from contacts.schemas import ContactUpdateIn, ContactCreateDataIn, ContactOut, MessageIn, MessageOut
from notifications.enums import NotificationType
Expand All @@ -16,10 +16,15 @@
@router.get("", response_model=list[ContactOut])
def get_contacts(
current_user: CurrentActiveCompletedUser,
contact_status: ContactState = Query(alias="status", default=ContactState.ESTABLISHED),
contact_type: ContactType = Query(alias="contactType"),
limit: int = 0,
):
return service.get_contacts_by_user(current_user, limit=limit, status=contact_status)
if contact_type == ContactType.LIKE:
return service.get_contacts_by_user(
current_user, limit=limit, status=None, initiator_state=ContactState.ESTABLISHED, respondent=current_user.id
)
# if contact_type == ContactType.DIALOG
return service.get_contacts_by_user(current_user, limit=limit, status=ContactState.ESTABLISHED)


@router.get("/{user_id}", response_model=ContactOut)
Expand All @@ -33,7 +38,7 @@ def get_contact(current_user: CurrentActiveCompletedUser, target_user: TargetAct
def create_contact(data_in: ContactCreateDataIn, current_user: CurrentActiveCompletedUser):
if data_in.user_id == current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="You cannot create contact with yourself",
status_code=status.HTTP_403_FORBIDDEN, detail="Cannot create contact with yourself",
)

target_user = get_active_completed_user(data_in.user_id)
Expand All @@ -43,7 +48,7 @@ def create_contact(data_in: ContactCreateDataIn, current_user: CurrentActiveComp
status_code=status.HTTP_409_CONFLICT, detail=f"Contact with profile `{target_user.id}` already exists"
)

contact = service.create_contact_by_initiator(current_user, target_user, data_in)
contact = service.create_contact_by_initiator(initiator=current_user, respondent=target_user, data_in=data_in)
if data_in.state == ContactState.ESTABLISHED:
notification_manager.put_notification(
UserPublicOut.model_validate(current_user, from_attributes=True),
Expand Down
Empty file removed src/likes/__init__.py
Empty file.
13 changes: 0 additions & 13 deletions src/likes/routers.py

This file was deleted.

24 changes: 0 additions & 24 deletions src/likes/service.py

This file was deleted.

6 changes: 2 additions & 4 deletions src/users/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ def get_current_user_by_token(subject: str = Depends(JWTBearer),) -> User:
return user


def get_current_active_user(user: User = Depends(get_current_user_by_token), set_online: bool = True) -> User:
def get_current_active_user(user: User = Depends(get_current_user_by_token)) -> User:
if not user.is_active:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Activation is required")

if set_online:
user.last_online = get_aware_datetime_now()

user.last_online = get_aware_datetime_now()
return user


Expand Down
15 changes: 15 additions & 0 deletions tests/test_contacts/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ def test_get_contacts_for_user(contact_factory):
raise AssertionError


def test_get_likes_for_user(user, contact_factory):
contact_factory.create_batch(5, likes=True, respondent=user)
contact_factory.create_batch(1, likes=True, initiator=user)

contacts = service.get_contacts_by_user(
user, status=None, respondent=user.id, initiator_state=ContactState.ESTABLISHED
)

for contact_doc in contacts:
assert contact_doc.get("initiator") != user.id
assert contact_doc.get("initiator_state") == ContactState.ESTABLISHED.value
assert contact_doc.get("respondent_state") is None
assert contact_doc.get("status") is None


def test_get_contact_by_user_pair(contact_factory):
contact = contact_factory(active_dialog=True)
current_user, target_user = contact.initiator, contact.respondent
Expand Down
Empty file removed tests/test_likes/__init__.py
Empty file.
14 changes: 0 additions & 14 deletions tests/test_likes/test_service.py

This file was deleted.

0 comments on commit 6b0e2b7

Please sign in to comment.