mirror of
https://github.com/wasp-lang/open-saas.git
synced 2025-04-11 13:29:13 +02:00
Add transactions to getDailyStats
This commit is contained in:
parent
dbca3a20c1
commit
805599504e
@ -1,5 +1,5 @@
|
||||
import { type DailyStats, type PageViewSource } from 'wasp/entities';
|
||||
import { HttpError } from 'wasp/server';
|
||||
import { HttpError, prisma } from 'wasp/server';
|
||||
import { type GetDailyStats } from 'wasp/server/operations';
|
||||
|
||||
type DailyStatsWithSources = DailyStats & {
|
||||
@ -12,31 +12,32 @@ type DailyStatsValues = {
|
||||
};
|
||||
|
||||
export const getDailyStats: GetDailyStats<void, DailyStatsValues | undefined> = async (_args, context) => {
|
||||
if (!context.user?.isAdmin) {
|
||||
throw new HttpError(401);
|
||||
if (!context.user) {
|
||||
throw new HttpError(401, 'Only authenticated users are allowed to perform this operation');
|
||||
}
|
||||
const dailyStats = await context.entities.DailyStats.findFirst({
|
||||
|
||||
if (!context.user.isAdmin) {
|
||||
throw new HttpError(403, 'Only admins are allowed to perform this operation');
|
||||
}
|
||||
|
||||
const statsQuery = {
|
||||
orderBy: {
|
||||
date: 'desc',
|
||||
},
|
||||
include: {
|
||||
sources: true,
|
||||
},
|
||||
});
|
||||
} as const;
|
||||
|
||||
const [dailyStats, weeklyStats] = await prisma.$transaction([
|
||||
context.entities.DailyStats.findFirst(statsQuery),
|
||||
context.entities.DailyStats.findMany({ ...statsQuery, take: 7 }),
|
||||
]);
|
||||
|
||||
if (!dailyStats) {
|
||||
console.log('\x1b[34mNote: No daily stats have been generated by the dailyStatsJob yet. \x1b[0m');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const weeklyStats = await context.entities.DailyStats.findMany({
|
||||
orderBy: {
|
||||
date: 'desc',
|
||||
},
|
||||
take: 7,
|
||||
include: {
|
||||
sources: true,
|
||||
},
|
||||
});
|
||||
|
||||
return { dailyStats, weeklyStats };
|
||||
};
|
||||
|
@ -19,8 +19,9 @@ export const generateCheckoutSession: GenerateCheckoutSession<
|
||||
CheckoutSession
|
||||
> = async (rawPaymentPlanId, context) => {
|
||||
if (!context.user) {
|
||||
throw new HttpError(401);
|
||||
throw new HttpError(401, 'Only authenticated users are allowed to perform this operation');
|
||||
}
|
||||
|
||||
const paymentPlanId = ensureArgsSchemaOrThrowHttpError(generateCheckoutSessionSchema, rawPaymentPlanId);
|
||||
const userId = context.user.id;
|
||||
const userEmail = context.user.email;
|
||||
@ -47,8 +48,9 @@ export const generateCheckoutSession: GenerateCheckoutSession<
|
||||
|
||||
export const getCustomerPortalUrl: GetCustomerPortalUrl<void, string | null> = async (_args, context) => {
|
||||
if (!context.user) {
|
||||
throw new HttpError(401);
|
||||
throw new HttpError(401, 'Only authenticated users are allowed to perform this operation');
|
||||
}
|
||||
|
||||
return paymentProcessor.fetchCustomerPortalUrl({
|
||||
userId: context.user.id,
|
||||
prismaUserDelegate: context.entities.User,
|
||||
|
Loading…
x
Reference in New Issue
Block a user