From 2c4d5edbcf3dd3b1010802162080a219e42feebd Mon Sep 17 00:00:00 2001 From: KozielGPC Date: Sun, 18 Jun 2023 19:47:11 -0300 Subject: [PATCH 1/4] #144 - atualizada rota de notifications --- backend/api/models/notifications.py | 5 +-- backend/api/routers/teams_routes.py | 32 +++++++++---------- backend/api/schemas/notifications.py | 5 +-- backend/api/schemas/teams.py | 9 +++--- ...a4ec6fd4cb0b_update_notifications_table.py | 32 +++++++++++++++++++ 5 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 backend/migrations/versions/a4ec6fd4cb0b_update_notifications_table.py diff --git a/backend/api/models/notifications.py b/backend/api/models/notifications.py index 179cb24..2de6deb 100644 --- a/backend/api/models/notifications.py +++ b/backend/api/models/notifications.py @@ -6,7 +6,8 @@ class Notification(Base): __tablename__ = "notifications" id = Column(Integer, primary_key=True, autoincrement=True) - name = Column(String) + team_name = Column(String) reference_user_id = Column(Integer, ForeignKey("users.id")) - text = Column(Text) + sender_name = Column(Text) + team_id = Column(Integer) visualized = Column(Boolean) diff --git a/backend/api/routers/teams_routes.py b/backend/api/routers/teams_routes.py index 06a9e3a..a102f08 100644 --- a/backend/api/routers/teams_routes.py +++ b/backend/api/routers/teams_routes.py @@ -147,26 +147,25 @@ async def addUserToTeam(input: AcceptTeamInviteInput, token: Annotated[str, Depe notification = session.query(Notification).filter(Notification.id == input.notification_id).first() if notification == None: raise HTTPException(status_code=404, detail="Notification not found") + if notification.reference_user_id != user.id: + raise HTTPException(status_code=401, detail="Notification is not from this user") if input.accepted == False: notification.visualized = True - return { - "user_id": input.user_id, - "team_id": input.team_id, - } - player = session.query(User).filter(User.id == input.user_id).first() + session.commit() + session.refresh(notification) + return notification + player = session.query(User).filter(User.id == notification.reference_user_id).first() if player == None: raise HTTPException(status_code=404, detail="Player not found") - team = session.query(Team).filter(Team.id == input.team_id).first() + team = session.query(Team).filter(Team.id == notification.team_id).first() if team == None: raise HTTPException(status_code=404, detail="Team not found") - if team.owner_id != user.id: - raise HTTPException(status_code=401, detail="User is not admin of Team") team_has_user = ( session.query(TeamsHasUsers) .filter( - TeamsHasUsers.user_id == input.user_id, - TeamsHasUsers.team_id == input.team_id, + TeamsHasUsers.user_id == notification.reference_user_id, + TeamsHasUsers.team_id == notification.team_id, ) .first() ) @@ -174,8 +173,8 @@ async def addUserToTeam(input: AcceptTeamInviteInput, token: Annotated[str, Depe raise HTTPException(status_code=400, detail="Player is already registered in this Team") data = TeamsHasUsers( - user_id=input.user_id, - team_id=input.team_id, + user_id=notification.reference_user_id, + team_id=notification.team_id, ) notification.visualized = True @@ -194,7 +193,7 @@ async def addUserToTeam(input: AcceptTeamInviteInput, token: Annotated[str, Depe response_model=NotificationSchema, response_description="Sucesso de resposta da aplicação.", ) -async def addUserToTeam(input: AddUserToTeamInput, token: Annotated[str, Depends(oauth2_scheme)]): +async def inviteUserToTeam(input: AddUserToTeamInput, token: Annotated[str, Depends(oauth2_scheme)]): user = await get_current_user(token) player = session.query(User).filter(User.id == input.user_id).first() if player == None: @@ -216,11 +215,10 @@ async def addUserToTeam(input: AddUserToTeamInput, token: Annotated[str, Depends raise HTTPException(status_code=400, detail="Player is already registered in this Team") data = Notification( - name="Convite para o time " + team.name, - text="Você foi convidado para o time " - + team.name - + ", para aceitar acesse as suas notifiações e aceite o convite.", + team_name=team.name, + sender_name=user.username, reference_user_id=player.id, + team_id=team.id, visualized=False, ) diff --git a/backend/api/schemas/notifications.py b/backend/api/schemas/notifications.py index 988e47d..2727107 100644 --- a/backend/api/schemas/notifications.py +++ b/backend/api/schemas/notifications.py @@ -4,9 +4,10 @@ class NotificationSchema(BaseModel): id: Optional[int] = None - name: Optional[str] = None - text: Optional[str] = None + team_name: Optional[str] = None + sender_name: Optional[str] = None reference_user_id: Optional[int] = Field(default=None, foreign_key="users.id") + team_id: Optional[int] = None visualized: Optional[bool] = None class Config: diff --git a/backend/api/schemas/teams.py b/backend/api/schemas/teams.py index 0978143..c0aaa1a 100644 --- a/backend/api/schemas/teams.py +++ b/backend/api/schemas/teams.py @@ -74,12 +74,10 @@ class Config: class AcceptTeamInviteInput(BaseModel): - team_id: int - user_id: int notification_id: int accepted: bool - @validator("team_id", "user_id", "notification_id") + @validator("notification_id") def check_positive_numbers(cls, v): assert v >= 0, "Negative numbers are not allowed." return v @@ -90,7 +88,10 @@ class Config: class AddUserToTeamReturn(BaseModel): team_id: int - user_id: int + reference_user_id: int + visualized: bool + sender_name: str + team_name: str class Config: orm_mode = True diff --git a/backend/migrations/versions/a4ec6fd4cb0b_update_notifications_table.py b/backend/migrations/versions/a4ec6fd4cb0b_update_notifications_table.py new file mode 100644 index 0000000..ff20769 --- /dev/null +++ b/backend/migrations/versions/a4ec6fd4cb0b_update_notifications_table.py @@ -0,0 +1,32 @@ +"""update notifications table + +Revision ID: a4ec6fd4cb0b +Revises: f5191b0310d5 +Create Date: 2023-06-18 19:10:33.836857 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "a4ec6fd4cb0b" +down_revision = "f5191b0310d5" +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column("notifications", sa.Column("team_id", sa.Integer)) + op.add_column("notifications", sa.Column("team_name", sa.String)) + op.add_column("notifications", sa.Column("sender_name", sa.String)) + op.drop_column("notifications", "name") + op.drop_column("notifications", "text") + + +def downgrade(): + op.add_column("notifications", sa.Column("name", sa.String)) + op.add_column("notifications", sa.Column("text", sa.String)) + op.drop_column("notifications", "team_id") + op.drop_column("notifications", "team_name") + op.drop_column("notifications", "sender_name") From fb9755a8208e3341cbb2b8146e97465101a882af Mon Sep 17 00:00:00 2001 From: KozielGPC Date: Sun, 18 Jun 2023 19:48:36 -0300 Subject: [PATCH 2/4] #144 - atualizado changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b2be4b..da5786f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - [#13](https://github.com/KozielGPC/championship-platform/issues/13) - Entrar em um Campeonato ### Changed +- [#144](https://github.com/KozielGPC/championship-platform/issues/144) - Atualizado tabela de notifications com campos novos - [#65](https://github.com/KozielGPC/championship-platform/issues/65) - Atualizado banco de dados para postgresql ### Fixed From d3ee322194ed47db2d4bd4e1cc29d2f69603b957 Mon Sep 17 00:00:00 2001 From: KozielGPC Date: Sun, 18 Jun 2023 20:04:10 -0300 Subject: [PATCH 3/4] #144 - atualizado migration number --- .../versions/a4ec6fd4cb0b_update_notifications_table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/migrations/versions/a4ec6fd4cb0b_update_notifications_table.py b/backend/migrations/versions/a4ec6fd4cb0b_update_notifications_table.py index ff20769..bb71e5f 100644 --- a/backend/migrations/versions/a4ec6fd4cb0b_update_notifications_table.py +++ b/backend/migrations/versions/a4ec6fd4cb0b_update_notifications_table.py @@ -1,7 +1,7 @@ """update notifications table Revision ID: a4ec6fd4cb0b -Revises: f5191b0310d5 +Revises: 41197dc92dd1 Create Date: 2023-06-18 19:10:33.836857 """ @@ -11,7 +11,7 @@ # revision identifiers, used by Alembic. revision = "a4ec6fd4cb0b" -down_revision = "f5191b0310d5" +down_revision = "41197dc92dd1" branch_labels = None depends_on = None From 0c1b77f15566d665e6094829c66946ffb6840037 Mon Sep 17 00:00:00 2001 From: Reginaldo-Neto Date: Sun, 18 Jun 2023 20:26:01 -0300 Subject: [PATCH 4/4] =?UTF-8?q?#144=20-=20Altera=C3=A7=C3=A3o=20no=20retor?= =?UTF-8?q?no=20de=20aceitar=20notifica=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/api/routers/teams_routes.py | 5 +++-- backend/api/schemas/teams.py | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/api/routers/teams_routes.py b/backend/api/routers/teams_routes.py index a102f08..1d0844c 100644 --- a/backend/api/routers/teams_routes.py +++ b/backend/api/routers/teams_routes.py @@ -23,6 +23,7 @@ from api.schemas.teams_has_users import UserWithTeams from fastapi.encoders import jsonable_encoder from api.websocket.connection_manager import ws_manager +from api.schemas.teams import RemoveUserFromTeamReturn router = APIRouter( prefix="/teams", @@ -184,7 +185,7 @@ async def addUserToTeam(input: AcceptTeamInviteInput, token: Annotated[str, Depe session.refresh(notification) session.refresh(data) - return data + return notification @router.post( @@ -233,7 +234,7 @@ async def inviteUserToTeam(input: AddUserToTeamInput, token: Annotated[str, Depe @router.post( "/remove-user", status_code=200, - response_model=AddUserToTeamReturn, + response_model=RemoveUserFromTeamReturn, response_description="Sucesso de resposta da aplicação.", ) async def addUserToTeam(input: AddUserToTeamInput, token: Annotated[str, Depends(oauth2_scheme)]): diff --git a/backend/api/schemas/teams.py b/backend/api/schemas/teams.py index c0aaa1a..140d180 100644 --- a/backend/api/schemas/teams.py +++ b/backend/api/schemas/teams.py @@ -96,7 +96,14 @@ class AddUserToTeamReturn(BaseModel): class Config: orm_mode = True +class RemoveUserFromTeamReturn(BaseModel): + team_id: int + user_id: int + class Config: + orm_mode = True + + class Response(GenericModel, Generic[T]): code: str status: str