Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sonar duplication #397

Merged
merged 15 commits into from
Dec 17, 2024
Merged
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
Loading