Files
webstudio/apps/builder/app/shared/db/user-plan-features.server.ts
T
Ivan Starkov 8482ec4b77 fix: Wrong user is used when counting available custom domains on a free plan (#3792)
## Description




AuthorisationToken didn't used in case of trpc call for context creation
Closes #3786

## Steps for reproduction

Open in incognito


https://fix-plan.development.webstudio.is/builder/d68d6dd1-bc0a-4b21-b5e5-08b7f99336f7?authToken=9d196f63-4256-4e74-98d4-ea678f9f94f1&mode=edit

Add domain, owner plan settings will be used 



## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-07-30 19:22:45 +03:00

67 lines
1.7 KiB
TypeScript

import { prisma } from "@webstudio-is/prisma-client";
import type { AppContext } from "@webstudio-is/trpc-interface/index.server";
import env from "~/env/env.server";
export type UserPlanFeatures = NonNullable<AppContext["userPlanFeatures"]>;
export const getUserPlanFeatures = async (
userId: string
): Promise<UserPlanFeatures> => {
const userProducts = await prisma.userProduct.findMany({
where: { userId },
select: {
customerId: true,
subscriptionId: true,
product: {
select: {
id: true,
name: true,
description: true,
features: true,
meta: true,
images: true,
},
},
},
});
// This is fast and dirty implementation
// @todo: implement this using products meta, custom table with aggregated transaction info
if (userProducts.length > 0) {
const hasSubscription = userProducts.some(
(log) => log.subscriptionId !== null
);
return {
allowShareAdminLinks: true,
allowDynamicData: true,
allowContactEmail: true,
maxDomainsAllowedPerUser: Number.MAX_SAFE_INTEGER,
hasSubscription,
hasProPlan: true,
planName: userProducts[0].product.name,
};
}
if (env.USER_PLAN === "pro") {
return {
allowShareAdminLinks: true,
allowDynamicData: true,
allowContactEmail: true,
maxDomainsAllowedPerUser: Number.MAX_SAFE_INTEGER,
hasSubscription: true,
hasProPlan: true,
planName: "env.USER_PLAN Pro",
};
}
return {
allowShareAdminLinks: false,
allowDynamicData: false,
allowContactEmail: false,
maxDomainsAllowedPerUser: 5,
hasSubscription: false,
hasProPlan: false,
};
};