diff --git a/barker/barker/routers/customer.py b/barker/barker/routers/customer.py index f26303a..21f6968 100644 --- a/barker/barker/routers/customer.py +++ b/barker/barker/routers/customer.py @@ -31,7 +31,8 @@ def save( db.add(item) add_discounts(item, data.discounts, db) db.commit() - return customer_info(item, db) + sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all() + return customer_info(item, sc) except SQLAlchemyError as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, @@ -54,7 +55,8 @@ def update_route( item.print_in_bill = data.print_in_bill add_discounts(item, data.discounts, db) db.commit() - return customer_info(item, db) + sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all() + return customer_info(item, sc) except SQLAlchemyError as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, @@ -89,15 +91,16 @@ def show_blank( user: UserToken = Security(get_user, scopes=["customers"]), ) -> schemas.CustomerBlank: with SessionFuture() as db: - return blank_customer_info(db) + sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all() + return blank_customer_info(sc) @router.get("/list", response_model=List[schemas.Customer]) def show_list(user: UserToken = Depends(get_user)) -> List[schemas.Customer]: with SessionFuture() as db: + sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all() return [ - customer_info_for_list(item) - for item in db.execute(select(Customer).order_by(Customer.name)).scalars().all() + customer_info(item, sc) for item in db.execute(select(Customer).order_by(Customer.name)).scalars().all() ] @@ -113,7 +116,8 @@ async def show_term( query = query.order_by(Customer.name) with SessionFuture() as db: - return [customer_info_for_list(item) for item in db.execute(query).scalars().all()] + sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all() + return [customer_info(item, sc) for item in db.execute(query).scalars().all()] @router.get("/{id_}", response_model=schemas.Customer) @@ -122,11 +126,12 @@ def show_id( user: UserToken = Security(get_user, scopes=["customers"]), ) -> schemas.Customer: with SessionFuture() as db: + sc: List[SaleCategory] = db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all() item: Customer = db.execute(select(Customer).where(Customer.id == id_)).scalar_one() - return customer_info(item, db) + return customer_info(item, sc) -def customer_info(item: Customer, db: Session) -> schemas.Customer: +def customer_info(item: Customer, sale_categories: List[SaleCategory]) -> schemas.Customer: return schemas.Customer( id=item.id, name=item.name, @@ -139,31 +144,12 @@ def customer_info(item: Customer, db: Session) -> schemas.Customer: "name": sc.name, "discount": next((d.discount for d in item.discounts if d.sale_category_id == sc.id), 0), } - for sc in db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all() + for sc in sale_categories ], ) -def customer_info_for_list(item: Customer) -> schemas.Customer: - return schemas.Customer( - id=item.id, - name=item.name, - address=item.address, - phone=item.phone, - printInBill=item.print_in_bill, - discounts=[ - { - "id": d.sale_category_id, - "name": d.sale_category.name, - "discount": d.discount, - } - for d in item.discounts - if d.discount != 0 - ], - ) - - -def blank_customer_info(db: Session) -> schemas.CustomerBlank: +def blank_customer_info(sale_categories: List[SaleCategory]) -> schemas.CustomerBlank: return schemas.CustomerBlank( name="", address="", @@ -175,6 +161,6 @@ def blank_customer_info(db: Session) -> schemas.CustomerBlank: "name": sc.name, "discount": 0, } - for sc in db.execute(select(SaleCategory).order_by(SaleCategory.name)).scalars().all() + for sc in sale_categories ], ) diff --git a/bookie/src/app/customers/customer-list/customer-list.component.ts b/bookie/src/app/customers/customer-list/customer-list.component.ts index 4c53523..56a4913 100644 --- a/bookie/src/app/customers/customer-list/customer-list.component.ts +++ b/bookie/src/app/customers/customer-list/customer-list.component.ts @@ -21,6 +21,7 @@ export class CustomerListComponent implements OnInit { ngOnInit() { this.route.data.subscribe((value) => { const data = value as { list: Customer[] }; + data.list.forEach((c) => (c.discounts = c.discounts.filter((d) => d.discount !== 0))); this.list = data.list; }); this.dataSource = new CustomerListDatasource(this.list);