2
from typing import Any, Optional, List
4
from fastapi.exceptions import HTTPException
5
from sqlalchemy.ext.asyncio import AsyncSession
6
from starlette.requests import Request
7
from app.inventory.order.enums.exceptions_suggest_enums import SuggestErrors
8
from app.inventory.order.enums.order_enum import SuggestStatus, SuggestType, MoveStatus
9
from app.inventory.order.models.order_models import Suggest
10
from app.inventory.order.schemas.suggest_schemas import SuggestCreateScheme, SuggestUpdateScheme, SuggestFilter
11
from core.exceptions.module import ModuleException
12
from core.permissions import permit
13
from core.service.base import BaseService, UpdateSchemaType, ModelType, FilterSchemaType, CreateSchemaType
14
from core.helpers.broker.tkq import list_brocker
16
class SuggestService(BaseService[Suggest, SuggestCreateScheme, SuggestUpdateScheme, SuggestFilter]):
17
def __init__(self, request: Request):
18
super(SuggestService, self).__init__(request, Suggest, SuggestCreateScheme, SuggestUpdateScheme)
20
@permit('suggest_edit')
21
async def update(self, id: Any, obj: UpdateSchemaType, commit=False) -> Optional[ModelType]:
22
return await super(SuggestService, self).update(id, obj, commit=commit)
24
@permit('suggest_list')
25
async def list(self, _filter: FilterSchemaType | dict, size: int):
26
return await super(SuggestService, self).list(_filter, size)
28
@permit('suggest_create')
29
async def create(self, obj: CreateSchemaType, commit=False) -> ModelType:
30
return await super(SuggestService, self).create(obj, commit)
32
@permit('suggest_delete')
33
async def delete(self, id: Any) -> None:
34
return await super(SuggestService, self).delete(id)
36
@permit('suggest_confirm')
37
async def suggest_confirm(self, suggest_ids: List[uuid.UUID], value: Any, commit: bool = True) -> Optional[ModelType]:
38
suggest_entities = await self.list({'id__in': suggest_ids}, 999)
39
for suggest_entity in suggest_entities:
41
if suggest_entity.status == SuggestStatus.DONE:
42
raise ModuleException(
44
enum=SuggestErrors.SUGGEST_ALREADY_DONE
46
elif suggest_entity.type == SuggestType.IN_QUANTITY:
48
val_in_cleaned = float(value)
49
except ValueError as e:
50
raise ModuleException(
52
enum=SuggestErrors.SUGGEST_INVALID_VALUE,
56
suggest_entity.result_value = str(val_in_cleaned)
57
suggest_entity.status = SuggestStatus.DONE
58
elif suggest_entity.type == SuggestType.IN_PRODUCT:
59
product_obj = await self.env['product'].adapter.product_by_barcode(value)
61
suggest_entity.result_value = value
62
suggest_entity.status = SuggestStatus.DONE
63
elif suggest_entity.type == SuggestType.IN_LOCATION:
65
val_in_cleaned = uuid.UUID(value)
66
except ValueError as e:
67
raise ModuleException(
69
enum=SuggestErrors.SUGGEST_INVALID_VALUE,
72
location_entity = await self.env['location'].service.list(_filter={'id__in': [val_in_cleaned]})
75
suggest_entity.result_value = value
76
suggest_entity.status = SuggestStatus.DONE
78
raise ModuleException(
80
enum=SuggestErrors.SUGGEST_TYPE_NOT_FOUND,
82
suggest_entity.user_id = self.user.user_id
84
move = await self.env['move'].service.get(suggest_entity.move_id)
85
for suggest in move.suggest_list_rel:
86
if suggest.id != suggest_entity.id and suggest.status != SuggestStatus.DONE:
89
move.status = MoveStatus.DONE
91
task__set_done = list_brocker.register_task(self.env['move'].service.set_done)
92
task = await task__set_done.kiq(move_id=move.id)
93
task_result = await task.wait_result()
97
await self.session.commit()
98
for suggest_entity in suggest_entities:
99
await self.session.refresh(suggest_entity)
100
await suggest_entity.notify('update')
101
except Exception as e:
102
await self.session.rollback()
103
raise HTTPException(status_code=500, detail=f"Some Error entity {str(e)}")
105
await self.session.flush(suggest_entities)
107
return suggest_entities