chore: refactored b2b endpoints

This commit is contained in:
ITQ
2025-01-23 22:52:02 +03:00
parent c27883b177
commit ead34fd867
2 changed files with 78 additions and 43 deletions
-1
View File
@@ -47,7 +47,6 @@ class PromocodeTarget(ModelSchema):
fields: ClassVar[list[str]] = [ fields: ClassVar[list[str]] = [
PromocodeTarget.age_from.field.name, PromocodeTarget.age_from.field.name,
PromocodeTarget.age_until.field.name, PromocodeTarget.age_until.field.name,
PromocodeTarget.categories.field.name,
] ]
+78 -42
View File
@@ -2,7 +2,6 @@ import datetime
from collections import Counter from collections import Counter
from http import HTTPStatus as status from http import HTTPStatus as status
from django.core.exceptions import ValidationError
from django.db.models import Count, Q, Value from django.db.models import Count, Q, Value
from django.db.models.functions import Coalesce from django.db.models.functions import Coalesce
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
@@ -51,7 +50,7 @@ def signin(
request: HttpRequest, request: HttpRequest,
login_data: schemas.BusinessSignInIn, login_data: schemas.BusinessSignInIn,
) -> tuple[int, schemas.BusinessSignInOut]: ) -> tuple[int, schemas.BusinessSignInOut]:
business_obj = Business(**dict(login_data)) business_obj = Business(**login_data.dict())
business_obj.validate( business_obj.validate(
include=[Business.email.field, Business.password.field], include=[Business.email.field, Business.password.field],
validate_unique=False, validate_unique=False,
@@ -86,24 +85,44 @@ def signin(
def create_promocode( def create_promocode(
request: HttpRequest, request: HttpRequest,
promocode: schemas.CreatePromocodeIn, promocode: schemas.CreatePromocodeIn,
) -> schemas.CreatePromocodeOut: ) -> tuple[int, schemas.CreatePromocodeOut]:
business = request.auth business = request.auth
promocode = dict(promocode) promocode = dict(promocode)
target = dict(promocode.pop("target")) target = dict(promocode.pop("target"))
target_obj = PromocodeTarget(**target, country_raw=target["country"]) target_obj = PromocodeTarget(**target, country_raw=target["country"])
target_obj.validate(
include=[
PromocodeTarget.age_from,
PromocodeTarget.age_until,
PromocodeTarget.country,
PromocodeTarget.categories,
],
validate_constraints=False,
validate_unique=False,
)
promocode_obj = Promocode(business=business, **promocode)
promocode_obj.validate(
include=[
Promocode.description,
Promocode.image_url,
Promocode.max_count,
Promocode.active_from,
Promocode.active_until,
Promocode.mode,
Promocode.promo_common,
Promocode.promo_unique,
],
validate_constraints=False,
validate_unique=False,
)
target_obj.save() target_obj.save()
promocode_obj = Promocode( promocode_obj.target = target_obj
business=business, promocode_obj.save()
target=target_obj,
**promocode,
)
try:
promocode_obj.save()
except ValidationError as e:
target_obj.delete()
raise e # noqa: TRY201
return status.CREATED, schemas.CreatePromocodeOut(id=promocode_obj.id) return status.CREATED, schemas.CreatePromocodeOut(id=promocode_obj.id)
@@ -121,11 +140,11 @@ def list_promocode(
request: HttpRequest, request: HttpRequest,
filters: Query[schemas.PromocodeListFilters], filters: Query[schemas.PromocodeListFilters],
response: HttpResponse, response: HttpResponse,
) -> list[schemas.PromocodeViewOut]: ) -> tuple[int, list[schemas.PromocodeViewOut]]:
business = request.auth business = request.auth
promocodes = Promocode.objects.filter(business=business).select_related( promocodes = Promocode.objects.select_related("target", "business").filter(
"target", "business" business=business
) )
if filters.country__in: if filters.country__in:
@@ -150,7 +169,7 @@ def list_promocode(
else: else:
promocodes = promocodes.order_by("-created_at") promocodes = promocodes.order_by("-created_at")
promocodes = promocodes.annotate( promocodes = promocodes.prefetch_related("activations", "likes").annotate(
used_count=Count("activations"), used_count=Count("activations"),
like_count=Count("likes"), like_count=Count("likes"),
) )
@@ -176,23 +195,29 @@ def get_promocode(
) -> schemas.PromocodeViewOut: ) -> schemas.PromocodeViewOut:
business = request.auth business = request.auth
promocodes = Promocode.objects.filter(id=promocode_id).select_related( promocodes = Promocode.objects.filter(id=promocode_id)
"target", "business"
)
if not promocodes.exists(): if not promocodes.exists():
raise HttpError(status.NOT_FOUND, status.NOT_FOUND.phrase) raise HttpError(status.NOT_FOUND, status.NOT_FOUND.phrase)
promocodes = promocodes.annotate( promocodes = promocodes.select_related("business").filter(
used_count=Count("activations"), business=business
like_count=Count("likes"), )
if not promocodes.exists():
raise HttpError(status.FORBIDDEN, status.FORBIDDEN.phrase)
promocodes = (
promocodes.select_related("target")
.prefetch_related("activations", "likes")
.annotate(
used_count=Count("activations"),
like_count=Count("likes"),
)
) )
promocode = promocodes.first() promocode = promocodes.first()
if promocode.business != business:
raise HttpError(status.FORBIDDEN, status.FORBIDDEN.phrase)
return utils.map_promocode_to_schema(promocode) return utils.map_promocode_to_schema(promocode)
@@ -212,23 +237,29 @@ def patch_promocode(
) -> schemas.PromocodeViewOut: ) -> schemas.PromocodeViewOut:
business = request.auth business = request.auth
promocodes = Promocode.objects.filter(id=promocode_id).select_related( promocodes = Promocode.objects.filter(id=promocode_id)
"target", "business"
)
if not promocodes.exists(): if not promocodes.exists():
raise HttpError(status.NOT_FOUND, status.NOT_FOUND.phrase) raise HttpError(status.NOT_FOUND, status.NOT_FOUND.phrase)
promocodes = promocodes.annotate( promocodes = promocodes.select_related("business").filter(
used_count=Count("activations"), business=business
like_count=Count("likes"), )
if not promocodes.exists():
raise HttpError(status.FORBIDDEN, status.FORBIDDEN.phrase)
promocodes = (
promocodes.select_related("target")
.prefetch_related("activations", "likes")
.annotate(
used_count=Count("activations"),
like_count=Count("likes"),
)
) )
promocode = promocodes.first() promocode = promocodes.first()
if promocode.business != business:
raise HttpError(status.FORBIDDEN, status.FORBIDDEN.phrase)
patch_data = patched_fields.dict(exclude_unset=True) patch_data = patched_fields.dict(exclude_unset=True)
target_data = patch_data.pop("target", None) target_data = patch_data.pop("target", None)
@@ -261,19 +292,24 @@ def promocode_stat(
) -> schemas.PromocodeStats: ) -> schemas.PromocodeStats:
business = request.auth business = request.auth
promocodes = Promocode.objects.filter(id=promocode_id).prefetch_related( promocodes = Promocode.objects.filter(id=promocode_id)
"activations",
)
if not promocodes.exists(): if not promocodes.exists():
raise HttpError(status.NOT_FOUND, status.NOT_FOUND.phrase) raise HttpError(status.NOT_FOUND, status.NOT_FOUND.phrase)
promocodes.prefetch_related("activations__user") promocodes = promocodes.select_related("business").filter(
promocode = promocodes.first() business=business
)
if promocode.business != business: if not promocodes.exists():
raise HttpError(status.FORBIDDEN, status.FORBIDDEN.phrase) raise HttpError(status.FORBIDDEN, status.FORBIDDEN.phrase)
promocodes = promocodes.prefetch_related(
"activations", "activations__user"
)
promocode = promocodes.first()
activations = promocode.activations.all() activations = promocode.activations.all()
activations_count = activations.count() activations_count = activations.count()
@@ -293,6 +329,6 @@ def promocode_stat(
) )
for country, count in sorted_countries for country, count in sorted_countries
] ]
if country_activations.items() if sorted_countries
else None, else None,
) )