Skip to content

Commit

Permalink
Add discount price param to GenerateAllocationTokens command (#2578)
Browse files Browse the repository at this point in the history
* Add discount price param to GenerateAlloCationTokens command

* add discount price param to UpdateAllocationTokens command
  • Loading branch information
jicelhay authored Oct 1, 2024
1 parent 142c910 commit 7a4abd9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import google.registry.model.domain.token.AllocationToken.TokenStatus;
import google.registry.model.domain.token.AllocationToken.TokenType;
import google.registry.persistence.VKey;
import google.registry.tools.params.MoneyParameter;
import google.registry.tools.params.TransitionListParameter.TokenStatusTransitions;
import google.registry.util.CollectionUtils;
import google.registry.util.DomainNameUtils;
Expand Down Expand Up @@ -140,6 +141,17 @@ class GenerateAllocationTokensCommand implements Command {
arity = 1)
private Boolean discountPremiums;

@Parameter(
names = {"--discount_price"},
description =
"A discount that allows the setting of promotional prices. This field is different from "
+ "{@code discountFraction} because the price set here is treated as the domain "
+ "price, versus {@code discountFraction} that applies a fraction discount to the "
+ "domain base price. Use CURRENCY PRICE format, example: USD 777.99",
converter = MoneyParameter.class,
validateWith = MoneyParameter.class)
private Money discountPrice;

@Parameter(
names = {"--discount_years"},
description = "The number of years the discount applies for. Default is 1, max value is 10.")
Expand Down Expand Up @@ -233,6 +245,7 @@ public void run() throws IOException {
.collect(toImmutableSet()));
Optional.ofNullable(discountFraction).ifPresent(token::setDiscountFraction);
Optional.ofNullable(discountPremiums).ifPresent(token::setDiscountPremiums);
Optional.ofNullable(discountPrice).ifPresent(token::setDiscountPrice);
Optional.ofNullable(discountYears).ifPresent(token::setDiscountYears);
Optional.ofNullable(tokenStatusTransitions)
.ifPresent(token::setTokenStatusTransitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import google.registry.model.domain.token.AllocationToken.RegistrationBehavior;
import google.registry.model.domain.token.AllocationToken.TokenStatus;
import google.registry.model.domain.token.AllocationToken.TokenType;
import google.registry.tools.params.MoneyParameter;
import google.registry.tools.params.StringListParameter;
import google.registry.tools.params.TransitionListParameter.TokenStatusTransitions;
import java.util.List;
Expand Down Expand Up @@ -91,6 +92,17 @@ final class UpdateAllocationTokensCommand extends UpdateOrDeleteAllocationTokens
arity = 1)
private Boolean discountPremiums;

@Parameter(
names = {"--discount_price"},
description =
"A discount that allows the setting of promotional prices. This field is different from "
+ "{@code discountFraction} because the price set here is treated as the domain "
+ "price, versus {@code discountFraction} that applies a fraction discount to the "
+ "domain base price. Use CURRENCY PRICE format, example: USD 777.99",
converter = MoneyParameter.class,
validateWith = MoneyParameter.class)
private Money discountPrice;

@Parameter(
names = {"-y", "--discount_years"},
description = "The number of years the discount applies for. Default is 1, max value is 10.")
Expand Down Expand Up @@ -203,6 +215,7 @@ private AllocationToken updateToken(AllocationToken original) {
.collect(toImmutableSet())));
Optional.ofNullable(discountFraction).ifPresent(builder::setDiscountFraction);
Optional.ofNullable(discountPremiums).ifPresent(builder::setDiscountPremiums);
Optional.ofNullable(discountPrice).ifPresent(builder::setDiscountPrice);
Optional.ofNullable(discountYears).ifPresent(builder::setDiscountYears);
Optional.ofNullable(tokenStatusTransitions).ifPresent(builder::setTokenStatusTransitions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,48 @@ void testSuccess_promotionToken() throws Exception {
.build());
}

@Test
void testSuccess_promotionToken_withDiscountPrice() throws Exception {
DateTime promoStart = DateTime.now(UTC);
DateTime promoEnd = promoStart.plusMonths(1);
runCommand(
"--number",
"1",
"--prefix",
"promo",
"--type",
"UNLIMITED_USE",
"--allowed_client_ids",
"TheRegistrar,NewRegistrar",
"--allowed_tlds",
"tld,example",
"--allowed_epp_actions",
"CREATE,RENEW",
"--discount_price",
"USD 3",
"--discount_years",
"6",
"--token_status_transitions",
String.format("%s=NOT_STARTED,%s=VALID,%s=ENDED", START_OF_TIME, promoStart, promoEnd));
assertAllocationTokens(
new AllocationToken.Builder()
.setToken("promo123456789ABCDEFG")
.setTokenType(UNLIMITED_USE)
.setAllowedRegistrarIds(ImmutableSet.of("TheRegistrar", "NewRegistrar"))
.setAllowedTlds(ImmutableSet.of("tld", "example"))
.setAllowedEppActions(ImmutableSet.of(CommandName.CREATE, CommandName.RENEW))
.setDiscountPrice(Money.of(CurrencyUnit.USD, 3))
.setDiscountPremiums(false)
.setDiscountYears(6)
.setTokenStatusTransitions(
ImmutableSortedMap.<DateTime, TokenStatus>naturalOrder()
.put(START_OF_TIME, TokenStatus.NOT_STARTED)
.put(promoStart, TokenStatus.VALID)
.put(promoEnd, TokenStatus.ENDED)
.build())
.build());
}

@Test
void testSuccess_specifyTokens() throws Exception {
runCommand("--tokens", "foobar,foobaz");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ void testUpdateDiscountFraction() throws Exception {
assertThat(reloadResource(token).getDiscountFraction()).isEqualTo(0.15);
}

@Test
void testUpdateDiscountPrice() throws Exception {
AllocationToken token =
persistResource(
builderWithPromo().setDiscountPrice(Money.of(CurrencyUnit.USD, 10)).build());
runCommandForced("--prefix", "token", "--discount_price", "USD 2.15");
assertThat(reloadResource(token).getDiscountPrice().get())
.isEqualTo(Money.of(CurrencyUnit.USD, 2.15));
}

@Test
void testUpdateDiscountPremiums() throws Exception {
AllocationToken token =
Expand Down

0 comments on commit 7a4abd9

Please sign in to comment.