Skip to content

Commit

Permalink
Merge branch 'develop' into feature/Remove-user-from-group
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Jun 17, 2024
2 parents 7e8c404 + 86b5219 commit c31430d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,16 @@ def put_recurring_spendings(

def create_payment(db: Session, payment: schemas.PaymentCreate):
db_payment = models.Payment(**dict(payment))
update_balances_from_payment(db, db_payment)
db.add(db_payment)
db.commit()
db.refresh(db_payment)
return db_payment


def get_payment_by_id(db: Session, payment_id: int):
return db.query(models.Payment).filter(models.Payment.id == payment_id).first()


def get_payments_by_group_id(db: Session, group_id: int):
return (
db.query(models.Payment)
Expand All @@ -334,6 +337,14 @@ def get_payments_by_group_id(db: Session, group_id: int):
)


def confirm_payment(db: Session, db_payment: models.Payment):
db_payment.confirmed = True
update_balances_from_payment(db, db_payment)
db.commit()
db.refresh(db_payment)
return db_payment


################################################
# BUDGETS
################################################
Expand Down
25 changes: 25 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,31 @@ def create_payment(
return crud.create_payment(db, payment)


@app.post("/payment/{payment_id}/confirm", status_code=HTTPStatus.OK)
def confirm_payment(db: DbDependency, user: UserDependency, payment_id: int):

payment = crud.get_payment_by_id(db, payment_id)
if payment is None:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail=f"No se consiguió el pago.",
)

if payment.confirmed:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"Este pago ya fue confirmado.",
)

if payment.to_id != user.id:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"Solo el receptor del pago puede confirmarlo.",
)

return crud.confirm_payment(db, payment)


@app.get("/payment")
def list_payments(db: DbDependency, user: UserDependency, group_id: int):
group = crud.get_group_by_id(db, group_id)
Expand Down
1 change: 1 addition & 0 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Payment(Base):
from_id = Column(ForeignKey("users.id"))
to_id = Column(ForeignKey("users.id"))
amount = Column(Integer)
confirmed = Column(Boolean, default=False)
date: Mapped[datetime] = mapped_column(DateTime, default=func.now())


Expand Down
4 changes: 2 additions & 2 deletions src/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class RecurringSpending(RecurringSpendingBase):
id: int
owner_id: int


################################################
# PAYMENTS
################################################
Expand All @@ -175,8 +176,6 @@ class Payment(PaymentBase):
id: int




################################################
# BUDGETS
################################################
Expand Down Expand Up @@ -213,6 +212,7 @@ class InviteStatus(StrEnum):
ACCEPTED = auto()
EXPIRED = auto()


class InviteBase(BaseModel):
creation_date: Optional[datetime] = Field(None)
receiver_id: Optional[int] = Field(None)
Expand Down
77 changes: 69 additions & 8 deletions src/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,14 +1048,9 @@ def some_payment(
some_other_credentials: schemas.UserCredentials,
some_group: schemas.Group,
):
res = client.post(
url=f"/group/{some_group.id}/member",
json={
"user_identifier": some_other_credentials.id,
},
headers={"x-user": some_credentials.jwt},
add_user_to_group(
client, some_group.id, some_other_credentials.id, some_credentials
)
assert res.status_code == HTTPStatus.CREATED

response = client.post(
url="/payment",
Expand All @@ -1075,19 +1070,85 @@ def some_payment(
return schemas.Payment(**response_body)


@pytest.fixture
def some_payment_confirmation(
client: TestClient,
some_other_credentials: schemas.UserCredentials,
some_payment: schemas.Payment,
):
response = client.post(
url=f"/payment/{some_payment.id}/confirm",
headers={"x-user": some_other_credentials.jwt},
)
response_body = response.json()
assert response.status_code == HTTPStatus.OK
assert response_body["confirmed"] == True
return schemas.Payment(**response_body)


def test_create_payment(some_payment: schemas.Payment):
# NOTE: test is inside fixture
pass


def test_create_payment_confirmation(some_payment_confirmation: schemas.Payment):
# NOTE: test is inside fixture
pass


def test_confirm_non_existant_payment(
client: TestClient,
some_credentials: schemas.UserCredentials,
some_other_credentials: schemas.UserCredentials,
some_group: schemas.Group,
):

add_user_to_group(
client, some_group.id, some_other_credentials.id, some_credentials
)

response = client.post(
url=f"/payment/{123}/confirm",
headers={"x-user": some_other_credentials.jwt},
)
assert response.status_code == HTTPStatus.NOT_FOUND


def test_confirm_non_existant_payment(
client: TestClient,
some_credentials: schemas.UserCredentials,
some_payment: schemas.Payment,
):

response = client.post(
url=f"/payment/{some_payment.id}/confirm",
headers={"x-user": some_credentials.jwt},
)
assert response.status_code == HTTPStatus.BAD_REQUEST


def test_confirm_already_confirmed(
client: TestClient,
some_other_credentials: schemas.UserCredentials,
some_payment_confirmation: schemas.Payment,
):

response = client.post(
url=f"/payment/{some_payment_confirmation.id}/confirm",
headers={"x-user": some_other_credentials.jwt},
)
assert response.status_code == HTTPStatus.BAD_REQUEST


def test_payment_updates_balance(
client: TestClient,
some_credentials: schemas.UserCredentials,
some_other_credentials: schemas.UserCredentials,
some_payment: schemas.Payment,
some_payment_confirmation: schemas.Payment,
):
response = client.get(
url=f"/group/{some_payment.group_id}/balance",
url=f"/group/{some_payment_confirmation.group_id}/balance",
headers={"x-user": some_credentials.jwt},
)
assert response.status_code == HTTPStatus.OK
Expand Down

0 comments on commit c31430d

Please sign in to comment.