From 1398977aa1187b912fd3f0028b3b44742b602dc5 Mon Sep 17 00:00:00 2001 From: McPizza0 Date: Mon, 26 Aug 2024 12:19:21 +0200 Subject: [PATCH] allow 1 free domain on free plan --- .../domains/_components/add-domain-modal.tsx | 11 ++---- ee/apps/billing/trpc/routers/iCanHazRouter.ts | 39 ++++++++++++------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/apps/web/src/app/[orgShortcode]/settings/org/mail/domains/_components/add-domain-modal.tsx b/apps/web/src/app/[orgShortcode]/settings/org/mail/domains/_components/add-domain-modal.tsx index 777dffc9..008caa45 100644 --- a/apps/web/src/app/[orgShortcode]/settings/org/mail/domains/_components/add-domain-modal.tsx +++ b/apps/web/src/app/[orgShortcode]/settings/org/mail/domains/_components/add-domain-modal.tsx @@ -32,14 +32,9 @@ export function AddDomainModal() { }); const { data: canAddDomain, isLoading } = - platform.org.iCanHaz.domain.useQuery( - { - orgShortcode: orgShortcode - }, - { - staleTime: 1000 - } - ); + platform.org.iCanHaz.domain.useQuery({ + orgShortcode: orgShortcode + }); const [open, setOpen] = useState(false); const [domain, setDomain] = useState(''); diff --git a/ee/apps/billing/trpc/routers/iCanHazRouter.ts b/ee/apps/billing/trpc/routers/iCanHazRouter.ts index fe4d3ab9..c5736623 100644 --- a/ee/apps/billing/trpc/routers/iCanHazRouter.ts +++ b/ee/apps/billing/trpc/routers/iCanHazRouter.ts @@ -12,6 +12,7 @@ export const iCanHazRouter = router({ const { db } = ctx; const orgId = input.orgId; + const allowedDomainsOnFreePlan = 1; const orgBillingResponse = await db.query.orgBilling.findFirst({ where: eq(orgBilling.orgId, orgId), @@ -19,11 +20,22 @@ export const iCanHazRouter = router({ plan: true } }); + + // if pro, has unlimited domains if (orgBillingResponse && orgBillingResponse.plan === 'pro') { return true; } - //for skiff users + // get existing org domain count + const domainQuery = await db.query.domains.findMany({ + where: and(eq(domains.orgId, orgId), eq(domains.disabled, false)), + columns: { + id: true + } + }); + const existingOrgDomainCount = domainQuery?.length || 0; + + // check if org has bonus domains const orgQuery = await db.query.orgs.findFirst({ where: eq(orgs.id, orgId), columns: { @@ -41,25 +53,22 @@ export const iCanHazRouter = router({ const orgMetadata = orgQuery.metadata; // get the bonus where item matches domain - const domainBonus = orgMetadata?.bonuses?.find( + const domainBonusMetadataEntry = orgMetadata?.bonuses?.find( (bonus) => bonus.item === 'domain' ); - if (!domainBonus || !('count' in domainBonus.bonus)) { - return false; - } - - const allowedDomains: number = domainBonus.bonus.count; - - const domainQuery = await db.query.domains.findMany({ - where: and(eq(domains.orgId, orgId), eq(domains.disabled, false)), - columns: { - id: true + if ( + domainBonusMetadataEntry && + 'count' in domainBonusMetadataEntry.bonus + ) { + const allowedBonusDomains: number = + domainBonusMetadataEntry.bonus.count; + if (existingOrgDomainCount < allowedBonusDomains) { + return true; } - }); - const domainCount = domainQuery?.length || 0; + } - if (domainCount < allowedDomains) { + if (existingOrgDomainCount < allowedDomainsOnFreePlan) { return true; }