__init__.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #
  2. # emulator of a Policy Management System
  3. #
  4. import os
  5. import json
  6. import requests
  7. import random
  8. import string
  9. from time import sleep
  10. from flask import current_app
  11. DELAY_SECONDS = 2
  12. POLICY_DATASET = 'pms/data/policies.json'
  13. activities = {
  14. 'reactivate': {
  15. "name": "Re-activate",
  16. "description": "Re-activate the policy",
  17. "fields": [
  18. {
  19. "name": "Re-activation Date",
  20. "brief": "Re-activation Date",
  21. "fieldVisibilityType": 1,
  22. "tooltip": "On what date should the re-activation be made?",
  23. "type": "Datum",
  24. "inputRange": [
  25. "2021-01-01",
  26. "2022-01-01"
  27. ],
  28. "onlyFromRange": False,
  29. "value": "",
  30. "inputTriggers": False,
  31. "isMandatory": True,
  32. }
  33. ]
  34. },
  35. 'cancel': {
  36. "name": "Cancel",
  37. "description": "Cancels the policy",
  38. "fields": [
  39. {
  40. "name": "Cancellation Reason",
  41. "brief": "Cancellation Reason",
  42. "fieldVisibilityType": 1,
  43. "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",
  44. "type": "Text",
  45. "inputRange": [
  46. "9002 Omission",
  47. "9003 Bankruptcy",
  48. ],
  49. "onlyFromRange": False,
  50. "value": "9002 Omission",
  51. "inputTriggers": True,
  52. "isMandatory": True,
  53. },
  54. {
  55. "name": "Cancellation Date",
  56. "brief": "Cancellation Date",
  57. "fieldVisibilityType": 1,
  58. "tooltip": "On what date should the cancellation be made?",
  59. "type": "Datum",
  60. "inputRange": [
  61. "2021-01-01",
  62. "2022-01-01",
  63. ],
  64. "onlyFromRange": False,
  65. "value": "",
  66. "inputTriggers": False,
  67. "isMandatory": False,
  68. },
  69. ],
  70. },
  71. 'suspend': {
  72. "name": "Suspend",
  73. "description": "Suspend the policy",
  74. "fields": [
  75. {
  76. "name": "Suspension Reason",
  77. "brief": "Suspension Reason",
  78. "fieldVisibilityType": 1,
  79. "tooltip": "Please select a reason for suspension from the list of possible reasons.",
  80. "type": "Text",
  81. "inputRange": [
  82. "8002 No Payment",
  83. "8003 Vacation",
  84. "8005 Other"
  85. ],
  86. "onlyFromRange": False,
  87. "value": "8002 No Payment",
  88. "inputTriggers": True,
  89. "isMandatory": True,
  90. },
  91. {
  92. "name": "Cancellation Date",
  93. "brief": "Cancellation Date",
  94. "fieldVisibilityType": 1,
  95. "tooltip": "Date of the policy suspension",
  96. "type": "Datum",
  97. "inputRange": [
  98. "2021-01-01",
  99. "2022-01-01",
  100. ],
  101. "onlyFromRange": False,
  102. "value": "",
  103. "inputTriggers": False,
  104. "isMandatory": False,
  105. },
  106. ],
  107. },
  108. }
  109. activities_by_status = {
  110. 'active': [
  111. 'cancel',
  112. 'suspend',
  113. ],
  114. 'canceled': [],
  115. 'suspended': [
  116. 'reactivate'
  117. ]
  118. }
  119. class Stages:
  120. @classmethod
  121. def getAllStages(cls):
  122. #
  123. # returns available stages
  124. #
  125. return [
  126. 'development',
  127. 'production',
  128. ]
  129. def random_objects():
  130. with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data', 'objects.json'), 'r') as f:
  131. return json.load(f).get('objects')
  132. def get(policy_number, effective_date):
  133. #
  134. # returns policy from dataset
  135. #
  136. # load policies
  137. with open(POLICY_DATASET, 'r') as f:
  138. policies = json.load(f)
  139. # delay emulation
  140. # sleep(DELAY_SECONDS)
  141. request_number = policy_number.upper().replace('-', '')
  142. request_date = effective_date.replace('-', '')
  143. print(f'**** REQUEST POLICY: {request_number} {request_date}')
  144. for item in policies:
  145. if request_number == item['number'].replace('-',
  146. ''): # and request_date == item['effective_date'].replace('-', ''):
  147. # generate clauses
  148. objects = random_objects()
  149. item['clauses'] = [{
  150. 'name': random.choice(objects).capitalize(),
  151. 'number': clause,
  152. 'link': f'/api/clauses/{clause}',
  153. 'description': f'Description of clause {clause}',
  154. } for clause in
  155. (''.join(random.choice(string.digits) for _ in range(4)) + random.choice(string.ascii_uppercase) for i
  156. in range(random.randint(1, 5)))]
  157. return item
  158. def get_activities(status):
  159. #
  160. # returns possible activities based on status
  161. #
  162. return [activities[_] for _ in activities_by_status[status]]
  163. def execute_activity(policy_number, activity):
  164. #
  165. # executes activity
  166. #
  167. print('----> PMS: Execute Activity')
  168. print(f'----> PMS: Policy {policy_number}')
  169. # load policies
  170. with open(POLICY_DATASET, 'r') as f:
  171. policies = json.load(f)
  172. # delay emulation
  173. sleep(DELAY_SECONDS)
  174. statuses = {
  175. 'Re-activate': 'active',
  176. 'Cancel': 'canceled',
  177. 'Suspend': 'suspended',
  178. }
  179. # find policy
  180. for item in policies:
  181. if item['number'] == policy_number:
  182. print('----> PMS: Policy Found')
  183. # execute activity
  184. item['status'] = statuses.get(activity['name'])
  185. for field in activity['fields']:
  186. if field.get('value'):
  187. item['attributes'][field['name']] = field['value']
  188. # update policies
  189. with open(POLICY_DATASET, 'w') as f:
  190. json.dump(policies, f)
  191. return True
  192. return False