Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7e10588
Version2 commit
Aug 23, 2023
f5cdc9b
Version2 commit - Refactoring
Aug 24, 2023
3a0776f
Version2 commit - Refactoring
Aug 29, 2023
bd2f185
ConditionExpression : Added
Sep 4, 2023
33c41b8
fix: Update condition expression
Sep 4, 2023
70c59dd
feat: updated contracts
sliedig Sep 4, 2023
00a6808
feat: updated web
sliedig Sep 4, 2023
2fa991e
fix: updated gitignore
sliedig Sep 4, 2023
41a009d
feat: updated Properties
sliedig Sep 4, 2023
224eabc
fix: updated properties temp
sliedig Sep 5, 2023
5a3ab14
Merge pull request #1 from sliedig/v2
sankeyraut Sep 5, 2023
915eeff
fix: unicorn_property refactoring
Sep 6, 2023
616f4c7
fix: based on changes requested for web
Sep 6, 2023
cdd99c8
fix: Dynamodb read write permissions
Sep 6, 2023
5750cbb
status changed to PENDING
Sep 6, 2023
4d16bb5
chore: Stage values are now lowercase. Update parameters to match nam…
sliedig Sep 7, 2023
0ea44be
Merge pull request #3 from sliedig/v2
sankeyraut Sep 8, 2023
9dbb6c2
impl:
Sep 11, 2023
bd44cc3
fix: Removed pascalCase Power tool variable
Sep 11, 2023
63d8c56
chore: Comments
Sep 11, 2023
0caf402
fix: Sync naming Conventions
Sep 11, 2023
260b4ec
fix: samconfig for contract service
Sep 11, 2023
dfb2edd
chore: Formatting and standardization
Sep 11, 2023
3c70faa
chore: Sync and Conventions
Sep 11, 2023
7907530
fix: Change resource name
Sep 12, 2023
cfa5467
Merge pull request #33 from sankeyraut/v2
sliedig Sep 13, 2023
bd203ef
fix: renaming the template file
Sep 13, 2023
b1e818d
refactor: Rebase templates with Python v2
igorlg Sep 13, 2023
7bf4a3b
fix: Replace template.yaml commented-out nested stack by commenting t…
igorlg Sep 13, 2023
2d51ae2
fix: Replace template.yaml commented-out nested stack by commenting t…
igorlg Sep 13, 2023
e0e3127
fix: Change API Gateway Integration Role resource name in API Spec
igorlg Sep 13, 2023
db7c831
chore: Add note on Unicorn Properties readme
igorlg Sep 13, 2023
98263a9
fix: remove unused SAM stack configs, add missing CAPABILITY_AUTOEXPA…
igorlg Sep 13, 2023
90649e4
fix: Equalise Unicorn.Web templates with other languages
igorlg Sep 13, 2023
f320909
fix: Equalise unicorn_web templates with other languages
igorlg Sep 13, 2023
9be657c
bug: unicorn_web requestApproval should only filter out properties in…
igorlg Sep 13, 2023
7105f79
test: add PublicationEvaluationCompleted event payload to unicorn_web
igorlg Sep 13, 2023
f074ef4
fix: Updated address field
Sep 13, 2023
09e5b1f
chore: leave PublicationApprovalRequested subscription rule commented…
igorlg Sep 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
unicorn_properties/samconfig.toml
unicorn_properties/PropertyFunctions/target/*
unicorn_contracts/samconfig.toml
unicorn_contracts/ContractsFunction/target/**
unicorn_web/samconfig.toml
unicorn_web/PropertyFunctions/target/**
**/.aws-sam/
.DS_Store**
Expand Down
8 changes: 8 additions & 0 deletions unicorn_contracts/ContractsFunction/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package contracts;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import com.amazonaws.services.lambda.runtime.events.SQSEvent;
import com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import contracts.utils.Contract;

import contracts.utils.ContractStatusEnum;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest;
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
import software.amazon.lambda.powertools.metrics.MetricsUtils;
import software.amazon.lambda.powertools.tracing.Tracing;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class ContractEventHandler implements RequestHandler<SQSEvent, Void> {

// Initialise environment variables
private static String DDB_TABLE = System.getenv("DYNAMODB_TABLE");
ObjectMapper objectMapper = new ObjectMapper();

DynamoDbClient dynamodbClient = DynamoDbClient.builder()
.build();

Logger logger = LogManager.getLogger();
MetricsLogger metricsLogger = MetricsUtils.metricsLogger();

@Override
public Void handleRequest(SQSEvent event, Context context) {

for (SQSMessage msg : event.getRecords()) {
// cehck in message attributes about the http method (HttpMethod)
logger.debug(msg.toString());
String httpMethod = msg.getMessageAttributes().get("HttpMethod").getStringValue();
if ("POST".equalsIgnoreCase(httpMethod)) {
try {
createContract(msg.getBody());
logger.debug("Contract Saved");
} catch (JsonProcessingException jsonException) {
logger.error("Unknown Exception occoured: " + jsonException.getMessage());
logger.fatal(jsonException);
jsonException.printStackTrace();
}

} else if ("PUT".equalsIgnoreCase(httpMethod)) {
try {
// update the event
updateContract(msg.getBody());
} catch (JsonProcessingException jsonException) {
logger.error("Unknown Exception occoured: " + jsonException.getMessage());
logger.fatal(jsonException);
jsonException.printStackTrace();
}

}

}
return null;
}

@Tracing
private void createContract(String strContract) throws JsonProcessingException {
String contractId = UUID.randomUUID().toString();
Long createDate = new Date().getTime();
Contract contract = objectMapper.readValue(strContract, Contract.class);

Map<String, AttributeValue> expressionValues = new HashMap<>();
expressionValues.put(":cancelled", AttributeValue.builder().s(ContractStatusEnum.CANCELLED.name()).build());
expressionValues.put(":closed", AttributeValue.builder().s(ContractStatusEnum.CLOSED.name()).build());
expressionValues.put(":expired", AttributeValue.builder().s(ContractStatusEnum.EXPIRED.name()).build());

HashMap<String, AttributeValue> itemValues = new HashMap<>();
itemValues.put("property_id", AttributeValue.builder().s(contract.getPropertyId()).build());
itemValues.put("seller_name", AttributeValue.builder().s(contract.getSellerName()).build());
itemValues.put("contract_created",
AttributeValue.builder().n(createDate.toString()).build());
itemValues.put("contract_last_modified_on",
AttributeValue.builder().n(createDate.toString()).build());
itemValues.put("contract_id", AttributeValue.builder().s(contractId).build());
itemValues.put("contract_status", AttributeValue.builder().s(ContractStatusEnum.DRAFT.name()).build());

HashMap<String, AttributeValue> address = new HashMap<>();
address.put("country", AttributeValue.builder().s(contract.getAddress().getCountry()).build());
address.put("city", AttributeValue.builder().s(contract.getAddress().getCity()).build());
address.put("street", AttributeValue.builder().s(contract.getAddress().getStreet()).build());
address.put("number",
AttributeValue.builder().n(Integer.valueOf(contract.getAddress().getNumber()).toString()).build());

itemValues.put("address",
AttributeValue.builder().m(address).build());
PutItemRequest putItemRequest = PutItemRequest.builder().tableName(DDB_TABLE)
.item(itemValues)
.conditionExpression(
"attribute_not_exists(property_id) OR contract_status IN (:cancelled , :closed, :expired)")
.expressionAttributeValues(expressionValues)
.build();
try {
dynamodbClient.putItem(putItemRequest);
} catch (ConditionalCheckFailedException conditionalCheckFailedException) {
logger.error("Unable to create contract for Property '" + contract.getPropertyId()
+ "'.There already is a contract for this property in status " + ContractStatusEnum.DRAFT + " or "
+ ContractStatusEnum.APPROVED);
}
}

@Tracing
private void updateContract(String strContract) throws JsonProcessingException {
Contract contract = objectMapper.readValue(strContract, Contract.class);
logger.info("Property ID is : " + contract.getPropertyId());
HashMap<String, AttributeValue> itemKey = new HashMap<>();

itemKey.put("property_id", AttributeValue.builder().s(contract.getPropertyId()).build());

Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":draft", AttributeValue.builder().s(ContractStatusEnum.DRAFT.name()).build());
expressionAttributeValues.put(":t", AttributeValue.builder().s(ContractStatusEnum.APPROVED.name()).build());
expressionAttributeValues.put(":m", AttributeValue.builder().s(String.valueOf(new Date().getTime())).build());

UpdateItemRequest request = UpdateItemRequest.builder()
.tableName(DDB_TABLE)
.key(itemKey)
.updateExpression("set contract_status=:t, modified_date=:m")
.expressionAttributeValues(expressionAttributeValues)
.conditionExpression(
"attribute_exists(property_id) AND contract_status IN (:draft)")
.build();
try {
dynamodbClient.updateItem(request);
} catch (ConditionalCheckFailedException conditionalCheckFailedException) {
logger.error("Unable to update contract for Property '" + contract.getPropertyId()
+ "'.Status is not in " + ContractStatusEnum.DRAFT);
} catch (ResourceNotFoundException conditionalCheckFailedException) {
logger.error("Unable to update contract for Property '" + contract.getPropertyId()
+ "'. Not Found");
}
}

public void setDynamodbClient(DynamoDbClient dynamodbClient) {
this.dynamodbClient = dynamodbClient;
}

}

This file was deleted.

Loading