|
34 | 34 | table = dynamodb.Table(DYNAMODB_TABLE) # type: ignore |
35 | 35 |
|
36 | 36 |
|
37 | | -@metrics.log_metrics(capture_cold_start_metric=True) # type: ignore |
| 37 | +@metrics.log_metrics(capture_cold_start_metric=True) # type: ignore |
38 | 38 | @logger.inject_lambda_context |
39 | 39 | @tracer.capture_method |
40 | 40 | @event_source(data_class=SQSEvent) |
41 | 41 | def lambda_handler(event: SQSEvent, context: LambdaContext): |
42 | 42 | # Multiple records can be delivered in a single event |
43 | 43 | for record in event.records: |
44 | | - http_method = record.message_attributes.get('HttpMethod', {}).get('stringValue') |
| 44 | + http_method = record.message_attributes.get("HttpMethod", {}).get("stringValue") |
45 | 45 |
|
46 | | - if http_method == 'POST': |
| 46 | + if http_method == "POST": |
47 | 47 | create_contract(record.json_body) |
48 | | - elif http_method == 'PUT': |
| 48 | + elif http_method == "PUT": |
49 | 49 | update_contract(record.json_body) |
50 | 50 | else: |
51 | | - raise Exception(f'Unable to handle HttpMethod {http_method}') |
| 51 | + raise Exception(f"Unable to handle HttpMethod {http_method}") |
52 | 52 |
|
53 | 53 |
|
54 | 54 | @tracer.capture_method |
@@ -78,37 +78,39 @@ def create_contract(event: dict) -> None: |
78 | 78 |
|
79 | 79 | current_date = datetime.now().strftime("%d/%m/%Y %H:%M:%S") |
80 | 80 | contract = { |
81 | | - "property_id": event["property_id"], # PK |
82 | | - "address": event["address"], |
83 | | - "seller_name": event["seller_name"], |
84 | | - "contract_created": current_date, |
85 | | - "contract_last_modified_on": current_date, |
86 | | - "contract_id": str(uuid.uuid4()), |
87 | | - "contract_status": ContractStatus.DRAFT.name, |
| 81 | + "property_id": event["property_id"], # PK |
| 82 | + "address": event["address"], |
| 83 | + "seller_name": event["seller_name"], |
| 84 | + "contract_created": current_date, |
| 85 | + "contract_last_modified_on": current_date, |
| 86 | + "contract_id": str(uuid.uuid4()), |
| 87 | + "contract_status": ContractStatus.DRAFT.name, |
88 | 88 | } |
89 | 89 |
|
90 | 90 | logger.info(msg={"Creating contract": contract, "From event": event}) |
91 | 91 |
|
92 | 92 | try: |
93 | 93 | response = table.put_item( |
94 | 94 | Item=contract, |
95 | | - ConditionExpression= |
96 | | - Attr('property_id').not_exists() |
97 | | - | Attr('contract_status').is_in([ |
98 | | - ContractStatus.CANCELLED.name, |
99 | | - ContractStatus.CLOSED.name, |
100 | | - ContractStatus.EXPIRED.name, |
101 | | - ])) |
| 95 | + ConditionExpression=Attr("property_id").not_exists() |
| 96 | + | Attr("contract_status").is_in( |
| 97 | + [ |
| 98 | + ContractStatus.CANCELLED.name, |
| 99 | + ContractStatus.CLOSED.name, |
| 100 | + ContractStatus.EXPIRED.name, |
| 101 | + ] |
| 102 | + ), |
| 103 | + ) |
102 | 104 | logger.info(f'var:response - "{response}"') |
103 | | - |
| 105 | + |
104 | 106 | # Annotate trace with contract status |
105 | 107 | tracer.put_annotation(key="ContractStatus", value=contract["contract_status"]) |
106 | 108 |
|
107 | 109 | except ClientError as e: |
108 | 110 | code = e.response["Error"]["Code"] |
109 | | - if code == 'ConditionalCheckFailedException': |
| 111 | + if code == "ConditionalCheckFailedException": |
110 | 112 | logger.info(f""" |
111 | | - Unable to create contract for Property {contract['property_id']}. |
| 113 | + Unable to create contract for Property {contract["property_id"]}. |
112 | 114 | There already is a contract for this property in status {ContractStatus.DRAFT.name} or {ContractStatus.APPROVED.name} |
113 | 115 | """) |
114 | 116 | else: |
@@ -149,29 +151,27 @@ def update_contract(contract: dict) -> None: |
149 | 151 |
|
150 | 152 | response = table.update_item( |
151 | 153 | Key={ |
152 | | - 'property_id': contract['property_id'], |
| 154 | + "property_id": contract["property_id"], |
153 | 155 | }, |
154 | 156 | UpdateExpression="set contract_status=:t, modified_date=:m", |
155 | | - ConditionExpression= |
156 | | - Attr('property_id').exists() |
157 | | - & Attr('contract_status').is_in([ |
158 | | - ContractStatus.DRAFT.name |
159 | | - ]), |
| 157 | + ConditionExpression=Attr("property_id").exists() |
| 158 | + & Attr("contract_status").is_in([ContractStatus.DRAFT.name]), |
160 | 159 | ExpressionAttributeValues={ |
161 | | - ':t': contract['contract_status'], |
162 | | - ':m': current_date, |
| 160 | + ":t": contract["contract_status"], |
| 161 | + ":m": current_date, |
163 | 162 | }, |
164 | | - ReturnValues="UPDATED_NEW") |
| 163 | + ReturnValues="UPDATED_NEW", |
| 164 | + ) |
165 | 165 | logger.info(f'var:response - "{response}"') |
166 | | - |
| 166 | + |
167 | 167 | # Annotate trace with contract status |
168 | 168 | tracer.put_annotation(key="ContractStatus", value=contract["contract_status"]) |
169 | 169 |
|
170 | 170 | except ClientError as e: |
171 | 171 | code = e.response["Error"]["Code"] |
172 | | - if code == 'ConditionalCheckFailedException': |
| 172 | + if code == "ConditionalCheckFailedException": |
173 | 173 | logger.exception(f"Unable to update contract Id {contract['property_id']}. Status is not in status DRAFT") |
174 | | - elif code == 'ResourceNotFoundException': |
| 174 | + elif code == "ResourceNotFoundException": |
175 | 175 | logger.exception(f"Unable to update contract Id {contract['property_id']}. Not Found") |
176 | 176 | else: |
177 | 177 | raise e |
0 commit comments