fix: applied timezone to default values, removed value nesting
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
from collections import defaultdict
|
||||
|
||||
from sqlalchemy import and_
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
@ -9,7 +11,11 @@ from models import (
|
||||
module_attribute,
|
||||
)
|
||||
from repositories.mixins import *
|
||||
from schemas.attribute import CreateAttributeSchema, UpdateAttributeSchema, UpdateDealModuleAttributeSchema
|
||||
from schemas.attribute import (
|
||||
CreateAttributeSchema,
|
||||
UpdateAttributeSchema,
|
||||
UpdateDealModuleAttributeSchema,
|
||||
)
|
||||
from utils.exceptions import ForbiddenException
|
||||
|
||||
|
||||
@ -83,7 +89,18 @@ class AttributeRepository(
|
||||
self, deal_id: int, module_id: int
|
||||
) -> list[tuple[Attribute, AttributeValue, AttributeLabel]]:
|
||||
stmt = (
|
||||
select(Attribute, AttributeValue, AttributeLabel)
|
||||
select(
|
||||
Attribute,
|
||||
AttributeValue,
|
||||
AttributeLabel,
|
||||
)
|
||||
.join(
|
||||
module_attribute,
|
||||
and_(
|
||||
module_attribute.c.attribute_id == Attribute.id,
|
||||
module_attribute.c.module_id == module_id,
|
||||
),
|
||||
)
|
||||
.outerjoin(
|
||||
AttributeValue,
|
||||
and_(
|
||||
@ -99,25 +116,21 @@ class AttributeRepository(
|
||||
AttributeLabel.module_id == module_id,
|
||||
),
|
||||
)
|
||||
.join(
|
||||
module_attribute,
|
||||
and_(
|
||||
module_attribute.c.attribute_id == Attribute.id,
|
||||
module_attribute.c.module_id == module_id,
|
||||
),
|
||||
.where(
|
||||
Attribute.is_deleted.is_(False),
|
||||
)
|
||||
)
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.all())
|
||||
|
||||
async def _get_deal_attribute_values(
|
||||
self, deal_id: int, module_id: int
|
||||
async def _get_deals_attribute_values(
|
||||
self, deal_ids: list[int], module_id: int
|
||||
) -> list[AttributeValue]:
|
||||
stmt = (
|
||||
select(AttributeValue)
|
||||
.join(Attribute, AttributeValue.attribute_id == Attribute.id)
|
||||
.where(
|
||||
AttributeValue.deal_id == deal_id,
|
||||
AttributeValue.deal_id.in_(deal_ids),
|
||||
AttributeValue.module_id == module_id,
|
||||
Attribute.is_deleted.is_(False),
|
||||
)
|
||||
@ -125,28 +138,44 @@ class AttributeRepository(
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def update_or_create_deal_attribute_values(
|
||||
async def update_or_create_deals_attribute_values(
|
||||
self,
|
||||
deal_id: int,
|
||||
main_deal_id: int,
|
||||
group_deal_ids: list[int],
|
||||
module_id: int,
|
||||
attributes: list[UpdateDealModuleAttributeSchema],
|
||||
):
|
||||
old_deal_attribute_values: list[AttributeValue] = await self._get_deal_attribute_values(deal_id, module_id)
|
||||
dict_old_attrs: dict[int, AttributeValue] = {obj.attribute_id: obj for obj in old_deal_attribute_values}
|
||||
old_deal_attribute_values: list[
|
||||
AttributeValue
|
||||
] = await self._get_deals_attribute_values(group_deal_ids, module_id)
|
||||
|
||||
dict_old_attrs: dict[int, dict[int, AttributeValue]] = defaultdict(dict)
|
||||
|
||||
for deal_attribute in old_deal_attribute_values:
|
||||
dict_old_attrs[deal_attribute.deal_id][deal_attribute.attribute_id] = (
|
||||
deal_attribute
|
||||
)
|
||||
|
||||
for attribute in attributes:
|
||||
if attribute.attribute_id in dict_old_attrs:
|
||||
# update
|
||||
attribute_value = dict_old_attrs[attribute.attribute_id]
|
||||
attribute_value.value = attribute.value
|
||||
if attribute.is_applicable_to_group:
|
||||
deal_ids_to_apply = group_deal_ids
|
||||
else:
|
||||
# create
|
||||
attribute_value = AttributeValue(
|
||||
attribute_id=attribute.attribute_id,
|
||||
deal_id=deal_id,
|
||||
module_id=module_id,
|
||||
value=attribute.value,
|
||||
)
|
||||
self.session.add(attribute_value)
|
||||
deal_ids_to_apply = [main_deal_id]
|
||||
|
||||
for deal_id in deal_ids_to_apply:
|
||||
if attribute.attribute_id in dict_old_attrs[deal_id]:
|
||||
attribute_value = dict_old_attrs[deal_id][attribute.attribute_id]
|
||||
attribute_value.value = attribute.value
|
||||
else:
|
||||
if attribute.value is None:
|
||||
continue
|
||||
|
||||
attribute_value = AttributeValue(
|
||||
attribute_id=attribute.attribute_id,
|
||||
deal_id=deal_id,
|
||||
module_id=module_id,
|
||||
value=attribute.value,
|
||||
)
|
||||
self.session.add(attribute_value)
|
||||
|
||||
await self.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user