Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions app/api/v1/exports/csv/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import { errors, handleZodError } from "@/lib/api";
import { exportQuerySchema } from "@/lib/schemas";
import { ZodError } from "zod";
import { Prisma } from "@prisma/client";

// CSV escape helper
function escapeCSV(value: unknown): string {
if (value === null || value === undefined) {
return "";
}
const str = String(value);
if (str.includes(",") || str.includes('"') || str.includes("\n")) {
return `"${str.replace(/"/g, '""')}"`;
}
return str;
}

// GET /api/v1/exports/csv - Export hazards as CSV
export async function GET(request: NextRequest) {
try {
const searchParams = Object.fromEntries(request.nextUrl.searchParams);
const query = exportQuerySchema.parse(searchParams);

// Build where clause
const where: Prisma.HazardWhereInput = {
deletedAt: null,
};

if (query.city) {
where.cityCode = query.city;
}
if (query.type) {
where.type = query.type;
}
if (query.repairStatus) {
where.repairStatus = query.repairStatus;
}
if (query.updatedSince) {
where.updatedAt = { gte: new Date(query.updatedSince) };
}

// Fetch hazards (limit to 10000 for performance)
const hazards = await db.hazard.findMany({
where,
take: 10000,
orderBy: { createdAt: "desc" },
select: {
id: true,
slug: true,
name: true,
description: true,
type: true,
latitude: true,
longitude: true,
severityScore: true,
upvotes: true,
downvotes: true,
reportsCount: true,
repairStatus: true,
cityTicketId: true,
cityCode: true,
createdAt: true,
updatedAt: true,
},
});

// Build CSV
const headers = [
"id",
"slug",
"name",
"description",
"type",
"latitude",
"longitude",
"severity_score",
"upvotes",
"downvotes",
"reports_count",
"repair_status",
"city_ticket_id",
"city_code",
"created_at",
"updated_at",
];

const rows = hazards.map((h) =>
[
h.id,
h.slug,
h.name,
h.description,
h.type,
h.latitude,
h.longitude,
h.severityScore,
h.upvotes,
h.downvotes,
h.reportsCount,
h.repairStatus,
h.cityTicketId,
h.cityCode,
h.createdAt.toISOString(),
h.updatedAt.toISOString(),
]
.map(escapeCSV)
.join(",")
);

const csv = [headers.join(","), ...rows].join("\n");

return new NextResponse(csv, {
status: 200,
headers: {
"Content-Type": "text/csv; charset=utf-8",
"Content-Disposition": `attachment; filename="hazards-${Date.now()}.csv"`,
},
});
} catch (error) {
if (error instanceof ZodError) {
return handleZodError(error);
}
console.error("GET /exports/csv error:", error);
return errors.internal();
}
}
99 changes: 99 additions & 0 deletions app/api/v1/exports/geojson/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import { errors, handleZodError } from "@/lib/api";
import { exportQuerySchema } from "@/lib/schemas";
import { ZodError } from "zod";
import { Prisma } from "@prisma/client";

// GET /api/v1/exports/geojson - Export hazards as GeoJSON
export async function GET(request: NextRequest) {
try {
const searchParams = Object.fromEntries(request.nextUrl.searchParams);
const query = exportQuerySchema.parse(searchParams);

// Build where clause
const where: Prisma.HazardWhereInput = {
deletedAt: null,
};

if (query.city) {
where.cityCode = query.city;
}
if (query.type) {
where.type = query.type;
}
if (query.repairStatus) {
where.repairStatus = query.repairStatus;
}
if (query.updatedSince) {
where.updatedAt = { gte: new Date(query.updatedSince) };
}

// Fetch hazards (limit to 10000 for performance)
const hazards = await db.hazard.findMany({
where,
take: 10000,
orderBy: { createdAt: "desc" },
select: {
id: true,
name: true,
slug: true,
description: true,
type: true,
latitude: true,
longitude: true,
severityScore: true,
upvotes: true,
downvotes: true,
reportsCount: true,
repairStatus: true,
cityTicketId: true,
cityCode: true,
createdAt: true,
updatedAt: true,
},
});

// Build GeoJSON FeatureCollection
const geojson = {
type: "FeatureCollection",
features: hazards.map((h) => ({
type: "Feature",
geometry: {
type: "Point",
coordinates: [Number(h.longitude), Number(h.latitude)],
},
properties: {
id: h.id,
name: h.name,
slug: h.slug,
description: h.description,
type: h.type,
severityScore: h.severityScore,
upvotes: h.upvotes,
downvotes: h.downvotes,
reportsCount: h.reportsCount,
repairStatus: h.repairStatus,
cityTicketId: h.cityTicketId,
cityCode: h.cityCode,
createdAt: h.createdAt.toISOString(),
updatedAt: h.updatedAt.toISOString(),
},
})),
};

return new NextResponse(JSON.stringify(geojson, null, 2), {
status: 200,
headers: {
"Content-Type": "application/geo+json",
"Content-Disposition": `attachment; filename="hazards-${Date.now()}.geojson"`,
},
});
} catch (error) {
if (error instanceof ZodError) {
return handleZodError(error);
}
console.error("GET /exports/geojson error:", error);
return errors.internal();
}
}
Loading