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

Recepcion de Pago #65

Merged
merged 3 commits into from
Jun 16, 2024
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
92 changes: 78 additions & 14 deletions src/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,58 @@ def add_user_to_group(db: Session, user: models.User, group: models.Group):
# ALL SPENDINGS
################################################


def get_all_spendings_by_group_id(db: Session, group_id: int):

unique_spendings = db.query(models.UniqueSpending).filter(models.UniqueSpending.group_id == group_id).limit(100).all()
installment_spendings = db.query(models.InstallmentSpending).filter(models.InstallmentSpending.group_id == group_id).limit(100).all()
recurring_spendings = db.query(models.RecurringSpending).filter(models.RecurringSpending.group_id == group_id).limit(100).all()

unique_spendings = list(map(lambda spending: {**spending.__dict__, "type": "unique_spending"}, unique_spendings))
installment_spendings = list(map(lambda spending: {**spending.__dict__, "type": "installment_spending"}, installment_spendings))
recurring_spendings = list(map(lambda spending: {**spending.__dict__, "type": "recurring_spending"}, recurring_spendings))
unique_spendings = (
db.query(models.UniqueSpending)
.filter(models.UniqueSpending.group_id == group_id)
.limit(100)
.all()
)
installment_spendings = (
db.query(models.InstallmentSpending)
.filter(models.InstallmentSpending.group_id == group_id)
.limit(100)
.all()
)
recurring_spendings = (
db.query(models.RecurringSpending)
.filter(models.RecurringSpending.group_id == group_id)
.limit(100)
.all()
)

unique_spendings = list(
map(
lambda spending: {**spending.__dict__, "type": "unique_spending"},
unique_spendings,
)
)
installment_spendings = list(
map(
lambda spending: {**spending.__dict__, "type": "installment_spending"},
installment_spendings,
)
)
recurring_spendings = list(
map(
lambda spending: {**spending.__dict__, "type": "recurring_spending"},
recurring_spendings,
)
)

return unique_spendings + installment_spendings + recurring_spendings


################################################
# UNIQUE SPENDINGS
################################################

def create_unique_spending(db: Session, spending: schemas.UniqueSpendingCreate, user_id: int):

def create_unique_spending(
db: Session, spending: schemas.UniqueSpendingCreate, user_id: int
):
db_spending = models.UniqueSpending(owner_id=user_id, **dict(spending))
db.add(db_spending)
db.commit()
Expand All @@ -177,8 +212,15 @@ def get_unique_spendings_by_group_id(db: Session, group_id: int):
################################################


def create_installment_spending(db: Session, spending: schemas.InstallmentSpendingCreate, user_id: int, current_installment:int):
db_spending = models.InstallmentSpending(owner_id=user_id, current_installment=current_installment,**dict(spending))
def create_installment_spending(
db: Session,
spending: schemas.InstallmentSpendingCreate,
user_id: int,
current_installment: int,
):
db_spending = models.InstallmentSpending(
owner_id=user_id, current_installment=current_installment, **dict(spending)
)
db.add(db_spending)
db.commit()
db.refresh(db_spending)
Expand All @@ -201,7 +243,9 @@ def get_installment_spendings_by_group_id(db: Session, group_id: int):
################################################


def create_recurring_spending(db: Session, spending: schemas.RecurringSpendingBase, user_id: int):
def create_recurring_spending(
db: Session, spending: schemas.RecurringSpendingBase, user_id: int
):
db_spending = models.RecurringSpending(owner_id=user_id, **dict(spending))
db.add(db_spending)
db.commit()
Expand All @@ -212,7 +256,11 @@ def create_recurring_spending(db: Session, spending: schemas.RecurringSpendingBa


def get_recurring_spendings_by_id(db: Session, recurring_spendig_id: int):
return db.query(models.RecurringSpending).filter(models.RecurringSpending.id == recurring_spendig_id).first()
return (
db.query(models.RecurringSpending)
.filter(models.RecurringSpending.id == recurring_spendig_id)
.first()
)


def get_recurring_spendings_by_group_id(db: Session, group_id: int):
Expand All @@ -224,7 +272,11 @@ def get_recurring_spendings_by_group_id(db: Session, group_id: int):
)


def put_recurring_spendings(db: Session, db_recurring_spending: models.RecurringSpending, put_recurring_spending: schemas.RecurringSpendingPut):
def put_recurring_spendings(
db: Session,
db_recurring_spending: models.RecurringSpending,
put_recurring_spending: schemas.RecurringSpendingPut,
):
db_recurring_spending.amount = put_recurring_spending.amount
db_recurring_spending.description = put_recurring_spending.description
db_recurring_spending.category_id = put_recurring_spending.categiry_id
Expand All @@ -240,13 +292,16 @@ def put_recurring_spendings(db: Session, db_recurring_spending: models.Recurring

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 @@ -255,6 +310,15 @@ def get_payments_by_group_id(db: Session, group_id: int):
.all()
)


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
53 changes: 45 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,12 @@ def list_group_categories(db: DbDependency, user: UserDependency, group_id: int)
categories = crud.get_categories_by_group_id(db, group_id)
return categories


################################################
# ALL SPENDINGS
################################################


@app.get("/group/{group_id}/spending")
def list_group_unique_spendings(db: DbDependency, user: UserDependency, group_id: int):
group = crud.get_group_by_id(db, group_id)
Expand Down Expand Up @@ -364,23 +366,26 @@ def create_installment_spending(
amount_of_installments = spending.amount_of_installments
spending_date = spending.date
for i in range(amount_of_installments):
spending.description = f"{spending_description} | cuota {i+1}/{amount_of_installments}"
spending.date = spending_date + timedelta(days=(30*i))
res.append(crud.create_installment_spending(db, spending, user.id, i+1))
spending.description = (
f"{spending_description} | cuota {i+1}/{amount_of_installments}"
)
spending.date = spending_date + timedelta(days=(30 * i))
res.append(crud.create_installment_spending(db, spending, user.id, i + 1))

return res


@app.get("/group/{group_id}/installment-spending")
def list_group_installment_spendings(db: DbDependency, user: UserDependency, group_id: int):
def list_group_installment_spendings(
db: DbDependency, user: UserDependency, group_id: int
):
group = crud.get_group_by_id(db, group_id)

check_group_exists_and_user_is_member(user.id, group)

return crud.get_installment_spendings_by_group_id(db, group_id)



################################################
# RECURRING SPENDINGS
################################################
Expand All @@ -405,13 +410,16 @@ def create_recurring_spending(


@app.get("/group/{group_id}/recurring-spending")
def list_group_recurring_spendings(db: DbDependency, user: UserDependency, group_id: int):
def list_group_recurring_spendings(
db: DbDependency, user: UserDependency, group_id: int
):
group = crud.get_group_by_id(db, group_id)

check_group_exists_and_user_is_member(user.id, group)

return crud.get_recurring_spendings_by_group_id(db, group_id)


@app.put("/recurring-spending/{recurring_spending_id}")
def put_recurring_spendings(
db: DbDependency,
Expand All @@ -420,7 +428,9 @@ def put_recurring_spendings(
put_recurring_spending: schemas.RecurringSpendingPut,
):

db_recurring_spending = crud.get_recurring_spendings_by_id(db, recurring_spending_id)
db_recurring_spending = crud.get_recurring_spendings_by_id(
db, recurring_spending_id
)

if db_recurring_spending is None:
raise HTTPException(
Expand All @@ -432,7 +442,9 @@ def put_recurring_spendings(
check_group_exists_and_user_is_member(user.id, group)
check_group_is_unarchived(group)

return crud.put_recurring_spendings(db, db_recurring_spending, put_recurring_spending)
return crud.put_recurring_spendings(
db, db_recurring_spending, put_recurring_spending
)


################################################
Expand Down Expand Up @@ -466,6 +478,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
3 changes: 3 additions & 0 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Category(Base):

__table_args__ = (UniqueConstraint("group_id", "name"),)


class UniqueSpending(Base):
__tablename__ = "unique_spendings"

Expand All @@ -79,6 +80,7 @@ class InstallmentSpending(Base):
current_installment = Column(Integer)
date: Mapped[datetime] = mapped_column(DateTime, default=func.now())


class RecurringSpending(Base):
__tablename__ = "recurring_spendings"

Expand All @@ -99,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
Loading
Loading