This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import type { APIRoute } from "astro";
|
||||
import { promises as fs } from "fs";
|
||||
import path from "path";
|
||||
import { db } from "../../../db";
|
||||
import { organizations, members } from "../../../db/schema";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
@@ -17,6 +19,7 @@ export const POST: APIRoute = async ({ request, locals, redirect }) => {
|
||||
const state = formData.get("state") as string | null;
|
||||
const zip = formData.get("zip") as string | null;
|
||||
const country = formData.get("country") as string | null;
|
||||
const logo = formData.get("logo") as File | null;
|
||||
|
||||
if (!organizationId || !name || name.trim().length === 0) {
|
||||
return new Response("Organization ID and name are required", {
|
||||
@@ -49,17 +52,63 @@ export const POST: APIRoute = async ({ request, locals, redirect }) => {
|
||||
);
|
||||
}
|
||||
|
||||
let logoUrl: string | undefined;
|
||||
|
||||
if (logo && logo.size > 0) {
|
||||
const allowedTypes = ["image/png", "image/jpeg"];
|
||||
if (!allowedTypes.includes(logo.type)) {
|
||||
return new Response(
|
||||
"Invalid file type. Only PNG and JPG are allowed.",
|
||||
{
|
||||
status: 400,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const ext = logo.name.split(".").pop() || "png";
|
||||
const filename = `${organizationId}-${Date.now()}.${ext}`;
|
||||
let uploadDir;
|
||||
|
||||
const envRootDir = process.env.ROOT_DIR
|
||||
? process.env.ROOT_DIR
|
||||
: import.meta.env.ROOT_DIR;
|
||||
|
||||
if (envRootDir) {
|
||||
uploadDir = path.join(envRootDir, "uploads");
|
||||
} else {
|
||||
uploadDir =
|
||||
process.env.UPLOAD_DIR ||
|
||||
path.join(process.cwd(), "public", "uploads");
|
||||
}
|
||||
|
||||
try {
|
||||
await fs.access(uploadDir);
|
||||
} catch {
|
||||
await fs.mkdir(uploadDir, { recursive: true });
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(await logo.arrayBuffer());
|
||||
await fs.writeFile(path.join(uploadDir, filename), buffer);
|
||||
logoUrl = `/uploads/${filename}`;
|
||||
}
|
||||
|
||||
// Update organization information
|
||||
const updateData: any = {
|
||||
name: name.trim(),
|
||||
street: street?.trim() || null,
|
||||
city: city?.trim() || null,
|
||||
state: state?.trim() || null,
|
||||
zip: zip?.trim() || null,
|
||||
country: country?.trim() || null,
|
||||
};
|
||||
|
||||
if (logoUrl) {
|
||||
updateData.logoUrl = logoUrl;
|
||||
}
|
||||
|
||||
await db
|
||||
.update(organizations)
|
||||
.set({
|
||||
name: name.trim(),
|
||||
street: street?.trim() || null,
|
||||
city: city?.trim() || null,
|
||||
state: state?.trim() || null,
|
||||
zip: zip?.trim() || null,
|
||||
country: country?.trim() || null,
|
||||
})
|
||||
.set(updateData)
|
||||
.where(eq(organizations.id, organizationId))
|
||||
.run();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user