check if subscription if not freeze
This commit is contained in:
@@ -6,7 +6,7 @@ from typing import Any, Literal, TypedDict
|
|||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from layercake.dateutils import now, ttl
|
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.extra_types import CpfStr, NameStr
|
||||||
from layercake.strutils import md5_hash
|
from layercake.strutils import md5_hash
|
||||||
from pydantic import (
|
from pydantic import (
|
||||||
@@ -18,7 +18,7 @@ from pydantic import (
|
|||||||
)
|
)
|
||||||
from typing_extensions import NotRequired
|
from typing_extensions import NotRequired
|
||||||
|
|
||||||
from config import DEDUP_WINDOW_OFFSET_DAYS
|
from config import DEDUP_WINDOW_OFFSET_DAYS, USER_TABLE
|
||||||
|
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
@@ -90,6 +90,16 @@ class DeduplicationConflictError(Exception):
|
|||||||
super().__init__('Enrollment already exists')
|
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(
|
def enroll(
|
||||||
enrollment: Enrollment,
|
enrollment: Enrollment,
|
||||||
*,
|
*,
|
||||||
@@ -164,6 +174,7 @@ def enroll(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if subscription:
|
if subscription:
|
||||||
|
org_id = subscription['org_id']
|
||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': enrollment.id,
|
'id': enrollment.id,
|
||||||
@@ -172,6 +183,24 @@ def enroll(
|
|||||||
}
|
}
|
||||||
| subscription,
|
| 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:
|
if created_by:
|
||||||
transact.put(
|
transact.put(
|
||||||
|
|||||||
@@ -194,6 +194,8 @@ Resources:
|
|||||||
Policies:
|
Policies:
|
||||||
- DynamoDBCrudPolicy:
|
- DynamoDBCrudPolicy:
|
||||||
TableName: !Ref EnrollmentTable
|
TableName: !Ref EnrollmentTable
|
||||||
|
- DynamoDBReadPolicy:
|
||||||
|
TableName: !Ref UserTable
|
||||||
Events:
|
Events:
|
||||||
DynamoDBEvent:
|
DynamoDBEvent:
|
||||||
Type: EventBridgeRule
|
Type: EventBridgeRule
|
||||||
|
|||||||
@@ -45,3 +45,6 @@
|
|||||||
{"id": "1e2eaf0e-e319-49eb-ab33-1ddec156dc94", "sk": "0", "name": "pytest"}
|
{"id": "1e2eaf0e-e319-49eb-ab33-1ddec156dc94", "sk": "0", "name": "pytest"}
|
||||||
// Org admins
|
// Org admins
|
||||||
{"id": "00237409-9384-4692-9be5-b4443a41e1c4", "sk": "admins#1234", "email": "sergio@somosbeta.com.br", "name": "Sérgio R Siqueira"}
|
{"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"}
|
||||||
Reference in New Issue
Block a user