Skip to content

Commit

Permalink
feat(console): Removing purchased entitlements needs guard rails
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosmin-Parvulescu committed Jul 14, 2023
1 parent 3f0eaf5 commit c9be561
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions apps/console/app/routes/__layout/billing/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
} from '~/services/billing/stripe'
import { useHydrated } from 'remix-utils'
import _ from 'lodash'
import { BadRequestError } from '@proofzero/errors'

type StripeInvoice = {
amount: number
Expand Down Expand Up @@ -180,12 +181,12 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
})

const starbaseClient = createStarbaseClient(Starbase, {
...getAuthzHeaderConditionallyFromToken(undefined),
...getAuthzHeaderConditionallyFromToken(jwt),
...traceHeader,
})

const addressClient = createAddressClient(Address, {
...getAuthzHeaderConditionallyFromToken(undefined),
...getAuthzHeaderConditionallyFromToken(jwt),
...traceHeader,
})

Expand All @@ -198,6 +199,18 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
txType: 'buy' | 'remove'
}

const apps = await starbaseClient.listApps.query()
const assignedEntitlementCount = apps.filter(
(a) => a.appPlan === ServicePlanType.PRO
).length
if (assignedEntitlementCount > quantity) {
throw new BadRequestError({
message: `Invalid quantity. Downgrade ${
quantity - assignedEntitlementCount
} of the ${assignedEntitlementCount} apps to a different plan first.`,
})
}

const entitlements = await accountClient.getEntitlements.query({
accountURN,
})
Expand Down Expand Up @@ -457,7 +470,7 @@ const RemoveEntitelmentModal = ({
weight="semibold"
className="text-left text-gray-800 mx-5"
>
Purchase Entitlement(s)
Remove Entitlement(s)
</Text>
<section className="m-5 border rounded-lg overflow-auto thin-scrollbar">
<div className="p-6">
Expand All @@ -477,9 +490,19 @@ const RemoveEntitelmentModal = ({
</div>
<div className="border-b border-gray-200"></div>
<div className="p-6 flex justify-between items-center">
<Text size="sm" weight="medium" className="text-gray-800 text-left">
Number of Entitlements
</Text>
<div>
<Text size="sm" weight="medium" className="text-gray-800 text-left">
Number of Entitlements
</Text>
<Text
size="sm"
weight="medium"
className="text-[#6B7280] text-left"
>{`${entitlementUsage} x ${
plans[ServicePlanType.PRO].price
}/month`}</Text>
</div>

<div className="flex flex-row text-[#6B7280] space-x-4">
<div className="flex flex-row items-center space-x-2">
<Text size="sm">{entitlements} Entitlements</Text>
Expand All @@ -490,6 +513,7 @@ const RemoveEntitelmentModal = ({
<Listbox
value={proEntitlementNew}
onChange={setProEntitlementNew}
disabled={entitlementUsage === entitlements}
>
{({ open }) => {
return (
Expand Down Expand Up @@ -564,9 +588,9 @@ const RemoveEntitelmentModal = ({
</Text>

<div className="flex flex-row gap-2 items-center">
<Text size="lg" weight="semibold" className="text-gray-900">{`-$${
plan.price * (entitlements - proEntitlementNew)
}`}</Text>
<Text size="lg" weight="semibold" className="text-gray-900">{`${
plan.price * (entitlements - proEntitlementNew) !== 0 ? '-' : ''
}$${plan.price * (entitlements - proEntitlementNew)}`}</Text>
<Text size="sm" weight="medium" className="text-gray-500">
per month
</Text>
Expand All @@ -576,7 +600,12 @@ const RemoveEntitelmentModal = ({
<section className="flex flex-row-reverse gap-4 mt-auto m-5">
<Button
btnType="dangerous-alt"
disabled={!paymentData?.paymentMethodID}
disabled={
!paymentData?.paymentMethodID ||
entitlementUsage === entitlements ||
proEntitlementNew < entitlementUsage ||
proEntitlementNew === entitlements
}
onClick={() => {
setIsOpen(false)
setProEntitlementNew(1)
Expand All @@ -596,7 +625,7 @@ const RemoveEntitelmentModal = ({
)
}}
>
Remove Entitelment(s)
Remove Entitlement(s)
</Button>
<Button btnType="secondary-alt" onClick={() => setIsOpen(false)}>
Cancel
Expand Down

0 comments on commit c9be561

Please sign in to comment.