123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- #
- # emulator of a Policy Management System
- #
- import os
- import json
- import requests
- import random
- import string
- from time import sleep
- from flask import current_app
- DELAY_SECONDS = 2
- POLICY_DATASET = 'pms/data/policies.json'
- activities = {
- 'reactivate': {
- "name": "Re-activate",
- "description": "Re-activate the policy",
- "fields": [
- {
- "name": "Re-activation Date",
- "brief": "Re-activation Date",
- "fieldVisibilityType": 1,
- "tooltip": "On what date should the re-activation be made?",
- "type": "Datum",
- "inputRange": [
- "2021-01-01",
- "2022-01-01"
- ],
- "onlyFromRange": False,
- "value": "",
- "inputTriggers": False,
- "isMandatory": True,
- }
- ]
- },
- 'cancel': {
- "name": "Cancel",
- "description": "Cancels the policy",
- "fields": [
- {
- "name": "Cancellation Reason",
- "brief": "Cancellation Reason",
- "fieldVisibilityType": 1,
- "tooltip": "Please select a reason for cancellation from the list of possible reasons for cancellation. After selecting the reason for cancellation, further input fields may appear",
- "type": "Text",
- "inputRange": [
- "9002 Omission",
- "9003 Bankruptcy",
- ],
- "onlyFromRange": False,
- "value": "9002 Omission",
- "inputTriggers": True,
- "isMandatory": True,
- },
- {
- "name": "Cancellation Date",
- "brief": "Cancellation Date",
- "fieldVisibilityType": 1,
- "tooltip": "On what date should the cancellation be made?",
- "type": "Datum",
- "inputRange": [
- "2021-01-01",
- "2022-01-01",
- ],
- "onlyFromRange": False,
- "value": "",
- "inputTriggers": False,
- "isMandatory": False,
- },
- ],
- },
- 'suspend': {
- "name": "Suspend",
- "description": "Suspend the policy",
- "fields": [
- {
- "name": "Suspension Reason",
- "brief": "Suspension Reason",
- "fieldVisibilityType": 1,
- "tooltip": "Please select a reason for suspension from the list of possible reasons.",
- "type": "Text",
- "inputRange": [
- "8002 No Payment",
- "8003 Vacation",
- "8005 Other"
- ],
- "onlyFromRange": False,
- "value": "8002 No Payment",
- "inputTriggers": True,
- "isMandatory": True,
- },
- {
- "name": "Cancellation Date",
- "brief": "Cancellation Date",
- "fieldVisibilityType": 1,
- "tooltip": "Date of the policy suspension",
- "type": "Datum",
- "inputRange": [
- "2021-01-01",
- "2022-01-01",
- ],
- "onlyFromRange": False,
- "value": "",
- "inputTriggers": False,
- "isMandatory": False,
- },
- ],
- },
- }
- activities_by_status = {
- 'active': [
- 'cancel',
- 'suspend',
- ],
- 'canceled': [],
- 'suspended': [
- 'reactivate'
- ]
- }
- class Stages:
- @classmethod
- def getAllStages(cls):
- #
- # returns available stages
- #
- return [
- 'development',
- 'production',
- ]
- def random_objects():
- with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data', 'objects.json'), 'r') as f:
- return json.load(f).get('objects')
- def get(policy_number, effective_date):
- #
- # returns policy from dataset
- #
- # load policies
- with open(POLICY_DATASET, 'r') as f:
- policies = json.load(f)
- # delay emulation
- # sleep(DELAY_SECONDS)
- request_number = policy_number.upper().replace('-', '')
- request_date = effective_date.replace('-', '')
- print(f'**** REQUEST POLICY: {request_number} {request_date}')
- for item in policies:
- if request_number == item['number'].replace('-',
- ''): # and request_date == item['effective_date'].replace('-', ''):
- # generate clauses
- objects = random_objects()
- item['clauses'] = [{
- 'name': random.choice(objects).capitalize(),
- 'number': clause,
- 'link': f'/api/clauses/{clause}',
- 'description': f'Description of clause {clause}',
- } for clause in
- (''.join(random.choice(string.digits) for _ in range(4)) + random.choice(string.ascii_uppercase) for i
- in range(random.randint(1, 5)))]
- return item
- def get_activities(status):
- #
- # returns possible activities based on status
- #
- return [activities[_] for _ in activities_by_status[status]]
- def execute_activity(policy_number, activity):
- #
- # executes activity
- #
- print('----> PMS: Execute Activity')
- print(f'----> PMS: Policy {policy_number}')
- # load policies
- with open(POLICY_DATASET, 'r') as f:
- policies = json.load(f)
- # delay emulation
- sleep(DELAY_SECONDS)
- statuses = {
- 'Re-activate': 'active',
- 'Cancel': 'canceled',
- 'Suspend': 'suspended',
- }
- # find policy
- for item in policies:
- if item['number'] == policy_number:
- print('----> PMS: Policy Found')
- # execute activity
- item['status'] = statuses.get(activity['name'])
- for field in activity['fields']:
- if field.get('value'):
- item['attributes'][field['name']] = field['value']
- # update policies
- with open(POLICY_DATASET, 'w') as f:
- json.dump(policies, f)
- return True
- return False
|