Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions opensaas-sh/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# We can't ignore `app/` because it messes up our patch/diff procedure (check
# the README for more info on this)
# app/
31 changes: 16 additions & 15 deletions template/app/src/analytics/operations.ts
Original file line number Diff line number Diff line change
@@ -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 & {
Expand All @@ -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;
Copy link
Collaborator Author

@sodic sodic Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: Necessary to prevent type widening.

For example, fi I didn't use as const, TS would infer the type of statsQuery.orderBy.date as string, which is too permissive to be accepted by findFirst and findMany.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Why do that instead of directly typing it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, what exactly do you mean by typing it directly?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I get it now. You want statsQuery.orderBy.date to be of type desc or asc and using as const is easier than typing the variable like statsQuery: statsQueryInfo = {...}


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 };
};
4 changes: 2 additions & 2 deletions template/app/src/demo-ai-app/DemoAppPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function DemoAppPage() {

function NewTaskForm({ handleCreateTask }: { handleCreateTask: typeof createTask }) {
const [description, setDescription] = useState<string>('');
const [todaysHours, setTodaysHours] = useState<string>('8');
const [todaysHours, setTodaysHours] = useState<number>(8);
const [response, setResponse] = useState<GeneratedSchedule | null>({
mainTasks: [
{
Expand Down Expand Up @@ -186,7 +186,7 @@ function NewTaskForm({ handleCreateTask }: { handleCreateTask: typeof createTask
max={24}
className='min-w-[7rem] text-gray-800/90 text-center font-medium rounded-md border border-gray-200 bg-yellow-50 hover:bg-yellow-100 shadow-md focus:outline-none focus:border-transparent focus:shadow-none duration-200 ease-in-out hover:shadow-none'
value={todaysHours}
onChange={(e) => setTodaysHours(e.currentTarget.value)}
onChange={(e) => setTodaysHours(+e.currentTarget.value)}
/>
</div>
</div>
Expand Down
Loading