Skip to content

Commit

Permalink
dates to strings helper
Browse files Browse the repository at this point in the history
  • Loading branch information
js0mmer committed Nov 3, 2024
1 parent a7af1a1 commit 73b483a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 3 additions & 4 deletions api/src/controllers/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { TRPCError } from '@trpc/server';
import { db } from '../db';
import { review, user, vote } from '../db/schema';
import { and, count, desc, eq, sql } from 'drizzle-orm';
import { datesToStrings } from '../helpers/date';

async function userWroteReview(userId: number | undefined, reviewId: number) {
if (!userId) {
Expand Down Expand Up @@ -218,12 +219,10 @@ const reviewsRouter = router({
.limit(1)
)[0];

return {
return datesToStrings<FeaturedReviewData>({
...featuredReview.review,
createdAt: featuredReview.review.createdAt.toISOString(),
updatedAt: featuredReview.review.updatedAt?.toISOString(),
score: featuredReview.vote_query?.score ?? 0,
} as FeaturedReviewData;
});
}),

/**
Expand Down
6 changes: 3 additions & 3 deletions api/src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import { db } from '../db';
import { user } from '../db/schema';
import { eq } from 'drizzle-orm';
import { z } from 'zod';
import { datesToStrings } from '../helpers/date';

const usersRouter = router({
/**
* Get the user's data
*/
get: userProcedure.query(async ({ ctx }) => {
const userData = (await db.select().from(user).where(eq(user.id, ctx.session.userId!)))[0];
return {
return datesToStrings<UserData>({
...userData,
lastRoadmapEditAt: userData.lastRoadmapEditAt?.toISOString(),
isAdmin: ctx.session.isAdmin,
} as UserData;
});
}),

/**
Expand Down
9 changes: 9 additions & 0 deletions api/src/helpers/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function datesToStrings<T>(object: Record<string, unknown>) {
for (const key of Object.keys(object)) {
if (object[key] instanceof Date) {
object[key] = object[key].toISOString();
}
}

return object as T;
}

0 comments on commit 73b483a

Please sign in to comment.