Remove `Subscriptions.C` attribute

This commit is contained in:
Chris Eager 2023-01-03 12:54:08 -06:00 committed by Chris Eager
parent 60edf4835f
commit 010f88a2ad
2 changed files with 13 additions and 33 deletions

View File

@ -46,8 +46,6 @@ public class SubscriptionManager {
public static final String KEY_USER = "U"; // B (Hash Key) public static final String KEY_USER = "U"; // B (Hash Key)
public static final String KEY_PASSWORD = "P"; // B public static final String KEY_PASSWORD = "P"; // B
@Deprecated
public static final String KEY_CUSTOMER_ID = "C"; // S (GSI Hash Key of `c_to_u` index)
public static final String KEY_PROCESSOR_ID_CUSTOMER_ID = "PC"; // B (GSI Hash Key of `pc_to_u` index) public static final String KEY_PROCESSOR_ID_CUSTOMER_ID = "PC"; // B (GSI Hash Key of `pc_to_u` index)
public static final String KEY_CREATED_AT = "R"; // N public static final String KEY_CREATED_AT = "R"; // N
public static final String KEY_PROCESSOR_CUSTOMER_IDS_MAP = "PCI"; // M public static final String KEY_PROCESSOR_CUSTOMER_IDS_MAP = "PCI"; // M
@ -59,7 +57,7 @@ public class SubscriptionManager {
public static final String KEY_CANCELED_AT = "B"; // N public static final String KEY_CANCELED_AT = "B"; // N
public static final String KEY_CURRENT_PERIOD_ENDS_AT = "D"; // N public static final String KEY_CURRENT_PERIOD_ENDS_AT = "D"; // N
public static final String INDEX_NAME = "c_to_u"; // Hash Key "C" public static final String INDEX_NAME = "pc_to_u"; // Hash Key "C"
public static class Record { public static class Record {
@ -188,26 +186,26 @@ public class SubscriptionManager {
/** /**
* Looks in the GSI for a record with the given customer id and returns the user id. * Looks in the GSI for a record with the given customer id and returns the user id.
*/ */
public CompletableFuture<byte[]> getSubscriberUserByStripeCustomerId(@Nonnull String customerId) { public CompletableFuture<byte[]> getSubscriberUserByProcessorCustomer(ProcessorCustomer processorCustomer) {
QueryRequest query = QueryRequest.builder() QueryRequest query = QueryRequest.builder()
.tableName(table) .tableName(table)
.indexName(INDEX_NAME) .indexName(INDEX_NAME)
.keyConditionExpression("#customer_id = :customer_id") .keyConditionExpression("#processor_customer_id = :processor_customer_id")
.projectionExpression("#user") .projectionExpression("#user")
.expressionAttributeNames(Map.of( .expressionAttributeNames(Map.of(
"#customer_id", KEY_CUSTOMER_ID, "#processor_customer_id", KEY_PROCESSOR_ID_CUSTOMER_ID,
"#user", KEY_USER)) "#user", KEY_USER))
.expressionAttributeValues(Map.of( .expressionAttributeValues(Map.of(
":customer_id", s(Objects.requireNonNull(customerId)))) ":processor_customer_id", b(processorCustomer.toDynamoBytes())))
.build(); .build();
return client.query(query).thenApply(queryResponse -> { return client.query(query).thenApply(queryResponse -> {
int count = queryResponse.count(); int count = queryResponse.count();
if (count == 0) { if (count == 0) {
return null; return null;
} else if (count > 1) { } else if (count > 1) {
logger.error("expected invariant of 1-1 subscriber-customer violated for customer {}", customerId); logger.error("expected invariant of 1-1 subscriber-customer violated for customer {}", processorCustomer);
throw new IllegalStateException( throw new IllegalStateException(
"expected invariant of 1-1 subscriber-customer violated for customer " + customerId); "expected invariant of 1-1 subscriber-customer violated for customer " + processorCustomer);
} else { } else {
Map<String, AttributeValue> result = queryResponse.items().get(0); Map<String, AttributeValue> result = queryResponse.items().get(0);
return result.get(KEY_USER).b().asByteArray(); return result.get(KEY_USER).b().asByteArray();
@ -328,14 +326,12 @@ public class SubscriptionManager {
"AND attribute_not_exists(#processors_to_customer_ids.#processor_name)" "AND attribute_not_exists(#processors_to_customer_ids.#processor_name)"
) )
.updateExpression("SET " .updateExpression("SET "
+ "#customer_id = :customer_id, "
+ "#processor_customer_id = :processor_customer_id, " + "#processor_customer_id = :processor_customer_id, "
+ "#processors_to_customer_ids.#processor_name = :customer_id, " + "#processors_to_customer_ids.#processor_name = :customer_id, "
+ "#accessed_at = :accessed_at" + "#accessed_at = :accessed_at"
) )
.expressionAttributeNames(Map.of( .expressionAttributeNames(Map.of(
"#accessed_at", KEY_ACCESSED_AT, "#accessed_at", KEY_ACCESSED_AT,
"#customer_id", KEY_CUSTOMER_ID,
"#processor_customer_id", KEY_PROCESSOR_ID_CUSTOMER_ID, "#processor_customer_id", KEY_PROCESSOR_ID_CUSTOMER_ID,
"#processor_name", activeProcessorCustomer.processor().name(), "#processor_name", activeProcessorCustomer.processor().name(),
"#processors_to_customer_ids", KEY_PROCESSOR_CUSTOMER_IDS_MAP "#processors_to_customer_ids", KEY_PROCESSOR_CUSTOMER_IDS_MAP

View File

@ -56,30 +56,12 @@ class SubscriptionManagerTest {
attributeName(SubscriptionManager.KEY_USER). attributeName(SubscriptionManager.KEY_USER).
attributeType(ScalarAttributeType.B). attributeType(ScalarAttributeType.B).
build()). build()).
attributeDefinition(AttributeDefinition.builder().
attributeName(SubscriptionManager.KEY_CUSTOMER_ID).
attributeType(ScalarAttributeType.S).
build()).
attributeDefinition(AttributeDefinition.builder(). attributeDefinition(AttributeDefinition.builder().
attributeName(SubscriptionManager.KEY_PROCESSOR_ID_CUSTOMER_ID). attributeName(SubscriptionManager.KEY_PROCESSOR_ID_CUSTOMER_ID).
attributeType(ScalarAttributeType.S). attributeType(ScalarAttributeType.B).
build()). build()).
globalSecondaryIndex(GlobalSecondaryIndex.builder(). globalSecondaryIndex(GlobalSecondaryIndex.builder().
indexName("c_to_u"). indexName(SubscriptionManager.INDEX_NAME).
keySchema(KeySchemaElement.builder().
attributeName(SubscriptionManager.KEY_CUSTOMER_ID).
keyType(KeyType.HASH).
build()).
projection(Projection.builder().
projectionType(ProjectionType.KEYS_ONLY).
build()).
provisionedThroughput(ProvisionedThroughput.builder().
readCapacityUnits(20L).
writeCapacityUnits(20L).
build()).
build()).
globalSecondaryIndex(GlobalSecondaryIndex.builder().
indexName("pc_to_u").
keySchema(KeySchemaElement.builder(). keySchema(KeySchemaElement.builder().
attributeName(SubscriptionManager.KEY_PROCESSOR_ID_CUSTOMER_ID). attributeName(SubscriptionManager.KEY_PROCESSOR_ID_CUSTOMER_ID).
keyType(KeyType.HASH). keyType(KeyType.HASH).
@ -193,7 +175,8 @@ class SubscriptionManagerTest {
// TODO test new customer ID with new processor does change the customer ID, once there is another processor // TODO test new customer ID with new processor does change the customer ID, once there is another processor
assertThat(subscriptionManager.getSubscriberUserByStripeCustomerId(customer)) assertThat(subscriptionManager.getSubscriberUserByProcessorCustomer(
new ProcessorCustomer(customer, SubscriptionProcessor.STRIPE)))
.succeedsWithin(Duration.ofSeconds(3)). .succeedsWithin(Duration.ofSeconds(3)).
isEqualTo(user); isEqualTo(user);
} }
@ -210,7 +193,8 @@ class SubscriptionManagerTest {
assertThat(subscriptionManager.updateProcessorAndCustomerId(userRecord, assertThat(subscriptionManager.updateProcessorAndCustomerId(userRecord,
new ProcessorCustomer(customer, SubscriptionProcessor.STRIPE), new ProcessorCustomer(customer, SubscriptionProcessor.STRIPE),
subscriptionUpdated)).succeedsWithin(Duration.ofSeconds(3)); subscriptionUpdated)).succeedsWithin(Duration.ofSeconds(3));
assertThat(subscriptionManager.getSubscriberUserByStripeCustomerId(customer)). assertThat(subscriptionManager.getSubscriberUserByProcessorCustomer(
new ProcessorCustomer(customer, SubscriptionProcessor.STRIPE))).
succeedsWithin(Duration.ofSeconds(3)). succeedsWithin(Duration.ofSeconds(3)).
isEqualTo(user); isEqualTo(user);
} }