mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 16:07:15 +02:00
Make GracePeriod OneTime vs Recurring logic explicit
This CL cleans up some old crufy logic in GracePeriod that overloaded methods to accept either OneTime or Recurring billing events refs, despite storing them in separate fields in the entity (since BillingEvent is not a polymorphic superclass, just a Java-only one, you can't store them as refs to BillingEvent). That overloading was ultimately only there as a convenience/hack from when we added Recurring events and didn't want to go back and change everything. It obfuscates what's really going on, requires extra casting/loss of type-safety, and relies on indirect signals (e.g. the grace period type being AUTO_RENEW) to guess what the right billing event type is. That latter aspect will likely no longer work in a monthly billing world, and was brittle in any case. A coming CL will rip the same logic out of Cancellation. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=117164286
This commit is contained in:
parent
d006506531
commit
9786e6732f
10 changed files with 186 additions and 38 deletions
|
@ -110,11 +110,10 @@ public class DomainDeleteFlow extends ResourceSyncDeleteFlow<DomainResource, Bui
|
|||
.setDeletionTime(deletionTime)
|
||||
// Clear out all old grace periods and add REDEMPTION, which does not include a ref
|
||||
// to a billing event because there isn't one for a domain delete.
|
||||
.setGracePeriods(ImmutableSet.of(GracePeriod.create(
|
||||
.setGracePeriods(ImmutableSet.of(GracePeriod.createWithoutBillingEvent(
|
||||
GracePeriodStatus.REDEMPTION,
|
||||
now.plus(registry.getRedemptionGracePeriodLength()),
|
||||
getClientId(),
|
||||
null)))
|
||||
getClientId())))
|
||||
.setDeletePollMessage(Key.create(deletePollMessage));
|
||||
}
|
||||
}
|
||||
|
@ -130,7 +129,7 @@ public class DomainDeleteFlow extends ResourceSyncDeleteFlow<DomainResource, Bui
|
|||
ImmutableList.Builder<Credit> creditsBuilder = new ImmutableList.Builder<>();
|
||||
for (GracePeriod gracePeriod : existingResource.getGracePeriods()) {
|
||||
// No cancellation is written if the grace period was not for a billable event.
|
||||
if (gracePeriod.getBillingEvent() != null) {
|
||||
if (gracePeriod.hasBillingEvent()) {
|
||||
ofy().save().entity(
|
||||
BillingEvent.Cancellation.forGracePeriod(gracePeriod, historyEntry, targetId));
|
||||
|
||||
|
|
|
@ -104,7 +104,7 @@ public class DomainUpdateFlow extends BaseDomainUpdateFlow<DomainResource, Build
|
|||
// occur at the same time as the sunrush add grace period, as the event time will differ
|
||||
// between them.
|
||||
BillingEvent.OneTime originalAddEvent =
|
||||
((BillingEvent.OneTime) sunrushAddGracePeriod.get().getBillingEvent().get());
|
||||
sunrushAddGracePeriod.get().getOneTimeBillingEvent().get();
|
||||
BillingEvent.OneTime billingEvent = new BillingEvent.OneTime.Builder()
|
||||
.setReason(Reason.CREATE)
|
||||
.setTargetId(targetId)
|
||||
|
|
|
@ -389,7 +389,9 @@ public abstract class BillingEvent extends ImmutableObject
|
|||
.setEventTime(historyEntry.getModificationTime())
|
||||
// The charge being cancelled will take place at the grace period's expiration time.
|
||||
.setBillingTime(gracePeriod.getExpirationTime())
|
||||
.setEventRef(gracePeriod.getBillingEvent())
|
||||
.setEventRef(firstNonNull(
|
||||
gracePeriod.getOneTimeBillingEvent(),
|
||||
gracePeriod.getRecurringBillingEvent()))
|
||||
.setParent(historyEntry)
|
||||
.build();
|
||||
}
|
||||
|
|
|
@ -287,7 +287,7 @@ public class DomainResource extends DomainBase implements ForeignKeyedEppResourc
|
|||
DateTime newExpirationTime = lastAutorenewTime.plusYears(1);
|
||||
builder
|
||||
.setRegistrationExpirationTime(newExpirationTime)
|
||||
.addGracePeriod(GracePeriod.create(
|
||||
.addGracePeriod(GracePeriod.createForRecurring(
|
||||
GracePeriodStatus.AUTO_RENEW,
|
||||
lastAutorenewTime.plus(Registry.get(getTld()).getAutoRenewGracePeriodLength()),
|
||||
getCurrentSponsorClientId(),
|
||||
|
|
|
@ -14,9 +14,9 @@
|
|||
|
||||
package com.google.domain.registry.model.domain;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.domain.registry.util.PreconditionsUtils.checkArgumentNotNull;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.domain.registry.model.ImmutableObject;
|
||||
import com.google.domain.registry.model.billing.BillingEvent;
|
||||
import com.google.domain.registry.model.domain.rgp.GracePeriodStatus;
|
||||
|
@ -78,15 +78,9 @@ public class GracePeriod extends ImmutableObject {
|
|||
return clientId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ref to the billing event associated with this grace period, or null if there is
|
||||
* no applicable billing event (i.e. this is a redemption grace period).
|
||||
*/
|
||||
@Nullable
|
||||
public Ref<? extends BillingEvent> getBillingEvent() {
|
||||
return Optional.<Ref<? extends BillingEvent>>fromNullable(billingEventOneTime)
|
||||
.or(Optional.<Ref<? extends BillingEvent>>fromNullable(billingEventRecurring))
|
||||
.orNull();
|
||||
/** Returns true if this GracePeriod has an associated BillingEvent; i.e. if it's refundable. */
|
||||
public boolean hasBillingEvent() {
|
||||
return billingEventOneTime != null || billingEventRecurring != null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -106,26 +100,53 @@ public class GracePeriod extends ImmutableObject {
|
|||
return billingEventRecurring;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a GracePeriod with some interpretation of the parameters. In particular, selects
|
||||
* the field to store the billing event ref in based on the specified grace period status type.
|
||||
private static GracePeriod createInternal(
|
||||
GracePeriodStatus type,
|
||||
DateTime expirationTime,
|
||||
String clientId,
|
||||
@Nullable Ref<BillingEvent.OneTime> billingEventOneTime,
|
||||
@Nullable Ref<BillingEvent.Recurring> billingEventRecurring) {
|
||||
checkArgument((billingEventOneTime == null) || (billingEventRecurring == null),
|
||||
"A grace period can have at most one billing event");
|
||||
checkArgument((billingEventRecurring != null) == (GracePeriodStatus.AUTO_RENEW.equals(type)),
|
||||
"Recurring billing events must be present on (and only on) autorenew grace periods");
|
||||
GracePeriod instance = new GracePeriod();
|
||||
instance.type = checkArgumentNotNull(type);
|
||||
instance.expirationTime = checkArgumentNotNull(expirationTime);
|
||||
instance.clientId = checkArgumentNotNull(clientId);
|
||||
instance.billingEventOneTime = billingEventOneTime;
|
||||
instance.billingEventRecurring = billingEventRecurring;
|
||||
return instance;
|
||||
}
|
||||
|
||||
/** Create a GracePeriod for an (optional) OneTime billing event.
|
||||
*
|
||||
* <p>Normal callers should always use {@link #forBillingEvent} instead, assuming they do not
|
||||
* need to avoid loading the BillingEvent from datastore. This method should typically be
|
||||
* called only from test code to explicitly construct GracePeriods.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static GracePeriod create(
|
||||
GracePeriodStatus type,
|
||||
DateTime expirationTime,
|
||||
String clientId,
|
||||
@Nullable Ref<? extends BillingEvent> billingEvent) {
|
||||
GracePeriod instance = new GracePeriod();
|
||||
instance.type = checkNotNull(type);
|
||||
instance.expirationTime = checkNotNull(expirationTime);
|
||||
instance.clientId = checkNotNull(clientId);
|
||||
if (GracePeriodStatus.AUTO_RENEW.equals(instance.type)) {
|
||||
instance.billingEventRecurring = (Ref<BillingEvent.Recurring>) billingEvent;
|
||||
} else {
|
||||
instance.billingEventOneTime = (Ref<BillingEvent.OneTime>) billingEvent;
|
||||
}
|
||||
return instance;
|
||||
@Nullable Ref<BillingEvent.OneTime> billingEventOneTime) {
|
||||
return createInternal(type, expirationTime, clientId, billingEventOneTime, null);
|
||||
}
|
||||
|
||||
/** Create a GracePeriod for a Recurring billing event. */
|
||||
public static GracePeriod createForRecurring(
|
||||
GracePeriodStatus type,
|
||||
DateTime expirationTime,
|
||||
String clientId,
|
||||
Ref<BillingEvent.Recurring> billingEventRecurring) {
|
||||
checkArgumentNotNull(billingEventRecurring);
|
||||
return createInternal(type, expirationTime, clientId, null, billingEventRecurring);
|
||||
}
|
||||
|
||||
/** Create a GracePeriod with no billing event. */
|
||||
public static GracePeriod createWithoutBillingEvent(
|
||||
GracePeriodStatus type, DateTime expirationTime, String clientId) {
|
||||
return createInternal(type, expirationTime, clientId, null, null);
|
||||
}
|
||||
|
||||
/** Constructs a GracePeriod of the given type from the provided one-time BillingEvent. */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue