check if subscription if not freeze

This commit is contained in:
2026-01-18 13:16:17 -03:00
parent ae348377a5
commit 2f390ab202
3 changed files with 36 additions and 2 deletions

View File

@@ -6,7 +6,7 @@ from typing import Any, Literal, TypedDict
from uuid import uuid4
from layercake.dateutils import now, ttl
from layercake.dynamodb import DynamoDBPersistenceLayer
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from layercake.extra_types import CpfStr, NameStr
from layercake.strutils import md5_hash
from pydantic import (
@@ -18,7 +18,7 @@ from pydantic import (
)
from typing_extensions import NotRequired
from config import DEDUP_WINDOW_OFFSET_DAYS
from config import DEDUP_WINDOW_OFFSET_DAYS, USER_TABLE
class User(BaseModel):
@@ -90,6 +90,16 @@ class DeduplicationConflictError(Exception):
super().__init__('Enrollment already exists')
class SubscriptionRequiredError(Exception):
def __init__(self, msg: str | dict):
super().__init__('Subscription required')
class SubscriptionFrozenError(Exception):
def __init__(self, msg: str | dict):
super().__init__('Subscription is frozen')
def enroll(
enrollment: Enrollment,
*,
@@ -164,6 +174,7 @@ def enroll(
)
if subscription:
org_id = subscription['org_id']
transact.put(
item={
'id': enrollment.id,
@@ -172,6 +183,24 @@ def enroll(
}
| subscription,
)
transact.condition(
key=KeyPair(
pk='SUBSCRIPTION',
sk=f'ORG#{org_id}',
),
cond_expr='attribute_exists(sk)',
exc_cls=SubscriptionRequiredError,
table_name=USER_TABLE,
)
transact.condition(
key=KeyPair(
pk='SUBSCRIPTION#FREEZE',
sk=f'ORG#{org_id}',
),
cond_expr='attribute_not_exists(sk)',
exc_cls=SubscriptionFrozenError,
table_name=USER_TABLE,
)
if created_by:
transact.put(

View File

@@ -194,6 +194,8 @@ Resources:
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref EnrollmentTable
- DynamoDBReadPolicy:
TableName: !Ref UserTable
Events:
DynamoDBEvent:
Type: EventBridgeRule

View File

@@ -45,3 +45,6 @@
{"id": "1e2eaf0e-e319-49eb-ab33-1ddec156dc94", "sk": "0", "name": "pytest"}
// Org admins
{"id": "00237409-9384-4692-9be5-b4443a41e1c4", "sk": "admins#1234", "email": "sergio@somosbeta.com.br", "name": "Sérgio R Siqueira"}
// file: tests/events/test_reenroll_if_failed.py::test_reenroll_custom_dedup_window
{"id": "SUBSCRIPTION", "sk": "ORG#123"}