Skip to content

Commit

Permalink
fix: sonar duplication (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
JBR90 authored Dec 17, 2024
1 parent b114694 commit dea3106
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 942 deletions.
63 changes: 19 additions & 44 deletions apps/nextjs/src/app/api/aila-download-all/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { auth } from "@clerk/nextjs/server";
import type { LessonExportType } from "@oakai/db";
import { prisma } from "@oakai/db";
import { prisma, type LessonExportType } from "@oakai/db";
import { downloadDriveFile } from "@oakai/exports";
import * as Sentry from "@sentry/node";
import { kv } from "@vercel/kv";
Expand All @@ -9,55 +8,14 @@ import { PassThrough } from "stream";

import { withSentry } from "@/lib/sentry/withSentry";

import { saveDownloadEvent } from "../aila-download/downloadHelpers";
import { sanitizeFilename } from "../sanitizeFilename";

type FileIdsAndFormats = {
fileId: string;
formats: ReadonlyArray<"pptx" | "docx" | "pdf">;
}[];

function getReadableExportType(exportType: LessonExportType) {
switch (exportType) {
case "EXIT_QUIZ_DOC":
return "Exit quiz";
case "LESSON_PLAN_DOC":
return "Lesson plan";
case "STARTER_QUIZ_DOC":
return "Starter quiz";
case "WORKSHEET_SLIDES":
return "Worksheet";
case "LESSON_SLIDES_SLIDES":
return "Lesson slides";
case "ADDITIONAL_MATERIALS_DOCS":
return "Additional materials";
}
}

async function saveDownloadEvent({
lessonExportId,
downloadedBy,
ext,
}: {
lessonExportId: string;
downloadedBy: string;
ext: string;
}) {
try {
await prisma.lessonExportDownload.create({
data: {
lessonExportId,
downloadedBy,
ext,
},
});
} catch (error) {
Sentry.captureException(error, {
level: "warning",
extra: { lessonExportId, downloadedBy, ext },
});
}
}

function nodePassThroughToReadableStream(passThrough: PassThrough) {
return new ReadableStream({
start(controller) {
Expand All @@ -77,6 +35,23 @@ function nodePassThroughToReadableStream(passThrough: PassThrough) {
});
}

function getReadableExportType(exportType: LessonExportType) {
switch (exportType) {
case "EXIT_QUIZ_DOC":
return "Exit quiz";
case "LESSON_PLAN_DOC":
return "Lesson plan";
case "STARTER_QUIZ_DOC":
return "Starter quiz";
case "WORKSHEET_SLIDES":
return "Worksheet";
case "LESSON_SLIDES_SLIDES":
return "Lesson slides";
case "ADDITIONAL_MATERIALS_DOCS":
return "Additional materials";
}
}

async function getHandler(req: Request): Promise<Response> {
const { searchParams } = new URL(req.url);
const fileIdsParam = searchParams.get("fileIds");
Expand Down
27 changes: 27 additions & 0 deletions apps/nextjs/src/app/api/aila-download/downloadHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { prisma } from "@oakai/db";
import * as Sentry from "@sentry/node";

export async function saveDownloadEvent({
lessonExportId,
downloadedBy,
ext,
}: {
lessonExportId: string;
downloadedBy: string;
ext: string;
}) {
try {
await prisma.lessonExportDownload.create({
data: {
lessonExportId,
downloadedBy,
ext,
},
});
} catch (error) {
Sentry.captureException(error, {
level: "warning",
extra: { lessonExportId, downloadedBy, ext },
});
}
}
31 changes: 3 additions & 28 deletions apps/nextjs/src/app/api/aila-download/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { auth } from "@clerk/nextjs/server";
import type { LessonExportType } from "@oakai/db";
import { prisma } from "@oakai/db";
import { prisma, type LessonExportType } from "@oakai/db";
import { downloadDriveFile } from "@oakai/exports";
import * as Sentry from "@sentry/node";

import { withSentry } from "@/lib/sentry/withSentry";

import { sanitizeFilename } from "../sanitizeFilename";
import { saveDownloadEvent } from "./downloadHelpers";

// From: https://www.ericburel.tech/blog/nextjs-stream-files
async function* nodeStreamToIterator(stream: NodeJS.ReadableStream) {
Expand Down Expand Up @@ -46,31 +46,6 @@ function getReadableExportType(exportType: LessonExportType) {
}
}

async function saveDownloadEvent({
lessonExportId,
downloadedBy,
ext,
}: {
lessonExportId: string;
downloadedBy: string;
ext: string;
}) {
try {
await prisma.lessonExportDownload.create({
data: {
lessonExportId,
downloadedBy,
ext,
},
});
} catch (error) {
Sentry.captureException(error, {
level: "warning",
extra: { lessonExportId, downloadedBy, ext },
});
}
}

async function getHandler(req: Request): Promise<Response> {
const { searchParams } = new URL(req.url);

Expand Down Expand Up @@ -135,7 +110,7 @@ async function getHandler(req: Request): Promise<Response> {
});
}

saveDownloadEvent({
await saveDownloadEvent({
lessonExportId: lessonExport.id,
downloadedBy: userId,
ext,
Expand Down
197 changes: 0 additions & 197 deletions apps/nextjs/src/components/AppComponents/Chat/chat-start-accordion.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useEffect, useMemo, useState } from "react";

import type { LessonDeepPartial } from "@oakai/exports/browser";
import { exportSlidesFullLessonSchema } from "@oakai/exports/browser";
import type { LessonSlidesInputData } from "@oakai/exports/src/schema/input.schema";
import type { LessonInputData } from "@oakai/exports/src/schema/input.schema";
import { aiLogger } from "@oakai/logger";
import * as Sentry from "@sentry/nextjs";
import { useDebounce } from "@uidotdev/usehooks";
Expand All @@ -29,8 +29,7 @@ export function useExportAllLessonAssets({
const query = trpc.exports.generateAllAssetExports.useMutation();

const [parseResult, setParseResult] = useState<
| { data?: LessonSlidesInputData; success: boolean; error?: ZodError }
| undefined
{ data?: LessonInputData; success: boolean; error?: ZodError } | undefined
>({ success: false });
const debouncedParseResult = useDebounce(parseResult, 500);

Expand Down
Loading

0 comments on commit dea3106

Please sign in to comment.