Skip to content

Commit

Permalink
[dashboard] Be more defensive when constructing "invalida billing add…
Browse files Browse the repository at this point in the history
…ress" notifications (#19443)
  • Loading branch information
geropl authored Feb 21, 2024
1 parent c67e266 commit c13409e
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions components/dashboard/src/AppNotifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { User } from "@gitpod/public-api/lib/gitpod/v1/user_pb";
import { useCurrentOrg } from "./data/organizations/orgs-query";
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
import { getGitpodService } from "./service/service";
import { useOrgBillingMode } from "./data/billing-mode/org-billing-mode-query";
import { Organization } from "@gitpod/public-api/lib/gitpod/v1/organization_pb";

const KEY_APP_DISMISSED_NOTIFICATIONS = "gitpod-app-notifications-dismissed";
const PRIVACY_POLICY_LAST_UPDATED = "2023-12-20";
Expand Down Expand Up @@ -91,8 +93,7 @@ export function AppNotifications() {
const { mutateAsync } = useUpdateCurrentUserMutation();

const currentOrg = useCurrentOrg().data;
const attrId = currentOrg ? AttributionId.createFromOrganizationId(currentOrg.id) : undefined;
const attributionId = attrId && AttributionId.render(attrId);
const { data: billingMode } = useOrgBillingMode();

useEffect(() => {
let ignore = false;
Expand All @@ -108,14 +109,10 @@ export function AppNotifications() {
notifications.push(UPDATED_PRIVACY_POLICY((u: Partial<UserProtocol>) => mutateAsync(u)));
}

if (isGitpodIo() && attributionId) {
const [subscriptionId, invalidBillingAddress, stripePortalUrl] = await Promise.all([
getGitpodService().server.findStripeSubscriptionId(attributionId),
getGitpodService().server.isCustomerBillingAddressInvalid(attributionId),
getGitpodService().server.getStripePortalUrl(attributionId),
]);
if (subscriptionId && invalidBillingAddress) {
notifications.push(INVALID_BILLING_ADDRESS(stripePortalUrl));
if (isGitpodIo() && currentOrg && billingMode?.mode === "usage-based") {
const notification = await checkForInvalidBillingAddress(currentOrg);
if (notification) {
notifications.push(notification);
}
}
}
Expand All @@ -131,7 +128,7 @@ export function AppNotifications() {
return () => {
ignore = true;
};
}, [loading, mutateAsync, user, attributionId]);
}, [loading, mutateAsync, user, currentOrg, billingMode]);

const dismissNotification = useCallback(() => {
if (!topNotification) {
Expand Down Expand Up @@ -171,6 +168,29 @@ export function AppNotifications() {
);
}

async function checkForInvalidBillingAddress(org: Organization): Promise<Notification | undefined> {
try {
const attributionId = AttributionId.render(AttributionId.createFromOrganizationId(org.id));

const subscriptionId = await getGitpodService().server.findStripeSubscriptionId(attributionId);
if (!subscriptionId) {
return undefined;
}

const invalidBillingAddress = await getGitpodService().server.isCustomerBillingAddressInvalid(attributionId);
if (!invalidBillingAddress) {
return undefined;
}

const stripePortalUrl = await getGitpodService().server.getStripePortalUrl(attributionId);
return INVALID_BILLING_ADDRESS(stripePortalUrl);
} catch (err) {
// On error we don't want to block but still would like to report against metrics
console.debug("failed to determine 'invalid billing address' state", err);
return undefined;
}
}

function getDismissedNotifications(): string[] {
try {
const str = window.localStorage.getItem(KEY_APP_DISMISSED_NOTIFICATIONS);
Expand Down

0 comments on commit c13409e

Please sign in to comment.