Load subscription data from API before rendering replay and heatmap views
This commit is contained in:
1 parent
385088d53a
commit
adba113b04
7 files changed
+111
-68
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import { z } from 'zod';
|
||||
import { fetchAccount, fetchTeam } from '@/lib/load';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { normalizeSubscription } from '@/lib/subscription';
|
||||
import { canViewTeam } from '@/permissions';
|
||||
|
||||
const headers = {
|
||||
'Cache-Control': 'private, no-store, max-age=0',
|
||||
};
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const schema = z.object({
|
||||
teamId: z.string().uuid().optional(),
|
||||
});
|
||||
|
||||
const { auth, query, error } = await parseRequest(request, schema);
|
||||
|
||||
if (error) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const { teamId } = query;
|
||||
|
||||
if (teamId && !(await canViewTeam(auth, teamId))) {
|
||||
return Response.json(
|
||||
{
|
||||
error: {
|
||||
message: 'Unauthorized',
|
||||
code: 'unauthorized',
|
||||
status: 401,
|
||||
},
|
||||
},
|
||||
{ status: 401, headers },
|
||||
);
|
||||
}
|
||||
|
||||
const account = teamId
|
||||
? ((await fetchTeam(teamId)) ?? (await fetchAccount(auth.user.id)))
|
||||
: await fetchAccount(auth.user.id);
|
||||
|
||||
return Response.json(normalizeSubscription(account), { headers });
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import { fetchAccount, fetchTeam } from '@/lib/load';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { json } from '@/lib/response';
|
||||
import { getAllUserTeams } from '@/queries/prisma';
|
||||
@@ -10,42 +9,7 @@ export async function POST(request: Request) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const user = { ...auth.user };
|
||||
const teams = await getAllUserTeams(user.id);
|
||||
const teams = await getAllUserTeams(auth.user.id);
|
||||
|
||||
if (process.env.CLOUD_MODE) {
|
||||
const account = await fetchAccount(user.id);
|
||||
|
||||
if (account) {
|
||||
user.subscription = {
|
||||
isPro: account.isPro || false,
|
||||
isBusiness: account.isBusiness || false,
|
||||
isNoBilling: account.isNoBilling || false,
|
||||
hasSubscription: account.hasSubscription || false,
|
||||
unlimitedWebsites: account.unlimitedWebsites || false,
|
||||
};
|
||||
}
|
||||
|
||||
const teamsWithSubscription = await Promise.all(
|
||||
teams.map(async (team: any) => {
|
||||
const teamAccount = await fetchTeam(team.id);
|
||||
return {
|
||||
...team,
|
||||
subscription: teamAccount
|
||||
? {
|
||||
isPro: teamAccount.isPro || false,
|
||||
isBusiness: teamAccount.isBusiness || false,
|
||||
isNoBilling: teamAccount.isNoBilling || false,
|
||||
hasSubscription: teamAccount.hasSubscription || false,
|
||||
unlimitedWebsites: teamAccount.unlimitedWebsites || false,
|
||||
}
|
||||
: null,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
return json({ ...user, teams: teamsWithSubscription });
|
||||
}
|
||||
|
||||
return json({ ...user, teams });
|
||||
return json({ ...auth.user, teams });
|
||||
}
|
||||
Reference in new issue
Block a user