Skip to content

Commit d42dc1f

Browse files
committed
fix(contracts): Add conditional check for contract status updates
1 parent 0e92817 commit d42dc1f

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

Unicorn.Contracts/ContractsService/ContractEventHandler.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,18 @@ private async Task UpdateContractAsync(SQSEvent.SQSMessage sqsMessage)
205205
{ "PropertyId", new AttributeValue { S = updateContractRequest.PropertyId } }
206206
},
207207
UpdateExpression = "SET #contract_status=:cs, #modified_date=:md",
208+
ConditionExpression = "attribute_exists(#property_id) AND #contract_status = :draft_status",
208209
ExpressionAttributeNames = new Dictionary<string, string>
209210
{
211+
{ "#property_id", "PropertyId" },
210212
{ "#modified_date", "ContractLastModifiedOn" },
211213
{ "#contract_status", "ContractStatus" }
212214
},
213215
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
214216
{
215217
{ ":md", new AttributeValue { S = DateTime.Now.ToString("O") } },
216-
{ ":cs", new AttributeValue { S = ContractStatus.Approved } }
218+
{ ":cs", new AttributeValue { S = ContractStatus.Approved } },
219+
{ ":draft_status", new AttributeValue { S = ContractStatus.Draft } }
217220
},
218221
ReturnValues = "UPDATED_NEW"
219222
};
@@ -222,6 +225,10 @@ private async Task UpdateContractAsync(SQSEvent.SQSMessage sqsMessage)
222225

223226
Logger.LogInformation(response);
224227
}
228+
catch (ConditionalCheckFailedException e)
229+
{
230+
Logger.LogError("Unable to update contract, because `{e.Message}`. Perhaps you are trying to add a contract that already has an active status?", e.Message);
231+
}
225232
catch (Exception e)
226233
{
227234
Logger.LogError(e.Message);

0 commit comments

Comments
 (0)