From e49f954e9b260a8203a2466bb180ca05a0a1cbdb Mon Sep 17 00:00:00 2001 From: AddisonTustin Date: Sun, 28 Apr 2024 16:08:17 -0700 Subject: [PATCH] Create admin operation to fix improperly escaped ratings (#122) * Create admin operation to fix improperly escaped ratings --- packages/backend/src/routers/admin.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/backend/src/routers/admin.ts b/packages/backend/src/routers/admin.ts index 12211b0..d1da158 100644 --- a/packages/backend/src/routers/admin.ts +++ b/packages/backend/src/routers/admin.ts @@ -14,6 +14,13 @@ const changeNameParser = z.object({ lastName: z.string(), }); +const fixEscapedCharsParser = z.object({ + professors: z + .array(z.string().uuid()) + .min(1) + .max(250, "Separate your request into batches of 250 professors."), +}); + export const adminRouter = t.router({ removeRating: protectedProcedure .input(z.object({ professorId: z.string(), ratingId: z.string() })) @@ -103,4 +110,22 @@ export const adminRouter = t.router({ await ctx.env.kvDao.removeRating(report.professorId, report.ratingId); await ctx.env.kvDao.removeReport(input); }), + fixEscapedChars: protectedProcedure + .input(fixEscapedCharsParser) + .mutation(async ({ ctx, input }) => { + for (const profId of input.professors) { + // eslint-disable-next-line no-await-in-loop + const professor = await ctx.env.kvDao.getProfessor(profId); + for (const [course, ratings] of Object.entries(professor.reviews)) { + professor.reviews[course] = ratings.map((rating) => { + // eslint-disable-next-line + rating.rating = rating.rating.replaceAll("\\'", "'").replaceAll('\\"', '"'); + return rating; + }); + } + + // eslint-disable-next-line no-await-in-loop + await ctx.env.kvDao.putProfessor(professor, true); + } + }), });