Skip to content

Commit

Permalink
🥽 Split manager table and export data (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
nezouse authored May 10, 2024
1 parent 698129e commit ce97cfb
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import { getModeratorPanelApplications } from "@/drizzle/queries/applications";
import * as csv from "csv/sync";
import { notFound } from "next/navigation";
import { NextResponse } from "next/server";
import { getApplicationsExport } from "@/drizzle/queries/applications";
import { stringify } from "csv/sync";

import { Button } from "@/components/ui/button";
import { userHasRole, UserPermission } from "@/config/userPermissions";

export async function ExportSubmissions() {
const csvData = await getCsvData();

return (
<Button asChild>
<a
download="submissionData.csv"
href={`data:text/csv;charset=utf-8,${encodeURIComponent(csvData)}`}
>
Export submissions
</a>
</Button>
);
}

async function getCsvData() {
const applications = await getModeratorPanelApplications();
export async function GET() {
const isModerator = await userHasRole(UserPermission.moderator);
if (!isModerator) {
throw notFound();
}
const applications = await getApplicationsExport();

const csvData: any = [
[
Expand Down Expand Up @@ -67,5 +58,5 @@ async function getCsvData() {
]);
}

return csv.stringify(csvData);
return new NextResponse(stringify(csvData));
}
9 changes: 7 additions & 2 deletions src/app/moderator/applications/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Metadata } from "next";
import { notFound } from "next/navigation";
import { urls } from "@/constants/urls";
import { getModeratorPanelApplications } from "@/drizzle/queries/applications";

import { userHasRole, UserPermission } from "@/config/userPermissions";
import { Button } from "@/components/ui/button";
import { ModeratorNavigation } from "@/components/ui/moderatorNavigation";
import { PageHeader } from "@/components/ui/pageHeader";

import { ExportSubmissions } from "./exportSubmissions";
import { Submissions } from "./submissions";

export const metadata: Metadata = {
Expand All @@ -23,7 +24,11 @@ export default async function ReviewersPage() {
return (
<div className="flex flex-col gap-4">
<PageHeader title="Manage">
<ExportSubmissions />
<Button asChild>
<a download="submissionsData.csv" href={urls.exports.submissions}>
Export submissions
</a>
</Button>
</PageHeader>
<ModeratorNavigation />

Expand Down
3 changes: 3 additions & 0 deletions src/constants/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ export const urls = {
loginCallback: "/api/loginCallback",
logout: "/api/auth/logout",
},
exports: {
submissions: "/api/exports/submissions",
},
} as const;
67 changes: 47 additions & 20 deletions src/drizzle/queries/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,38 +101,65 @@ function countApplicationValue(value: ContentValue) {
);
}

const tableVisibleFields = {
id: Application.id,
name: Application.name,
entityName: Application.entityName,
budget: Application.budget,
createdAt: Application.createdAt,
user: { name: User.name, ethereumAddress: User.ethereumAddress },
userImage: {
id: Image.id,
placeholder: Image.placeholder,
width: Image.width,
height: Image.height,
},
category: {
id: Category.id,
name: Category.name,
color: Category.color,
description: Category.description,
},
helpfulCount: countApplicationValue(ContentValue.positive),
invalidCount: countApplicationValue(ContentValue.invalid),
} as const;

export const getModeratorPanelApplications = cache(async () => {
return db
.select(tableVisibleFields)
.from(Application)
.leftJoin(
ApplicationValue,
eq(Application.id, ApplicationValue.applicationId),
)
.innerJoin(User, eq(Application.userId, User.id))
.innerJoin(Category, eq(Application.categoryId, Category.id))
.leftJoin(Image, eq(User.imageId, Image.id))
.groupBy(
Application.id,
User.name,
User.ethereumAddress,
Category.id,
Category.name,
Category.color,
Category.description,
Image.id,
);
});

export const getApplicationsExport = cache(async () => {
return db
.select({
id: Application.id,
name: Application.name,
summary: Application.summary,
entityName: Application.entityName,
email: Application.email,
duration: Application.duration,
budget: Application.budget,
teamSummary: Application.teamSummary,
idea: Application.idea,
reason: Application.reason,
state: Application.state,
goals: Application.goals,
requirements: Application.requirements,
createdAt: Application.createdAt,
user: { name: User.name, ethereumAddress: User.ethereumAddress },
userImage: {
id: Image.id,
placeholder: Image.placeholder,
width: Image.width,
height: Image.height,
},
category: {
id: Category.id,
name: Category.name,
color: Category.color,
description: Category.description,
},
helpfulCount: countApplicationValue(ContentValue.positive),
invalidCount: countApplicationValue(ContentValue.invalid),
...tableVisibleFields,
})
.from(Application)
.leftJoin(
Expand Down
2 changes: 1 addition & 1 deletion src/types/Application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
getModeratorPanelApplications,
type getApplicationWithComments,
type getModeratorPanelApplications,
} from "@/drizzle/queries/applications";
import { type getWaveWithApplications } from "@/drizzle/queries/waves";

Expand Down

0 comments on commit ce97cfb

Please sign in to comment.