Skip to content

Commit

Permalink
feat: shuffle recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
dartt0n committed Jul 21, 2024
1 parent d83efc3 commit b4d6538
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/adapter/external/graphql/operation/recommentaions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import annotations

from random import shuffle

import strawberry as sb

import src.domain.model as domain
from src.adapter.external.graphql import scalar
from src.adapter.external.graphql.tool.context import Info
from src.adapter.external.graphql.type.participant import ParticipantType


@sb.type
class RecommendationsQuery:
@sb.field
async def recommendations(
root: RecommendationsQuery,
info: Info[RecommendationsQuery],
user_id: scalar.ObjectID,
allocation_id: scalar.ObjectID,
) -> list[ParticipantType]: # type: ignore
participants = await info.context.participant.service.read_all()

selected = [
participant.id
for participant in participants
if participant.user_id != user_id
and participant.allocation_id == allocation_id
and participant.state == domain.ParticipantState.ACTIVE
]
if not selected:
return []

shuffle(selected)

return await info.context.participant.loader.load_many(selected[:10])
2 changes: 2 additions & 0 deletions src/adapter/external/graphql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
PreferenceMutation,
PreferenceQuery,
)
from src.adapter.external.graphql.operation.recommentaions import RecommendationsQuery
from src.adapter.external.graphql.operation.room import RoomMutation, RoomQuery
from src.adapter.external.graphql.operation.user import UserMutation, UserQuery

Expand All @@ -31,6 +32,7 @@ class Query(
PreferenceQuery,
RoomQuery,
UserQuery,
RecommendationsQuery,
): ...


Expand Down

0 comments on commit b4d6538

Please sign in to comment.