Skip to content

Commit

Permalink
Merge pull request #4789 from systeminit/feat(auth-portal)-Expose-whe…
Browse files Browse the repository at this point in the history
…ther-a-person-has-exceeded-free-tier

feat(auth-portal): Expose whether a person has exceeded free tier
  • Loading branch information
stack72 authored Oct 14, 2024
2 parents 8519b45 + 4e3bd01 commit d407f2f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
17 changes: 13 additions & 4 deletions app/auth-portal/src/pages/WorkspacesPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@
"
>
<!-- In the future we are going to need to check if they are outside the free tier as well -->
{{
activeSubscriptionDetails?.isTrial ? "FREE TRIAL" : "FREE TIER"
}}
{{ getSubscriptionTier }}
</div>
</template>
<template
Expand All @@ -79,7 +77,8 @@
"
#infoRow2
>
Ends on <Timestamp :date="activeSubscriptionDetails.endingAt" />
Ends end of day
<Timestamp :date="activeSubscriptionDetails.endingAt" />
</template>
</InfoCard>
</div>
Expand Down Expand Up @@ -230,4 +229,14 @@ const createNewWorkspace = async () => {
params: { workspaceId: "new" },
});
};
const getSubscriptionTier = computed(() => {
if (activeSubscriptionDetails.value?.isTrial) {
return "30-DAY FREE TRIAL";
} else if (activeSubscriptionDetails.value?.exceededFreeTier) {
return "MONTHLY SUBSCRIPTION";
}
return "FREE SUBSCRIPTION";
});
</script>
1 change: 1 addition & 0 deletions app/auth-portal/src/store/auth.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export type ActiveSubscription = {
isTrial: boolean;
endingAt?: string | null;
subscriptionAt?: string | null;
exceededFreeTier: boolean;
};

export type SuspendedUser = {
Expand Down
65 changes: 44 additions & 21 deletions bin/auth-api/src/lib/lago.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Client, Country } from "lago-javascript-client";
import { ChargeObject, Client, Country } from "lago-javascript-client";
import _ from "lodash";

const client = Client(process.env.LAGO_API_KEY as string, {
baseUrl: "https://api.getlago.com/api/v1",
Expand Down Expand Up @@ -138,32 +139,54 @@ export async function getCustomerPortalUrl(userPk: string) {
}

export async function getCustomerActiveSubscription(userPk: string) {
const trial_resp = await client.subscriptions.findSubscription(
`${userPk}_launch_trial`,
);
if (trial_resp.ok && trial_resp.data.subscription.status === "active") {
return {
planCode: trial_resp.data.subscription.plan_code,
subscriptionAt: trial_resp.data.subscription.subscription_at,
endingAt: trial_resp.data.subscription.ending_at,
isTrial: true,
};
try {
const trial_resp = await client.subscriptions.findSubscription(
`${userPk}_launch_trial`,
);
if (trial_resp.ok && trial_resp.data.subscription.status === "active") {
return {
planCode: trial_resp.data.subscription.plan_code,
subscriptionAt: trial_resp.data.subscription.subscription_at,
endingAt: trial_resp.data.subscription.ending_at,
isTrial: true,
exceededFreeTier: false,
};
}
} catch (err) {
/* empty */
// We default to an NOT_FOUND plan so we are fine here for now
}

const payg_resp = await client.subscriptions.findSubscription(
`${userPk}_launch_pay_as_you_go`,
);
if (payg_resp.ok) {
return {
planCode: payg_resp.data.subscription.plan_code,
subscriptionAt: payg_resp.data.subscription.subscription_at,
endingAt: payg_resp.data.subscription.ending_at,
isTrial: false,
};
try {
const payg_resp = await client.subscriptions.findSubscription(
`${userPk}_launch_pay_as_you_go`,
);
if (payg_resp.ok) {
const charges = _.get(
payg_resp.data.subscription,
"plan.charges",
[],
) as ChargeObject[];
const paidCharge = _.find(
charges,
(charge: ChargeObject) => charge.billable_metric_code === "resource-hours" && parseFloat(_.get(charge, "properties.amount", "0")) > 0,
);
return {
planCode: payg_resp.data.subscription.plan_code,
subscriptionAt: payg_resp.data.subscription.subscription_at,
endingAt: payg_resp.data.subscription.ending_at,
isTrial: false,
exceededFreeTier: !!paidCharge,
};
}
} catch (err) {
/* empty */
// We default to an NOT_FOUND plan so we are fine here for now
}

return {
planCode: "NOT_FOUND",
isTrial: false,
exceededFreeTier: false,
};
}
2 changes: 1 addition & 1 deletion bin/auth-api/src/routes/user.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ router.get("/users/:userId/billingDetails", async (ctx) => {
router.get("/users/:userId/activeSubscription", async (ctx) => {
const user = await extractOwnUserIdParam(ctx);

const activeUser = getCustomerBillingDetails(user.id);
const activeUser = await getCustomerBillingDetails(user.id);
if (!activeUser) {
ctx.body = {};
}
Expand Down

0 comments on commit d407f2f

Please sign in to comment.