feat: a few shipping products in box
This commit is contained in:
		@@ -5,7 +5,7 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
 | 
				
			|||||||
from models import BaseModel
 | 
					from models import BaseModel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if TYPE_CHECKING:
 | 
					if TYPE_CHECKING:
 | 
				
			||||||
    from models import Card, Product, Client
 | 
					    from models import Card, Product
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Pallet(BaseModel):
 | 
					class Pallet(BaseModel):
 | 
				
			||||||
@@ -30,6 +30,24 @@ class Pallet(BaseModel):
 | 
				
			|||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Box(BaseModel):
 | 
				
			||||||
 | 
					    __tablename__ = 'boxes'
 | 
				
			||||||
 | 
					    id: Mapped[int] = mapped_column(primary_key=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    shipping_products: Mapped[list['ShippingProduct']] = relationship(
 | 
				
			||||||
 | 
					        back_populates='box',
 | 
				
			||||||
 | 
					        uselist=True,
 | 
				
			||||||
 | 
					        lazy='joined',
 | 
				
			||||||
 | 
					        cascade='all, delete-orphan',
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pallet_id: Mapped[Optional[int]] = mapped_column(ForeignKey('pallets.id'))
 | 
				
			||||||
 | 
					    pallet: Mapped[Pallet] = relationship(back_populates='boxes')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    card_id: Mapped[Optional[int]] = mapped_column(ForeignKey('cards.id'))
 | 
				
			||||||
 | 
					    card: Mapped['Card'] = relationship(back_populates='boxes')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ShippingProduct(BaseModel):
 | 
					class ShippingProduct(BaseModel):
 | 
				
			||||||
    __tablename__ = 'shipping_products'
 | 
					    __tablename__ = 'shipping_products'
 | 
				
			||||||
    id: Mapped[int] = mapped_column(primary_key=True)
 | 
					    id: Mapped[int] = mapped_column(primary_key=True)
 | 
				
			||||||
@@ -38,21 +56,8 @@ class ShippingProduct(BaseModel):
 | 
				
			|||||||
    product_id: Mapped[int] = mapped_column(ForeignKey('products.id'))
 | 
					    product_id: Mapped[int] = mapped_column(ForeignKey('products.id'))
 | 
				
			||||||
    product: Mapped['Product'] = relationship(lazy='joined')
 | 
					    product: Mapped['Product'] = relationship(lazy='joined')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pallet_id: Mapped[int] = mapped_column(ForeignKey('pallets.id'))
 | 
					 | 
				
			||||||
    pallet: Mapped['Pallet'] = relationship(lazy='joined')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class Box(BaseModel):
 | 
					 | 
				
			||||||
    __tablename__ = 'boxes'
 | 
					 | 
				
			||||||
    id: Mapped[int] = mapped_column(primary_key=True)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    quantity: Mapped[int] = mapped_column(default=0)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    product_id: Mapped[Optional[int]] = mapped_column(ForeignKey('products.id'), nullable=True)
 | 
					 | 
				
			||||||
    product: Mapped['Product'] = relationship(lazy='joined')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pallet_id: Mapped[Optional[int]] = mapped_column(ForeignKey('pallets.id'))
 | 
					    pallet_id: Mapped[Optional[int]] = mapped_column(ForeignKey('pallets.id'))
 | 
				
			||||||
    pallet: Mapped[Pallet] = relationship(back_populates='boxes')
 | 
					    pallet: Mapped[Optional['Pallet']] = relationship(lazy='joined')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    card_id: Mapped[Optional[int]] = mapped_column(ForeignKey('cards.id'))
 | 
					    box_id: Mapped[Optional[int]] = mapped_column(ForeignKey('boxes.id'))
 | 
				
			||||||
    card: Mapped['Card'] = relationship(back_populates='boxes')
 | 
					    box: Mapped[Optional['Box']] = relationship(lazy='joined')
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -68,15 +68,15 @@ async def delete_shipping_product(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@shipping_router.post(
 | 
					@shipping_router.post(
 | 
				
			||||||
    '/box',
 | 
					    '/box',
 | 
				
			||||||
    response_model=UpdateBoxResponse,
 | 
					    response_model=CreateBoxResponse,
 | 
				
			||||||
    operation_id='update_box',
 | 
					    operation_id='create_box',
 | 
				
			||||||
    dependencies=[Depends(authorized_user)],
 | 
					    dependencies=[Depends(authorized_user)],
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
async def update_box(
 | 
					async def create_box(
 | 
				
			||||||
        session: SessionDependency,
 | 
					        session: SessionDependency,
 | 
				
			||||||
        request: UpdateBoxRequest,
 | 
					        request: CreateBoxRequest,
 | 
				
			||||||
):
 | 
					):
 | 
				
			||||||
    return await ShippingService(session).update_box(request)
 | 
					    return await ShippingService(session).create_box(request)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@shipping_router.delete(
 | 
					@shipping_router.delete(
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,19 +11,19 @@ class ProductAndQuantitySchema(BaseSchema):
 | 
				
			|||||||
    quantity: Optional[int]
 | 
					    quantity: Optional[int]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BoxSchema(BaseSchema):
 | 
					 | 
				
			||||||
    id: int
 | 
					 | 
				
			||||||
    quantity: int
 | 
					 | 
				
			||||||
    product: Optional[ProductSchema]
 | 
					 | 
				
			||||||
    pallet_id: Optional[int]
 | 
					 | 
				
			||||||
    card_id: Optional[int]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class ShippingProductSchema(BaseSchema):
 | 
					class ShippingProductSchema(BaseSchema):
 | 
				
			||||||
    id: int
 | 
					    id: int
 | 
				
			||||||
    quantity: int
 | 
					    quantity: int
 | 
				
			||||||
    product: ProductSchema
 | 
					    product: ProductSchema
 | 
				
			||||||
    pallet_id: int
 | 
					    pallet_id: Optional[int]
 | 
				
			||||||
 | 
					    box_id: Optional[int]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class BoxSchema(BaseSchema):
 | 
				
			||||||
 | 
					    id: int
 | 
				
			||||||
 | 
					    pallet_id: Optional[int]
 | 
				
			||||||
 | 
					    card_id: Optional[int]
 | 
				
			||||||
 | 
					    shipping_products: list[ShippingProductSchema]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PalletSchema(BaseSchema):
 | 
					class PalletSchema(BaseSchema):
 | 
				
			||||||
@@ -33,7 +33,8 @@ class PalletSchema(BaseSchema):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CreateShippingProductSchema(ProductAndQuantitySchema):
 | 
					class CreateShippingProductSchema(ProductAndQuantitySchema):
 | 
				
			||||||
    pallet_id: int
 | 
					    pallet_id: Optional[int] = None
 | 
				
			||||||
 | 
					    box_id: Optional[int] = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UpdateShippingProductSchema(ProductAndQuantitySchema):
 | 
					class UpdateShippingProductSchema(ProductAndQuantitySchema):
 | 
				
			||||||
@@ -48,10 +49,6 @@ class CreateBoxInCardSchema(BaseSchema):
 | 
				
			|||||||
    card_id: Optional[int]
 | 
					    card_id: Optional[int]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UpdateBoxSchema(ProductAndQuantitySchema):
 | 
					 | 
				
			||||||
    box_id: Optional[int]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# endregion
 | 
					# endregion
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# region Requests
 | 
					# region Requests
 | 
				
			||||||
@@ -60,8 +57,8 @@ class UpdateShippingProductRequest(BaseSchema):
 | 
				
			|||||||
    data: CreateShippingProductSchema | UpdateShippingProductSchema
 | 
					    data: CreateShippingProductSchema | UpdateShippingProductSchema
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UpdateBoxRequest(BaseSchema):
 | 
					class CreateBoxRequest(BaseSchema):
 | 
				
			||||||
    data: CreateBoxInCardSchema | CreateBoxInPalletSchema | UpdateBoxSchema
 | 
					    data: CreateBoxInCardSchema | CreateBoxInPalletSchema
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# endregion
 | 
					# endregion
 | 
				
			||||||
@@ -80,7 +77,7 @@ class UpdateShippingProductResponse(OkMessageSchema):
 | 
				
			|||||||
    pass
 | 
					    pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UpdateBoxResponse(OkMessageSchema):
 | 
					class CreateBoxResponse(OkMessageSchema):
 | 
				
			||||||
    pass
 | 
					    pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -322,7 +322,8 @@ class CardsService(BaseService):
 | 
				
			|||||||
                .selectinload(ShippingProduct.product)
 | 
					                .selectinload(ShippingProduct.product)
 | 
				
			||||||
                .noload(Product.barcodes),
 | 
					                .noload(Product.barcodes),
 | 
				
			||||||
                selectinload(Card.boxes)
 | 
					                selectinload(Card.boxes)
 | 
				
			||||||
                .selectinload(Box.product)
 | 
					                .selectinload(Box.shipping_products)
 | 
				
			||||||
 | 
					                .selectinload(ShippingProduct.product)
 | 
				
			||||||
                .noload(Product.barcodes),
 | 
					                .noload(Product.barcodes),
 | 
				
			||||||
                selectinload(Card.employees)
 | 
					                selectinload(Card.employees)
 | 
				
			||||||
                .joinedload(CardEmployees.user),
 | 
					                .joinedload(CardEmployees.user),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,16 +26,6 @@ class ShippingService(BaseService):
 | 
				
			|||||||
        await self.session.commit()
 | 
					        await self.session.commit()
 | 
				
			||||||
        return DeletePalletResponse(ok=True, message="Паллет успешно удален")
 | 
					        return DeletePalletResponse(ok=True, message="Паллет успешно удален")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def _update_box(self, data: UpdateBoxSchema) -> tuple[bool, str]:
 | 
					 | 
				
			||||||
        box = await self.session.get(Box, data.box_id)
 | 
					 | 
				
			||||||
        if not box:
 | 
					 | 
				
			||||||
            return False, f"Короб с ID:{data.box_id} не найден"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        box.quantity = data.quantity
 | 
					 | 
				
			||||||
        box.product_id = data.product_id
 | 
					 | 
				
			||||||
        await self.session.commit()
 | 
					 | 
				
			||||||
        return True, "Короб обновлен"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async def _create_box(self, data: CreateBoxInCardSchema | CreateBoxInPalletSchema):
 | 
					    async def _create_box(self, data: CreateBoxInCardSchema | CreateBoxInPalletSchema):
 | 
				
			||||||
        box = Box(**data.model_dump())
 | 
					        box = Box(**data.model_dump())
 | 
				
			||||||
        self.session.add(box)
 | 
					        self.session.add(box)
 | 
				
			||||||
@@ -59,16 +49,14 @@ class ShippingService(BaseService):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return True, f"Короб добавлен в паллет"
 | 
					        return True, f"Короб добавлен в паллет"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def update_box(self, request: UpdateBoxRequest) -> UpdateBoxResponse:
 | 
					    async def create_box(self, request: CreateBoxRequest) -> CreateBoxResponse:
 | 
				
			||||||
        data_keys = request.data.model_dump().keys()
 | 
					        data_keys = request.data.model_dump().keys()
 | 
				
			||||||
        if "box_id" in data_keys:
 | 
					        if "pallet_id" in data_keys:
 | 
				
			||||||
            ok, message = await self._update_box(request.data)
 | 
					 | 
				
			||||||
        elif "pallet_id" in data_keys:
 | 
					 | 
				
			||||||
            ok, message = await self._create_box_in_pallet(CreateBoxInPalletSchema.model_validate(request.data))
 | 
					            ok, message = await self._create_box_in_pallet(CreateBoxInPalletSchema.model_validate(request.data))
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            ok, message = await self._create_box_in_card(CreateBoxInCardSchema.model_validate(request.data))
 | 
					            ok, message = await self._create_box_in_card(CreateBoxInCardSchema.model_validate(request.data))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return UpdateBoxResponse(ok=ok, message=message)
 | 
					        return CreateBoxResponse(ok=ok, message=message)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def delete_box(self, deal_id: int) -> DeleteBoxResponse:
 | 
					    async def delete_box(self, deal_id: int) -> DeleteBoxResponse:
 | 
				
			||||||
        box = await self.session.get(Box, deal_id)
 | 
					        box = await self.session.get(Box, deal_id)
 | 
				
			||||||
@@ -94,13 +82,13 @@ class ShippingService(BaseService):
 | 
				
			|||||||
        shipping_product.product_id = data.product_id
 | 
					        shipping_product.product_id = data.product_id
 | 
				
			||||||
        shipping_product.quantity = data.quantity
 | 
					        shipping_product.quantity = data.quantity
 | 
				
			||||||
        await self.session.commit()
 | 
					        await self.session.commit()
 | 
				
			||||||
        return True, "Запись о товаре на паллете успешно изменена"
 | 
					        return True, "Запись о товаре успешно изменена"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def _create_shipping_product(self, data: CreateShippingProductSchema) -> tuple[bool, str]:
 | 
					    async def _create_shipping_product(self, data: CreateShippingProductSchema) -> tuple[bool, str]:
 | 
				
			||||||
        shipping_product = ShippingProduct(**data.model_dump())
 | 
					        shipping_product = ShippingProduct(**data.model_dump())
 | 
				
			||||||
        self.session.add(shipping_product)
 | 
					        self.session.add(shipping_product)
 | 
				
			||||||
        await self.session.commit()
 | 
					        await self.session.commit()
 | 
				
			||||||
        return True, "Запись о товаре на паллете успешно добавлена"
 | 
					        return True, "Запись о товаре успешно добавлена"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def update_shipping_product(self, request: UpdateShippingProductRequest) -> UpdateShippingProductResponse:
 | 
					    async def update_shipping_product(self, request: UpdateShippingProductRequest) -> UpdateShippingProductResponse:
 | 
				
			||||||
        data_keys = request.data.model_dump().keys()
 | 
					        data_keys = request.data.model_dump().keys()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user