Skip to content

Commit

Permalink
chore(domains): SSL cert removal when downgrading apps (#2527)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosmin-Parvulescu authored Jul 25, 2023
1 parent a126622 commit 16ccd49
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 17 deletions.
132 changes: 115 additions & 17 deletions apps/console/app/routes/apps/$clientId/billing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,15 @@ import {
} from '~/services/billing/stripe'
import { Modal } from '@proofzero/design-system/src/molecules/modal/Modal'
import { ToastWithLink } from '@proofzero/design-system/src/atoms/toast/ToastWithLink'
import { useEffect, useMemo, useRef, useState } from 'react'
import {
HiArrowUp,
HiOutlineExternalLink,
HiOutlineShoppingCart,
} from 'react-icons/hi'
import { useEffect, useMemo, useState } from 'react'
import { HiArrowUp, HiOutlineShoppingCart } from 'react-icons/hi'
import {
ToastType,
Toaster,
toast,
} from '@proofzero/design-system/src/atoms/toast'
import { Env } from 'bindings'
import dangerVector from '~/images/danger.svg'

export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
async ({ request, context }) => {
Expand Down Expand Up @@ -125,6 +122,21 @@ const processUpdateOp = async (
plan,
})

if (plan === ServicePlanType.FREE) {
const appDetails = apps.find((a) => a.clientId === clientId)
if (!appDetails) {
throw new BadRequestError({
message: `App not found`,
})
}

if (appDetails.customDomain) {
await coreClient.starbase.deleteCustomDomain.mutate({
clientId,
})
}
}

flashSession.flash('success_toast', `${plans[plan].title} assigned.`)
}

Expand Down Expand Up @@ -486,6 +498,78 @@ const PurchaseConfirmationModal = ({
)
}

const DowngradeConfirmationModal = ({
isOpen,
setIsOpen,
currentPlan,
updatedPlan,
}: {
isOpen: boolean
setIsOpen: (open: boolean) => void
currentPlan: ServicePlanType
updatedPlan: ServicePlanType
}) => {
const submit = useSubmit()

return (
<Modal isOpen={isOpen} handleClose={() => setIsOpen(false)}>
<div
className={`w-[48vw] rounded-lg bg-white px-4 pb-4 sm:px-6 sm:pb-6 flex flex-col gap-4 text-left`}
>
<div className="flex flex-row gap-4 items-start">
<img src={dangerVector} />

<div className="flex-1 flex flex-col gap-4">
<Text size="lg" weight="medium" className="text-gray-900 mb-2">
Downgrade Application
</Text>

<Text>
You are about to downgrade the application to the{' '}
{plans[updatedPlan].title}, removing the following features:
</Text>

<ul className="list-disc">
{plans[currentPlan].features
.filter((f) => f.type === 'addon')
.map((f) => (
<li>{f.title}</li>
))}
</ul>

<Text>Are you sure you want to proceed?</Text>
</div>
</div>

<div className="flex justify-end items-center space-x-3">
<Button btnType="secondary-alt" onClick={() => setIsOpen(false)}>
Cancel
</Button>
<Button
type="submit"
btnType="dangerous"
onClick={() => {
submit(
{
op: 'update',
payload: JSON.stringify({
plan: updatedPlan,
}),
},
{
method: 'post',
}
)
}}
>
Downgrade
</Button>
</div>
</div>
</Modal>
)
}

const EntitlementsCardButton = ({
currentPlan,
entitlement,
Expand All @@ -500,6 +584,8 @@ const EntitlementsCardButton = ({
paymentData: PaymentData
}) => {
const [showPurchaseModal, setShowPurchaseModal] = useState(false)
const [showDowngradeConfirmationModal, setShowDowngradeConfirmationModal] =
useState(false)

const isUpgrade = (
planType: ServicePlanType,
Expand Down Expand Up @@ -529,21 +615,33 @@ const EntitlementsCardButton = ({
plan={plans[entitlement.planType]}
paymentData={paymentData}
/>

<DowngradeConfirmationModal
isOpen={showDowngradeConfirmationModal}
setIsOpen={setShowDowngradeConfirmationModal}
currentPlan={currentPlan}
updatedPlan={ServicePlanType.FREE}
/>

<Button
btnType={upgrade ? 'primary-alt' : 'secondary-alt'}
onClick={() => {
if (op === 'update') {
submit(
{
op: 'update',
payload: JSON.stringify({
plan: entitlement.planType,
}),
},
{
method: 'post',
}
)
if (upgrade) {
submit(
{
op: 'update',
payload: JSON.stringify({
plan: entitlement.planType,
}),
},
{
method: 'post',
}
)
} else {
setShowDowngradeConfirmationModal(true)
}
} else {
setShowPurchaseModal(true)
}
Expand Down
16 changes: 16 additions & 0 deletions apps/console/app/services/billing/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,22 @@ export const reconcileAppSubscriptions = async (
}

if (reconciliations.length > 0) {
await Promise.all(
reconciliations
.filter((r) => r.customDomain)
.map(async (app) => {
try {
await coreClient.starbase.deleteCustomDomain.mutate({
clientId: app.clientID,
})
} catch (e) {
console.error(
`Failed to delete custom domain for app ${app.clientID}`
)
}
})
)

await coreClient.address.sendReconciliationNotification.query({
planType: plans[reconciliations[0].plan].title, // Only pro for now
count: reconciliations.length,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const ReconcileAppsSubscriptionsOutputSchema = z.array(
plan: z.nativeEnum(ServicePlanType),
devEmail: z.string().optional(),
appName: z.string(),
customDomain: z.boolean(),
})
)
export type ReconcileAppsSubscriptionsOutput = z.infer<
Expand Down Expand Up @@ -84,6 +85,7 @@ export const reconcileAppSubscriptions = async ({
devEmail: app.devEmail,
appName: app.app?.name ?? 'Undefined',
plan,
customDomain: Boolean(app.customDomain),
}))

for (const app of targetApps) {
Expand Down

0 comments on commit 16ccd49

Please sign in to comment.