Skip to content

Commit

Permalink
add ':force' option for policy sync
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaszimmermann committed Dec 24, 2024
1 parent 46a17ad commit bd32261
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
6 changes: 5 additions & 1 deletion app/server/api/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ async def create_policy(policy: PolicyIn) -> PolicyOut:

@router.post("/{policy_id}/sync", response_description="Policy synched onchain")
async def sync_policy(policy_id: str):
force = policy_id.endswith(":force")
if force:
policy_id = policy_id[:-6]

policy = find_in_collection(policy_id, PolicyOut)
sync_policy_onchain(policy)
sync_policy_onchain(policy, force)

@router.get("/{policy_id}", response_description="Policy data obtained")
async def get_policy(policy_id: str):
Expand Down
4 changes: 2 additions & 2 deletions app/server/sync/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# setup for module
logger = get_logger()

def sync_config_onchain(config: ConfigOut):
if config.tx:
def sync_config_onchain(config: ConfigOut, force: bool = False):
if not force and config.tx:
logger.info(f"config {config.id} already synched onchain (tx: {config.tx})")
return

Expand Down
4 changes: 2 additions & 2 deletions app/server/sync/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# setup for module
logger = get_logger()

def sync_location_onchain(location: LocationOut):
if location.tx:
def sync_location_onchain(location: LocationOut, force: bool = False):
if not force and location.tx:
logger.info(f"location {location.id} already synched onchain (tx: {location.tx})")
return

Expand Down
4 changes: 2 additions & 2 deletions app/server/sync/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# setup for module
logger = get_logger()

def sync_person_onchain(person: PersonOut):
if person.tx:
def sync_person_onchain(person: PersonOut, force: bool = False):
if not force and person.tx:
logger.info(f"person {person.id} already synched onchain (tx: {person.tx})")
return

Expand Down
14 changes: 7 additions & 7 deletions app/server/sync/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
# setup for module
logger = get_logger()

def sync_policy_onchain(policy: PolicyOut):
if policy.tx:
def sync_policy_onchain(policy: PolicyOut, force: bool = False):
if not force and policy.tx:
logger.info(f"policy {policy.id} already synced onchain (nft {policy.nft}")
return

logger.info(f"synching policy {policy.id} onchain")

# sync person if not yet done
person = find_in_collection(policy.personId, PersonOut)
sync_person_onchain(person)
sync_person_onchain(person, force)

# sync risk if not yet done
risk = find_in_collection(policy.riskId, RiskOut)
sync_risk_onchain(risk)
sync_risk_onchain(risk, force)

# execute transaction
policy_holder = person.wallet
Expand All @@ -43,8 +43,8 @@ def sync_policy_onchain(policy: PolicyOut):
logger.info(f"{tx} onchain policy {policy.id} created")

# update policy with tx and nft id
logs = product.get_logs({'fromBlock':'latest'})
log_policy = product.contract.events.LogCropPolicyCreated.process_log(logs[0])
policy.nft = log_policy.args['policyNftId']
# logs = product.get_logs({'fromBlock':'latest'})
# log_policy = product.contract.events.LogCropPolicyCreated.process_log(logs[0])
# policy.nft = log_policy.args['policyNftId']
policy.tx = tx
update_in_collection(policy, PolicyOut)
8 changes: 4 additions & 4 deletions app/server/sync/risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
# setup for module
logger = get_logger()

def sync_risk_onchain(risk: RiskOut):
if risk.tx:
def sync_risk_onchain(risk: RiskOut, force: bool = False):
if not force and risk.tx:
logger.info(f"risk {risk.id} already synched onchain (tx: {risk.tx})")
return

logger.info(f"synching risk {risk.id} onchain")

# sync configuration (season) if not yet done
config = find_in_collection(risk.configId, ConfigOut)
sync_config_onchain(config)
sync_config_onchain(config, force)

# sync risk if not yet done
location = find_in_collection(risk.locationId, LocationOut)
sync_location_onchain(location)
sync_location_onchain(location, force)

# execute transaction
id = product.toStr(risk.id)
Expand Down

0 comments on commit bd32261

Please sign in to comment.