in warehouse/src/on_order_events/main.py [0:0]
def get_diff(old_products: List[dict], new_products: List[dict]) -> Dict[str, dict]:
"""
Returns the difference between two lists of products
The difference contains three possible keys: 'created', 'deleted' or 'modified'.
"""
# Transform lists into dict
old = {p["productId"]: p for p in old_products}
new = {p["productId"]: p for p in new_products}
diff = {
"created": [],
"deleted": [],
"modified": []
}
for product_id, product in new.items():
if product_id not in old:
diff["created"].append(product)
continue
if product != old[product_id]:
# As DynamoDB put_item operations overwrite the whole item, we
# don't need to save the old item.
diff["modified"].append(product)
# Remove the item from old
del old[product_id]
# Since we delete values from 'old' as we encounter them, only values that
# are in 'old' but not in 'new' are left. Therefore, these were deleted
# from the original order.
diff["deleted"] = list(old.values())
return diff