fix: client delete fix

This commit is contained in:
2025-07-18 19:28:08 +04:00
parent c492e9a57e
commit 85492a31e8
3 changed files with 16 additions and 5 deletions

View File

@@ -22,6 +22,11 @@ class Client(BaseModel):
) )
created_at: Mapped[datetime] = mapped_column(nullable=False, comment='Дата создания') created_at: Mapped[datetime] = mapped_column(nullable=False, comment='Дата создания')
is_deleted: Mapped[bool] = mapped_column(
nullable=False,
default=False,
server_default='0',
)
products: Mapped[list['Product']] = relationship('Product', back_populates='client') products: Mapped[list['Product']] = relationship('Product', back_populates='client')
details: Mapped['ClientDetails'] = relationship( details: Mapped['ClientDetails'] = relationship(

View File

@@ -28,6 +28,7 @@ class ClientSchema(BaseSchema):
comment: Optional[str] = None comment: Optional[str] = None
details: ClientDetailsSchema | None = None details: ClientDetailsSchema | None = None
chat: ChatSchema | None = None chat: ChatSchema | None = None
is_deleted: bool | None = False
class ClientDetailedSchema(ClientSchema): class ClientDetailedSchema(ClientSchema):

View File

@@ -5,7 +5,7 @@ from fastapi import HTTPException
from sqlalchemy import select, update from sqlalchemy import select, update
from sqlalchemy.orm import joinedload, selectinload, noload from sqlalchemy.orm import joinedload, selectinload, noload
from models import Client, ClientDetails, User, ResidualPallet, ResidualBox, ResidualProduct, Product from models import Client, ClientDetails, User, ResidualPallet, ResidualBox, ResidualProduct, Product, Card
from schemas.client import * from schemas.client import *
from services.auth import AuthService from services.auth import AuthService
from services.base import BaseService from services.base import BaseService
@@ -100,9 +100,14 @@ class ClientService(BaseService):
return client return client
async def search_clients(self, request: ClientSearchRequest) -> ClientSearchResponse: async def search_clients(self, request: ClientSearchRequest) -> ClientSearchResponse:
query = await self.session.scalars(select(Client) query = await self.session.scalars(
.where(Client.name.ilike(f'%{request.name}%')) select(Client)
.options(joinedload(Client.details))) .where(
Client.name.ilike(f'%{request.name}%'),
Client.is_deleted == False,
)
.options(joinedload(Client.details))
)
clients = [] clients = []
for client in query.all(): for client in query.all():
clients.append(ClientSchema.model_validate(client)) clients.append(ClientSchema.model_validate(client))
@@ -161,7 +166,7 @@ class ClientService(BaseService):
client = await self._get_by_id(request.client_id) client = await self._get_by_id(request.client_id)
if not client: if not client:
return ClientDeleteResponse(ok=False, message='Клиент не найден') return ClientDeleteResponse(ok=False, message='Клиент не найден')
await self.session.delete(client) client.is_deleted = True
await self.session.commit() await self.session.commit()
return ClientDeleteResponse(ok=True, message='Клиент удален') return ClientDeleteResponse(ok=True, message='Клиент удален')
except Exception as e: except Exception as e: