Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#144 - Adicionar team id para tabela de notificacoes #147

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions backend/api/models/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
37 changes: 18 additions & 19 deletions backend/api/routers/teams_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -147,35 +148,34 @@ 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()
)
if team_has_user != None:
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
Expand All @@ -185,7 +185,7 @@ async def addUserToTeam(input: AcceptTeamInviteInput, token: Annotated[str, Depe
session.refresh(notification)
session.refresh(data)

return data
return notification


@router.post(
Expand All @@ -194,7 +194,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:
Expand All @@ -216,11 +216,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,
)

Expand All @@ -235,7 +234,7 @@ async def addUserToTeam(input: AddUserToTeamInput, token: Annotated[str, Depends
@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)]):
Expand Down
5 changes: 3 additions & 2 deletions backend/api/schemas/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 12 additions & 4 deletions backend/api/schemas/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -90,12 +88,22 @@ 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

class RemoveUserFromTeamReturn(BaseModel):
team_id: int
user_id: int

class Config:
orm_mode = True


class Response(GenericModel, Generic[T]):
code: str
status: str
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""update notifications table

Revision ID: a4ec6fd4cb0b
Revises: 41197dc92dd1
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 = "41197dc92dd1"
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")