Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
warehouse: add newDate on new packaging requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nmoutschen committed Mar 18, 2020
1 parent b5d4c08 commit e298078
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion warehouse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ See [resources/events.yaml](resources/events.yaml) for a list of available event

## SSM Parameters

_None at the moment._
* `/ecommerce/{Environment}/warehouse/api/url`: URL for the API Gateway
1 change: 1 addition & 0 deletions warehouse/metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: warehouse
dependencies:
- platform
- users
parameters:
EventBusName: /ecommerce/{Environment}/platform/event-bus/name
9 changes: 7 additions & 2 deletions warehouse/src/on_order_events/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from typing import Dict, List, Optional
import boto3
from boto3.dynamodb.conditions import Key
from aws_lambda_powertools.tracing import Tracer
from aws_lambda_powertools.logging import logger_setup, logger_inject_lambda_context
from aws_lambda_powertools.tracing import Tracer # pylint: disable=import-error
from aws_lambda_powertools.logging import logger_setup, logger_inject_lambda_context # pylint: disable=import-error


ENVIRONMENT = os.environ["ENVIRONMENT"]
Expand Down Expand Up @@ -176,6 +176,11 @@ def save_metadata(order_id: str, modified_date: str, status: str = "NEW") -> Non
"status": status
}

# Inject newDate for new requests
# This allow to make a sparse projects in DynamoDB using a Local Secondary Index.
if status == "NEW":
item["newDate"] = modified_date

table.put_item(Item=item)


Expand Down
32 changes: 32 additions & 0 deletions warehouse/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Resources:
AttributeType: S
- AttributeName: productId
AttributeType: S
- AttributeName: newDate
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: orderId
Expand All @@ -68,6 +70,14 @@ Resources:
KeyType: RANGE
Projection:
ProjectionType: ALL
- IndexName: orderId-new
KeySchema:
- AttributeName: orderId
KeyType: HASH
- AttributeName: newDate
KeyType: RANGE
Projection:
ProjectionType: ALL
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES

Expand Down Expand Up @@ -160,6 +170,28 @@ Resources:
LogGroupName: !Sub "/aws/lambda/${TableUpdateFunction}"
RetentionInDays: !Ref RetentionInDays

###############
# API GATEWAY #
###############
# Api:
# Type: AWS::Serverless::Api
# Properties:
# DefinitionBody:
# Fn::Transform:
# Name: "AWS::Include"
# Parameters:
# Location: "resources/openapi.yaml"
# EndpointConfiguration: REGIONAL
# StageName: prod
# TracingEnabled: true

# ApiUrlParameter:
# Type: AWS::SSM::Parameter
# Properties:
# Name: !Sub /ecommerce/${Environment}/warehouse/api/url
# Type: String
# Value: !Sub "https://${Api}.execute-api.${AWS::Region}.amazonaws.com/prod"

#####################
# DEAD LETTER QUEUE #
#####################
Expand Down
7 changes: 4 additions & 3 deletions warehouse/tests/integ/test_on_order_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import boto3
from boto3.dynamodb.conditions import Key
import pytest
from helpers import get_parameter # pylint: disable=import-error
from helpers import get_parameter # pylint: disable=import-error,no-name-in-module


METADATA_KEY = "__metadata"
Expand Down Expand Up @@ -110,7 +110,8 @@ def metadata(order):
"status": "NEW",
"orderId": order["orderId"],
"productId": METADATA_KEY,
"modifiedDate": order["modifiedDate"]
"modifiedDate": order["modifiedDate"],
"newDate": order["modifiedDate"]
}

@pytest.fixture(scope="module")
Expand All @@ -131,7 +132,7 @@ def test_on_order_events(order_created, order_modified_products, order_modified_
"""

eventbridge = boto3.client("events")
table = boto3.resource("dynamodb").Table(table_name)
table = boto3.resource("dynamodb").Table(table_name) #pylint: disable=no-member

# Send the event on EventBridge
eventbridge.put_events(Entries=[order_created])
Expand Down
11 changes: 10 additions & 1 deletion warehouse/tests/unit/test_on_order_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,13 @@ def test_save_metadata(lambda_module, order_metadata):
Test save_metadata()
"""

item = copy.deepcopy(order_metadata)
item["newDate"] = order_metadata["modifiedDate"]

table = mock_table(
lambda_module.table, "put_item",
["orderId", "productId"],
items=order_metadata
items=item
)

lambda_module.save_metadata(
Expand Down Expand Up @@ -302,6 +305,8 @@ def test_on_order_created(lambda_module, order, order_products, order_metadata):
for product in order_products
]
)
order_metadata = copy.deepcopy(order_metadata)
order_metadata["newDate"] = order_metadata["modifiedDate"]
mock_table(
table, "put_item", ["orderId", "productId"],
table_name=lambda_module.table.name,
Expand Down Expand Up @@ -347,6 +352,8 @@ def test_on_order_modified_new(lambda_module, order, order_products, order_metad
for product in order_products
]
)
order_metadata = copy.deepcopy(order_metadata)
order_metadata["newDate"] = order_metadata["modifiedDate"]
mock_table(
table, "put_item", ["orderId", "productId"],
table_name=lambda_module.table.name,
Expand Down Expand Up @@ -441,6 +448,8 @@ def test_handler_created(lambda_module, context, order, order_products, order_me
for product in order_products
]
)
order_metadata = copy.deepcopy(order_metadata)
order_metadata["newDate"] = order_metadata["modifiedDate"]
mock_table(
table, "put_item", ["orderId", "productId"],
table_name=lambda_module.table.name,
Expand Down

0 comments on commit e298078

Please sign in to comment.