feat(backend): migrate freelancer and portal runtimes

This commit is contained in:
poyrazavsever
2026-07-17 00:16:38 +03:00
parent 561af11b70
commit 678c0236db
41 changed files with 5293 additions and 2324 deletions
+94 -303
View File
@@ -1,361 +1,152 @@
"use server";
import { createClient } from "@/lib/supabase/server";
import { createServiceRoleClient } from "@/lib/supabase/admin";
import { randomUUID } from "crypto";
import { randomUUID } from "node:crypto";
import { revalidatePath } from "next/cache";
import { getFileService } from "@/server/files/runtime";
import { cleanText, decimalToMinor, requiredText } from "@/server/web/form-data";
import { requireFreelancerBackend } from "@/server/web/freelancer";
const PROJECT_TYPES = ["client_project", "side_project"] as const;
const PROJECT_STATUSES = ["planning", "active", "paused", "completed", "cancelled"] as const;
const PLANNING_SECTION_CATEGORIES = [
"overview",
"problem",
"goal",
"audience",
"scope",
"design_system",
"color_palette",
"typography",
"assets",
"notes",
] as const;
const PROJECT_ASSETS_BUCKET = "project-assets";
const SECTION_CATEGORIES = ["overview", "problem", "goal", "audience", "scope", "design_system", "color_palette", "typography", "assets", "notes"] as const;
const REVISION_STATUSES = ["pending", "in_progress", "completed", "rejected"] as const;
function cleanText(value: FormDataEntryValue | null) {
const text = typeof value === "string" ? value.trim() : "";
return text.length > 0 ? text : null;
function enumValue<T extends readonly string[]>(value: FormDataEntryValue | string | null, values: T, fallback: T[number]): T[number] {
return typeof value === "string" && values.includes(value) ? value as T[number] : fallback;
}
function readProjectType(value: FormDataEntryValue | null) {
const type = typeof value === "string" ? value : "client_project";
return PROJECT_TYPES.includes(type as (typeof PROJECT_TYPES)[number])
? type
: "client_project";
function numberValue(value: FormDataEntryValue | null, fallback = 0) {
const parsed = Number(typeof value === "string" ? value.replace(",", ".") : value);
return Number.isFinite(parsed) ? parsed : fallback;
}
function readProjectStatus(value: FormDataEntryValue | null) {
const status = typeof value === "string" ? value : "planning";
return PROJECT_STATUSES.includes(status as (typeof PROJECT_STATUSES)[number])
? status
: "planning";
}
function readPlanningSectionCategory(value: FormDataEntryValue | null) {
const category = typeof value === "string" ? value : "overview";
return PLANNING_SECTION_CATEGORIES.includes(
category as (typeof PLANNING_SECTION_CATEGORIES)[number],
)
? category
: "overview";
}
function readNumber(value: FormDataEntryValue | null) {
const number = Number(typeof value === "string" ? value.replace(",", ".") : value);
return Number.isFinite(number) ? number : null;
}
function readProgress(value: FormDataEntryValue | null) {
const progress = Math.round(readNumber(value) ?? 0);
return Math.min(Math.max(progress, 0), 100);
}
async function getCurrentUserId() {
const supabase = await createClient();
const {
data: { user },
error,
} = await supabase.auth.getUser();
if (error || !user) {
throw new Error("Proje işlemi için giriş yapmış kullanıcı bulunamadı.");
}
return { supabase, userId: user.id };
}
function readPayload(formData: FormData) {
const type = readProjectType(formData.get("type"));
const clientId = cleanText(formData.get("client_id"));
function projectPayload(formData: FormData) {
const type = enumValue(formData.get("type"), PROJECT_TYPES, "client_project");
return {
name: cleanText(formData.get("name")),
name: requiredText(formData.get("name"), "Proje adı zorunludur."),
type,
client_id: type === "client_project" ? clientId : null,
clientId: type === "client_project" ? cleanText(formData.get("client_id")) : null,
description: cleanText(formData.get("description")),
status: readProjectStatus(formData.get("status")),
start_date: cleanText(formData.get("start_date")),
due_date: cleanText(formData.get("due_date")),
budget_amount: readNumber(formData.get("budget_amount")),
currency: cleanText(formData.get("currency")) || "USD",
progress: readProgress(formData.get("progress")),
cover_image_alt: cleanText(formData.get("cover_image_alt")),
status: enumValue(formData.get("status"), PROJECT_STATUSES, "planning"),
startDate: cleanText(formData.get("start_date")),
dueDate: cleanText(formData.get("due_date")),
budgetAmountMinor: decimalToMinor(formData.get("budget_amount")),
currency: cleanText(formData.get("currency")) ?? "USD",
progress: Math.min(100, Math.max(0, Math.round(numberValue(formData.get("progress"))))),
coverImageAlt: cleanText(formData.get("cover_image_alt")),
};
}
function readImageFile(formData: FormData) {
async function uploadCover(
actor: Parameters<ReturnType<typeof getFileService>["upload"]>[0],
projectId: string,
formData: FormData,
) {
const file = formData.get("cover_image");
if (!(file instanceof File) || file.size === 0) {
return null;
}
if (!file.type.startsWith("image/")) {
throw new Error("Kapak görseli bir görsel dosyası olmalıdır.");
}
return file;
}
function sanitizeFileName(name: string) {
return name
.toLowerCase()
.replace(/[^a-z0-9._-]+/g, "-")
.replace(/^-+|-+$/g, "")
.slice(0, 120);
}
async function uploadCoverImage({
userId,
projectId,
formData,
}: {
userId: string;
projectId: string;
formData: FormData;
}) {
const file = readImageFile(formData);
if (!file) {
return null;
}
const fileName = `${Date.now()}-${sanitizeFileName(file.name) || "cover-image"}`;
const path = `${userId}/projects/${projectId}/${fileName}`;
const admin = createServiceRoleClient();
const { error } = await admin.storage
.from(PROJECT_ASSETS_BUCKET)
.upload(path, file, {
cacheControl: "3600",
contentType: file.type,
upsert: true,
});
if (error) {
throw new Error(`Kapak görseli yüklenemedi: ${error.message}`);
}
return path;
if (!(file instanceof File) || file.size === 0) return null;
const stored = getFileService().upload(actor, {
kind: "project_asset",
originalName: file.name,
claimedMimeType: file.type,
bytes: new Uint8Array(await file.arrayBuffer()),
projectId,
portalVisible: true,
});
return `/api/files/${stored.id}`;
}
export async function createProjectRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId();
const projectId = randomUUID();
const payload = readPayload(formData);
if (!payload.name) {
throw new Error("Proje adı zorunludur.");
const { actor, service } = await requireFreelancerBackend();
const id = randomUUID();
service.createProject(actor, { id, ...projectPayload(formData) });
try {
const cover = await uploadCover(actor, id, formData);
if (cover) service.updateProject(actor, id, { legacyCoverImagePath: cover });
} catch (error) {
service.deleteProject(actor, id);
throw error;
}
const coverImagePath = await uploadCoverImage({
userId,
projectId,
formData,
});
const { error } = await supabase.from("projects").insert({
id: projectId,
user_id: userId,
...payload,
cover_image_path: coverImagePath,
});
if (error) {
throw new Error(`Proje eklenemedi: ${error.message}`);
}
revalidatePath("/projects");
}
export async function updateProjectRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId();
const id = cleanText(formData.get("id"));
const payload = readPayload(formData);
if (!id || !payload.name) {
throw new Error("Proje güncellemek için proje adı ve kayıt kimliği zorunludur.");
}
const coverImagePath = await uploadCoverImage({
userId,
projectId: id,
formData,
});
const updatePayload = {
...payload,
...(coverImagePath ? { cover_image_path: coverImagePath } : {}),
};
const { error } = await supabase
.from("projects")
.update(updatePayload)
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Proje güncellenemedi: ${error.message}`);
}
revalidatePath("/projects");
}
export async function completeProjectRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId();
const id = cleanText(formData.get("id"));
if (!id) {
throw new Error("Tamamlanacak proje bulunamadı.");
}
const { error } = await supabase
.from("projects")
.update({ status: "completed", progress: 100 })
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Proje tamamlanamadı: ${error.message}`);
}
const { actor, service } = await requireFreelancerBackend();
const id = requiredText(formData.get("id"), "Proje kaydı bulunamadı.");
service.updateProject(actor, id, projectPayload(formData));
const cover = await uploadCover(actor, id, formData);
if (cover) service.updateProject(actor, id, { legacyCoverImagePath: cover });
revalidatePath("/projects");
revalidatePath(`/projects/${id}`);
}
function readPlanningSectionPayload(formData: FormData) {
export async function completeProjectRecord(formData: FormData) {
const { actor, service } = await requireFreelancerBackend();
const id = requiredText(formData.get("id"), "Tamamlanacak proje bulunamadı.");
service.updateProject(actor, id, { status: "completed", progress: 100 });
revalidatePath("/projects");
revalidatePath(`/projects/${id}`);
}
function sectionPayload(formData: FormData) {
return {
project_id: cleanText(formData.get("project_id")),
category: readPlanningSectionCategory(formData.get("category")),
title: cleanText(formData.get("title")),
projectId: requiredText(formData.get("project_id"), "Proje zorunludur."),
category: enumValue(formData.get("category"), SECTION_CATEGORIES, "overview"),
title: requiredText(formData.get("title"), "Planlama başlığı zorunludur."),
content: cleanText(formData.get("content")),
sort_order: Math.round(readNumber(formData.get("sort_order")) ?? 0),
sortOrder: Math.max(0, Math.round(numberValue(formData.get("sort_order")))),
};
}
export async function createProjectPlanningSectionRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId();
const payload = readPlanningSectionPayload(formData);
if (!payload.project_id || !payload.title) {
throw new Error("Planlama alanı eklemek için proje ve başlık zorunludur.");
}
const { error } = await supabase.from("project_planning_sections").insert({
user_id: userId,
project_id: payload.project_id,
category: payload.category,
title: payload.title,
content: payload.content,
sort_order: payload.sort_order,
});
if (error) {
throw new Error(`Planlama alanı eklenemedi: ${error.message}`);
}
const { actor, service } = await requireFreelancerBackend();
const payload = sectionPayload(formData);
service.addPlanningSection(actor, payload);
revalidatePath("/projects");
revalidatePath(`/projects/${payload.project_id}`);
revalidatePath(`/projects/${payload.projectId}`);
}
export async function updateProjectPlanningSectionRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId();
const id = cleanText(formData.get("id"));
const payload = readPlanningSectionPayload(formData);
if (!id || !payload.project_id || !payload.title) {
throw new Error("Planlama alanını güncellemek için kayıt kimliği, proje ve başlık zorunludur.");
const { actor, service } = await requireFreelancerBackend();
const id = requiredText(formData.get("id"), "Planlama alanı bulunamadı.");
const payload = sectionPayload(formData);
if (!service.listPlanningSections(actor, payload.projectId).some((section) => section.id === id)) {
throw new Error("Planlama alanı bu projeye ait değil.");
}
const { error } = await supabase
.from("project_planning_sections")
.update({
category: payload.category,
title: payload.title,
content: payload.content,
sort_order: payload.sort_order,
})
.eq("id", id)
.eq("project_id", payload.project_id)
.eq("user_id", userId);
if (error) {
throw new Error(`Planlama alanı güncellenemedi: ${error.message}`);
}
service.updatePlanningSection(actor, id, {
category: payload.category,
title: payload.title,
content: payload.content,
sortOrder: payload.sortOrder,
});
revalidatePath("/projects");
revalidatePath(`/projects/${payload.project_id}`);
revalidatePath(`/projects/${payload.projectId}`);
}
export async function deleteProjectPlanningSectionRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId();
const id = cleanText(formData.get("id"));
const projectId = cleanText(formData.get("project_id"));
if (!id || !projectId) {
throw new Error("Silinecek planlama alanı bulunamadı.");
const { actor, service } = await requireFreelancerBackend();
const id = requiredText(formData.get("id"), "Silinecek planlama alanı bulunamadı.");
const projectId = requiredText(formData.get("project_id"), "Proje zorunludur.");
if (!service.listPlanningSections(actor, projectId).some((section) => section.id === id)) {
throw new Error("Planlama alanı bu projeye ait değil.");
}
const { error } = await supabase
.from("project_planning_sections")
.delete()
.eq("id", id)
.eq("project_id", projectId)
.eq("user_id", userId);
if (error) {
throw new Error(`Planlama alanı silinemedi: ${error.message}`);
}
service.deletePlanningSection(actor, id);
revalidatePath("/projects");
revalidatePath(`/projects/${projectId}`);
}
export async function updateRevisionStatus(id: string, projectId: string, status: string) {
const { supabase } = await getCurrentUserId();
const { error } = await supabase
.from("project_revisions")
.update({ status })
.eq("id", id)
.eq("project_id", projectId);
if (error) {
throw new Error(`Revizyon durumu güncellenemedi: ${error.message}`);
}
const { actor, service } = await requireFreelancerBackend();
service.updateRevisionStatus(actor, id, enumValue(status, REVISION_STATUSES, "pending"), projectId);
revalidatePath(`/projects/${projectId}`);
}
export async function updateProjectSettings(projectId: string, progressType: "manual" | "auto", progress: number, revisionQuota: number) {
const { supabase, userId } = await getCurrentUserId();
if (!projectId) {
throw new Error("Proje ID zorunludur.");
}
const { error } = await supabase
.from("projects")
.update({
progress_type: progressType,
progress: progress,
revision_quota: revisionQuota
})
.eq("id", projectId)
.eq("user_id", userId);
if (error) {
throw new Error(`Ayarlar güncellenemedi: ${error.message}`);
}
const { actor, service } = await requireFreelancerBackend();
service.updateProject(actor, projectId, {
progressType,
progress: Math.min(100, Math.max(0, Math.round(progress))),
revisionQuota: Math.max(0, Math.round(revisionQuota)),
});
revalidatePath("/projects");
revalidatePath(`/projects/${projectId}`);
}