from http import HTTPStatus from typing import Literal from aws_lambda_powertools.event_handler.api_gateway import Router from layercake.dynamodb import ( DynamoDBPersistenceLayer, SortKey, TransactKey, ) from pydantic import BaseModel from api_gateway import JSONResponse from boto3clients import dynamodb_client from config import USER_TABLE from rules.org import update_policies router = Router() user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client) @router.get('//policies', compress=True, tags=['Organization']) def get_policies(id: str): return user_layer.collection.get_items( TransactKey(id) + SortKey('metadata#billing_policy', remove_prefix='metadata#') + SortKey('metadata#payment_policy', remove_prefix='metadata#'), flatten_top=False, ) class BillingPolicy(BaseModel): billing_day: int payment_method: Literal['BANK_SLIP', 'MANUAL'] class PaymentPolicy(BaseModel): due_days: int class Policies(BaseModel): billing_policy: BillingPolicy | None = None payment_policy: PaymentPolicy | None = None @router.put('//policies', compress=True, tags=['Organization']) def put_policies(id: str, payload: Policies): payment_policy = payload.payment_policy billing_policy = payload.billing_policy update_policies( id, payment_policy=payment_policy.model_dump() if payment_policy else {}, billing_policy=billing_policy.model_dump() if billing_policy else {}, persistence_layer=user_layer, ) return JSONResponse( body=payload, status_code=HTTPStatus.OK, )