Skip to content

Commit

Permalink
bug: charge min amount should default to undefined if not set
Browse files Browse the repository at this point in the history
  • Loading branch information
ansmonjol committed Jun 28, 2024
1 parent 15f981c commit 5a69708
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/components/plans/ChargeAccordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ export const ChargeAccordion = memo(
variant="quaternary"
disabled={disabled}
onClick={() => {
formikProps.setFieldValue(`charges.${index}.minAmountCents`, null)
formikProps.setFieldValue(`charges.${index}.minAmountCents`, undefined)
setShowSpendingMinimum(false)
}}
/>
Expand Down
14 changes: 7 additions & 7 deletions src/core/serializers/__tests__/serializePlanInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe('serializePlanInput()', () => {
billableMetricId: '1234',
chargeModel: 'package',
filters: [],
minAmountCents: 0,
minAmountCents: undefined,
properties: {
amount: '1',
fixedAmount: '2',
Expand Down Expand Up @@ -363,7 +363,7 @@ describe('serializePlanInput()', () => {
{
billableMetricId: '1234',
chargeModel: 'percentage',
minAmountCents: 0,
minAmountCents: undefined,
filters: [],
properties: {
amount: undefined,
Expand Down Expand Up @@ -431,7 +431,7 @@ describe('serializePlanInput()', () => {
{
billableMetricId: '1234',
chargeModel: 'standard',
minAmountCents: 0,
minAmountCents: undefined,
filters: [],
properties: {
amount: '1',
Expand Down Expand Up @@ -498,7 +498,7 @@ describe('serializePlanInput()', () => {
{
billableMetricId: '1234',
chargeModel: 'standard',
minAmountCents: 0,
minAmountCents: undefined,
filters: [],
properties: {
amount: undefined,
Expand Down Expand Up @@ -588,7 +588,7 @@ describe('serializePlanInput()', () => {
{
billableMetricId: '1234',
chargeModel: 'standard',
minAmountCents: 0,
minAmountCents: undefined,
properties: {
amount: undefined,
freeUnits: undefined,
Expand Down Expand Up @@ -691,7 +691,7 @@ describe('serializePlanInput()', () => {
{
billableMetricId: '1234',
chargeModel: 'volume',
minAmountCents: 0,
minAmountCents: undefined,
filters: [],
properties: {
amount: '1',
Expand Down Expand Up @@ -770,7 +770,7 @@ describe('serializePlanInput()', () => {
{
billableMetricId: '1234',
chargeModel: 'custom',
minAmountCents: 0,
minAmountCents: undefined,
filters: [],
properties: {
amount: '1',
Expand Down
4 changes: 3 additions & 1 deletion src/core/serializers/serializePlanInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ export const serializePlanInput = (values: PlanFormInput) => {
return {
chargeModel,
billableMetricId: billableMetric.id,
minAmountCents: Number(serializeAmount(minAmountCents, values.amountCurrency) || 0),
minAmountCents: !!minAmountCents
? Number(serializeAmount(minAmountCents, values.amountCurrency) || 0)
: undefined,
taxCodes: chargeTaxes?.map(({ code }) => code) || [],
filters: serializeFilters(filters, chargeModel),
properties: properties
Expand Down
16 changes: 11 additions & 5 deletions src/hooks/plans/usePlanForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,17 @@ export const usePlanForm: ({
// Used to not enable submit button on invoiceDisplayName reset
invoiceDisplayName: invoiceDisplayName || '',
taxes: taxes || [],
minAmountCents: isNaN(minAmountCents)
? undefined
: String(
deserializeAmount(minAmountCents || 0, plan.amountCurrency || CurrencyEnum.Usd),
),
minAmountCents:
// Some plan have been saved with minAmountCents as 0 string but it makes the sub form send an override plan each time
// This || minAmountCents === '0' serves to prevent this to happen
isNaN(minAmountCents) || minAmountCents === '0'
? undefined
: String(
deserializeAmount(
minAmountCents || 0,
plan.amountCurrency || CurrencyEnum.Usd,
),
),
payInAdvance: payInAdvance || false,
properties: properties ? getPropertyShape(properties) : undefined,
filters: (filters || []).map((filter) => {
Expand Down

0 comments on commit 5a69708

Please sign in to comment.