Skip to content

Commit

Permalink
Fix cdr result (#573)
Browse files Browse the repository at this point in the history
### Description

Please explain the changes you made here.

### Checklist

- [ ] Created tests which fail without the change (if possible)
- [ ] All tests passing
- [ ] Extended the documentation, if necessary
  • Loading branch information
Rotheem authored Sep 9, 2024
1 parent 0a293cd commit b5022eb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ class Settings(BaseSettings):

# Hyperion follows Semantic Versioning
# https://semver.org/
HYPERION_VERSION: str = "3.0.1"
MINIMAL_TITAN_VERSION_CODE: int = 130
HYPERION_VERSION: str = "3.0.2"
MINIMAL_TITAN_VERSION_CODE: int = 113

######################################
# Automatically generated parameters #
Expand Down
18 changes: 18 additions & 0 deletions app/modules/cdr/cruds_cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,25 @@ async def delete_purchase(
db: AsyncSession,
user_id: str,
product_variant_id: UUID,
product_id: UUID,
):
fields = (
(
await db.execute(
select(models_cdr.CustomDataField).where(
models_cdr.CustomDataField.product_id == product_id,
),
)
)
.scalars()
.all()
)
await db.execute(
delete(models_cdr.CustomData).where(
models_cdr.CustomData.user_id == user_id,
models_cdr.CustomData.field_id.in_([field.id for field in fields]),
),
)
await db.execute(
delete(models_cdr.Purchase).where(
models_cdr.Purchase.user_id == user_id,
Expand Down
3 changes: 2 additions & 1 deletion app/modules/cdr/endpoints_cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ async def generate_and_send_results(
product.id,
),
)
variant_ids = [v.id for v in product_variants]
variant_ids = [v.id for v in variants]
purchases = await cruds_cdr.get_all_purchases(db)
if len(purchases) == 0:
raise HTTPException(
Expand Down Expand Up @@ -1767,6 +1767,7 @@ async def delete_purchase(
user_id=user_id,
product_variant_id=product_variant_id,
db=db,
product_id=product.id,
)
cruds_cdr.create_action(db, db_action)
await db.commit()
Expand Down
25 changes: 13 additions & 12 deletions app/modules/cdr/utils_cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ def construct_dataframe_from_users_purchases(
"""
columns = ["Nom", "Prénom", "Surnom", "Email"]
data = ["", "", "", "Prix"]
field_to_column = {}
variant_to_column = {}
field_to_column: dict[UUID, str] = {}
variant_to_column: dict[UUID, str] = {}
for product in products:
product_variants = [
variant for variant in variants if variant.product_id == product.id
Expand All @@ -189,7 +189,7 @@ def construct_dataframe_from_users_purchases(
columns.extend(
[f"{product.name_fr} : {field.name}" for field in fields],
)
data.extend([str(variant.price) for variant in product_variants])
data.extend([str(variant.price / 100) for variant in product_variants])

data.extend([" " for _ in fields])
field_to_column.update(
Expand Down Expand Up @@ -241,14 +241,15 @@ def construct_dataframe_from_users_purchases(
df.loc[user_id, "Prénom"] = user.firstname
df.loc[user_id, "Surnom"] = user.nickname
df.loc[user_id, "Email"] = user.email
if all(purchase.validated for purchase in users_purchases[user_id]):
df.loc[user_id, "Panier payé"] = True
else:
df.loc[user_id, "Panier payé"] = False
df.loc[user_id, "Commentaire"] = "Manquant : \n-" + "\n-".join(
variant_to_column[purchase.product_variant_id]
for purchase in users_purchases[user_id]
if not purchase.validated
)
if user_id in users_purchases:
if all(purchase.validated for purchase in users_purchases[user_id]):
df.loc[user_id, "Panier payé"] = True
else:
df.loc[user_id, "Panier payé"] = False
df.loc[user_id, "Commentaire"] = "Manquant : \n-" + "\n-".join(
variant_to_column[purchase.product_variant_id]
for purchase in users_purchases[user_id]
if not purchase.validated
)
df.fillna("", inplace=True)
return df
27 changes: 27 additions & 0 deletions tests/test_cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2750,3 +2750,30 @@ async def test_delete_customdata(client: TestClient):
headers={"Authorization": f"Bearer {token_bde}"},
)
assert response.status_code == 204


async def test_customdata_deletion_on_purchase_deletion(client: TestClient):
field = models_cdr.CustomDataField(
id=uuid.uuid4(),
product_id=product.id,
name="Supprime",
)
await add_object_to_db(field)
customdata = models_cdr.CustomData(
field_id=field.id,
user_id=cdr_user.id,
value="Edit",
)
await add_object_to_db(customdata)

response = client.delete(
f"/cdr/users/{cdr_user.id}/purchases/{variant.id}/",
headers={"Authorization": f"Bearer {token_admin}"},
)
assert response.status_code == 204

response = client.get(
f"/cdr/sellers/{seller.id}/products/{product.id}/users/{cdr_user.id}/data/{field.id}/",
headers={"Authorization": f"Bearer {token_admin}"},
)
assert response.status_code == 404

0 comments on commit b5022eb

Please sign in to comment.