in server/store/serializers.py [0:0]
def validate(self, data):
try:
product = Product.objects.get(pk=data["id"])
except Product.DoesNotExist:
raise serializers.ValidationError(detail={"status": "product_not_found"})
requested = data["countRequested"]
if product.inventory_count < requested:
data["countFulfilled"] = product.inventory_count
# Log error by writing structured JSON. Can be then used with log-based alerting, metrics, etc.
error_name = "INSUFFICIENT_PRODUCT_ERROR"
print(
json.dumps(
{
"severity": "ERROR",
"error": error_name,
"message": f"{error_name}: A purchase was attempted where there was insufficient inventory to fulfil the order.",
"product": product.id,
"method": "CartItemSerializer.validate()",
"countRequested": data["countRequested"],
"countFulfilled": data["countFulfilled"],
}
)
)
raise serializers.ValidationError(
detail={"status": "insufficient_product", "items": data}
)
else:
data["countFulfilled"] = requested
return data