Skip to content

Commit

Permalink
Update children in parents' lists when the children are updated
Browse files Browse the repository at this point in the history
  • Loading branch information
markusjevringsesame committed Apr 30, 2024
1 parent 96d23e8 commit 2fcf145
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ abstract class AbstractEntityManager<T extends ApiResource & HasId> implements E
*/
protected static final String MAGIC_UPDATE_OPERATION = "__update";
protected final Map<String, T> entities = new HashMap<>();
protected final StripeEntities stripeEntities;
protected final Clock clock;

private final Class<T> entityClass;
private final String idPrefix;
private final int idLength;

protected AbstractEntityManager(Clock clock, Class<T> entityClass, String idPrefix, int idLength) {
protected AbstractEntityManager(StripeEntities stripeEntities, Clock clock, Class<T> entityClass, String idPrefix, int idLength) {
this.stripeEntities = stripeEntities;
this.clock = clock;
this.entityClass = entityClass;
this.idPrefix = idPrefix;
Expand Down Expand Up @@ -87,6 +89,8 @@ public final Optional<T> perform(String id, String operation, Map<String, Object
T postOperationEntity = perform(existingEntity, newEntity, operation, formData);
validate(postOperationEntity);
entities.put(id, postOperationEntity);
stripeEntities.updateLists(postOperationEntity);

// For now, there's nothing to do here. In reality we'd do stuff like trigger webhooks etc.
return Optional.of(postOperationEntity);
}
Expand Down Expand Up @@ -156,7 +160,6 @@ public Optional<T> delete(String id) throws ResponseCodeException {

@Override
public Optional<T> delete(String id, String stripeAccount, String parentEntityType, String parentEntityId) throws ResponseCodeException {
// Most entities do not support related sub-entities, so this is a reasonable default
// Most entities do not support related sub-entities, so this is a reasonable default
throw new UnsupportedOperationException(String.format("Entity %s does not support sub-entities. Attempted to delete under parent %s/%s",
getNormalizedEntityName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
import java.util.Optional;

class AccountManager extends AbstractEntityManager<Account> {
private final StripeEntities stripeEntities;

protected AccountManager(Clock clock, StripeEntities stripeEntities) {
super(clock, Account.class, "acct", 24);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, Account.class, "acct", 24);
}

@Override
Expand Down Expand Up @@ -45,6 +42,7 @@ protected Account initialize(Account account, Map<String, Object> formData, Stri
bankAccount.setAccount(account.getId());
externalAccounts.getData()
.add(bankAccount);
stripeEntities.bindChildToParentCollection(Account.class, account.getId(), "getExternalAccounts", bankAccount.getId());
}
return super.initialize(account, formData, stripeAccount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
import java.util.*;

class BalanceTransactionManager extends AbstractEntityManager<BalanceTransaction> {
private final Map<String, BalanceTransactionSource> sourcesByBalanceTransactionId = new HashMap<>();
private final StripeEntities stripeEntities;
private final Map<String, String> sourcesByBalanceTransactionId = new HashMap<>();

BalanceTransactionManager(Clock clock, StripeEntities stripeEntities) {
super(clock, BalanceTransaction.class, "txn", 24);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, BalanceTransaction.class, "txn", 24);
}

@Override
Expand All @@ -35,14 +33,14 @@ public Optional<BalanceTransaction> update(String id, Map<String, Object> formDa
@Override
public Optional<BalanceTransaction> get(String id, String stripeAccount) throws ResponseCodeException {
return Optional.ofNullable(sourcesByBalanceTransactionId.get(id))
.map(source -> BalanceTransactionMapper.toBalanceTransaction(source, stripeAccount));
.flatMap(stripeEntities::getEntityById)
.map(source -> BalanceTransactionMapper.toBalanceTransaction((BalanceTransactionSource) source, stripeAccount));
}

void register(String id, BalanceTransactionSource balanceTransactionSource) {
sourcesByBalanceTransactionId.put(id, balanceTransactionSource);
sourcesByBalanceTransactionId.put(id, balanceTransactionSource.getId());
}


@Override
public List<BalanceTransaction> list(QueryParameters query, String stripeAccount) throws ResponseCodeException {
if (stripeAccount != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

class BankAccountManager extends AbstractEntityManager<BankAccount> {
private final Map<String, String> providedBankAccountNumbers = new HashMap<>();
private final StripeEntities stripeEntities;

BankAccountManager(Clock clock, StripeEntities stripeEntities) {
super(clock, BankAccount.class, "ba", 24);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, BankAccount.class, "ba", 24);
}

@Override
Expand All @@ -32,6 +30,7 @@ public BankAccount add(Map<String, Object> formData, String stripeAccount, Strin
parentAccount.getExternalAccounts()
.getData()
.add(bankAccount);
stripeEntities.bindChildToParentCollection(Account.class, parentAccount.getId(), "getExternalAccounts", bankAccount.getId());
return bankAccount;
}

Expand Down Expand Up @@ -79,6 +78,7 @@ public Optional<BankAccount> delete(String id, String stripeAccount, String pare
parentAccount.getExternalAccounts()
.getData()
.remove(bankAccount);
stripeEntities.unbindChildFromParentCollection(Account.class, parentAccount.getId(), "getExternalAccounts", bankAccount.getId());

bankAccount.setDeleted(true);
return Optional.of(bankAccount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
import java.util.Map;

class ChargeManager extends AbstractEntityManager<Charge> {
private final StripeEntities stripeEntities;

ChargeManager(Clock clock, StripeEntities stripeEntities) {
super(clock, Charge.class, "ch", 24);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, Charge.class, "ch", 24);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
import java.util.Optional;

class CustomerManager extends AbstractEntityManager<Customer> {
private final StripeEntities stripeEntities;

protected CustomerManager(Clock clock, StripeEntities stripeEntities) {
super(clock, Customer.class, "cus", 14);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, Customer.class, "cus", 14);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
import java.util.Optional;

class InvoiceItemManager extends AbstractEntityManager<InvoiceItem> {
private final StripeEntities stripeEntities;

protected InvoiceItemManager(Clock clock, StripeEntities stripeEntities) {
super(clock, InvoiceItem.class, "ii", 24);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, InvoiceItem.class, "ii", 24);
}

@Override
Expand All @@ -29,6 +26,7 @@ protected InvoiceItem initialize(InvoiceItem invoiceItem, Map<String, Object> fo
invoice.getLines()
.getData()
.add(convertToLineItem(invoiceItem));
stripeEntities.bindChildToParentCollection(Invoice.class, invoiceId, "getLines", invoiceItem.getId());
}
return super.initialize(invoiceItem, formData, stripeAccount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
import java.util.Optional;

class InvoiceManager extends AbstractEntityManager<Invoice> {
private final StripeEntities stripeEntities;

protected InvoiceManager(Clock clock, StripeEntities stripeEntities) {
super(clock, Invoice.class, "in", 24);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, Invoice.class, "in", 24);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
import java.util.Objects;

class PaymentIntentManager extends AbstractEntityManager<PaymentIntent> {
private final StripeEntities stripeEntities;

PaymentIntentManager(Clock clock, StripeEntities stripeEntities) {
super(clock, PaymentIntent.class, "pi", 24);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, PaymentIntent.class, "pi", 24);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
class PaymentMethodManager extends AbstractEntityManager<PaymentMethod> {
// todo: test methods for things like charge_declined etc

private final StripeEntities stripeEntities;

PaymentMethodManager(Clock clock, StripeEntities stripeEntities) {
super(clock, PaymentMethod.class, "pm", 24);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, PaymentMethod.class, "pm", 24);
}

@Override
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/com/sesame/oss/stripemock/entities/PayoutManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,20 @@
import com.stripe.model.*;

import java.time.Clock;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

class PayoutManager extends AbstractEntityManager<Payout> {
private final Map<String, List<Payout>> byStripeAccount = new HashMap<>();
private final StripeEntities stripeEntities;
private final Map<String, Set<String>> stripeAccountToPayoutId = new HashMap<>();

PayoutManager(Clock clock, StripeEntities stripeEntities) {
super(clock, Payout.class, "po", 24);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, Payout.class, "po", 24);
}

@Override
public Payout add(Map<String, Object> formData, String stripeAccount) throws ResponseCodeException {
Payout payout = super.add(formData, stripeAccount);
byStripeAccount.computeIfAbsent(stripeAccount, ignored -> new ArrayList<>())
.add(payout);
stripeAccountToPayoutId.computeIfAbsent(stripeAccount, ignored -> new HashSet<>())
.add(payout.getId());
return payout;
}

Expand Down Expand Up @@ -130,13 +125,17 @@ public List<Payout> list(QueryParameters query, String stripeAccount) {
.stream()
.toList();
} else {
return byStripeAccount.getOrDefault(stripeAccount, new ArrayList<>());
Set<String> payoutIdsForStripeAccount = stripeAccountToPayoutId.getOrDefault(stripeAccount, new HashSet<>());
return entities.values()
.stream()
.filter(payout -> payoutIdsForStripeAccount.contains(payout.getId()))
.toList();
}
}

@Override
public void clear() {
super.clear();
byStripeAccount.clear();
stripeAccountToPayoutId.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.util.Optional;

class ProductManager extends AbstractEntityManager<Product> {
protected ProductManager(Clock clock) {
super(clock, Product.class, "prod", 24);
protected ProductManager(Clock clock, StripeEntities stripeEntities) {
super(stripeEntities, clock, Product.class, "prod", 24);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
import java.util.Objects;

class RefundManager extends AbstractEntityManager<Refund> {
private final StripeEntities stripeEntities;

protected RefundManager(Clock clock, StripeEntities stripeEntities) {
super(clock, Refund.class, "re", 24);
this.stripeEntities = stripeEntities;
super(stripeEntities, clock, Refund.class, "re", 24);
}

@Override
Expand All @@ -41,6 +38,7 @@ protected Refund initialize(Refund refund, Map<String, Object> formData, String
charge.getRefunds()
.getData()
.add(refund);
stripeEntities.bindChildToParentCollection(Charge.class, charge.getId(), "getRefunds", refund.getId());
if (refund.getAmount() == null) {
refund.setAmount(charge.getAmount());
charge.setAmountRefunded(charge.getAmount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.Map;

class SetupIntentManager extends AbstractEntityManager<SetupIntent> {
protected SetupIntentManager(Clock clock) {
super(clock, SetupIntent.class, "seti", 24);
protected SetupIntentManager(Clock clock, StripeEntities stripeEntities) {
super(stripeEntities, clock, SetupIntent.class, "seti", 24);
}

@Override
Expand Down
Loading

0 comments on commit 2fcf145

Please sign in to comment.