fix: tenant subs plan expired logic
This commit is contained in:
@@ -60,7 +60,7 @@ def get_current_user(
|
|||||||
)
|
)
|
||||||
|
|
||||||
today = datetime.now(timezone.utc).date()
|
today = datetime.now(timezone.utc).date()
|
||||||
if tenant.end_date and tenant.end_date < today and tenant.status != "EXPIRED":
|
if tenant.end_date and tenant.end_date <= today and tenant.status != "EXPIRED":
|
||||||
tenant.status = "EXPIRED"
|
tenant.status = "EXPIRED"
|
||||||
tenant.is_active = False
|
tenant.is_active = False
|
||||||
db.commit()
|
db.commit()
|
||||||
@@ -142,4 +142,4 @@ def require_access_hierarchical(access_code: str):
|
|||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return check_permission
|
return check_permission
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class TenantService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
normalized_status = TenantService._normalize_status(status_value, is_active)
|
normalized_status = TenantService._normalize_status(status_value, is_active)
|
||||||
if end_date and end_date < TenantService._today():
|
if end_date and end_date <= TenantService._today():
|
||||||
return TenantService.STATUS_EXPIRED, False
|
return TenantService.STATUS_EXPIRED, False
|
||||||
if normalized_status in {
|
if normalized_status in {
|
||||||
TenantService.STATUS_INACTIVE,
|
TenantService.STATUS_INACTIVE,
|
||||||
@@ -60,6 +60,24 @@ class TenantService:
|
|||||||
return normalized_status, False
|
return normalized_status, False
|
||||||
return normalized_status, True
|
return normalized_status, True
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _sync_tenant_lifecycle(tenant: Tenant) -> bool:
|
||||||
|
resolved_status, resolved_is_active = TenantService._resolve_lifecycle(
|
||||||
|
start_date=tenant.start_date,
|
||||||
|
end_date=tenant.end_date,
|
||||||
|
status_value=tenant.status,
|
||||||
|
is_active=tenant.is_active,
|
||||||
|
)
|
||||||
|
|
||||||
|
changed = (
|
||||||
|
tenant.status != resolved_status
|
||||||
|
or tenant.is_active != resolved_is_active
|
||||||
|
)
|
||||||
|
if changed:
|
||||||
|
tenant.status = resolved_status
|
||||||
|
tenant.is_active = resolved_is_active
|
||||||
|
return changed
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_tenant(db: Session, tenant_data: TenantCreate) -> Tenant:
|
def create_tenant(db: Session, tenant_data: TenantCreate) -> Tenant:
|
||||||
existing = db.query(Tenant).filter(Tenant.tenant_name == tenant_data.tenant_name).first()
|
existing = db.query(Tenant).filter(Tenant.tenant_name == tenant_data.tenant_name).first()
|
||||||
@@ -217,7 +235,15 @@ class TenantService:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_all_tenants(db: Session):
|
def get_all_tenants(db: Session):
|
||||||
return db.query(Tenant).all()
|
tenants = db.query(Tenant).all()
|
||||||
|
changed = False
|
||||||
|
for tenant in tenants:
|
||||||
|
changed = TenantService._sync_tenant_lifecycle(tenant) or changed
|
||||||
|
if changed:
|
||||||
|
db.commit()
|
||||||
|
for tenant in tenants:
|
||||||
|
db.refresh(tenant)
|
||||||
|
return tenants
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_tenant_by_id(db: Session, tenant_id: uuid.UUID) -> Tenant:
|
def get_tenant_by_id(db: Session, tenant_id: uuid.UUID) -> Tenant:
|
||||||
@@ -227,6 +253,9 @@ class TenantService:
|
|||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
detail="Tenant not found"
|
detail="Tenant not found"
|
||||||
)
|
)
|
||||||
|
if TenantService._sync_tenant_lifecycle(tenant):
|
||||||
|
db.commit()
|
||||||
|
db.refresh(tenant)
|
||||||
return tenant
|
return tenant
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -518,6 +547,14 @@ class TenantService:
|
|||||||
offset = (page - 1) * page_size
|
offset = (page - 1) * page_size
|
||||||
tenants = query.offset(offset).limit(page_size).all()
|
tenants = query.offset(offset).limit(page_size).all()
|
||||||
|
|
||||||
|
changed = False
|
||||||
|
for tenant in tenants:
|
||||||
|
changed = TenantService._sync_tenant_lifecycle(tenant) or changed
|
||||||
|
if changed:
|
||||||
|
db.commit()
|
||||||
|
for tenant in tenants:
|
||||||
|
db.refresh(tenant)
|
||||||
|
|
||||||
total_pages = (total + page_size - 1) // page_size if total > 0 else 0
|
total_pages = (total + page_size - 1) // page_size if total > 0 else 0
|
||||||
|
|
||||||
return TenantPaginatedResponse(
|
return TenantPaginatedResponse(
|
||||||
@@ -526,4 +563,4 @@ class TenantService:
|
|||||||
page=page,
|
page=page,
|
||||||
page_size=page_size,
|
page_size=page_size,
|
||||||
total_pages=total_pages,
|
total_pages=total_pages,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user