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_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_CREATED_AT = "R"; // N
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_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 {
@ -188,26 +186,26 @@ public class SubscriptionManager {
/**
* 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()
.tableName(table)
.indexName(INDEX_NAME)
.keyConditionExpression("#customer_id = :customer_id")
.keyConditionExpression("#processor_customer_id = :processor_customer_id")
.projectionExpression("#user")
.expressionAttributeNames(Map.of(
"#customer_id", KEY_CUSTOMER_ID,
"#processor_customer_id", KEY_PROCESSOR_ID_CUSTOMER_ID,
"#user", KEY_USER))
.expressionAttributeValues(Map.of(
":customer_id", s(Objects.requireNonNull(customerId))))
":processor_customer_id", b(processorCustomer.toDynamoBytes())))
.build();
return client.query(query).thenApply(queryResponse -> {
int count = queryResponse.count();
if (count == 0) {
return null;
} 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(
"expected invariant of 1-1 subscriber-customer violated for customer " + customerId);
"expected invariant of 1-1 subscriber-customer violated for customer " + processorCustomer);
} else {
Map<String, AttributeValue> result = queryResponse.items().get(0);
return result.get(KEY_USER).b().asByteArray();
@ -328,14 +326,12 @@ public class SubscriptionManager {
"AND attribute_not_exists(#processors_to_customer_ids.#processor_name)"
)
.updateExpression("SET "
+ "#customer_id = :customer_id, "
+ "#processor_customer_id = :processor_customer_id, "
+ "#processors_to_customer_ids.#processor_name = :customer_id, "
+ "#accessed_at = :accessed_at"
)
.expressionAttributeNames(Map.of(
"#accessed_at", KEY_ACCESSED_AT,
"#customer_id", KEY_CUSTOMER_ID,
"#processor_customer_id", KEY_PROCESSOR_ID_CUSTOMER_ID,
"#processor_name", activeProcessorCustomer.processor().name(),
"#processors_to_customer_ids", KEY_PROCESSOR_CUSTOMER_IDS_MAP

View File

@ -56,30 +56,12 @@ class SubscriptionManagerTest {
attributeName(SubscriptionManager.KEY_USER).
attributeType(ScalarAttributeType.B).
build()).
attributeDefinition(AttributeDefinition.builder().
attributeName(SubscriptionManager.KEY_CUSTOMER_ID).
attributeType(ScalarAttributeType.S).
build()).
attributeDefinition(AttributeDefinition.builder().
attributeName(SubscriptionManager.KEY_PROCESSOR_ID_CUSTOMER_ID).
attributeType(ScalarAttributeType.S).
attributeType(ScalarAttributeType.B).
build()).
globalSecondaryIndex(GlobalSecondaryIndex.builder().
indexName("c_to_u").
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").
indexName(SubscriptionManager.INDEX_NAME).
keySchema(KeySchemaElement.builder().
attributeName(SubscriptionManager.KEY_PROCESSOR_ID_CUSTOMER_ID).
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
assertThat(subscriptionManager.getSubscriberUserByStripeCustomerId(customer))
assertThat(subscriptionManager.getSubscriberUserByProcessorCustomer(
new ProcessorCustomer(customer, SubscriptionProcessor.STRIPE)))
.succeedsWithin(Duration.ofSeconds(3)).
isEqualTo(user);
}
@ -210,7 +193,8 @@ class SubscriptionManagerTest {
assertThat(subscriptionManager.updateProcessorAndCustomerId(userRecord,
new ProcessorCustomer(customer, SubscriptionProcessor.STRIPE),
subscriptionUpdated)).succeedsWithin(Duration.ofSeconds(3));
assertThat(subscriptionManager.getSubscriberUserByStripeCustomerId(customer)).
assertThat(subscriptionManager.getSubscriberUserByProcessorCustomer(
new ProcessorCustomer(customer, SubscriptionProcessor.STRIPE))).
succeedsWithin(Duration.ofSeconds(3)).
isEqualTo(user);
}