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
+11 -51
View File
@@ -1,59 +1,19 @@
import { createClient } from "@/lib/supabase/server"; import { AnalyticsClient, type AnalyticsData } from "./analytics-client";
import { AnalyticsClient } from "./analytics-client"; import { parseDashboardRange, resolveDashboardRange } from "@/server/services/analytics-range";
import { redirect } from "next/navigation"; import { requireFreelancerBackend } from "@/server/web/freelancer";
export const metadata = { export const metadata = { title: "Analizler - Neta" };
title: "Analizler - Neta",
};
export default async function AnalyticsPage({ export default async function AnalyticsPage({
searchParams, searchParams,
}: { }: {
searchParams: { [key: string]: string | string[] | undefined }; searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
}) { }) {
const supabase = await createClient(); const params = await searchParams;
const { data: { user } } = await supabase.auth.getUser(); const range = parseDashboardRange(params.range);
const { actor, service } = await requireFreelancerBackend();
const metrics = service.getFreelancerAnalytics(actor, resolveDashboardRange(range));
const data: AnalyticsData = { metrics, range };
if (!user) { return <AnalyticsClient data={data} />;
redirect("/login");
}
const range = typeof searchParams.range === "string" ? searchParams.range : "this_month";
const now = new Date();
let startDate = new Date();
let endDate = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59);
if (range === "this_week") {
const tempNow = new Date();
const firstDay = new Date(tempNow.setDate(tempNow.getDate() - tempNow.getDay() + (tempNow.getDay() === 0 ? -6 : 1)));
firstDay.setHours(0, 0, 0, 0);
startDate = firstDay;
endDate = new Date(firstDay.getTime());
endDate.setDate(endDate.getDate() + 6);
endDate.setHours(23, 59, 59, 999);
} else if (range === "this_month") {
startDate = new Date(now.getFullYear(), now.getMonth(), 1, 0, 0, 0);
endDate = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59);
} else if (range === "this_year") {
startDate = new Date(now.getFullYear(), 0, 1, 0, 0, 0);
endDate = new Date(now.getFullYear(), 11, 31, 23, 59, 59);
}
// Fetch metrics using RPC
const { data: metricsData } = await supabase.rpc('get_analytics_metrics', {
p_start_date: startDate.toISOString(),
p_end_date: endDate.toISOString()
});
const analyticsData = {
metrics: metricsData || {
projectIncomeData: [],
completedTasks: 0,
activeTasks: 0
},
range
};
return <AnalyticsClient data={analyticsData} />;
} }
+43 -82
View File
@@ -1,106 +1,67 @@
"use server"; "use server";
import { createClient } from "@/lib/supabase/server";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { cleanText, optionalDate, requiredText } from "@/server/web/form-data";
import { requireFreelancerBackend } from "@/server/web/freelancer";
const EVENT_TYPES = ["meeting", "focus", "deadline", "personal", "finance"] as const; const EVENT_TYPES = ["meeting", "focus", "deadline", "personal", "finance"] as const;
function cleanText(value: FormDataEntryValue | null) { function eventType(value: FormDataEntryValue | null) {
const text = typeof value === "string" ? value.trim() : ""; return typeof value === "string" && EVENT_TYPES.includes(value as (typeof EVENT_TYPES)[number])
return text.length > 0 && text !== "__none" ? text : null; ? value as (typeof EVENT_TYPES)[number]
: "focus";
} }
function readType(value: FormDataEntryValue | null) { function payload(formData: FormData) {
const type = typeof value === "string" ? value : "focus";
return EVENT_TYPES.includes(type as (typeof EVENT_TYPES)[number]) ? type : "focus";
}
async function getCurrentUserId() {
const supabase = await createClient();
const {
data: { user },
error,
} = await supabase.auth.getUser();
if (error || !user) {
throw new Error("Takvim işlemi için giriş yapmış kullanıcı bulunamadı.");
}
return { supabase, userId: user.id };
}
function readPayload(formData: FormData) {
return { return {
title: cleanText(formData.get("title")), title: requiredText(formData.get("title"), "Etkinlik başlığı zorunludur."),
description: cleanText(formData.get("description")), description: cleanText(formData.get("description")),
type: readType(formData.get("type")), type: eventType(formData.get("type")),
starts_at: cleanText(formData.get("starts_at")), startsAt: optionalDate(formData.get("starts_at")),
ends_at: cleanText(formData.get("ends_at")), endsAt: optionalDate(formData.get("ends_at")),
client_id: cleanText(formData.get("client_id")), clientId: cleanText(formData.get("client_id")),
project_id: cleanText(formData.get("project_id")), projectId: cleanText(formData.get("project_id")),
task_id: cleanText(formData.get("task_id")), taskId: cleanText(formData.get("task_id")),
};
}
function completeRelations(
value: ReturnType<typeof payload>,
service: Awaited<ReturnType<typeof requireFreelancerBackend>>["service"],
actor: Awaited<ReturnType<typeof requireFreelancerBackend>>["actor"],
) {
const task = value.taskId ? service.listTasks(actor).find((item) => item.id === value.taskId) : null;
const projectId = value.projectId ?? task?.projectId ?? null;
const project = projectId ? service.getProject(actor, projectId) : null;
return {
...value,
projectId,
clientId: value.clientId ?? task?.clientId ?? project?.clientId ?? null,
}; };
} }
export async function createCalendarEventRecord(formData: FormData) { export async function createCalendarEventRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const backend = await requireFreelancerBackend();
const payload = readPayload(formData); const value = completeRelations(payload(formData), backend.service, backend.actor);
if (!value.startsAt) throw new Error("Etkinlik başlangıç zamanı zorunludur.");
if (!payload.title || !payload.starts_at) { backend.service.createCalendarEvent(backend.actor, value);
throw new Error("Etkinlik başlığı ve başlangıç zamanı zorunludur.");
}
const { error } = await supabase.from("calendar_events").insert({
user_id: userId,
...payload,
});
if (error) {
throw new Error(`Etkinlik eklenemedi: ${error.message}`);
}
revalidatePath("/calendar"); revalidatePath("/calendar");
} }
export async function updateCalendarEventRecord(formData: FormData) { export async function updateCalendarEventRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const backend = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); const id = requiredText(formData.get("id"), "Etkinlik kaydı bulunamadı.");
const payload = readPayload(formData); const value = completeRelations(payload(formData), backend.service, backend.actor);
if (!value.startsAt) throw new Error("Etkinlik başlangıç zamanı zorunludur.");
if (!id || !payload.title || !payload.starts_at) { backend.service.updateCalendarEvent(backend.actor, id, value);
throw new Error("Etkinlik güncellemek için başlık, başlangıç ve kayıt kimliği zorunludur.");
}
const { error } = await supabase
.from("calendar_events")
.update(payload)
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Etkinlik güncellenemedi: ${error.message}`);
}
revalidatePath("/calendar"); revalidatePath("/calendar");
} }
export async function deleteCalendarEventRecord(formData: FormData) { export async function deleteCalendarEventRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); service.deleteCalendarEvent(
actor,
if (!id) { requiredText(formData.get("id"), "Silinecek etkinlik bulunamadı."),
throw new Error("Silinecek etkinlik bulunamadı."); );
}
const { error } = await supabase
.from("calendar_events")
.delete()
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Etkinlik silinemedi: ${error.message}`);
}
revalidatePath("/calendar"); revalidatePath("/calendar");
} }
+30 -98
View File
@@ -1,107 +1,39 @@
import { import { CalendarClient, type CalendarEventItem, type CalendarRelationOption, type CalendarTaskOption } from "@/app/(dashboard)/calendar/calendar-client";
CalendarClient, import { requireFreelancerBackend } from "@/server/web/freelancer";
type CalendarEventItem,
type CalendarRelationOption,
type CalendarTaskOption,
} from "@/app/(dashboard)/calendar/calendar-client";
import { createClient } from "@/lib/supabase/server";
type CalendarEventRow = {
id: string;
title: string;
description: string | null;
type: CalendarEventItem["type"];
starts_at: string;
ends_at: string | null;
client_id: string | null;
project_id: string | null;
task_id: string | null;
clients: { name: string } | { name: string }[] | null;
projects: { name: string } | { name: string }[] | null;
tasks: { title: string } | { title: string }[] | null;
};
export default async function CalendarPage() { export default async function CalendarPage() {
const supabase = await createClient(); const { actor, service } = await requireFreelancerBackend();
const { const eventRows = service.listCalendarEvents(actor);
data: { user }, const clientRows = service.listClients(actor);
} = await supabase.auth.getUser(); const projectRows = service.listProjects(actor);
const taskRows = service.listTasks(actor);
const clients = new Map(clientRows.map((item) => [item.id, item.name]));
const projects = new Map(projectRows.map((item) => [item.id, item.name]));
const tasks = new Map(taskRows.map((item) => [item.id, item.title]));
if (!user) { const events: CalendarEventItem[] = eventRows.map((event) => ({
return null;
}
const [{ data: eventRows }, { data: clientRows }, { data: projectRows }, { data: taskRows }] =
await Promise.all([
supabase
.from("calendar_events")
.select("id, title, description, type, starts_at, ends_at, client_id, project_id, task_id, clients(name), projects(name), tasks(title)")
.eq("user_id", user.id)
.order("starts_at", { ascending: true }),
supabase
.from("clients")
.select("id, name")
.eq("user_id", user.id)
.neq("status", "archived")
.order("name", { ascending: true }),
supabase
.from("projects")
.select("id, name")
.eq("user_id", user.id)
.neq("status", "cancelled")
.order("name", { ascending: true }),
supabase
.from("tasks")
.select("id, title")
.eq("user_id", user.id)
.neq("status", "done")
.order("created_at", { ascending: false }),
]);
const events: CalendarEventItem[] = ((eventRows || []) as unknown as CalendarEventRow[]).map((event) => ({
id: event.id, id: event.id,
title: event.title, title: event.title,
description: event.description, description: event.description,
type: normalizeType(event.type), type: event.type,
starts_at: event.starts_at, starts_at: event.startsAt.toISOString(),
ends_at: event.ends_at, ends_at: event.endsAt?.toISOString() ?? null,
client_id: event.client_id, client_id: event.clientId,
project_id: event.project_id, project_id: event.projectId,
task_id: event.task_id, task_id: event.taskId,
clientName: getRelationName(event.clients), clientName: event.clientId ? clients.get(event.clientId) ?? null : null,
projectName: getRelationName(event.projects), projectName: event.projectId ? projects.get(event.projectId) ?? null : null,
taskTitle: getRelationTitle(event.tasks), taskTitle: event.taskId ? tasks.get(event.taskId) ?? null : null,
})); }));
const clientOptions: CalendarRelationOption[] = clientRows
.filter((item) => item.status !== "archived")
.map(({ id, name }) => ({ id, name }));
const projectOptions: CalendarRelationOption[] = projectRows
.filter((item) => item.status !== "cancelled")
.map(({ id, name }) => ({ id, name }));
const taskOptions: CalendarTaskOption[] = taskRows
.filter((item) => item.status !== "done" && item.status !== "cancelled")
.map(({ id, title }) => ({ id, title }));
return ( return <CalendarClient events={events} clients={clientOptions} projects={projectOptions} tasks={taskOptions} />;
<CalendarClient
events={events}
clients={(clientRows || []) as CalendarRelationOption[]}
projects={(projectRows || []) as CalendarRelationOption[]}
tasks={(taskRows || []) as CalendarTaskOption[]}
/>
);
}
function getRelationName(relation: CalendarEventRow["clients"] | CalendarEventRow["projects"]) {
if (!relation) return null;
return Array.isArray(relation) ? relation[0]?.name || null : relation.name;
}
function getRelationTitle(relation: CalendarEventRow["tasks"]) {
if (!relation) return null;
return Array.isArray(relation) ? relation[0]?.title || null : relation.title;
}
function normalizeType(type: string): CalendarEventItem["type"] {
if (
type === "meeting" ||
type === "deadline" ||
type === "personal" ||
type === "finance"
) {
return type;
}
return "focus";
} }
+14 -37
View File
@@ -1,49 +1,26 @@
"use server"; "use server";
import { createClient } from "@/lib/supabase/server";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { cleanText, optionalDate, requiredText } from "@/server/web/form-data";
import { requireFreelancerBackend } from "@/server/web/freelancer";
function cleanText(value: FormDataEntryValue | null) { const ACTIVITY_TYPES = ["note", "call", "meeting", "email"] as const;
const text = typeof value === "string" ? value.trim() : "";
return text.length > 0 ? text : null;
}
export async function addClientActivity(clientId: string, formData: FormData) { export async function addClientActivity(clientId: string, formData: FormData) {
const supabase = await createClient(); const { actor, service } = await requireFreelancerBackend();
const { const rawType = cleanText(formData.get("type"));
data: { user }, const type = rawType && ACTIVITY_TYPES.includes(rawType as (typeof ACTIVITY_TYPES)[number])
error: userError, ? rawType as (typeof ACTIVITY_TYPES)[number]
} = await supabase.auth.getUser(); : "note";
if (userError || !user) { service.addClientActivity(actor, {
throw new Error("Kullanıcı bulunamadı."); clientId,
} type,
title: requiredText(formData.get("title"), "Aktivite başlığı zorunludur."),
const title = cleanText(formData.get("title"));
if (!title) {
throw new Error("Aktivite başlığı zorunludur.");
}
const { error } = await supabase.from("client_activities").insert({
user_id: user.id,
client_id: clientId,
type: formData.get("type") as string || "note",
title,
content: cleanText(formData.get("content")), content: cleanText(formData.get("content")),
activity_date: formData.get("activity_date") as string || new Date().toISOString(), activityDate: optionalDate(formData.get("activity_date")) ?? new Date(),
}); });
if (error) {
throw new Error(`Aktivite eklenemedi: ${error.message}`);
}
// Update client's last_contact_date
await supabase
.from("clients")
.update({ last_contact_date: new Date().toISOString() })
.eq("id", clientId)
.eq("user_id", user.id);
revalidatePath(`/clients/${clientId}`); revalidatePath(`/clients/${clientId}`);
revalidatePath(`/clients`); revalidatePath("/clients");
} }
+32 -25
View File
@@ -1,34 +1,41 @@
import { createClient } from "@/lib/supabase/server";
import { ClientDetailClient, type ClientDetailData, type ClientActivity } from "./client-detail-client";
import { notFound } from "next/navigation"; import { notFound } from "next/navigation";
import { ClientDetailClient, type ClientDetailData, type ClientActivity } from "./client-detail-client";
import { DomainError } from "@/server/domain/errors";
import { requireFreelancerBackend } from "@/server/web/freelancer";
export default async function ClientDetailPage({ params }: { params: Promise<{ id: string }> }) { export default async function ClientDetailPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params; const { id } = await params;
const supabase = await createClient(); const { actor, service } = await requireFreelancerBackend();
const { data: { user } } = await supabase.auth.getUser();
if (!user) return null; let data: { client: ClientDetailData; activities: ClientActivity[] };
try {
const row = service.getClient(actor, id);
const client: ClientDetailData = {
id: row.id,
name: row.name,
company_name: row.companyName,
email: row.email,
phone: row.phone,
website: row.website,
pipeline_stage: row.pipelineStage,
status: row.status,
notes: row.notes,
client_auth_id: row.authUserId,
};
const activities: ClientActivity[] = service.listClientActivities(actor, id).map((activity) => ({
id: activity.id,
type: activity.type,
title: activity.title,
content: activity.content,
activity_date: activity.activityDate.toISOString(),
created_at: activity.createdAt.toISOString(),
}));
const { data: clientData, error } = await supabase data = { client, activities };
.from("clients") } catch (error) {
.select("id, name, company_name, email, phone, website, pipeline_stage, status, notes, client_auth_id") if (error instanceof DomainError && error.code === "NOT_FOUND") notFound();
.eq("id", id) throw error;
.eq("user_id", user.id)
.single();
if (error || !clientData) {
notFound();
} }
const { data: activitiesData } = await supabase return <ClientDetailClient client={data.client} activities={data.activities} />;
.from("client_activities")
.select("id, type, title, content, activity_date, created_at")
.eq("client_id", id)
.eq("user_id", user.id)
.order("activity_date", { ascending: false });
const client: ClientDetailData = clientData as ClientDetailData;
const activities: ClientActivity[] = (activitiesData || []) as ClientActivity[];
return <ClientDetailClient client={client} activities={activities} />;
} }
+36 -113
View File
@@ -1,143 +1,66 @@
"use server"; "use server";
import { createClient } from "@/lib/supabase/server";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { requireFreelancerBackend } from "@/server/web/freelancer";
import { cleanText, requiredText } from "@/server/web/form-data";
const CLIENT_STATUSES = ["active", "paused", "archived"] as const; const CLIENT_STATUSES = ["active", "paused", "archived"] as const;
const PIPELINE_STAGES = ["lead", "contacted", "proposal_sent", "won", "lost"] as const;
function cleanText(value: FormDataEntryValue | null) { function enumValue<T extends readonly string[]>(
const text = typeof value === "string" ? value.trim() : ""; value: FormDataEntryValue | string | null,
return text.length > 0 ? text : null; values: T,
} fallback: T[number],
): T[number] {
function readStatus(value: FormDataEntryValue | null) { return typeof value === "string" && values.includes(value) ? value as T[number] : fallback;
const status = typeof value === "string" ? value : "active";
return CLIENT_STATUSES.includes(status as (typeof CLIENT_STATUSES)[number])
? status
: "active";
} }
function cleanWebsite(value: FormDataEntryValue | null) { function cleanWebsite(value: FormDataEntryValue | null) {
const website = cleanText(value)?.replace(/\s/g, "") || null; const website = cleanText(value)?.replace(/\s/g, "") ?? null;
return website && !/^https?:\/\//i.test(website) ? `https://${website}` : website;
if (!website) {
return null;
}
return /^https?:\/\//i.test(website) ? website : `https://${website}`;
} }
async function getCurrentUserId() { function readPayload(formData: FormData) {
const supabase = await createClient(); return {
const { name: requiredText(formData.get("name"), "Müşteri adı zorunludur."),
data: { user }, companyName: cleanText(formData.get("company_name")),
error,
} = await supabase.auth.getUser();
if (error || !user) {
throw new Error("Müşteri işlemi için giriş yapmış kullanıcı bulunamadı.");
}
return { supabase, userId: user.id };
}
export async function createClientRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId();
const name = cleanText(formData.get("name"));
if (!name) {
throw new Error("Müşteri adı zorunludur.");
}
const { error } = await supabase.from("clients").insert({
user_id: userId,
name,
company_name: cleanText(formData.get("company_name")),
email: cleanText(formData.get("email")), email: cleanText(formData.get("email")),
phone: cleanText(formData.get("phone")), phone: cleanText(formData.get("phone")),
website: cleanWebsite(formData.get("website")), website: cleanWebsite(formData.get("website")),
status: readStatus(formData.get("status")), status: enumValue(formData.get("status"), CLIENT_STATUSES, "active"),
notes: cleanText(formData.get("notes")), notes: cleanText(formData.get("notes")),
pipeline_stage: formData.get("pipeline_stage") ? String(formData.get("pipeline_stage")) : "lead", pipelineStage: enumValue(formData.get("pipeline_stage"), PIPELINE_STAGES, "lead"),
next_follow_up_date: cleanText(formData.get("next_follow_up_date")) || null, nextFollowUpDate: cleanText(formData.get("next_follow_up_date")),
}); };
}
if (error) {
throw new Error(`Müşteri eklenemedi: ${error.message}`);
}
export async function createClientRecord(formData: FormData) {
const { actor, service } = await requireFreelancerBackend();
service.createClient(actor, readPayload(formData));
revalidatePath("/clients"); revalidatePath("/clients");
} }
export async function updateClientRecord(formData: FormData) { export async function updateClientRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); const id = requiredText(formData.get("id"), "Müşteri kaydı bulunamadı.");
const name = cleanText(formData.get("name")); service.updateClient(actor, id, readPayload(formData));
if (!id || !name) {
throw new Error("Müşteri güncellemek için müşteri adı ve kayıt kimliği zorunludur.");
}
const { error } = await supabase
.from("clients")
.update({
name,
company_name: cleanText(formData.get("company_name")),
email: cleanText(formData.get("email")),
phone: cleanText(formData.get("phone")),
website: cleanWebsite(formData.get("website")),
status: readStatus(formData.get("status")),
notes: cleanText(formData.get("notes")),
pipeline_stage: formData.get("pipeline_stage") ? String(formData.get("pipeline_stage")) : "lead",
next_follow_up_date: cleanText(formData.get("next_follow_up_date")) || null,
})
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Müşteri güncellenemedi: ${error.message}`);
}
revalidatePath("/clients"); revalidatePath("/clients");
revalidatePath(`/clients/${id}`);
} }
export async function archiveClientRecord(formData: FormData) { export async function archiveClientRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); const id = requiredText(formData.get("id"), "Arşivlenecek müşteri bulunamadı.");
service.updateClient(actor, id, { status: "archived" });
if (!id) {
throw new Error("Arşivlenecek müşteri bulunamadı.");
}
const { error } = await supabase
.from("clients")
.update({ status: "archived" })
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Müşteri arşivlenemedi: ${error.message}`);
}
revalidatePath("/clients"); revalidatePath("/clients");
revalidatePath(`/clients/${id}`);
} }
export async function updateClientPipelineStage(id: string, stage: string) { export async function updateClientPipelineStage(id: string, stage: string) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
service.updateClient(actor, id, {
if (!id || !stage) { pipelineStage: enumValue(stage, PIPELINE_STAGES, "lead"),
throw new Error("Eksik bilgi."); });
}
const { error } = await supabase
.from("clients")
.update({ pipeline_stage: stage })
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Aşama güncellenemedi: ${error.message}`);
}
revalidatePath("/clients"); revalidatePath("/clients");
revalidatePath(`/clients/${id}`);
} }
+49 -95
View File
@@ -1,110 +1,64 @@
import { ClientsClient, type ClientListItem } from "@/app/(dashboard)/clients/clients-client"; import { ClientsClient, type ClientListItem } from "@/app/(dashboard)/clients/clients-client";
import { createClient } from "@/lib/supabase/server"; import { requireFreelancerBackend } from "@/server/web/freelancer";
type ClientRow = {
id: string;
name: string;
company_name: string | null;
email: string | null;
phone: string | null;
website: string | null;
status: "active" | "paused" | "archived";
notes: string | null;
pipeline_stage: "lead" | "contacted" | "proposal_sent" | "won" | "lost";
next_follow_up_date: string | null;
last_contact_date: string | null;
client_value_score: number;
created_at: string;
};
type ProjectRow = {
client_id: string | null;
};
type FinanceRow = {
client_id: string | null;
amount: number | string;
type: "income" | "expense";
payment_status: "planned" | "pending" | "paid" | "cancelled";
};
export default async function ClientsPage() { export default async function ClientsPage() {
const supabase = await createClient(); const { actor, service } = await requireFreelancerBackend();
const { const clientsData = service.listClients(actor);
data: { user }, const projects = service.listProjects(actor);
} = await supabase.auth.getUser(); const finance = service.listFinanceTransactions(actor);
const activities = service.listAllClientActivities(actor);
if (!user) { const projectCountByClient = new Map<string, number>();
return null; for (const project of projects) {
if (project.clientId) {
projectCountByClient.set(project.clientId, (projectCountByClient.get(project.clientId) ?? 0) + 1);
}
} }
const [{ data: clientRows }, { data: projectRows }, { data: financeRows }] = const revenueByClient = new Map<string, number>();
await Promise.all([ for (const transaction of finance) {
supabase if (transaction.clientId && transaction.type === "income" && transaction.paymentStatus === "paid") {
.from("clients") revenueByClient.set(
.select("id, name, company_name, email, phone, website, status, notes, created_at, pipeline_stage, next_follow_up_date, last_contact_date, client_value_score") transaction.clientId,
.eq("user_id", user.id) (revenueByClient.get(transaction.clientId) ?? 0) + transaction.amountMinor / 100,
.order("created_at", { ascending: false }), );
supabase.from("projects").select("client_id").eq("user_id", user.id), }
supabase }
.from("finance_transactions")
.select("client_id, amount, type, payment_status")
.eq("user_id", user.id),
]);
const projectCountByClient = countProjectsByClient((projectRows || []) as ProjectRow[]); const lastActivityByClient = new Map<string, Date>();
const revenueByClient = sumRevenueByClient((financeRows || []) as FinanceRow[]); for (const activity of activities) {
if (!lastActivityByClient.has(activity.clientId)) {
lastActivityByClient.set(activity.clientId, activity.activityDate);
}
}
const clients: ClientListItem[] = ((clientRows || []) as ClientRow[]).map((client) => ({ const clients: ClientListItem[] = clientsData.map((client) => {
...client, return {
projectCount: projectCountByClient.get(client.id) || 0, id: client.id,
revenueTotal: revenueByClient.get(client.id) || 0, name: client.name,
})); company_name: client.companyName,
email: client.email,
const activeCount = clients.filter((client) => client.status === "active").length; phone: client.phone,
const pausedCount = clients.filter((client) => client.status === "paused").length; website: client.website,
const archivedCount = clients.filter((client) => client.status === "archived").length; status: client.status,
const totalRevenue = clients.reduce((sum, client) => sum + client.revenueTotal, 0); notes: client.notes,
pipeline_stage: client.pipelineStage,
next_follow_up_date: client.nextFollowUpDate,
last_contact_date: lastActivityByClient.get(client.id)?.toISOString() ?? null,
client_value_score: 0,
created_at: client.createdAt.toISOString(),
projectCount: projectCountByClient.get(client.id) ?? 0,
revenueTotal: revenueByClient.get(client.id) ?? 0,
};
});
return ( return (
<ClientsClient <ClientsClient
clients={clients} clients={clients}
totalRevenue={totalRevenue} totalRevenue={clients.reduce((sum, client) => sum + client.revenueTotal, 0)}
activeCount={activeCount} activeCount={clients.filter((client) => client.status === "active").length}
pausedCount={pausedCount} pausedCount={clients.filter((client) => client.status === "paused").length}
archivedCount={archivedCount} archivedCount={clients.filter((client) => client.status === "archived").length}
/> />
); );
} }
function countProjectsByClient(projects: ProjectRow[]) {
const countByClient = new Map<string, number>();
for (const project of projects) {
if (!project.client_id) continue;
countByClient.set(project.client_id, (countByClient.get(project.client_id) || 0) + 1);
}
return countByClient;
}
function sumRevenueByClient(transactions: FinanceRow[]) {
const revenueByClient = new Map<string, number>();
for (const transaction of transactions) {
if (
!transaction.client_id ||
transaction.type !== "income" ||
transaction.payment_status !== "paid"
) {
continue;
}
revenueByClient.set(
transaction.client_id,
(revenueByClient.get(transaction.client_id) || 0) + Number(transaction.amount || 0),
);
}
return revenueByClient;
}
+48 -98
View File
@@ -1,122 +1,72 @@
"use server"; "use server";
import { createClient } from "@/lib/supabase/server";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { cleanText, decimalToMinor, requiredText } from "@/server/web/form-data";
import { requireFreelancerBackend } from "@/server/web/freelancer";
const TRANSACTION_TYPES = ["income", "expense"] as const; const TYPES = ["income", "expense"] as const;
const PAYMENT_STATUSES = ["planned", "pending", "paid", "cancelled"] as const; const STATUSES = ["planned", "pending", "paid", "cancelled"] as const;
function cleanText(value: FormDataEntryValue | null) { function enumValue<T extends readonly string[]>(value: FormDataEntryValue | null, values: T, fallback: T[number]): T[number] {
const text = typeof value === "string" ? value.trim() : ""; return typeof value === "string" && values.includes(value) ? value as T[number] : fallback;
return text.length > 0 && text !== "__none" ? text : null;
} }
function readType(value: FormDataEntryValue | null) { function payload(formData: FormData) {
const type = typeof value === "string" ? value : "expense"; const amountMinor = decimalToMinor(formData.get("amount"));
return TRANSACTION_TYPES.includes(type as (typeof TRANSACTION_TYPES)[number]) if (amountMinor == null) throw new Error("Tutar zorunludur.");
? type
: "expense";
}
function readPaymentStatus(value: FormDataEntryValue | null) {
const status = typeof value === "string" ? value : "planned";
return PAYMENT_STATUSES.includes(status as (typeof PAYMENT_STATUSES)[number])
? status
: "planned";
}
function readAmount(value: FormDataEntryValue | null) {
const amount = Number(typeof value === "string" ? value.replace(",", ".") : value);
return Number.isFinite(amount) && amount >= 0 ? amount : null;
}
async function getCurrentUserId() {
const supabase = await createClient();
const {
data: { user },
error,
} = await supabase.auth.getUser();
if (error || !user) {
throw new Error("Finans işlemi için giriş yapmış kullanıcı bulunamadı.");
}
return { supabase, userId: user.id };
}
function readPayload(formData: FormData) {
return { return {
type: readType(formData.get("type")), type: enumValue(formData.get("type"), TYPES, "expense"),
amount: readAmount(formData.get("amount")), amountMinor,
currency: cleanText(formData.get("currency")) || "USD", currency: cleanText(formData.get("currency")) ?? "USD",
transaction_date: cleanText(formData.get("transaction_date")) || new Date().toISOString().slice(0, 10), transactionDate: cleanText(formData.get("transaction_date")) ?? new Date().toISOString().slice(0, 10),
category: cleanText(formData.get("category")), category: cleanText(formData.get("category")),
payment_status: readPaymentStatus(formData.get("payment_status")), paymentStatus: enumValue(formData.get("payment_status"), STATUSES, "planned"),
client_id: cleanText(formData.get("client_id")), clientId: cleanText(formData.get("client_id")),
project_id: cleanText(formData.get("project_id")), projectId: cleanText(formData.get("project_id")),
description: cleanText(formData.get("description")), description: cleanText(formData.get("description")),
}; };
} }
function completeRelations(
value: ReturnType<typeof payload>,
service: Awaited<ReturnType<typeof requireFreelancerBackend>>["service"],
actor: Awaited<ReturnType<typeof requireFreelancerBackend>>["actor"],
) {
const project = value.projectId ? service.getProject(actor, value.projectId) : null;
return { ...value, clientId: value.clientId ?? project?.clientId ?? null };
}
export async function createFinanceTransactionRecord(formData: FormData) { export async function createFinanceTransactionRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const backend = await requireFreelancerBackend();
const payload = readPayload(formData); backend.service.createFinanceTransaction(
backend.actor,
if (payload.amount === null) { completeRelations(payload(formData), backend.service, backend.actor),
throw new Error("Tutar zorunludur."); );
}
const { error } = await supabase.from("finance_transactions").insert({
user_id: userId,
...payload,
});
if (error) {
throw new Error(`Finans işlemi eklenemedi: ${error.message}`);
}
revalidatePath("/finance"); revalidatePath("/finance");
revalidatePath("/clients");
revalidatePath("/projects");
} }
export async function updateFinanceTransactionRecord(formData: FormData) { export async function updateFinanceTransactionRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const backend = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); const id = requiredText(formData.get("id"), "Finans kaydı bulunamadı.");
const payload = readPayload(formData); backend.service.updateFinanceTransaction(
backend.actor,
if (!id || payload.amount === null) { id,
throw new Error("Finans işlemini güncellemek için kayıt kimliği ve tutar zorunludur."); completeRelations(payload(formData), backend.service, backend.actor),
} );
const { error } = await supabase
.from("finance_transactions")
.update(payload)
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Finans işlemi güncellenemedi: ${error.message}`);
}
revalidatePath("/finance"); revalidatePath("/finance");
revalidatePath("/clients");
revalidatePath("/projects");
} }
export async function deleteFinanceTransactionRecord(formData: FormData) { export async function deleteFinanceTransactionRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); service.deleteFinanceTransaction(
actor,
if (!id) { requiredText(formData.get("id"), "Silinecek finans kaydı bulunamadı."),
throw new Error("Silinecek finans işlemi bulunamadı."); );
}
const { error } = await supabase
.from("finance_transactions")
.delete()
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Finans işlemi silinemedi: ${error.message}`);
}
revalidatePath("/finance"); revalidatePath("/finance");
revalidatePath("/clients");
revalidatePath("/projects");
} }
+24 -83
View File
@@ -1,93 +1,34 @@
import { import { FinanceClient, type FinanceRelationOption, type FinanceTransactionItem } from "@/app/(dashboard)/finance/finance-client";
FinanceClient, import { requireFreelancerBackend } from "@/server/web/freelancer";
type FinanceRelationOption,
type FinanceTransactionItem,
} from "@/app/(dashboard)/finance/finance-client";
import { createClient } from "@/lib/supabase/server";
type FinanceRow = {
id: string;
type: "income" | "expense";
amount: number | string;
currency: string;
transaction_date: string;
category: string | null;
payment_status: "planned" | "pending" | "paid" | "cancelled";
client_id: string | null;
project_id: string | null;
description: string | null;
clients: { name: string } | { name: string }[] | null;
projects: { name: string } | { name: string }[] | null;
};
export default async function FinancePage() { export default async function FinancePage() {
const supabase = await createClient(); const { actor, service } = await requireFreelancerBackend();
const { const rows = service.listFinanceTransactions(actor);
data: { user }, const clientRows = service.listClients(actor);
} = await supabase.auth.getUser(); const projectRows = service.listProjects(actor);
const clients = new Map(clientRows.map((item) => [item.id, item.name]));
const projects = new Map(projectRows.map((item) => [item.id, item.name]));
if (!user) { const transactions: FinanceTransactionItem[] = rows.map((transaction) => ({
return null;
}
const [{ data: financeRows }, { data: clientRows }, { data: projectRows }] =
await Promise.all([
supabase
.from("finance_transactions")
.select("id, type, amount, currency, transaction_date, category, payment_status, client_id, project_id, description, clients(name), projects(name)")
.eq("user_id", user.id)
.order("transaction_date", { ascending: false }),
supabase
.from("clients")
.select("id, name")
.eq("user_id", user.id)
.neq("status", "archived")
.order("name", { ascending: true }),
supabase
.from("projects")
.select("id, name, client_id")
.eq("user_id", user.id)
.neq("status", "cancelled")
.order("name", { ascending: true }),
]);
const transactions: FinanceTransactionItem[] = ((financeRows || []) as unknown as FinanceRow[]).map((transaction) => ({
id: transaction.id, id: transaction.id,
type: normalizeType(transaction.type), type: transaction.type,
amount: Number(transaction.amount), amount: transaction.amountMinor / 100,
currency: transaction.currency, currency: transaction.currency,
transaction_date: transaction.transaction_date, transaction_date: transaction.transactionDate,
category: transaction.category, category: transaction.category,
payment_status: normalizePaymentStatus(transaction.payment_status), payment_status: transaction.paymentStatus,
client_id: transaction.client_id, client_id: transaction.clientId,
project_id: transaction.project_id, project_id: transaction.projectId,
clientName: getRelationName(transaction.clients), clientName: transaction.clientId ? clients.get(transaction.clientId) ?? null : null,
projectName: getRelationName(transaction.projects), projectName: transaction.projectId ? projects.get(transaction.projectId) ?? null : null,
description: transaction.description, description: transaction.description,
})); }));
const clientOptions: FinanceRelationOption[] = clientRows
.filter((item) => item.status !== "archived")
.map(({ id, name }) => ({ id, name }));
const projectOptions: FinanceRelationOption[] = projectRows
.filter((item) => item.status !== "cancelled")
.map(({ id, name, clientId }) => ({ id, name, client_id: clientId }));
return ( return <FinanceClient transactions={transactions} clients={clientOptions} projects={projectOptions} />;
<FinanceClient
transactions={transactions}
clients={(clientRows || []) as FinanceRelationOption[]}
projects={(projectRows || []) as FinanceRelationOption[]}
/>
);
}
function getRelationName(relation: FinanceRow["clients"] | FinanceRow["projects"]) {
if (!relation) return null;
return Array.isArray(relation) ? relation[0]?.name || null : relation.name;
}
function normalizeType(type: string): FinanceTransactionItem["type"] {
return type === "income" ? "income" : "expense";
}
function normalizePaymentStatus(status: string): FinanceTransactionItem["payment_status"] {
if (status === "pending" || status === "paid" || status === "cancelled") {
return status;
}
return "planned";
} }
+26 -84
View File
@@ -1,106 +1,48 @@
"use server"; "use server";
import { createClient } from "@/lib/supabase/server";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { cleanText, requiredText } from "@/server/web/form-data";
import { requireFreelancerBackend } from "@/server/web/freelancer";
function cleanText(value: FormDataEntryValue | null) { function score(value: FormDataEntryValue | null): number | null {
const text = typeof value === "string" ? value.trim() : ""; const parsed = Number(value);
return text.length > 0 ? text : null; return Number.isInteger(parsed) && parsed >= 1 && parsed <= 5 ? parsed : null;
} }
function readScore(value: FormDataEntryValue | null) { function payload(formData: FormData) {
const score = Number(typeof value === "string" ? value : value?.toString()); const moodScore = score(formData.get("mood_score"));
return Number.isInteger(score) && score >= 1 && score <= 5 ? score : null; const energyScore = score(formData.get("energy_score"));
} if (!moodScore || !energyScore) throw new Error("Mood ve enerji skorları zorunludur.");
async function getCurrentUserId() {
const supabase = await createClient();
const {
data: { user },
error,
} = await supabase.auth.getUser();
if (error || !user) {
throw new Error("Günlük kaydı için giriş yapmış kullanıcı bulunamadı.");
}
return { supabase, userId: user.id };
}
function readPayload(formData: FormData) {
return { return {
log_date: cleanText(formData.get("log_date")) || new Date().toISOString().slice(0, 10), entryDate: cleanText(formData.get("log_date")) ?? new Date().toISOString().slice(0, 10),
mood_score: readScore(formData.get("mood_score")), moodScore,
energy_score: readScore(formData.get("energy_score")), energyScore,
work_satisfaction_score: readScore(formData.get("work_satisfaction_score")), workSatisfactionScore: score(formData.get("work_satisfaction_score")),
note: cleanText(formData.get("note")), note: cleanText(formData.get("note")),
}; };
} }
export async function createDailyLogRecord(formData: FormData) { export async function createDailyLogRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const payload = readPayload(formData); service.saveJournalEntry(actor, payload(formData));
if (!payload.mood_score || !payload.energy_score) {
throw new Error("Mood ve enerji skorları zorunludur.");
}
const { error } = await supabase
.from("daily_logs")
.upsert(
{
user_id: userId,
...payload,
},
{ onConflict: "user_id,log_date" },
);
if (error) {
throw new Error(`Günlük kaydı eklenemedi: ${error.message}`);
}
revalidatePath("/journal"); revalidatePath("/journal");
} }
export async function updateDailyLogRecord(formData: FormData) { export async function updateDailyLogRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); service.updateJournalEntry(
const payload = readPayload(formData); actor,
requiredText(formData.get("id"), "Günlük kaydı bulunamadı."),
if (!id || !payload.mood_score || !payload.energy_score) { payload(formData),
throw new Error("Günlük kaydını güncellemek için kayıt kimliği, mood ve enerji skorları zorunludur."); );
}
const { error } = await supabase
.from("daily_logs")
.update(payload)
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Günlük kaydı güncellenemedi: ${error.message}`);
}
revalidatePath("/journal"); revalidatePath("/journal");
} }
export async function deleteDailyLogRecord(formData: FormData) { export async function deleteDailyLogRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); service.deleteJournalEntry(
actor,
if (!id) { requiredText(formData.get("id"), "Silinecek günlük kaydı bulunamadı."),
throw new Error("Silinecek günlük kaydı bulunamadı."); );
}
const { error } = await supabase
.from("daily_logs")
.delete()
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Günlük kaydı silinemedi: ${error.message}`);
}
revalidatePath("/journal"); revalidatePath("/journal");
} }
+16 -35
View File
@@ -1,41 +1,22 @@
import { JournalClient, type DailyLogItem } from "@/app/(dashboard)/journal/journal-client"; import { JournalClient, type DailyLogItem } from "@/app/(dashboard)/journal/journal-client";
import { createClient } from "@/lib/supabase/server"; import { requireFreelancerBackend } from "@/server/web/freelancer";
type DailyLogRow = {
id: string;
log_date: string;
mood_score: number;
energy_score: number;
work_satisfaction_score: number | null;
note: string | null;
};
export default async function JournalPage() { export default async function JournalPage() {
const supabase = await createClient(); const { actor, service } = await requireFreelancerBackend();
const { const logs: DailyLogItem[] = service.listJournalEntries(actor)
data: { user }, .slice(0, 180)
} = await supabase.auth.getUser(); .flatMap((entry) =>
entry.moodScore == null || entry.energyScore == null
if (!user) { ? []
return null; : [{
} id: entry.id,
log_date: entry.entryDate,
const { data: logRows } = await supabase mood_score: entry.moodScore,
.from("daily_logs") energy_score: entry.energyScore,
.select("id, log_date, mood_score, energy_score, work_satisfaction_score, note") work_satisfaction_score: entry.workSatisfactionScore,
.eq("user_id", user.id) note: entry.note,
.order("log_date", { ascending: false }) }],
.limit(180); );
const logs: DailyLogItem[] = ((logRows || []) as DailyLogRow[]).map((log) => ({
id: log.id,
log_date: log.log_date,
mood_score: Number(log.mood_score),
energy_score: Number(log.energy_score),
work_satisfaction_score:
typeof log.work_satisfaction_score === "number" ? Number(log.work_satisfaction_score) : null,
note: log.note,
}));
return <JournalClient logs={logs} />; return <JournalClient logs={logs} />;
} }
+25 -75
View File
@@ -1,85 +1,35 @@
import { createClient } from "@/lib/supabase/server"; import { DashboardClient, type DashboardData } from "./dashboard-client";
import { DashboardClient } from "./dashboard-client"; import { parseDashboardRange, resolveDashboardRange } from "@/server/services/analytics-range";
import { redirect } from "next/navigation"; import { requireFreelancerBackend } from "@/server/web/freelancer";
export const metadata = { export const metadata = { title: "Dashboard - Neta" };
title: "Dashboard - Neta",
};
export default async function DashboardPage({ export default async function DashboardPage({
searchParams, searchParams,
}: { }: {
searchParams: { [key: string]: string | string[] | undefined }; searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
}) { }) {
const supabase = await createClient(); const params = await searchParams;
const { data: { user } } = await supabase.auth.getUser(); const range = parseDashboardRange(params.range);
const { actor, service } = await requireFreelancerBackend();
const result = service.getFreelancerDashboard(actor, resolveDashboardRange(range));
if (!user) { const data: DashboardData = {
redirect("/login"); metrics: result.metrics,
} projects: result.projects.map((project) => ({
id: project.id,
const range = typeof searchParams.range === "string" ? searchParams.range : "this_month"; status: project.status,
name: project.name,
const now = new Date(); created_at: project.createdAt.toISOString(),
let startDate = new Date(); })),
let endDate = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59); // default to end of month clients: result.clients.map((client) => ({
id: client.id,
if (range === "today") { name: client.name,
startDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0); company_name: client.companyName ?? "",
endDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59); created_at: client.createdAt.toISOString(),
} else if (range === "this_week") { })),
// Reset `now` because setDate mutates range,
const tempNow = new Date();
const firstDay = new Date(tempNow.setDate(tempNow.getDate() - tempNow.getDay() + (tempNow.getDay() === 0 ? -6 : 1)));
firstDay.setHours(0, 0, 0, 0);
startDate = firstDay;
endDate = new Date(firstDay.getTime());
endDate.setDate(endDate.getDate() + 6);
endDate.setHours(23, 59, 59, 999);
} else if (range === "this_month") {
startDate = new Date(now.getFullYear(), now.getMonth(), 1, 0, 0, 0);
endDate = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59);
} else if (range === "this_year") {
startDate = new Date(now.getFullYear(), 0, 1, 0, 0, 0);
endDate = new Date(now.getFullYear(), 11, 31, 23, 59, 59);
}
// Fetch metrics using RPC
const { data: metricsData } = await supabase.rpc('get_dashboard_metrics', {
p_start_date: startDate.toISOString(),
p_end_date: endDate.toISOString()
});
// Fetch limited recent data
const [
{ data: projects },
{ data: clients },
] = await Promise.all([
supabase
.from("projects")
.select("id, status, name, created_at")
.order("created_at", { ascending: false })
.limit(5),
supabase
.from("clients")
.select("id, name, company_name, created_at")
.order("created_at", { ascending: false })
.limit(5),
]);
const dashboardData = {
metrics: metricsData || {
netProfit: 0,
activeProjectsCount: 0,
completedTasksCount: 0,
avgMood: "0.0",
financeTrend: [],
moodTrend: []
},
projects: projects || [],
clients: clients || [],
range
}; };
return <DashboardClient data={dashboardData} />; return <DashboardClient data={data} />;
} }
+78 -222
View File
@@ -1,3 +1,4 @@
import { notFound } from "next/navigation";
import { import {
ProjectDetailClient, ProjectDetailClient,
type ProjectDetail, type ProjectDetail,
@@ -5,236 +6,91 @@ import {
type ProjectFinanceItem, type ProjectFinanceItem,
type ProjectPlanningSectionItem, type ProjectPlanningSectionItem,
} from "@/app/(dashboard)/projects/[id]/project-detail-client"; } from "@/app/(dashboard)/projects/[id]/project-detail-client";
import { createServiceRoleClient } from "@/lib/supabase/admin"; import { DomainError } from "@/server/domain/errors";
import { createClient } from "@/lib/supabase/server"; import { requireFreelancerBackend } from "@/server/web/freelancer";
import { notFound } from "next/navigation";
type ProjectRow = { export default async function ProjectDetailPage({ params }: { params: Promise<{ id: string }> }) {
id: string;
client_id: string | null;
name: string;
type: "client_project" | "side_project";
description: string | null;
status: "planning" | "active" | "paused" | "completed" | "cancelled";
start_date: string | null;
due_date: string | null;
budget_amount: number | string | null;
currency: string;
progress: number;
progress_type: "manual" | "auto" | null;
revision_quota: number | null;
cover_image_path: string | null;
cover_image_alt: string | null;
clients: { name: string } | { name: string }[] | null;
};
type SectionRow = ProjectPlanningSectionItem;
type TaskRow = {
id: string;
title: string;
status: string | null;
priority: string | null;
due_at: string | null;
is_public_to_client: boolean | null;
};
type FinanceRow = {
id: string;
type: string;
amount: number | string;
currency: string;
payment_status: string;
transaction_date: string;
category: string | null;
};
export default async function ProjectDetailPage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params; const { id } = await params;
const supabase = await createClient(); const { actor, service } = await requireFreelancerBackend();
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) { let data: {
return null; project: ProjectDetail;
} sections: ProjectPlanningSectionItem[];
tasks: ProjectDetailTaskItem[];
const [{ data: projectRow }, { data: sectionRows }, { data: taskRows }, { data: financeRows }, { data: revisionRows }] = financeTransactions: ProjectFinanceItem[];
await Promise.all([ revisions: Array<Record<string, unknown>>;
supabase
.from("projects")
.select(
"id, client_id, name, type, description, status, start_date, due_date, budget_amount, currency, progress, progress_type, revision_quota, cover_image_path, cover_image_alt, clients(name)",
)
.eq("id", id)
.eq("user_id", user.id)
.maybeSingle(),
supabase
.from("project_planning_sections")
.select("id, project_id, category, title, content, sort_order")
.eq("project_id", id)
.eq("user_id", user.id)
.order("sort_order", { ascending: true })
.order("created_at", { ascending: true }),
supabase
.from("tasks")
.select("id, title, status, priority, due_at, is_public_to_client")
.eq("project_id", id)
.eq("user_id", user.id)
.order("created_at", { ascending: false }),
supabase
.from("finance_transactions")
.select("id, type, amount, currency, payment_status, transaction_date, category")
.eq("project_id", id)
.eq("user_id", user.id)
.order("transaction_date", { ascending: false }),
supabase
.from("project_revisions")
.select("id, description, status, created_at, requested_by")
.eq("project_id", id)
.order("created_at", { ascending: false }),
]);
if (!projectRow) {
notFound();
}
const projectData = projectRow as unknown as ProjectRow;
const coverImageUrl = projectData.cover_image_path
? await createProjectImageUrl(projectData.cover_image_path)
: null;
const project: ProjectDetail = {
id: projectData.id,
client_id: projectData.client_id,
clientName: getClientName(projectData.clients),
name: projectData.name,
type: normalizeProjectType(projectData.type),
description: projectData.description,
status: normalizeProjectStatus(projectData.status),
start_date: projectData.start_date,
due_date: projectData.due_date,
budget_amount:
projectData.budget_amount === null ? null : Number(projectData.budget_amount),
currency: projectData.currency,
progress: Number(projectData.progress || 0),
progress_type: projectData.progress_type === "auto" ? "auto" : "manual",
revision_quota: Number(projectData.revision_quota || 0),
cover_image_alt: projectData.cover_image_alt,
coverImageUrl,
}; };
try {
const row = service.getProject(actor, id);
const client = row.clientId ? service.getClient(actor, row.clientId) : null;
const project: ProjectDetail = {
id: row.id,
client_id: row.clientId,
clientName: client?.name ?? null,
name: row.name,
type: row.type,
description: row.description,
status: row.status,
start_date: row.startDate,
due_date: row.dueDate,
budget_amount: row.budgetAmountMinor == null ? null : row.budgetAmountMinor / 100,
currency: row.currency,
progress: row.progress,
progress_type: row.progressType,
revision_quota: row.revisionQuota,
cover_image_alt: row.coverImageAlt,
coverImageUrl: row.legacyCoverImagePath,
};
const sections: ProjectPlanningSectionItem[] = service.listPlanningSections(actor, id).map((section) => ({
id: section.id,
project_id: section.projectId,
category: section.category,
title: section.title,
content: section.content,
sort_order: section.sortOrder,
}));
const tasks: ProjectDetailTaskItem[] = service.listTasks(actor, id)
.filter((task) => task.status !== "cancelled")
.map((task) => ({
id: task.id,
title: task.title,
status: task.status as ProjectDetailTaskItem["status"],
priority: task.priority,
due_at: task.dueAt?.toISOString() ?? null,
is_public_to_client: task.isPublicToClient,
}));
const financeTransactions: ProjectFinanceItem[] = service.listFinanceTransactions(actor)
.filter((transaction) => transaction.projectId === id)
.map((transaction) => ({
id: transaction.id,
type: transaction.type,
amount: transaction.amountMinor / 100,
currency: transaction.currency,
payment_status: transaction.paymentStatus,
transaction_date: transaction.transactionDate,
category: transaction.category,
}));
const revisions = service.listRevisions(actor, id).map((revision) => ({
id: revision.id,
description: revision.description,
status: revision.status,
created_at: revision.createdAt.toISOString(),
requested_by: revision.requestedByUserId,
}));
const sections = ((sectionRows || []) as unknown as SectionRow[]).map((section) => ({ data = { project, sections, tasks, financeTransactions, revisions };
...section, } catch (error) {
category: normalizeSectionCategory(section.category), if (error instanceof DomainError && error.code === "NOT_FOUND") notFound();
sort_order: Number(section.sort_order || 0), throw error;
})); }
const tasks: ProjectDetailTaskItem[] = ((taskRows || []) as TaskRow[]).map((task) => ({
id: task.id,
title: task.title,
status: normalizeTaskStatus(task.status),
priority: normalizeTaskPriority(task.priority),
due_at: task.due_at,
is_public_to_client: task.is_public_to_client || false,
}));
const revisions = revisionRows || [];
const financeTransactions: ProjectFinanceItem[] = ((financeRows || []) as FinanceRow[]).map(
(transaction) => ({
id: transaction.id,
type: transaction.type === "income" ? "income" : "expense",
amount: Number(transaction.amount || 0),
currency: transaction.currency,
payment_status: normalizePaymentStatus(transaction.payment_status),
transaction_date: transaction.transaction_date,
category: transaction.category,
}),
);
return ( return (
<ProjectDetailClient <ProjectDetailClient
project={project} project={data.project}
sections={sections} sections={data.sections}
tasks={tasks} tasks={data.tasks}
financeTransactions={financeTransactions} financeTransactions={data.financeTransactions}
revisions={revisions} revisions={data.revisions}
/> />
); );
} }
async function createProjectImageUrl(path: string) {
const admin = createServiceRoleClient();
const { data } = await admin.storage
.from("project-assets")
.createSignedUrl(path, 60 * 15);
return data?.signedUrl || null;
}
function getClientName(client: ProjectRow["clients"]) {
if (!client) return null;
return Array.isArray(client) ? client[0]?.name || null : client.name;
}
function normalizeProjectType(type: string): ProjectDetail["type"] {
return type === "side_project" ? "side_project" : "client_project";
}
function normalizeProjectStatus(status: string): ProjectDetail["status"] {
if (
status === "active" ||
status === "paused" ||
status === "completed" ||
status === "cancelled"
) {
return status;
}
return "planning";
}
function normalizeSectionCategory(category: string): ProjectPlanningSectionItem["category"] {
if (
category === "problem" ||
category === "goal" ||
category === "audience" ||
category === "scope" ||
category === "design_system" ||
category === "color_palette" ||
category === "typography" ||
category === "assets" ||
category === "notes"
) {
return category;
}
return "overview";
}
function normalizeTaskStatus(status: string | null): ProjectDetailTaskItem["status"] {
if (status === "in_progress" || status === "done") {
return status;
}
return "todo";
}
function normalizeTaskPriority(priority: string | null): ProjectDetailTaskItem["priority"] {
if (priority === "low" || priority === "high" || priority === "urgent") {
return priority;
}
return "medium";
}
function normalizePaymentStatus(status: string): ProjectFinanceItem["payment_status"] {
if (status === "pending" || status === "paid" || status === "cancelled") {
return status;
}
return "planned";
}
+94 -303
View File
@@ -1,361 +1,152 @@
"use server"; "use server";
import { createClient } from "@/lib/supabase/server"; import { randomUUID } from "node:crypto";
import { createServiceRoleClient } from "@/lib/supabase/admin";
import { randomUUID } from "crypto";
import { revalidatePath } from "next/cache"; 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_TYPES = ["client_project", "side_project"] as const;
const PROJECT_STATUSES = ["planning", "active", "paused", "completed", "cancelled"] as const; const PROJECT_STATUSES = ["planning", "active", "paused", "completed", "cancelled"] as const;
const PLANNING_SECTION_CATEGORIES = [ const SECTION_CATEGORIES = ["overview", "problem", "goal", "audience", "scope", "design_system", "color_palette", "typography", "assets", "notes"] as const;
"overview", const REVISION_STATUSES = ["pending", "in_progress", "completed", "rejected"] as const;
"problem",
"goal",
"audience",
"scope",
"design_system",
"color_palette",
"typography",
"assets",
"notes",
] as const;
const PROJECT_ASSETS_BUCKET = "project-assets";
function cleanText(value: FormDataEntryValue | null) { function enumValue<T extends readonly string[]>(value: FormDataEntryValue | string | null, values: T, fallback: T[number]): T[number] {
const text = typeof value === "string" ? value.trim() : ""; return typeof value === "string" && values.includes(value) ? value as T[number] : fallback;
return text.length > 0 ? text : null;
} }
function readProjectType(value: FormDataEntryValue | null) { function numberValue(value: FormDataEntryValue | null, fallback = 0) {
const type = typeof value === "string" ? value : "client_project"; const parsed = Number(typeof value === "string" ? value.replace(",", ".") : value);
return PROJECT_TYPES.includes(type as (typeof PROJECT_TYPES)[number]) return Number.isFinite(parsed) ? parsed : fallback;
? type
: "client_project";
} }
function readProjectStatus(value: FormDataEntryValue | null) { function projectPayload(formData: FormData) {
const status = typeof value === "string" ? value : "planning"; const type = enumValue(formData.get("type"), PROJECT_TYPES, "client_project");
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"));
return { return {
name: cleanText(formData.get("name")), name: requiredText(formData.get("name"), "Proje adı zorunludur."),
type, type,
client_id: type === "client_project" ? clientId : null, clientId: type === "client_project" ? cleanText(formData.get("client_id")) : null,
description: cleanText(formData.get("description")), description: cleanText(formData.get("description")),
status: readProjectStatus(formData.get("status")), status: enumValue(formData.get("status"), PROJECT_STATUSES, "planning"),
start_date: cleanText(formData.get("start_date")), startDate: cleanText(formData.get("start_date")),
due_date: cleanText(formData.get("due_date")), dueDate: cleanText(formData.get("due_date")),
budget_amount: readNumber(formData.get("budget_amount")), budgetAmountMinor: decimalToMinor(formData.get("budget_amount")),
currency: cleanText(formData.get("currency")) || "USD", currency: cleanText(formData.get("currency")) ?? "USD",
progress: readProgress(formData.get("progress")), progress: Math.min(100, Math.max(0, Math.round(numberValue(formData.get("progress"))))),
cover_image_alt: cleanText(formData.get("cover_image_alt")), 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"); const file = formData.get("cover_image");
if (!(file instanceof File) || file.size === 0) return null;
if (!(file instanceof File) || file.size === 0) { const stored = getFileService().upload(actor, {
return null; kind: "project_asset",
} originalName: file.name,
claimedMimeType: file.type,
if (!file.type.startsWith("image/")) { bytes: new Uint8Array(await file.arrayBuffer()),
throw new Error("Kapak görseli bir görsel dosyası olmalıdır."); projectId,
} portalVisible: true,
});
return file; return `/api/files/${stored.id}`;
}
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;
} }
export async function createProjectRecord(formData: FormData) { export async function createProjectRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const projectId = randomUUID(); const id = randomUUID();
const payload = readPayload(formData); service.createProject(actor, { id, ...projectPayload(formData) });
try {
if (!payload.name) { const cover = await uploadCover(actor, id, formData);
throw new Error("Proje adı zorunludur."); 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"); revalidatePath("/projects");
} }
export async function updateProjectRecord(formData: FormData) { export async function updateProjectRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); const id = requiredText(formData.get("id"), "Proje kaydı bulunamadı.");
const payload = readPayload(formData); service.updateProject(actor, id, projectPayload(formData));
const cover = await uploadCover(actor, id, formData);
if (!id || !payload.name) { if (cover) service.updateProject(actor, id, { legacyCoverImagePath: cover });
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}`);
}
revalidatePath("/projects"); revalidatePath("/projects");
revalidatePath(`/projects/${id}`); 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 { return {
project_id: cleanText(formData.get("project_id")), projectId: requiredText(formData.get("project_id"), "Proje zorunludur."),
category: readPlanningSectionCategory(formData.get("category")), category: enumValue(formData.get("category"), SECTION_CATEGORIES, "overview"),
title: cleanText(formData.get("title")), title: requiredText(formData.get("title"), "Planlama başlığı zorunludur."),
content: cleanText(formData.get("content")), 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) { export async function createProjectPlanningSectionRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const payload = readPlanningSectionPayload(formData); const payload = sectionPayload(formData);
service.addPlanningSection(actor, payload);
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}`);
}
revalidatePath("/projects"); revalidatePath("/projects");
revalidatePath(`/projects/${payload.project_id}`); revalidatePath(`/projects/${payload.projectId}`);
} }
export async function updateProjectPlanningSectionRecord(formData: FormData) { export async function updateProjectPlanningSectionRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); const id = requiredText(formData.get("id"), "Planlama alanı bulunamadı.");
const payload = readPlanningSectionPayload(formData); const payload = sectionPayload(formData);
if (!service.listPlanningSections(actor, payload.projectId).some((section) => section.id === id)) {
if (!id || !payload.project_id || !payload.title) { throw new Error("Planlama alanı bu projeye ait değil.");
throw new Error("Planlama alanını güncellemek için kayıt kimliği, proje ve başlık zorunludur.");
} }
service.updatePlanningSection(actor, id, {
const { error } = await supabase category: payload.category,
.from("project_planning_sections") title: payload.title,
.update({ content: payload.content,
category: payload.category, sortOrder: payload.sortOrder,
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}`);
}
revalidatePath("/projects"); revalidatePath("/projects");
revalidatePath(`/projects/${payload.project_id}`); revalidatePath(`/projects/${payload.projectId}`);
} }
export async function deleteProjectPlanningSectionRecord(formData: FormData) { export async function deleteProjectPlanningSectionRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); const id = requiredText(formData.get("id"), "Silinecek planlama alanı bulunamadı.");
const projectId = cleanText(formData.get("project_id")); const projectId = requiredText(formData.get("project_id"), "Proje zorunludur.");
if (!service.listPlanningSections(actor, projectId).some((section) => section.id === id)) {
if (!id || !projectId) { throw new Error("Planlama alanı bu projeye ait değil.");
throw new Error("Silinecek planlama alanı bulunamadı.");
} }
service.deletePlanningSection(actor, id);
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}`);
}
revalidatePath("/projects"); revalidatePath("/projects");
revalidatePath(`/projects/${projectId}`); revalidatePath(`/projects/${projectId}`);
} }
export async function updateRevisionStatus(id: string, projectId: string, status: string) { export async function updateRevisionStatus(id: string, projectId: string, status: string) {
const { supabase } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
service.updateRevisionStatus(actor, id, enumValue(status, REVISION_STATUSES, "pending"), projectId);
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}`);
}
revalidatePath(`/projects/${projectId}`); revalidatePath(`/projects/${projectId}`);
} }
export async function updateProjectSettings(projectId: string, progressType: "manual" | "auto", progress: number, revisionQuota: number) { export async function updateProjectSettings(projectId: string, progressType: "manual" | "auto", progress: number, revisionQuota: number) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
service.updateProject(actor, projectId, {
if (!projectId) { progressType,
throw new Error("Proje ID zorunludur."); progress: Math.min(100, Math.max(0, Math.round(progress))),
} revisionQuota: Math.max(0, Math.round(revisionQuota)),
});
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}`);
}
revalidatePath("/projects"); revalidatePath("/projects");
revalidatePath(`/projects/${projectId}`); revalidatePath(`/projects/${projectId}`);
} }
+28 -119
View File
@@ -1,139 +1,48 @@
import { import { ProjectsClient, type ProjectClientOption, type ProjectListItem } from "@/app/(dashboard)/projects/projects-client";
ProjectsClient, import { requireFreelancerBackend } from "@/server/web/freelancer";
type ProjectClientOption,
type ProjectListItem,
} from "@/app/(dashboard)/projects/projects-client";
import { createClient } from "@/lib/supabase/server";
import { createServiceRoleClient } from "@/lib/supabase/admin";
type ProjectRow = {
id: string;
user_id: string;
client_id: string | null;
name: string;
type: "client_project" | "side_project";
description: string | null;
status: "planning" | "active" | "paused" | "completed" | "cancelled";
start_date: string | null;
due_date: string | null;
budget_amount: number | string | null;
currency: string;
progress: number;
cover_image_path: string | null;
cover_image_alt: string | null;
clients: { name: string } | { name: string }[] | null;
};
type TaskRow = {
project_id: string | null;
status: string | null;
};
export default async function ProjectsPage() { export default async function ProjectsPage() {
const supabase = await createClient(); const { actor, service } = await requireFreelancerBackend();
const { const projectRows = service.listProjects(actor);
data: { user }, const clientRows = service.listClients(actor);
} = await supabase.auth.getUser(); const taskRows = service.listTasks(actor);
const clientNames = new Map(clientRows.map((client) => [client.id, client.name]));
const taskStats = new Map<string, { total: number; done: number }>();
if (!user) { for (const task of taskRows) {
return null; if (!task.projectId || task.status === "cancelled") continue;
const stats = taskStats.get(task.projectId) ?? { total: 0, done: 0 };
stats.total += 1;
if (task.status === "done") stats.done += 1;
taskStats.set(task.projectId, stats);
} }
const [{ data: projectRows }, { data: clientRows }, { data: taskRows }] = const projects: ProjectListItem[] = projectRows.map((project) => {
await Promise.all([ const stats = taskStats.get(project.id) ?? { total: 0, done: 0 };
supabase
.from("projects")
.select(
"id, user_id, client_id, name, type, description, status, start_date, due_date, budget_amount, currency, progress, cover_image_path, cover_image_alt, clients(name)",
)
.eq("user_id", user.id)
.order("created_at", { ascending: false }),
supabase
.from("clients")
.select("id, name")
.eq("user_id", user.id)
.neq("status", "archived")
.order("name", { ascending: true }),
supabase.from("tasks").select("project_id, status").eq("user_id", user.id),
]);
const taskStats = countTasksByProject((taskRows || []) as TaskRow[]);
const clients = (clientRows || []) as ProjectClientOption[];
const signedUrls = await createProjectImageUrls(
((projectRows || []) as unknown as ProjectRow[])
.map((project) => project.cover_image_path)
.filter(Boolean) as string[],
);
const projects: ProjectListItem[] = ((projectRows || []) as unknown as ProjectRow[]).map((project) => {
const stats = taskStats.get(project.id) || { total: 0, done: 0 };
return { return {
id: project.id, id: project.id,
client_id: project.client_id, client_id: project.clientId,
clientName: getClientName(project.clients), clientName: project.clientId ? clientNames.get(project.clientId) ?? null : null,
name: project.name, name: project.name,
type: project.type, type: project.type,
description: project.description, description: project.description,
status: project.status, status: project.status,
start_date: project.start_date, start_date: project.startDate,
due_date: project.due_date, due_date: project.dueDate,
budget_amount: project.budget_amount === null ? null : Number(project.budget_amount), budget_amount: project.budgetAmountMinor == null ? null : project.budgetAmountMinor / 100,
currency: project.currency, currency: project.currency,
progress: project.progress, progress: project.progress,
cover_image_path: project.cover_image_path, cover_image_path: project.legacyCoverImagePath,
cover_image_alt: project.cover_image_alt, cover_image_alt: project.coverImageAlt,
coverImageUrl: project.cover_image_path ? signedUrls.get(project.cover_image_path) || null : null, coverImageUrl: project.legacyCoverImagePath,
taskCount: stats.total, taskCount: stats.total,
doneTaskCount: stats.done, doneTaskCount: stats.done,
}; };
}); });
const clients: ProjectClientOption[] = clientRows
.filter((client) => client.status !== "archived")
.sort((a, b) => a.name.localeCompare(b.name, "tr"))
.map(({ id, name }) => ({ id, name }));
return <ProjectsClient projects={projects} clients={clients} />; return <ProjectsClient projects={projects} clients={clients} />;
} }
async function createProjectImageUrls(
paths: string[],
) {
const admin = createServiceRoleClient();
const urls = new Map<string, string>();
const uniquePaths = Array.from(new Set(paths));
await Promise.all(
uniquePaths.map(async (path) => {
const { data } = await admin.storage
.from("project-assets")
.createSignedUrl(path, 60 * 15);
if (data?.signedUrl) {
urls.set(path, data.signedUrl);
}
}),
);
return urls;
}
function getClientName(client: ProjectRow["clients"]) {
if (!client) return null;
return Array.isArray(client) ? client[0]?.name || null : client.name;
}
function countTasksByProject(tasks: TaskRow[]) {
const statsByProject = new Map<string, { total: number; done: number }>();
for (const task of tasks) {
if (!task.project_id) continue;
const current = statsByProject.get(task.project_id) || { total: 0, done: 0 };
current.total += 1;
if (task.status === "done") {
current.done += 1;
}
statsByProject.set(task.project_id, current);
}
return statsByProject;
}
+81 -71
View File
@@ -1,90 +1,100 @@
'use server' "use server";
import { revalidatePath } from 'next/cache' import { eq } from "drizzle-orm";
import { headers } from "next/headers";
import { revalidatePath } from "next/cache";
import { auth } from "@/server/auth/auth";
import { getSqliteConnection } from "@/server/db/client";
import { appProfiles } from "@/server/db/schema";
import { domainActorFromSession } from "@/server/auth/domain-actor";
import { getFileService } from "@/server/files/runtime";
import { getPublicAiSettings, updateAiSettings } from "@/server/settings/ai";
import { requireFreelancerBackend } from "@/server/web/freelancer";
import { cleanText } from "@/server/web/form-data";
import { createServiceRoleClient } from '@/lib/supabase/admin' export async function loadSettings() {
import { createClient } from '@/lib/supabase/server' const { context, actor } = await requireFreelancerBackend();
const [firstName = "", ...lastNameParts] = context.profile.displayName.trim().split(/\s+/);
const ai = getPublicAiSettings(actor);
type ProfileUpdateData = { return {
first_name: string firstName,
last_name: string lastName: lastNameParts.join(" "),
avatar_url?: string avatarUrl: context.user.image ?? "",
aiProvider: ai.provider,
hasApiKey: ai.hasApiKey,
};
} }
export async function updateProfile(formData: FormData) { export async function updateProfile(formData: FormData) {
const supabase = await createClient() try {
const { context } = await requireFreelancerBackend();
const { const firstName = cleanText(formData.get("firstName"));
data: { user }, const lastName = cleanText(formData.get("lastName"));
} = await supabase.auth.getUser() if (!firstName || !lastName || firstName.length > 80 || lastName.length > 120) {
return { error: "Ad ve soyad zorunludur." };
if (!user) {
return { error: 'Kullanıcı bulunamadı.' }
}
const firstName = formData.get('firstName') as string
const lastName = formData.get('lastName') as string
const avatarFile = formData.get('avatar') as File | null
let avatarUrl: string | undefined
if (avatarFile && avatarFile.size > 0) {
const fileExt = avatarFile.name.split('.').pop()
const fileName = `${user.id}/${Math.random()}.${fileExt}`
const admin = createServiceRoleClient()
const { error: uploadError } = await admin.storage
.from('avatars')
.upload(fileName, avatarFile, { upsert: true })
if (uploadError) {
return {
error: `Profil fotoğrafı yüklenirken hata oluştu: ${uploadError.message}`,
}
} }
const { const displayName = `${firstName} ${lastName}`;
data: { publicUrl }, await auth.api.updateUser({
} = admin.storage.from('avatars').getPublicUrl(fileName) headers: await headers(),
body: { name: displayName },
});
getSqliteConnection().db
.update(appProfiles)
.set({ displayName, updatedAt: new Date() })
.where(eq(appProfiles.authUserId, context.user.id))
.run();
avatarUrl = publicUrl const avatar = formData.get("avatar");
if (avatar instanceof File && avatar.size > 0) {
getFileService().upload(domainActorFromSession(context), {
kind: "avatar",
originalName: avatar.name,
claimedMimeType: avatar.type,
bytes: new Uint8Array(await avatar.arrayBuffer()),
});
}
revalidatePath("/settings");
revalidatePath("/", "layout");
return { success: true };
} catch (error) {
return { error: error instanceof Error ? error.message : "Profil güncellenemedi." };
} }
const updateData: ProfileUpdateData = {
first_name: firstName,
last_name: lastName,
}
if (avatarUrl) {
updateData.avatar_url = avatarUrl
}
const { error } = await supabase.from('profiles').upsert({
id: user.id,
...updateData,
})
if (error) {
return { error: `Profil güncellenirken hata oluştu: ${error.message}` }
}
revalidatePath('/settings')
return { success: true }
} }
export async function updatePassword(formData: FormData) { export async function updatePassword(formData: FormData) {
const supabase = await createClient() const currentPassword = cleanText(formData.get("currentPassword"));
const password = formData.get('password') as string const newPassword = cleanText(formData.get("password"));
if (!password || password.length < 6) { if (!currentPassword || !newPassword || newPassword.length < 8) {
return { error: ifre en az 6 karakter olmalıdır.' } return { error: "Mevcut şifre zorunludur; yeni şifre en az 8 karakter olmalıdır." };
} }
const { error } = await supabase.auth.updateUser({ password }) try {
await requireFreelancerBackend();
await auth.api.changePassword({
headers: await headers(),
body: {
currentPassword,
newPassword,
revokeOtherSessions: true,
},
});
return { success: true };
} catch {
return { error: "Mevcut şifre doğrulanamadı veya şifre güncellenemedi." };
}
}
if (error) { export async function saveAiSettings(provider: string, apiKey: string) {
return { error: `Şifre güncellenirken hata oluştu: ${error.message}` } try {
const { actor } = await requireFreelancerBackend();
const settings = updateAiSettings(actor, { provider, apiKey });
revalidatePath("/settings");
return { success: true, hasApiKey: settings.hasApiKey };
} catch (error) {
return { error: error instanceof Error ? error.message : "Ayarlar kaydedilemedi." };
} }
return { success: true }
} }
+33 -66
View File
@@ -1,9 +1,9 @@
"use client"; "use client";
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { AlertTriangle, Blocks, Brain, Key, Save, Shield, User } from "lucide-react"; import Image from "next/image";
import { updatePassword, updateProfile } from "./actions"; import { Blocks, Brain, Key, Save, Shield, User } from "lucide-react";
import { createClient } from "@/lib/supabase/client"; import { loadSettings, saveAiSettings, updatePassword, updateProfile } from "./actions";
import { Button, Card, CardContent, Input, Label } from "poyraz-ui/atoms"; import { Button, Card, CardContent, Input, Label } from "poyraz-ui/atoms";
import { toast } from "poyraz-ui/molecules"; import { toast } from "poyraz-ui/molecules";
@@ -23,9 +23,7 @@ export default function SettingsPage() {
// AI States // AI States
const [aiProvider, setAiProvider] = useState<AiProvider>("gemini"); const [aiProvider, setAiProvider] = useState<AiProvider>("gemini");
const [apiKey, setApiKey] = useState(""); const [apiKey, setApiKey] = useState("");
const [hasApiKey, setHasApiKey] = useState(false);
// Supabase
const [supabase] = useState(() => createClient());
const tabs = [ const tabs = [
{ name: "Profile & Account", icon: User }, { name: "Profile & Account", icon: User },
@@ -37,42 +35,18 @@ export default function SettingsPage() {
let isActive = true; let isActive = true;
const fetchData = async () => { const fetchData = async () => {
const { data: { user } } = await supabase.auth.getUser(); const settings = await loadSettings();
if (!user || !isActive) return; if (!isActive) return;
setFirstName(settings.firstName);
// 1. Fetch Profile setLastName(settings.lastName);
const { data: profile } = await supabase setAvatarUrl(settings.avatarUrl);
.from("profiles") setAiProvider(settings.aiProvider);
.select("*") setHasApiKey(settings.hasApiKey);
.eq("id", user.id)
.single();
if (profile && isActive) {
setFirstName(profile.first_name || "");
setLastName(profile.last_name || "");
setAvatarUrl(profile.avatar_url || "");
}
// 2. Fetch User Settings from Supabase
const { data: settings } = await supabase
.from("app_settings")
.select("*")
.eq("user_id", user.id)
.single();
if (settings && isActive) {
setAiProvider((settings.ai_provider as AiProvider) || "gemini");
setApiKey(settings.api_key || "");
// Also sync to local storage for existing API route calls if they use it
localStorage.setItem("mindspace_ai_provider", settings.ai_provider || "gemini");
localStorage.setItem("mindspace_api_key", settings.api_key || "");
}
}; };
void fetchData(); void fetchData();
return () => { isActive = false; }; return () => { isActive = false; };
}, [supabase]); }, []);
const handleProfileAction = async (formData: FormData) => { const handleProfileAction = async (formData: FormData) => {
const response = await updateProfile(formData); const response = await updateProfile(formData);
@@ -96,32 +70,14 @@ export default function SettingsPage() {
}; };
const handleSaveAI = async () => { const handleSaveAI = async () => {
try { const response = await saveAiSettings(aiProvider, apiKey);
const { data: { user } } = await supabase.auth.getUser(); if (response.error) {
if (!user) throw new Error("Giriş yapılmamış"); toast.error(response.error);
return;
// Save to Supabase app_settings table
const { error } = await supabase
.from("app_settings")
.upsert({
user_id: user.id,
ai_provider: aiProvider,
ai_model: null, // Reset to allow default model fallback
api_key: apiKey,
updated_at: new Date().toISOString()
}, { onConflict: 'user_id' });
if (error) throw error;
// Sync to localStorage as a redundant fallback
localStorage.setItem("mindspace_ai_provider", aiProvider);
localStorage.setItem("mindspace_api_key", apiKey);
toast.success("Yapay Zeka ayarları kaydedildi!");
} catch (e: any) {
console.error(e);
toast.error("Hata oluştu, veritabanına kaydedilemedi.");
} }
setHasApiKey(Boolean(response.hasApiKey));
setApiKey("");
toast.success("Yapay Zeka ayarları kaydedildi!");
}; };
return ( return (
@@ -173,7 +129,14 @@ export default function SettingsPage() {
<form action={handleProfileAction} className="space-y-6 max-w-xl"> <form action={handleProfileAction} className="space-y-6 max-w-xl">
<div className="flex items-center gap-4 mb-6"> <div className="flex items-center gap-4 mb-6">
{avatarUrl ? ( {avatarUrl ? (
<img src={avatarUrl} alt="Avatar" className="h-16 w-16 rounded-full border border-border object-cover" /> <Image
src={avatarUrl}
alt="Avatar"
width={64}
height={64}
unoptimized
className="h-16 w-16 rounded-full border border-border object-cover"
/>
) : ( ) : (
<div className="flex h-16 w-16 items-center justify-center rounded-full border border-border bg-muted/50"> <div className="flex h-16 w-16 items-center justify-center rounded-full border border-border bg-muted/50">
<User className="h-8 w-8 text-muted-foreground" /> <User className="h-8 w-8 text-muted-foreground" />
@@ -211,9 +174,13 @@ export default function SettingsPage() {
<CardContent className="p-6 sm:p-8"> <CardContent className="p-6 sm:p-8">
<h2 className="text-xl font-bold mb-6 text-foreground">Şifre İşlemleri</h2> <h2 className="text-xl font-bold mb-6 text-foreground">Şifre İşlemleri</h2>
<form ref={formRef} action={handlePasswordAction} className="space-y-6 max-w-xl"> <form ref={formRef} action={handlePasswordAction} className="space-y-6 max-w-xl">
<div className="space-y-2">
<Label htmlFor="currentPassword">Mevcut Şifre</Label>
<Input id="currentPassword" name="currentPassword" type="password" required />
</div>
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor="password">Yeni Şifre</Label> <Label htmlFor="password">Yeni Şifre</Label>
<Input id="password" name="password" type="password" minLength={6} placeholder="En az 6 karakter" required /> <Input id="password" name="password" type="password" minLength={8} placeholder="En az 8 karakter" required />
</div> </div>
<div className="flex items-center gap-4 pt-4"> <div className="flex items-center gap-4 pt-4">
<Button type="submit" className="gap-2"> <Button type="submit" className="gap-2">
@@ -269,7 +236,7 @@ export default function SettingsPage() {
type="password" type="password"
value={apiKey} value={apiKey}
onChange={(e) => setApiKey(e.target.value)} onChange={(e) => setApiKey(e.target.value)}
placeholder="sk-..." placeholder={hasApiKey ? "Kayıtlı anahtarı korumak için boş bırakın" : "sk-..."}
/> />
</div> </div>
</div> </div>
+56 -161
View File
@@ -1,193 +1,88 @@
"use server"; "use server";
import { createClient } from "@/lib/supabase/server";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { cleanText, optionalDate, requiredText } from "@/server/web/form-data";
import { requireFreelancerBackend } from "@/server/web/freelancer";
const TASK_STATUSES = ["todo", "in_progress", "done"] as const; const TASK_STATUSES = ["todo", "in_progress", "done"] as const;
const TASK_PRIORITIES = ["low", "medium", "high", "urgent"] as const; const TASK_PRIORITIES = ["low", "medium", "high", "urgent"] as const;
function cleanText(value: FormDataEntryValue | null) { function enumValue<T extends readonly string[]>(value: FormDataEntryValue | string | null, values: T, fallback: T[number]): T[number] {
const text = typeof value === "string" ? value.trim() : ""; return typeof value === "string" && values.includes(value) ? value as T[number] : fallback;
return text.length > 0 ? text : null;
} }
function cleanRelationId(value: FormDataEntryValue | null) { function minutes(value: FormDataEntryValue | null): number | null {
const id = cleanText(value); const parsed = Number(value);
return id && id !== "__none" ? id : null; return Number.isFinite(parsed) && parsed >= 0 ? Math.round(parsed) : null;
} }
function readStatus(value: FormDataEntryValue | null) { function payload(formData: FormData) {
const status = typeof value === "string" ? value : "todo"; const dueAt = optionalDate(formData.get("due_at"));
return TASK_STATUSES.includes(status as (typeof TASK_STATUSES)[number])
? status
: "todo";
}
function readPriority(value: FormDataEntryValue | null) {
const priority = typeof value === "string" ? value : "medium";
return TASK_PRIORITIES.includes(priority as (typeof TASK_PRIORITIES)[number])
? priority
: "medium";
}
function readMinutes(value: FormDataEntryValue | null) {
const number = Number(value);
return Number.isFinite(number) && number >= 0 ? Math.round(number) : null;
}
async function getCurrentUserId() {
const supabase = await createClient();
const {
data: { user },
error,
} = await supabase.auth.getUser();
if (error || !user) {
throw new Error("Görev işlemi için giriş yapmış kullanıcı bulunamadı.");
}
return { supabase, userId: user.id };
}
function readPayload(formData: FormData) {
return { return {
title: cleanText(formData.get("title")), title: requiredText(formData.get("title"), "Görev başlığı zorunludur."),
description: cleanText(formData.get("description")), description: cleanText(formData.get("description")),
status: readStatus(formData.get("status")), status: enumValue(formData.get("status"), TASK_STATUSES, "todo"),
priority: readPriority(formData.get("priority")), priority: enumValue(formData.get("priority"), TASK_PRIORITIES, "medium"),
client_id: cleanRelationId(formData.get("client_id")), clientId: cleanText(formData.get("client_id")),
project_id: cleanRelationId(formData.get("project_id")), projectId: cleanText(formData.get("project_id")),
due_at: cleanText(formData.get("due_at")), scheduledDate: dueAt?.toISOString().slice(0, 10) ?? null,
estimated_minutes: readMinutes(formData.get("estimated_minutes")), dueAt,
actual_minutes: readMinutes(formData.get("actual_minutes")), estimatedMinutes: minutes(formData.get("estimated_minutes")),
is_public_to_client: formData.get("is_public_to_client") === "on", actualMinutes: minutes(formData.get("actual_minutes")),
isPublicToClient: formData.get("is_public_to_client") === "on",
}; };
} }
export async function createTaskRecord(formData: FormData) { function completeRelations(
const { supabase, userId } = await getCurrentUserId(); value: ReturnType<typeof payload>,
const payload = readPayload(formData); service: Awaited<ReturnType<typeof requireFreelancerBackend>>["service"],
actor: Awaited<ReturnType<typeof requireFreelancerBackend>>["actor"],
if (!payload.title) { ) {
throw new Error("Görev başlığı zorunludur."); const project = value.projectId ? service.getProject(actor, value.projectId) : null;
} return { ...value, clientId: value.clientId ?? project?.clientId ?? null };
}
const { error } = await supabase.from("tasks").insert({
user_id: userId,
date: payload.due_at || new Date().toISOString(),
...payload,
});
if (error) {
throw new Error(`Görev eklenemedi: ${error.message}`);
}
function revalidate(projectId?: string | null) {
revalidatePath("/tasks"); revalidatePath("/tasks");
revalidatePath("/projects");
if (projectId) revalidatePath(`/projects/${projectId}`);
}
if (payload.project_id) { export async function createTaskRecord(formData: FormData) {
revalidatePath(`/projects/${payload.project_id}`); const { actor, service } = await requireFreelancerBackend();
} const value = completeRelations(payload(formData), service, actor);
service.createTask(actor, value);
revalidate(value.projectId);
} }
export async function updateTaskRecord(formData: FormData) { export async function updateTaskRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const id = cleanText(formData.get("id")); const id = requiredText(formData.get("id"), "Görev kaydı bulunamadı.");
const payload = readPayload(formData); const value = completeRelations(payload(formData), service, actor);
const current = service.listTasks(actor).find((task) => task.id === id);
if (!id || !payload.title) { service.updateTask(actor, id, value);
throw new Error("Görev güncellemek için başlık ve kayıt kimliği zorunludur."); revalidate(value.projectId);
} if (current?.projectId !== value.projectId) revalidate(current?.projectId);
const { error } = await supabase
.from("tasks")
.update(payload)
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Görev güncellenemedi: ${error.message}`);
}
revalidatePath("/tasks");
if (payload.project_id) {
revalidatePath(`/projects/${payload.project_id}`);
}
} }
export async function completeTaskRecord(formData: FormData) { export async function completeTaskRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const id = requiredText(formData.get("id"), "Tamamlanacak görev bulunamadı.");
const id = cleanText(formData.get("id")); const projectId = cleanText(formData.get("project_id"));
const projectId = cleanRelationId(formData.get("project_id")); const { actor, service } = await requireFreelancerBackend();
service.updateTask(actor, id, { status: "done" });
if (!id) { revalidate(projectId);
throw new Error("Tamamlanacak görev bulunamadı.");
}
const { error } = await supabase
.from("tasks")
.update({ status: "done" })
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Görev tamamlanamadı: ${error.message}`);
}
revalidatePath("/tasks");
if (projectId) {
revalidatePath(`/projects/${projectId}`);
}
} }
export async function updateTaskStatusRecord(taskId: string, status: string, projectId?: string) { export async function updateTaskStatusRecord(taskId: string, status: string, projectId?: string) {
const { supabase, userId } = await getCurrentUserId(); const { actor, service } = await requireFreelancerBackend();
const nextStatus = readStatus(status); service.updateTask(actor, taskId, { status: enumValue(status, TASK_STATUSES, "todo") });
revalidate(projectId);
if (!taskId) {
throw new Error("Durumu güncellenecek görev bulunamadı.");
}
const { error } = await supabase
.from("tasks")
.update({ status: nextStatus })
.eq("id", taskId)
.eq("user_id", userId);
if (error) {
throw new Error(`Görev durumu güncellenemedi: ${error.message}`);
}
revalidatePath("/tasks");
if (projectId) {
revalidatePath(`/projects/${projectId}`);
}
} }
export async function deleteTaskRecord(formData: FormData) { export async function deleteTaskRecord(formData: FormData) {
const { supabase, userId } = await getCurrentUserId(); const id = requiredText(formData.get("id"), "Silinecek görev bulunamadı.");
const id = cleanText(formData.get("id")); const projectId = cleanText(formData.get("project_id"));
const projectId = cleanRelationId(formData.get("project_id")); const { actor, service } = await requireFreelancerBackend();
service.deleteTask(actor, id);
if (!id) { revalidate(projectId);
throw new Error("Silinecek görev bulunamadı.");
}
const { error } = await supabase
.from("tasks")
.delete()
.eq("id", id)
.eq("user_id", userId);
if (error) {
throw new Error(`Görev silinemedi: ${error.message}`);
}
revalidatePath("/tasks");
if (projectId) {
revalidatePath(`/projects/${projectId}`);
}
} }
+31 -87
View File
@@ -1,93 +1,37 @@
import { import { TasksClient, type TaskListItem, type TaskRelationOption } from "@/app/(dashboard)/tasks/tasks-client";
TasksClient, import { requireFreelancerBackend } from "@/server/web/freelancer";
type TaskListItem,
type TaskRelationOption,
} from "@/app/(dashboard)/tasks/tasks-client";
import { createClient } from "@/lib/supabase/server";
type TaskRow = {
id: string;
title: string;
description: string | null;
status: "todo" | "in_progress" | "done";
priority: "low" | "medium" | "high" | "urgent";
due_at: string | null;
estimated_minutes: number | null;
actual_minutes: number | null;
client_id: string | null;
project_id: string | null;
created_at: string;
clients: { name: string } | { name: string }[] | null;
projects: { name: string } | { name: string }[] | null;
};
export default async function TasksPage() { export default async function TasksPage() {
const supabase = await createClient(); const { actor, service } = await requireFreelancerBackend();
const { const taskRows = service.listTasks(actor);
data: { user }, const clientRows = service.listClients(actor);
} = await supabase.auth.getUser(); const projectRows = service.listProjects(actor);
const clientNames = new Map(clientRows.map((item) => [item.id, item.name]));
const projectNames = new Map(projectRows.map((item) => [item.id, item.name]));
if (!user) { const tasks: TaskListItem[] = taskRows
return null; .filter((task) => task.status !== "cancelled")
} .map((task) => ({
id: task.id,
const [{ data: taskRows }, { data: clientRows }, { data: projectRows }] = title: task.title,
await Promise.all([ description: task.description,
supabase status: task.status as TaskListItem["status"],
.from("tasks") priority: task.priority,
.select( due_at: task.dueAt?.toISOString() ?? null,
"id, title, description, status, priority, due_at, estimated_minutes, actual_minutes, client_id, project_id, created_at, clients(name), projects(name)", estimated_minutes: task.estimatedMinutes,
) actual_minutes: task.actualMinutes,
.eq("user_id", user.id) client_id: task.clientId,
.order("created_at", { ascending: false }), clientName: task.clientId ? clientNames.get(task.clientId) ?? null : null,
supabase project_id: task.projectId,
.from("clients") projectName: task.projectId ? projectNames.get(task.projectId) ?? null : null,
.select("id, name") created_at: task.createdAt.toISOString(),
.eq("user_id", user.id) }));
.neq("status", "archived") const clients: TaskRelationOption[] = clientRows
.order("name", { ascending: true }), .filter((client) => client.status !== "archived")
supabase .map(({ id, name }) => ({ id, name }));
.from("projects") const projects: TaskRelationOption[] = projectRows
.select("id, name, client_id") .filter((project) => project.status !== "cancelled")
.eq("user_id", user.id) .map(({ id, name, clientId }) => ({ id, name, client_id: clientId }));
.neq("status", "cancelled")
.order("name", { ascending: true }),
]);
const clients = (clientRows || []) as TaskRelationOption[];
const projects = (projectRows || []) as TaskRelationOption[];
const tasks: TaskListItem[] = ((taskRows || []) as unknown as TaskRow[]).map((task) => ({
id: task.id,
title: task.title,
description: task.description,
status: normalizeStatus(task.status),
priority: normalizePriority(task.priority),
due_at: task.due_at,
estimated_minutes: task.estimated_minutes,
actual_minutes: task.actual_minutes,
client_id: task.client_id,
clientName: getRelationName(task.clients),
project_id: task.project_id,
projectName: getRelationName(task.projects),
created_at: task.created_at,
}));
return <TasksClient tasks={tasks} clients={clients} projects={projects} />; return <TasksClient tasks={tasks} clients={clients} projects={projects} />;
} }
function getRelationName(relation: TaskRow["clients"] | TaskRow["projects"]) {
if (!relation) return null;
return Array.isArray(relation) ? relation[0]?.name || null : relation.name;
}
function normalizeStatus(status: string): TaskListItem["status"] {
return status === "in_progress" || status === "done" ? status : "todo";
}
function normalizePriority(priority: string): TaskListItem["priority"] {
if (priority === "low" || priority === "high" || priority === "urgent") {
return priority;
}
return "medium";
}
+8 -3
View File
@@ -1,14 +1,19 @@
import { PortalShell } from "@/components/layout/portal-shell"; import { PortalShell } from "@/components/layout/portal-shell";
import { requireClientUser } from "@/server/auth/session";
import { getPublicBranding } from "@/server/branding/runtime"; import { getPublicBranding } from "@/server/branding/runtime";
import { requirePortalBackend } from "@/server/web/portal";
export default async function PortalLayout({ export default async function PortalLayout({
children, children,
}: Readonly<{ }: Readonly<{
children: React.ReactNode; children: React.ReactNode;
}>) { }>) {
const { user, profile } = await requireClientUser(); const { context, actor, service } = await requirePortalBackend();
const { user, profile } = context;
const branding = getPublicBranding(); const branding = getPublicBranding();
const projects = service.listProjects(actor);
const progress = projects.length
? Math.round(projects.reduce((sum, project) => sum + project.progress, 0) / projects.length)
: 0;
const displayName = profile.displayName || user.name || user.email.split("@")[0] || "Müşteri"; const displayName = profile.displayName || user.name || user.email.split("@")[0] || "Müşteri";
const shortName = const shortName =
@@ -34,7 +39,7 @@ export default async function PortalLayout({
shortName, shortName,
avatarUrl: user.image || null, avatarUrl: user.image || null,
}} }}
progress={0} progress={progress}
> >
{children} {children}
</PortalShell> </PortalShell>
+49 -95
View File
@@ -1,51 +1,22 @@
import { createClient } from "@/lib/supabase/server";
import { Card, CardContent, Badge } from "poyraz-ui/atoms"; import { Card, CardContent, Badge } from "poyraz-ui/atoms";
import { FolderKanban, CheckCircle2, Clock, Activity, BarChart } from "lucide-react"; import { FolderKanban, CheckCircle2, Clock, Activity, BarChart, type LucideIcon } from "lucide-react";
import Link from "next/link"; import Link from "next/link";
import { format } from "date-fns"; import { format } from "date-fns";
import { tr } from "date-fns/locale"; import { tr } from "date-fns/locale";
import { requirePortalBackend } from "@/server/web/portal";
export default async function PortalDashboardPage() { export default async function PortalDashboardPage() {
const supabase = await createClient(); const { context, actor, service } = await requirePortalBackend();
const { data: { user } } = await supabase.auth.getUser(); const client = service.getClient(actor, context.profile.clientId!);
const projects = service.listProjects(actor);
if (!user) return null; const activeProjects = projects.filter((project) => project.status !== "completed" && project.status !== "cancelled");
const completedProjects = projects.filter((project) => project.status === "completed");
// 1. Get the Client record const avgProgress = projects.length
const { data: clientData } = await supabase ? Math.round(projects.reduce((sum, project) => sum + project.progress, 0) / projects.length)
.from("clients") : 0;
.select("id, name, company_name")
.eq("client_auth_id", user.id)
.single();
if (!clientData) {
return (
<div className="flex flex-col items-center justify-center h-[50vh] text-center gap-4">
<h2 className="text-2xl font-semibold">Hesabınız Henüz Aktif Değil</h2>
<p className="text-muted-foreground max-w-md">
Freelancer'ınız sizin için hesabı oluşturdu ancak müşteri kartınızla henüz eşleşmedi veya bir hata oluştu. Lütfen iletişime geçin.
</p>
</div>
);
}
// 2. Get Projects
const { data: projectsData } = await supabase
.from("projects")
.select("id, name, status, progress, due_date, created_at")
.eq("client_id", clientData.id)
.order("created_at", { ascending: false });
const projects = projectsData || [];
const activeProjects = projects.filter(p => p.status !== 'completed' && p.status !== 'cancelled');
const completedProjects = projects.filter(p => p.status === 'completed');
const avgProgress = projects.length > 0 ? (projects.reduce((sum, p) => sum + (p.progress || 0), 0) / projects.length).toFixed(0) : "0";
return ( return (
<div className="mx-auto flex max-w-7xl flex-col gap-6"> <div className="mx-auto flex max-w-7xl flex-col gap-6">
{/* Header */}
<div className="flex flex-col gap-4 border-b border-border pb-5 lg:flex-row lg:items-end lg:justify-between"> <div className="flex flex-col gap-4 border-b border-border pb-5 lg:flex-row lg:items-end lg:justify-between">
<div className="space-y-2"> <div className="space-y-2">
<div className="flex items-center gap-2 text-sm text-muted-foreground"> <div className="flex items-center gap-2 text-sm text-muted-foreground">
@@ -53,24 +24,20 @@ export default async function PortalDashboardPage() {
Genel Bakış Genel Bakış
</div> </div>
<div> <div>
<h1 className="text-3xl font-semibold tracking-normal text-foreground"> <h1 className="text-3xl font-semibold tracking-normal text-foreground">Müşteri Paneli</h1>
Müşteri Paneli
</h1>
<p className="mt-1 max-w-2xl text-sm text-muted-foreground"> <p className="mt-1 max-w-2xl text-sm text-muted-foreground">
Hoş geldiniz, {clientData.name}. Aktif projelerinizi ve ilerlemeleri buradan takip edin. Hoş geldiniz, {client.name}. Aktif projelerinizi ve ilerlemeleri buradan takip edin.
</p> </p>
</div> </div>
</div> </div>
</div> </div>
{/* KPI Cards */}
<div className="grid gap-4 md:grid-cols-3"> <div className="grid gap-4 md:grid-cols-3">
<StatCard label="Aktif Projeler" value={activeProjects.length.toString()} icon={FolderKanban} tone="blue" /> <StatCard label="Aktif Projeler" value={String(activeProjects.length)} icon={FolderKanban} tone="blue" />
<StatCard label="Tamamlanan" value={completedProjects.length.toString()} icon={CheckCircle2} tone="green" /> <StatCard label="Tamamlanan" value={String(completedProjects.length)} icon={CheckCircle2} tone="green" />
<StatCard label="Ortalama İlerleme" value={`%${avgProgress}`} icon={BarChart} tone="amber" /> <StatCard label="Ortalama İlerleme" value={`%${avgProgress}`} icon={BarChart} tone="amber" />
</div> </div>
{/* Projects */}
<div className="space-y-4"> <div className="space-y-4">
<h2 className="text-xl font-semibold">Tüm Projeleriniz</h2> <h2 className="text-xl font-semibold">Tüm Projeleriniz</h2>
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3"> <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
@@ -78,64 +45,52 @@ export default async function PortalDashboardPage() {
<div className="col-span-full py-10 text-center border rounded-lg border-dashed text-muted-foreground"> <div className="col-span-full py-10 text-center border rounded-lg border-dashed text-muted-foreground">
Henüz size atanmış bir proje bulunmuyor. Henüz size atanmış bir proje bulunmuyor.
</div> </div>
) : ( ) : projects.map((project) => (
projects.map(project => ( <Link key={project.id} href={`/portal/projects/${project.id}`}>
<Link key={project.id} href={`/portal/projects/${project.id}`}> <Card className="hover:border-primary/50 transition-colors h-full">
<Card className="hover:border-primary/50 transition-colors h-full"> <CardContent className="p-5 flex flex-col h-full justify-between gap-4">
<CardContent className="p-5 flex flex-col h-full justify-between gap-4"> <div className="space-y-3">
<div className="space-y-3"> <div className="flex items-start justify-between">
<div className="flex items-start justify-between"> <div className="flex items-center gap-2">
<div className="flex items-center gap-2"> <div className={`h-2 w-2 shrink-0 rounded-full ${project.status === "completed" ? "bg-emerald-500" : project.status === "active" ? "bg-blue-500" : "bg-amber-500"}`} />
<div className={`h-2 w-2 shrink-0 rounded-full ${project.status === 'completed' ? 'bg-emerald-500' : project.status === 'active' ? 'bg-blue-500' : 'bg-amber-500'}`} /> <h3 className="font-semibold text-base line-clamp-2 leading-tight">{project.name}</h3>
<h3 className="font-semibold text-base line-clamp-2 leading-tight">{project.name}</h3> </div>
</div>
<div className="flex items-center gap-2 flex-wrap">
<Badge variant={project.status === "completed" ? "secondary" : "default"} className="capitalize text-[10px] px-1.5 py-0">
{project.status === "completed" ? "Tamamlandı" : project.status === "active" ? "Aktif" : "Beklemede"}
</Badge>
{project.dueDate && (
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
<Clock className="h-3.5 w-3.5" />
<span>Teslim: {format(new Date(project.dueDate), "d MMM yyyy", { locale: tr })}</span>
</div> </div>
</div> )}
<div className="flex items-center gap-2 flex-wrap">
<Badge variant={project.status === 'completed' ? 'secondary' : 'default'} className="capitalize text-[10px] px-1.5 py-0">
{project.status === 'completed' ? 'Tamamlandı' : project.status === 'active' ? 'Aktif' : 'Beklemede'}
</Badge>
{project.due_date && (
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
<Clock className="h-3.5 w-3.5" />
<span>Teslim: {format(new Date(project.due_date), 'd MMM yyyy', { locale: tr })}</span>
</div>
)}
</div>
</div> </div>
</div>
<div className="space-y-1.5 mt-2"> <div className="space-y-1.5 mt-2">
<div className="flex items-center justify-between text-xs font-medium"> <div className="flex items-center justify-between text-xs font-medium">
<span className="text-muted-foreground">İlerleme</span> <span className="text-muted-foreground">İlerleme</span>
<span>%{project.progress}</span> <span>%{project.progress}</span>
</div>
<div className="h-2 w-full bg-secondary rounded-full overflow-hidden">
<div
className="h-full bg-primary transition-all duration-500"
style={{ width: `${project.progress}%` }}
/>
</div>
</div> </div>
</CardContent> <div className="h-2 w-full bg-secondary rounded-full overflow-hidden">
</Card> <div className="h-full bg-primary transition-all duration-500" style={{ width: `${project.progress}%` }} />
</Link> </div>
)) </div>
)} </CardContent>
</Card>
</Link>
))}
</div> </div>
</div> </div>
</div> </div>
); );
} }
function StatCard({ function StatCard({ label, value, icon: Icon, tone }: {
label,
value,
icon: Icon,
tone,
}: {
label: string; label: string;
value: string; value: string;
icon: any; icon: LucideIcon;
tone: "green" | "blue" | "amber"; tone: "green" | "blue" | "amber";
}) { }) {
const toneClass = { const toneClass = {
@@ -143,7 +98,6 @@ function StatCard({
blue: "bg-blue-50 text-blue-700", blue: "bg-blue-50 text-blue-700",
amber: "bg-amber-50 text-amber-700", amber: "bg-amber-50 text-amber-700",
}[tone]; }[tone];
return ( return (
<Card> <Card>
<CardContent className="flex items-center justify-between gap-3 p-4"> <CardContent className="flex items-center justify-between gap-3 p-4">
+15 -29
View File
@@ -1,36 +1,22 @@
"use server"; "use server";
import { createClient } from "@/lib/supabase/server";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { cleanText } from "@/server/web/form-data";
import { requirePortalBackend } from "@/server/web/portal";
export async function createRevisionRequest(projectId: string, clientId: string, formData: FormData) { export async function createRevisionRequest(projectId: string, formData: FormData) {
const supabase = await createClient(); try {
const { data: { user } } = await supabase.auth.getUser(); const { actor, service } = await requirePortalBackend();
const description = cleanText(formData.get("description"));
if (!description) return { error: "Revizyon açıklaması boş olamaz." };
if (!user) { service.requestRevision(actor, { projectId, description });
return { error: "Oturum süresi dolmuş." }; revalidatePath(`/portal/projects/${projectId}`);
revalidatePath("/portal/revisions");
return { success: true };
} catch (error) {
return {
error: error instanceof Error ? error.message : "Revizyon talebi oluşturulamadı.",
};
} }
const description = formData.get("description") as string;
if (!description?.trim()) {
return { error: "Revizyon açıklaması boş olamaz." };
}
const { error } = await supabase
.from("project_revisions")
.insert({
project_id: projectId,
client_id: clientId,
requested_by: user.id,
description,
status: "pending"
});
if (error) {
return { error: error.message };
}
revalidatePath(`/portal/projects/${projectId}`);
return { success: true };
} }
+58 -55
View File
@@ -1,67 +1,70 @@
import { createClient } from "@/lib/supabase/server";
import { notFound } from "next/navigation"; import { notFound } from "next/navigation";
import { PortalProjectClient } from "./portal-project-client"; import { DomainError } from "@/server/domain/errors";
import { requirePortalBackend } from "@/server/web/portal";
import {
PortalProjectClient,
type PortalPlanningSection,
type PortalProjectDetail,
type PortalRevision,
type PortalTask,
} from "./portal-project-client";
export default async function PortalProjectPage({ params }: { params: Promise<{ id: string }> }) { export default async function PortalProjectPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params; const { id } = await params;
const supabase = await createClient(); const { actor, service } = await requirePortalBackend();
const { data: { user } } = await supabase.auth.getUser(); let data: {
project: PortalProjectDetail;
sections: PortalPlanningSection[];
tasks: PortalTask[];
revisions: PortalRevision[];
};
if (!user) return null; try {
const row = service.getProject(actor, id);
// 1. Get Client Record const allowance = service.getRevisionAllowance(actor, id);
const { data: clientData } = await supabase data = {
.from("clients") project: {
.select("id") id: row.id,
.eq("client_auth_id", user.id) name: row.name,
.single(); description: row.description,
status: row.status,
if (!clientData) { progress: row.progress,
notFound(); due_date: row.dueDate,
revision_quota: allowance.remaining,
can_request_revision: allowance.canRequest,
},
sections: service.listPlanningSections(actor, id).map((section) => ({
id: section.id,
title: section.title,
content: section.content,
type: section.category,
})),
tasks: service.listTasks(actor, id)
.filter((task) => task.status !== "cancelled")
.map((task) => ({
id: task.id,
title: task.title,
status: task.status as PortalTask["status"],
date: task.dueAt?.toISOString() ?? task.scheduledDate,
})),
revisions: service.listRevisions(actor, id).map((revision) => ({
id: revision.id,
description: revision.description,
status: revision.status,
created_at: revision.createdAt.toISOString(),
})),
};
} catch (error) {
if (error instanceof DomainError && error.code === "NOT_FOUND") notFound();
throw error;
} }
// 2. Get Project
const { data: project, error } = await supabase
.from("projects")
.select("id, name, description, status, progress, due_date, revision_quota")
.eq("id", id)
.eq("client_id", clientData.id)
.single();
if (error || !project) {
notFound();
}
// 3. Get Planning Sections (Milestones etc.)
const { data: sectionsData } = await supabase
.from("project_planning_sections")
.select("*")
.eq("project_id", id)
.order("order_index", { ascending: true });
// 4. Get Public Tasks
const { data: tasksData } = await supabase
.from("tasks")
.select("*")
.eq("project_id", id)
.eq("is_public_to_client", true)
.order("date", { ascending: false });
// 5. Get Revisions
const { data: revisionsData } = await supabase
.from("project_revisions")
.select("id, description, status, created_at, requested_by")
.eq("project_id", id)
.eq("client_id", clientData.id)
.order("created_at", { ascending: false });
return ( return (
<PortalProjectClient <PortalProjectClient
project={project} project={data.project}
sections={sectionsData || []} sections={data.sections}
tasks={tasksData || []} tasks={data.tasks}
revisions={revisionsData || []} revisions={data.revisions}
clientId={clientData.id}
/> />
); );
} }
@@ -11,7 +11,46 @@ import { createRevisionRequest } from "./actions";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "poyraz-ui/molecules"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "poyraz-ui/molecules";
export function PortalProjectClient({ project, sections, tasks, revisions, clientId }: any) { export type PortalProjectDetail = {
id: string;
name: string;
description: string | null;
status: "planning" | "active" | "paused" | "completed" | "cancelled";
progress: number;
due_date: string | null;
revision_quota: number;
can_request_revision: boolean;
};
export type PortalPlanningSection = {
id: string;
title: string;
content: string | null;
type: string;
};
export type PortalTask = {
id: string;
title: string;
status: "todo" | "in_progress" | "done";
date: string | null;
};
export type PortalRevision = {
id: string;
description: string;
status: "pending" | "in_progress" | "completed" | "rejected";
created_at: string;
};
type PortalProjectClientProps = {
project: PortalProjectDetail;
sections: PortalPlanningSection[];
tasks: PortalTask[];
revisions: PortalRevision[];
};
export function PortalProjectClient({ project, sections, tasks, revisions }: PortalProjectClientProps) {
const [isSubmitting, setIsSubmitting] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false);
const [openRevision, setOpenRevision] = useState(false); const [openRevision, setOpenRevision] = useState(false);
@@ -20,19 +59,19 @@ export function PortalProjectClient({ project, sections, tasks, revisions, clien
setIsSubmitting(true); setIsSubmitting(true);
const formData = new FormData(e.currentTarget); const formData = new FormData(e.currentTarget);
try { try {
const res = await createRevisionRequest(project.id, clientId, formData); const res = await createRevisionRequest(project.id, formData);
if (res.error) throw new Error(res.error); if (res.error) throw new Error(res.error);
toast.success("Revizyon talebiniz başarıyla iletildi."); toast.success("Revizyon talebiniz başarıyla iletildi.");
setOpenRevision(false); setOpenRevision(false);
} catch (err: any) { } catch (error: unknown) {
toast.error(err.message); toast.error(error instanceof Error ? error.message : "Revizyon talebi oluşturulamadı.");
} finally { } finally {
setIsSubmitting(false); setIsSubmitting(false);
} }
}; };
const pendingRevisions = revisions.filter((r: any) => r.status === 'pending' || r.status === 'in_progress').length; const pendingRevisions = revisions.filter((revision) => revision.status === 'pending' || revision.status === 'in_progress').length;
const hasRevisionQuota = project.revision_quota === null || project.revision_quota > 0; const hasRevisionQuota = project.can_request_revision;
return ( return (
<div className="mx-auto flex max-w-7xl flex-col gap-8 animate-in fade-in slide-in-from-bottom-4 duration-500"> <div className="mx-auto flex max-w-7xl flex-col gap-8 animate-in fade-in slide-in-from-bottom-4 duration-500">
@@ -64,8 +103,8 @@ export function PortalProjectClient({ project, sections, tasks, revisions, clien
</div> </div>
)} )}
<div className="space-y-2"> <div className="space-y-2">
<Label>Lütfen yapılmasını istediğiniz değişiklikleri detaylıca açıklayın</Label> <Label htmlFor="revision-description">Lütfen yapılmasını istediğiniz değişiklikleri detaylıca açıklayın</Label>
<Textarea name="description" required rows={5} placeholder="Şu kısmın rengi mavi olabilir mi? Ayrıca metinleri güncelleyelim..." /> <Textarea id="revision-description" name="description" required rows={5} placeholder="Şu kısmın rengi mavi olabilir mi? Ayrıca metinleri güncelleyelim..." />
</div> </div>
</div> </div>
<DialogFooter> <DialogFooter>
@@ -138,15 +177,15 @@ export function PortalProjectClient({ project, sections, tasks, revisions, clien
<p className="text-sm text-muted-foreground italic">Listelenecek görev bulunmuyor.</p> <p className="text-sm text-muted-foreground italic">Listelenecek görev bulunmuyor.</p>
) : ( ) : (
<ul className="space-y-3 max-h-60 overflow-y-auto tiny-scrollbar pr-2"> <ul className="space-y-3 max-h-60 overflow-y-auto tiny-scrollbar pr-2">
{tasks.map((task: any) => ( {tasks.map((task) => (
<li key={task.id} className="text-sm flex gap-3 p-2 rounded hover:bg-muted/30 transition-colors"> <li key={task.id} className="text-sm flex gap-3 p-2 rounded hover:bg-muted/30 transition-colors">
{task.status === 'completed' || task.status === 'done' ? ( {task.status === 'done' ? (
<CheckCircle2 className="h-4 w-4 text-emerald-500 shrink-0 mt-0.5" /> <CheckCircle2 className="h-4 w-4 text-emerald-500 shrink-0 mt-0.5" />
) : ( ) : (
<div className="h-4 w-4 rounded-full border-2 border-muted-foreground/30 shrink-0 mt-0.5" /> <div className="h-4 w-4 rounded-full border-2 border-muted-foreground/30 shrink-0 mt-0.5" />
)} )}
<div> <div>
<span className={task.status === 'completed' || task.status === 'done' ? "text-muted-foreground" : "text-foreground font-medium"}> <span className={task.status === 'done' ? "text-muted-foreground" : "text-foreground font-medium"}>
{task.title} {task.title}
</span> </span>
{task.date && ( {task.date && (
@@ -171,7 +210,7 @@ export function PortalProjectClient({ project, sections, tasks, revisions, clien
</div> </div>
) : ( ) : (
<div className="space-y-4"> <div className="space-y-4">
{sections.map((section: any) => ( {sections.map((section) => (
<Card key={section.id}> <Card key={section.id}>
<CardContent className="p-5 space-y-3"> <CardContent className="p-5 space-y-3">
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
@@ -205,7 +244,7 @@ export function PortalProjectClient({ project, sections, tasks, revisions, clien
</div> </div>
) : ( ) : (
<div className="space-y-4"> <div className="space-y-4">
{revisions.map((rev: any) => ( {revisions.map((rev) => (
<Card key={rev.id} className="transition-colors hover:border-primary/30"> <Card key={rev.id} className="transition-colors hover:border-primary/30">
<CardContent className="p-5"> <CardContent className="p-5">
<div className="flex justify-between items-start mb-3"> <div className="flex justify-between items-start mb-3">
+31 -62
View File
@@ -1,37 +1,13 @@
import { createClient } from "@/lib/supabase/server";
import { Card, CardContent, Badge } from "poyraz-ui/atoms"; import { Card, CardContent, Badge } from "poyraz-ui/atoms";
import { FolderKanban, Clock } from "lucide-react"; import { FolderKanban, Clock } from "lucide-react";
import Link from "next/link"; import Link from "next/link";
import { format } from "date-fns"; import { format } from "date-fns";
import { tr } from "date-fns/locale"; import { tr } from "date-fns/locale";
import { requirePortalBackend } from "@/server/web/portal";
export default async function PortalProjectsPage() { export default async function PortalProjectsPage() {
const supabase = await createClient(); const { actor, service } = await requirePortalBackend();
const { data: { user } } = await supabase.auth.getUser(); const projects = service.listProjects(actor);
if (!user) return null;
const { data: clientData } = await supabase
.from("clients")
.select("id")
.eq("client_auth_id", user.id)
.single();
if (!clientData) {
return (
<div className="flex flex-col items-center justify-center h-[50vh] text-center gap-4">
<h2 className="text-2xl font-semibold">Hesabınız Henüz Aktif Değil</h2>
</div>
);
}
const { data: projectsData } = await supabase
.from("projects")
.select("id, name, status, progress, due_date")
.eq("client_id", clientData.id)
.order("created_at", { ascending: false });
const projects = projectsData || [];
return ( return (
<div className="mx-auto flex max-w-7xl flex-col gap-8 animate-in fade-in slide-in-from-bottom-4 duration-500"> <div className="mx-auto flex max-w-7xl flex-col gap-8 animate-in fade-in slide-in-from-bottom-4 duration-500">
@@ -46,44 +22,37 @@ export default async function PortalProjectsPage() {
<FolderKanban className="w-10 h-10 text-muted-foreground/50" /> <FolderKanban className="w-10 h-10 text-muted-foreground/50" />
Henüz size atanmış bir proje bulunmuyor. Henüz size atanmış bir proje bulunmuyor.
</div> </div>
) : ( ) : projects.map((project) => (
projects.map(project => ( <Link key={project.id} href={`/portal/projects/${project.id}`}>
<Link key={project.id} href={`/portal/projects/${project.id}`}> <Card className="hover:border-primary/50 transition-colors h-full">
<Card className="hover:border-primary/50 transition-colors h-full"> <CardContent className="p-5 flex flex-col h-full justify-between gap-4">
<CardContent className="p-5 flex flex-col h-full justify-between gap-4"> <div className="space-y-2">
<div className="space-y-2"> <div className="flex items-start justify-between">
<div className="flex items-start justify-between"> <h3 className="font-semibold text-lg line-clamp-2">{project.name}</h3>
<h3 className="font-semibold text-lg line-clamp-2">{project.name}</h3> <Badge variant={project.status === "completed" ? "secondary" : "default"} className="capitalize shrink-0">
<Badge variant={project.status === 'completed' ? 'secondary' : 'default'} className="capitalize shrink-0"> {project.status}
{project.status} </Badge>
</Badge>
</div>
{project.due_date && (
<div className="flex items-center gap-1.5 text-sm text-muted-foreground">
<Clock className="h-4 w-4" />
<span>Son Teslim: {format(new Date(project.due_date), 'd MMM yyyy', { locale: tr })}</span>
</div>
)}
</div> </div>
{project.dueDate && (
<div className="space-y-1"> <div className="flex items-center gap-1.5 text-sm text-muted-foreground">
<div className="flex items-center justify-between text-xs font-medium"> <Clock className="h-4 w-4" />
<span>İlerleme</span> <span>Son Teslim: {format(new Date(project.dueDate), "d MMM yyyy", { locale: tr })}</span>
<span>%{project.progress}</span>
</div>
<div className="h-2 w-full bg-secondary rounded-full overflow-hidden">
<div
className="h-full bg-primary transition-all duration-500"
style={{ width: `${project.progress}%` }}
/>
</div> </div>
)}
</div>
<div className="space-y-1">
<div className="flex items-center justify-between text-xs font-medium">
<span>İlerleme</span>
<span>%{project.progress}</span>
</div> </div>
</CardContent> <div className="h-2 w-full bg-secondary rounded-full overflow-hidden">
</Card> <div className="h-full bg-primary transition-all duration-500" style={{ width: `${project.progress}%` }} />
</Link> </div>
)) </div>
)} </CardContent>
</Card>
</Link>
))}
</div> </div>
</div> </div>
); );
+35 -84
View File
@@ -1,56 +1,16 @@
import { createClient } from "@/lib/supabase/server";
import { Card, CardContent, Badge } from "poyraz-ui/atoms"; import { Card, CardContent, Badge } from "poyraz-ui/atoms";
import { Clock, MessageSquare } from "lucide-react"; import { Clock, MessageSquare } from "lucide-react";
import Link from "next/link"; import Link from "next/link";
import { format } from "date-fns"; import { format } from "date-fns";
import { tr } from "date-fns/locale"; import { tr } from "date-fns/locale";
import { requirePortalBackend } from "@/server/web/portal";
type RevisionRow = {
id: string;
description: string;
status: string;
project_id: string;
created_at: string;
};
export default async function PortalRevisionsPage() { export default async function PortalRevisionsPage() {
const supabase = await createClient(); const { actor, service } = await requirePortalBackend();
const { data: { user } } = await supabase.auth.getUser(); const projects = service.listProjects(actor);
const projectNames = new Map(projects.map((project) => [project.id, project.name]));
if (!user) return null; const revisions = service.listPortalRevisions(actor)
.filter((revision) => projectNames.has(revision.projectId));
const { data: clientData } = await supabase
.from("clients")
.select("id")
.eq("client_auth_id", user.id)
.single();
if (!clientData) {
return (
<div className="flex flex-col items-center justify-center h-[50vh] text-center gap-4">
<h2 className="text-2xl font-semibold">Hesabınız Henüz Aktif Değil</h2>
</div>
);
}
const { data: projectsData } = await supabase
.from("projects")
.select("id, name")
.eq("client_id", clientData.id);
const projectIds = projectsData?.map(p => p.id) || [];
let revisions: RevisionRow[] = [];
if (projectIds.length > 0) {
const { data: revisionsData } = await supabase
.from("project_revisions")
.select("id, description, status, project_id, created_at")
.in("project_id", projectIds)
.order("created_at", { ascending: false });
revisions = revisionsData || [];
}
const getProjectName = (id: string) => projectsData?.find(p => p.id === id)?.name || "Bilinmeyen Proje";
return ( return (
<div className="mx-auto flex max-w-7xl flex-col gap-8 animate-in fade-in slide-in-from-bottom-4 duration-500"> <div className="mx-auto flex max-w-7xl flex-col gap-8 animate-in fade-in slide-in-from-bottom-4 duration-500">
@@ -65,47 +25,38 @@ export default async function PortalRevisionsPage() {
<MessageSquare className="w-10 h-10 text-muted-foreground/50" /> <MessageSquare className="w-10 h-10 text-muted-foreground/50" />
Henüz bir revizyon talebinde bulunmadınız. Henüz bir revizyon talebinde bulunmadınız.
</div> </div>
) : ( ) : revisions.map((revision) => (
revisions.map(rev => ( <Card key={revision.id} className="h-full">
<Card key={rev.id} className="h-full"> <CardContent className="p-5 flex flex-col h-full justify-between gap-4">
<CardContent className="p-5 flex flex-col h-full justify-between gap-4"> <div className="space-y-4">
<div className="space-y-4"> <div className="flex items-start justify-between gap-2 border-b border-border pb-3">
<div className="flex items-start justify-between gap-2 border-b border-border pb-3"> <div className="flex items-center gap-2 text-sm text-muted-foreground">
<div className="flex items-center gap-2 text-sm text-muted-foreground"> <Clock className="h-4 w-4" />
<Clock className="h-4 w-4" /> {format(revision.createdAt, "d MMM yyyy, HH:mm", { locale: tr })}
{format(new Date(rev.created_at), "d MMM yyyy, HH:mm", { locale: tr })}
</div>
<Badge variant={
rev.status === 'completed' ? 'default' :
rev.status === 'rejected' ? 'destructive' : 'secondary'
} className="capitalize shrink-0">
{rev.status === 'pending' ? 'Bekliyor' :
rev.status === 'in_progress' ? 'İşleniyor' :
rev.status === 'completed' ? 'Tamamlandı' : 'Reddedildi'}
</Badge>
</div> </div>
<Badge
<div className="flex flex-col gap-2"> variant={revision.status === "completed" ? "default" : revision.status === "rejected" ? "destructive" : "secondary"}
<span className="text-xs font-medium uppercase text-muted-foreground">Proje:</span> className="capitalize shrink-0"
<span className="text-sm font-semibold truncate bg-muted/30 p-2 rounded-md"> >
{getProjectName(rev.project_id)} {revision.status === "pending" ? "Bekliyor" : revision.status === "in_progress" ? "İşleniyor" : revision.status === "completed" ? "Tamamlandı" : "Reddedildi"}
</span> </Badge>
</div>
<p className="text-sm text-foreground whitespace-pre-wrap leading-relaxed">
{rev.description}
</p>
</div> </div>
<div className="flex flex-col gap-2">
<div className="flex items-center justify-end border-t border-border pt-4"> <span className="text-xs font-medium uppercase text-muted-foreground">Proje:</span>
<Link href={`/portal/projects/${rev.project_id}`} className="text-xs text-primary font-medium hover:underline"> <span className="text-sm font-semibold truncate bg-muted/30 p-2 rounded-md">
Projeye Git &rarr; {projectNames.get(revision.projectId)}
</Link> </span>
</div> </div>
</CardContent> <p className="text-sm text-foreground whitespace-pre-wrap leading-relaxed">{revision.description}</p>
</Card> </div>
)) <div className="flex items-center justify-end border-t border-border pt-4">
)} <Link href={`/portal/projects/${revision.projectId}`} className="text-xs text-primary font-medium hover:underline">
Projeye Git &rarr;
</Link>
</div>
</CardContent>
</Card>
))}
</div> </div>
</div> </div>
); );
+19 -66
View File
@@ -1,59 +1,16 @@
import { createClient } from "@/lib/supabase/server";
import { Card, CardContent, Badge } from "poyraz-ui/atoms"; import { Card, CardContent, Badge } from "poyraz-ui/atoms";
import { CheckCircle2, Clock, CalendarDays, KanbanSquare } from "lucide-react"; import { CheckCircle2, Clock, CalendarDays, KanbanSquare } from "lucide-react";
import Link from "next/link";
import { format } from "date-fns"; import { format } from "date-fns";
import { tr } from "date-fns/locale"; import { tr } from "date-fns/locale";
import { requirePortalBackend } from "@/server/web/portal";
type PortalTaskRow = {
id: string;
title: string;
status: string;
project_id: string;
created_at: string;
date: string | null;
priority: string | null;
};
export default async function PortalTasksPage() { export default async function PortalTasksPage() {
const supabase = await createClient(); const { actor, service } = await requirePortalBackend();
const { data: { user } } = await supabase.auth.getUser(); const projects = service.listProjects(actor);
const projectNames = new Map(projects.map((project) => [project.id, project.name]));
if (!user) return null; const tasks = service.listTasks(actor)
.filter((task) => task.projectId && projectNames.has(task.projectId) && task.status !== "cancelled")
const { data: clientData } = await supabase .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
.from("clients")
.select("id")
.eq("client_auth_id", user.id)
.single();
if (!clientData) {
return (
<div className="flex flex-col items-center justify-center h-[50vh] text-center gap-4">
<h2 className="text-2xl font-semibold">Hesabınız Henüz Aktif Değil</h2>
</div>
);
}
const { data: projectsData } = await supabase
.from("projects")
.select("id, name")
.eq("client_id", clientData.id);
const projectIds = projectsData?.map(p => p.id) || [];
let tasks: PortalTaskRow[] = [];
if (projectIds.length > 0) {
const { data: tasksData } = await supabase
.from("tasks")
.select("id, title, status, project_id, created_at, date, priority")
.in("project_id", projectIds)
.eq("is_public_to_client", true)
.order("created_at", { ascending: false });
tasks = tasksData || [];
}
const getProjectName = (id: string) => projectsData?.find(p => p.id === id)?.name || "Bilinmeyen Proje";
return ( return (
<div className="mx-auto flex max-w-7xl flex-col gap-8 animate-in fade-in slide-in-from-bottom-4 duration-500"> <div className="mx-auto flex max-w-7xl flex-col gap-8 animate-in fade-in slide-in-from-bottom-4 duration-500">
@@ -68,40 +25,36 @@ export default async function PortalTasksPage() {
<KanbanSquare className="w-10 h-10 text-muted-foreground/50" /> <KanbanSquare className="w-10 h-10 text-muted-foreground/50" />
Henüz sizinle paylaşılan bir görev bulunmuyor. Henüz sizinle paylaşılan bir görev bulunmuyor.
</div> </div>
) : ( ) : tasks.map((task) => {
tasks.map(task => ( const isDone = task.status === "done";
const date = task.dueAt?.toISOString() ?? task.scheduledDate;
return (
<Card key={task.id} className="h-full"> <Card key={task.id} className="h-full">
<CardContent className="p-5 flex flex-col h-full justify-between gap-4"> <CardContent className="p-5 flex flex-col h-full justify-between gap-4">
<div className="space-y-3"> <div className="space-y-3">
<div className="flex items-start justify-between gap-2"> <div className="flex items-start justify-between gap-2">
<h3 className={task.status === 'completed' || task.status === 'done' ? "font-semibold text-lg line-through text-muted-foreground line-clamp-2" : "font-semibold text-lg line-clamp-2"}> <h3 className={isDone ? "font-semibold text-lg line-through text-muted-foreground line-clamp-2" : "font-semibold text-lg line-clamp-2"}>
{task.title} {task.title}
</h3> </h3>
<Badge variant={task.status === 'completed' || task.status === 'done' ? 'secondary' : 'outline'} className="capitalize shrink-0"> <Badge variant={isDone ? "secondary" : "outline"} className="capitalize shrink-0">
{task.status === 'todo' ? 'Bekliyor' : task.status === 'in_progress' ? 'İşleniyor' : 'Tamamlandı'} {task.status === "todo" ? "Bekliyor" : task.status === "in_progress" ? "İşleniyor" : "Tamamlandı"}
</Badge> </Badge>
</div> </div>
<div className="flex items-center gap-2 text-sm text-muted-foreground bg-muted/30 p-2 rounded-md"> <div className="flex items-center gap-2 text-sm text-muted-foreground bg-muted/30 p-2 rounded-md">
<span className="font-medium truncate">{getProjectName(task.project_id)}</span> <span className="font-medium truncate">{projectNames.get(task.projectId!)}</span>
</div> </div>
</div> </div>
<div className="flex items-center justify-between text-sm text-muted-foreground border-t border-border pt-4"> <div className="flex items-center justify-between text-sm text-muted-foreground border-t border-border pt-4">
<div className="flex items-center gap-1.5"> <div className="flex items-center gap-1.5">
<CalendarDays className="h-4 w-4" /> <CalendarDays className="h-4 w-4" />
<span>{task.date ? format(new Date(task.date), 'd MMM yyyy', { locale: tr }) : 'Tarih yok'}</span> <span>{date ? format(new Date(date), "d MMM yyyy", { locale: tr }) : "Tarih yok"}</span>
</div> </div>
{task.status === 'completed' || task.status === 'done' ? ( {isDone ? <CheckCircle2 className="h-4 w-4 text-emerald-500" /> : <Clock className="h-4 w-4" />}
<CheckCircle2 className="h-4 w-4 text-emerald-500" />
) : (
<Clock className="h-4 w-4" />
)}
</div> </div>
</CardContent> </CardContent>
</Card> </Card>
)) );
)} })}
</div> </div>
</div> </div>
); );
@@ -0,0 +1,10 @@
CREATE TABLE `user_ai_settings` (
`owner_user_id` text PRIMARY KEY NOT NULL,
`provider` text DEFAULT 'gemini' NOT NULL,
`model` text,
`encrypted_api_key` text,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
`updated_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (`owner_user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade,
CONSTRAINT "user_ai_settings_provider_check" CHECK("user_ai_settings"."provider" in ('gemini', 'openai', 'groq', 'ollama'))
);
File diff suppressed because it is too large Load Diff
+7
View File
@@ -36,6 +36,13 @@
"when": 1784210311370, "when": 1784210311370,
"tag": "0004_fancy_baron_zemo", "tag": "0004_fancy_baron_zemo",
"breakpoints": true "breakpoints": true
},
{
"idx": 5,
"version": "6",
"when": 1784234752708,
"tag": "0005_brief_black_bolt",
"breakpoints": true
} }
] ]
} }
+1
View File
@@ -1,4 +1,5 @@
export * from "./auth"; export * from "./auth";
export * from "./domain"; export * from "./domain";
export * from "./runtime"; export * from "./runtime";
export * from "./settings";
export * from "./storage"; export * from "./storage";
+25
View File
@@ -0,0 +1,25 @@
import { sql } from "drizzle-orm";
import { check, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { user } from "./auth";
export type AiProvider = "gemini" | "openai" | "groq" | "ollama";
export const userAiSettings = sqliteTable(
"user_ai_settings",
{
ownerUserId: text("owner_user_id")
.primaryKey()
.references(() => user.id, { onDelete: "cascade" }),
provider: text("provider").$type<AiProvider>().default("gemini").notNull(),
model: text("model"),
encryptedApiKey: text("encrypted_api_key"),
createdAt: text("created_at").default(sql`CURRENT_TIMESTAMP`).notNull(),
updatedAt: text("updated_at").default(sql`CURRENT_TIMESTAMP`).notNull(),
},
(table) => [
check(
"user_ai_settings_provider_check",
sql`${table.provider} in ('gemini', 'openai', 'groq', 'ollama')`,
),
],
);
+70 -6
View File
@@ -49,7 +49,17 @@ export const clientCreateSchema = z.object({
nextFollowUpDate: optionalDate, nextFollowUpDate: optionalDate,
notes: optionalText(10_000), notes: optionalText(10_000),
}); });
export const clientUpdateSchema = clientCreateSchema.omit({ id: true }).partial(); export const clientUpdateSchema = z.object({
name: z.string().trim().min(1).max(160).optional(),
companyName: optionalText(160),
email: z.email().nullable().optional(),
phone: optionalText(40),
website: z.url().nullable().optional(),
status: z.enum(clientStatuses).optional(),
pipelineStage: z.enum(clientPipelineStages).optional(),
nextFollowUpDate: optionalDate,
notes: optionalText(10_000),
});
export const clientActivityCreateSchema = z.object({ export const clientActivityCreateSchema = z.object({
id: resourceIdSchema.optional(), id: resourceIdSchema.optional(),
@@ -77,7 +87,22 @@ export const projectCreateSchema = z.object({
legacyCoverImagePath: optionalText(1_000), legacyCoverImagePath: optionalText(1_000),
coverImageAlt: optionalText(500), coverImageAlt: optionalText(500),
}); });
export const projectUpdateSchema = projectCreateSchema.omit({ id: true }).partial(); export const projectUpdateSchema = z.object({
clientId: optionalId,
name: z.string().trim().min(1).max(200).optional(),
type: z.enum(projectTypes).optional(),
description: optionalText(20_000),
status: z.enum(projectStatuses).optional(),
startDate: optionalDate,
dueDate: optionalDate,
budgetAmountMinor: minorAmountSchema.nullable().optional(),
currency: currencySchema.optional(),
progress: z.number().int().min(0).max(100).optional(),
progressType: z.enum(projectProgressTypes).optional(),
revisionQuota: z.number().int().min(0).max(10_000).optional(),
legacyCoverImagePath: optionalText(1_000),
coverImageAlt: optionalText(500),
});
export const taskCreateSchema = z.object({ export const taskCreateSchema = z.object({
id: resourceIdSchema.optional(), id: resourceIdSchema.optional(),
@@ -95,7 +120,21 @@ export const taskCreateSchema = z.object({
aiGenerated: z.boolean().default(false), aiGenerated: z.boolean().default(false),
isPublicToClient: z.boolean().default(false), isPublicToClient: z.boolean().default(false),
}); });
export const taskUpdateSchema = taskCreateSchema.omit({ id: true }).partial(); export const taskUpdateSchema = z.object({
clientId: optionalId,
projectId: optionalId,
sourceJournalEntryId: optionalId,
title: z.string().trim().min(1).max(300).optional(),
description: optionalText(20_000),
status: z.enum(taskStatuses).optional(),
priority: z.enum(taskPriorities).optional(),
scheduledDate: optionalDate,
dueAt: z.date().nullable().optional(),
estimatedMinutes: z.number().int().min(0).nullable().optional(),
actualMinutes: z.number().int().min(0).nullable().optional(),
aiGenerated: z.boolean().optional(),
isPublicToClient: z.boolean().optional(),
});
const calendarEventBaseSchema = z.object({ const calendarEventBaseSchema = z.object({
id: resourceIdSchema.optional(), id: resourceIdSchema.optional(),
@@ -113,7 +152,16 @@ export const calendarEventCreateSchema = calendarEventBaseSchema
message: "Bitiş zamanı başlangıç zamanından önce olamaz.", message: "Bitiş zamanı başlangıç zamanından önce olamaz.",
path: ["endsAt"], path: ["endsAt"],
}); });
export const calendarEventUpdateSchema = calendarEventBaseSchema.omit({ id: true }).partial(); export const calendarEventUpdateSchema = z.object({
clientId: optionalId,
projectId: optionalId,
taskId: optionalId,
title: z.string().trim().min(1).max(300).optional(),
description: optionalText(20_000),
type: z.enum(calendarEventTypes).optional(),
startsAt: z.date().optional(),
endsAt: z.date().nullable().optional(),
});
export const financeTransactionCreateSchema = z.object({ export const financeTransactionCreateSchema = z.object({
id: resourceIdSchema.optional(), id: resourceIdSchema.optional(),
@@ -127,7 +175,17 @@ export const financeTransactionCreateSchema = z.object({
paymentStatus: z.enum(paymentStatuses).default("planned"), paymentStatus: z.enum(paymentStatuses).default("planned"),
description: optionalText(10_000), description: optionalText(10_000),
}); });
export const financeTransactionUpdateSchema = financeTransactionCreateSchema.omit({ id: true }).partial(); export const financeTransactionUpdateSchema = z.object({
clientId: optionalId,
projectId: optionalId,
type: z.enum(financeTransactionTypes).optional(),
amountMinor: minorAmountSchema.optional(),
currency: currencySchema.optional(),
transactionDate: businessDateSchema.optional(),
category: optionalText(160),
paymentStatus: z.enum(paymentStatuses).optional(),
description: optionalText(10_000),
});
export const journalEntrySchema = z.object({ export const journalEntrySchema = z.object({
id: resourceIdSchema.optional(), id: resourceIdSchema.optional(),
@@ -149,7 +207,13 @@ export const planningSectionCreateSchema = z.object({
metadata: z.record(z.string(), z.unknown()).default({}), metadata: z.record(z.string(), z.unknown()).default({}),
sortOrder: z.number().int().min(0).default(0), sortOrder: z.number().int().min(0).default(0),
}); });
export const planningSectionUpdateSchema = planningSectionCreateSchema.omit({ id: true, projectId: true }).partial(); export const planningSectionUpdateSchema = z.object({
category: z.enum(planningSectionCategories).optional(),
title: z.string().trim().min(1).max(300).optional(),
content: optionalText(50_000),
metadata: z.record(z.string(), z.unknown()).optional(),
sortOrder: z.number().int().min(0).optional(),
});
export const revisionCreateSchema = z.object({ export const revisionCreateSchema = z.object({
id: resourceIdSchema.optional(), id: resourceIdSchema.optional(),
+1
View File
@@ -176,6 +176,7 @@ export class FileService {
const scope = requireClientScope(actor); const scope = requireClientScope(actor);
if (file.kind === "avatar" && file.authUserId === scope.authUserId) return; if (file.kind === "avatar" && file.authUserId === scope.authUserId) return;
if (file.kind === "project_asset" && file.visibility === "portal" && file.projectId) { if (file.kind === "project_asset" && file.visibility === "portal" && file.projectId) {
this.getClientOwner(actor);
const project = this.db const project = this.db
.select({ id: projects.id }) .select({ id: projects.id })
.from(projects) .from(projects)
+78 -3
View File
@@ -1,4 +1,4 @@
import { and, asc, count, desc, eq, ne, sql } from "drizzle-orm"; import { and, asc, count, desc, eq, gte, lte, ne, sql } from "drizzle-orm";
import { import {
calendarEvents, calendarEvents,
chatMessages, chatMessages,
@@ -24,6 +24,8 @@ export function createDomainRepositories(db: DomainDatabase) {
clients: { clients: {
list: (scope: OwnerScope) => list: (scope: OwnerScope) =>
db.select().from(clients).where(eq(clients.ownerUserId, scope.ownerUserId)).orderBy(desc(clients.updatedAt)).all(), db.select().from(clients).where(eq(clients.ownerUserId, scope.ownerUserId)).orderBy(desc(clients.updatedAt)).all(),
recent: (scope: OwnerScope, limit: number) =>
db.select().from(clients).where(eq(clients.ownerUserId, scope.ownerUserId)).orderBy(desc(clients.createdAt)).limit(limit).all(),
get: (scope: OwnerScope, id: string) => get: (scope: OwnerScope, id: string) =>
db.select().from(clients).where(and(eq(clients.id, id), eq(clients.ownerUserId, scope.ownerUserId))).get(), db.select().from(clients).where(and(eq(clients.id, id), eq(clients.ownerUserId, scope.ownerUserId))).get(),
getByPortalScope: (scope: ClientScope) => getByPortalScope: (scope: ClientScope) =>
@@ -36,18 +38,45 @@ export function createDomainRepositories(db: DomainDatabase) {
db.delete(clients).where(and(eq(clients.id, id), eq(clients.ownerUserId, scope.ownerUserId))).returning().get(), db.delete(clients).where(and(eq(clients.id, id), eq(clients.ownerUserId, scope.ownerUserId))).returning().get(),
listActivities: (scope: OwnerScope, clientId: string) => listActivities: (scope: OwnerScope, clientId: string) =>
db.select().from(clientActivities).where(and(eq(clientActivities.ownerUserId, scope.ownerUserId), eq(clientActivities.clientId, clientId))).orderBy(desc(clientActivities.activityDate)).all(), db.select().from(clientActivities).where(and(eq(clientActivities.ownerUserId, scope.ownerUserId), eq(clientActivities.clientId, clientId))).orderBy(desc(clientActivities.activityDate)).all(),
listAllActivities: (scope: OwnerScope) =>
db.select().from(clientActivities).where(eq(clientActivities.ownerUserId, scope.ownerUserId)).orderBy(desc(clientActivities.activityDate)).all(),
createActivity: (scope: OwnerScope, value: Omit<typeof clientActivities.$inferInsert, "ownerUserId">) => createActivity: (scope: OwnerScope, value: Omit<typeof clientActivities.$inferInsert, "ownerUserId">) =>
db.insert(clientActivities).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(), db.insert(clientActivities).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(),
}, },
projects: { projects: {
list: (scope: OwnerScope) => list: (scope: OwnerScope) =>
db.select().from(projects).where(eq(projects.ownerUserId, scope.ownerUserId)).orderBy(desc(projects.updatedAt)).all(), db.select().from(projects).where(eq(projects.ownerUserId, scope.ownerUserId)).orderBy(desc(projects.updatedAt)).all(),
recent: (scope: OwnerScope, limit: number) =>
db.select().from(projects).where(eq(projects.ownerUserId, scope.ownerUserId)).orderBy(desc(projects.createdAt)).limit(limit).all(),
get: (scope: OwnerScope, id: string) => get: (scope: OwnerScope, id: string) =>
db.select().from(projects).where(and(eq(projects.id, id), eq(projects.ownerUserId, scope.ownerUserId))).get(), db.select().from(projects).where(and(eq(projects.id, id), eq(projects.ownerUserId, scope.ownerUserId))).get(),
getForClient: (scope: ClientScope, id: string) => getForClient: (scope: ClientScope, id: string) =>
db.select().from(projects).where(and(eq(projects.id, id), eq(projects.clientId, scope.clientId))).get(), db.select({ project: projects })
.from(projects)
.innerJoin(
clients,
and(
eq(projects.clientId, clients.id),
eq(clients.id, scope.clientId),
eq(clients.authUserId, scope.authUserId),
),
)
.where(eq(projects.id, id))
.get()?.project,
listForClient: (scope: ClientScope) => listForClient: (scope: ClientScope) =>
db.select().from(projects).where(eq(projects.clientId, scope.clientId)).orderBy(desc(projects.updatedAt)).all(), db.select({ project: projects })
.from(projects)
.innerJoin(
clients,
and(
eq(projects.clientId, clients.id),
eq(clients.id, scope.clientId),
eq(clients.authUserId, scope.authUserId),
),
)
.orderBy(desc(projects.updatedAt))
.all()
.map(({ project }) => project),
create: (scope: OwnerScope, value: Omit<typeof projects.$inferInsert, "ownerUserId">) => create: (scope: OwnerScope, value: Omit<typeof projects.$inferInsert, "ownerUserId">) =>
db.insert(projects).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(), db.insert(projects).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(),
update: (scope: OwnerScope, id: string, value: Partial<typeof projects.$inferInsert>) => update: (scope: OwnerScope, id: string, value: Partial<typeof projects.$inferInsert>) =>
@@ -91,6 +120,8 @@ export function createDomainRepositories(db: DomainDatabase) {
}, },
finance: { finance: {
list: (scope: OwnerScope) => db.select().from(financeTransactions).where(eq(financeTransactions.ownerUserId, scope.ownerUserId)).orderBy(desc(financeTransactions.transactionDate)).all(), list: (scope: OwnerScope) => db.select().from(financeTransactions).where(eq(financeTransactions.ownerUserId, scope.ownerUserId)).orderBy(desc(financeTransactions.transactionDate)).all(),
listInRange: (scope: OwnerScope, startDate: string, endDate: string) =>
db.select().from(financeTransactions).where(and(eq(financeTransactions.ownerUserId, scope.ownerUserId), gte(financeTransactions.transactionDate, startDate), lte(financeTransactions.transactionDate, endDate))).orderBy(asc(financeTransactions.transactionDate)).all(),
get: (scope: OwnerScope, id: string) => db.select().from(financeTransactions).where(and(eq(financeTransactions.id, id), eq(financeTransactions.ownerUserId, scope.ownerUserId))).get(), get: (scope: OwnerScope, id: string) => db.select().from(financeTransactions).where(and(eq(financeTransactions.id, id), eq(financeTransactions.ownerUserId, scope.ownerUserId))).get(),
create: (scope: OwnerScope, value: Omit<typeof financeTransactions.$inferInsert, "ownerUserId">) => db.insert(financeTransactions).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(), create: (scope: OwnerScope, value: Omit<typeof financeTransactions.$inferInsert, "ownerUserId">) => db.insert(financeTransactions).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(),
update: (scope: OwnerScope, id: string, value: Partial<typeof financeTransactions.$inferInsert>) => db.update(financeTransactions).set(value).where(and(eq(financeTransactions.id, id), eq(financeTransactions.ownerUserId, scope.ownerUserId))).returning().get(), update: (scope: OwnerScope, id: string, value: Partial<typeof financeTransactions.$inferInsert>) => db.update(financeTransactions).set(value).where(and(eq(financeTransactions.id, id), eq(financeTransactions.ownerUserId, scope.ownerUserId))).returning().get(),
@@ -98,16 +129,39 @@ export function createDomainRepositories(db: DomainDatabase) {
}, },
journal: { journal: {
list: (scope: OwnerScope) => db.select().from(journalEntries).where(eq(journalEntries.ownerUserId, scope.ownerUserId)).orderBy(desc(journalEntries.entryDate)).all(), list: (scope: OwnerScope) => db.select().from(journalEntries).where(eq(journalEntries.ownerUserId, scope.ownerUserId)).orderBy(desc(journalEntries.entryDate)).all(),
listInRange: (scope: OwnerScope, startDate: string, endDate: string) =>
db.select().from(journalEntries).where(and(eq(journalEntries.ownerUserId, scope.ownerUserId), gte(journalEntries.entryDate, startDate), lte(journalEntries.entryDate, endDate))).orderBy(asc(journalEntries.entryDate)).all(),
getByDate: (scope: OwnerScope, entryDate: string) => db.select().from(journalEntries).where(and(eq(journalEntries.ownerUserId, scope.ownerUserId), eq(journalEntries.entryDate, entryDate))).get(), getByDate: (scope: OwnerScope, entryDate: string) => db.select().from(journalEntries).where(and(eq(journalEntries.ownerUserId, scope.ownerUserId), eq(journalEntries.entryDate, entryDate))).get(),
get: (scope: OwnerScope, id: string) => db.select().from(journalEntries).where(and(eq(journalEntries.id, id), eq(journalEntries.ownerUserId, scope.ownerUserId))).get(), get: (scope: OwnerScope, id: string) => db.select().from(journalEntries).where(and(eq(journalEntries.id, id), eq(journalEntries.ownerUserId, scope.ownerUserId))).get(),
create: (scope: OwnerScope, value: Omit<typeof journalEntries.$inferInsert, "ownerUserId">) => db.insert(journalEntries).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(), create: (scope: OwnerScope, value: Omit<typeof journalEntries.$inferInsert, "ownerUserId">) => db.insert(journalEntries).values({ ...value, ownerUserId: scope.ownerUserId }).returning().get(),
updateByDate: (scope: OwnerScope, entryDate: string, value: Partial<typeof journalEntries.$inferInsert>) => db.update(journalEntries).set(value).where(and(eq(journalEntries.ownerUserId, scope.ownerUserId), eq(journalEntries.entryDate, entryDate))).returning().get(), updateByDate: (scope: OwnerScope, entryDate: string, value: Partial<typeof journalEntries.$inferInsert>) => db.update(journalEntries).set(value).where(and(eq(journalEntries.ownerUserId, scope.ownerUserId), eq(journalEntries.entryDate, entryDate))).returning().get(),
update: (scope: OwnerScope, id: string, value: Partial<typeof journalEntries.$inferInsert>) => db.update(journalEntries).set(value).where(and(eq(journalEntries.id, id), eq(journalEntries.ownerUserId, scope.ownerUserId))).returning().get(),
remove: (scope: OwnerScope, id: string) => db.delete(journalEntries).where(and(eq(journalEntries.id, id), eq(journalEntries.ownerUserId, scope.ownerUserId))).returning().get(), remove: (scope: OwnerScope, id: string) => db.delete(journalEntries).where(and(eq(journalEntries.id, id), eq(journalEntries.ownerUserId, scope.ownerUserId))).returning().get(),
}, },
revisions: { revisions: {
list: (scope: OwnerScope, projectId: string) => db.select().from(projectRevisions).where(and(eq(projectRevisions.ownerUserId, scope.ownerUserId), eq(projectRevisions.projectId, projectId))).orderBy(desc(projectRevisions.createdAt)).all(), list: (scope: OwnerScope, projectId: string) => db.select().from(projectRevisions).where(and(eq(projectRevisions.ownerUserId, scope.ownerUserId), eq(projectRevisions.projectId, projectId))).orderBy(desc(projectRevisions.createdAt)).all(),
updateStatus: (scope: OwnerScope, id: string, status: typeof projectRevisions.$inferInsert.status) => db.update(projectRevisions).set({ status }).where(and(eq(projectRevisions.id, id), eq(projectRevisions.ownerUserId, scope.ownerUserId))).returning().get(), updateStatus: (scope: OwnerScope, id: string, status: typeof projectRevisions.$inferInsert.status) => db.update(projectRevisions).set({ status }).where(and(eq(projectRevisions.id, id), eq(projectRevisions.ownerUserId, scope.ownerUserId))).returning().get(),
listForClient: (scope: ClientScope, projectId: string) => db.select().from(projectRevisions).where(and(eq(projectRevisions.clientId, scope.clientId), eq(projectRevisions.projectId, projectId))).orderBy(desc(projectRevisions.createdAt)).all(), listForClient: (scope: ClientScope, projectId: string) => db.select().from(projectRevisions).where(and(eq(projectRevisions.clientId, scope.clientId), eq(projectRevisions.projectId, projectId))).orderBy(desc(projectRevisions.createdAt)).all(),
listAllForClient: (scope: ClientScope) => db.select({ revision: projectRevisions })
.from(projectRevisions)
.innerJoin(
projects,
and(
eq(projectRevisions.projectId, projects.id),
eq(projects.clientId, scope.clientId),
),
)
.innerJoin(
clients,
and(
eq(projects.clientId, clients.id),
eq(clients.authUserId, scope.authUserId),
),
)
.where(eq(projectRevisions.clientId, scope.clientId))
.orderBy(desc(projectRevisions.createdAt))
.all()
.map(({ revision }) => revision),
}, },
chat: { chat: {
listSessions: (scope: OwnerScope) => db.select().from(chatSessions).where(eq(chatSessions.ownerUserId, scope.ownerUserId)).orderBy(desc(chatSessions.updatedAt)).all(), listSessions: (scope: OwnerScope) => db.select().from(chatSessions).where(eq(chatSessions.ownerUserId, scope.ownerUserId)).orderBy(desc(chatSessions.updatedAt)).all(),
@@ -131,6 +185,27 @@ export function createDomainRepositories(db: DomainDatabase) {
}).from(financeTransactions).where(eq(financeTransactions.ownerUserId, scope.ownerUserId)).get(), }).from(financeTransactions).where(eq(financeTransactions.ownerUserId, scope.ownerUserId)).get(),
projectStatusCounts: (scope: OwnerScope) => db.select({ status: projects.status, value: count() }).from(projects).where(eq(projects.ownerUserId, scope.ownerUserId)).groupBy(projects.status).all(), projectStatusCounts: (scope: OwnerScope) => db.select({ status: projects.status, value: count() }).from(projects).where(eq(projects.ownerUserId, scope.ownerUserId)).groupBy(projects.status).all(),
taskStatusCounts: (scope: OwnerScope) => db.select({ status: tasks.status, value: count() }).from(tasks).where(eq(tasks.ownerUserId, scope.ownerUserId)).groupBy(tasks.status).all(), taskStatusCounts: (scope: OwnerScope) => db.select({ status: tasks.status, value: count() }).from(tasks).where(eq(tasks.ownerUserId, scope.ownerUserId)).groupBy(tasks.status).all(),
taskStatusCountsInRange: (scope: OwnerScope, startDate: Date, endDate: Date) =>
db.select({ status: tasks.status, value: count() }).from(tasks).where(and(eq(tasks.ownerUserId, scope.ownerUserId), gte(tasks.updatedAt, startDate), lte(tasks.updatedAt, endDate))).groupBy(tasks.status).all(),
projectIncomeInRange: (scope: OwnerScope, startDate: string, endDate: string) =>
db.select({
projectId: projects.id,
name: projects.name,
amountMinor: sql<number>`coalesce(sum(${financeTransactions.amountMinor}), 0)`,
})
.from(financeTransactions)
.innerJoin(projects, eq(financeTransactions.projectId, projects.id))
.where(and(
eq(financeTransactions.ownerUserId, scope.ownerUserId),
eq(projects.ownerUserId, scope.ownerUserId),
eq(financeTransactions.type, "income"),
eq(financeTransactions.paymentStatus, "paid"),
gte(financeTransactions.transactionDate, startDate),
lte(financeTransactions.transactionDate, endDate),
))
.groupBy(projects.id, projects.name)
.orderBy(desc(sql`sum(${financeTransactions.amountMinor})`))
.all(),
}, },
}; };
} }
+56
View File
@@ -0,0 +1,56 @@
import "server-only";
import { z } from "zod";
export const dashboardRangeSchema = z.enum([
"today",
"this_week",
"this_month",
"this_year",
]);
export type DashboardRange = z.infer<typeof dashboardRangeSchema>;
export function parseDashboardRange(
value: string | string[] | undefined,
fallback: DashboardRange = "this_month",
): DashboardRange {
const parsed = dashboardRangeSchema.safeParse(value);
return parsed.success ? parsed.data : fallback;
}
export function resolveDashboardRange(range: DashboardRange, now = new Date()) {
let startAt: Date;
let endAt: Date;
if (range === "today") {
startAt = new Date(now.getFullYear(), now.getMonth(), now.getDate());
endAt = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999);
} else if (range === "this_week") {
const mondayOffset = now.getDay() === 0 ? -6 : 1 - now.getDay();
startAt = new Date(now.getFullYear(), now.getMonth(), now.getDate() + mondayOffset);
endAt = new Date(startAt);
endAt.setDate(endAt.getDate() + 6);
endAt.setHours(23, 59, 59, 999);
} else if (range === "this_year") {
startAt = new Date(now.getFullYear(), 0, 1);
endAt = new Date(now.getFullYear(), 11, 31, 23, 59, 59, 999);
} else {
startAt = new Date(now.getFullYear(), now.getMonth(), 1);
endAt = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999);
}
return {
startAt,
endAt,
startDate: toBusinessDate(startAt),
endDate: toBusinessDate(endAt),
};
}
function toBusinessDate(date: Date): string {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}
+145 -3
View File
@@ -82,6 +82,16 @@ export class DomainService {
return this.repositories.clients.createActivity(scope, { ...value, id: value.id ?? this.id() }); return this.repositories.clients.createActivity(scope, { ...value, id: value.id ?? this.id() });
} }
listClientActivities(actor: DomainActor, clientId: string) {
const scope = requireOwnerScope(actor);
this.requireOwnedClient(scope, clientId);
return this.repositories.clients.listActivities(scope, clientId);
}
listAllClientActivities(actor: DomainActor) {
return this.repositories.clients.listAllActivities(requireOwnerScope(actor));
}
listProjects(actor: DomainActor) { listProjects(actor: DomainActor) {
if (actor.role === "client") { if (actor.role === "client") {
return this.repositories.projects.listForClient(requireClientScope(actor)); return this.repositories.projects.listForClient(requireClientScope(actor));
@@ -108,7 +118,15 @@ export class DomainService {
const current = this.repositories.projects.get(scope, projectId) ?? this.throwNotFound("Proje"); const current = this.repositories.projects.get(scope, projectId) ?? this.throwNotFound("Proje");
const value = parseDomainInput(projectUpdateSchema, input); const value = parseDomainInput(projectUpdateSchema, input);
this.assertProjectClient(scope, value.type ?? current.type, value.clientId === undefined ? current.clientId : value.clientId); this.assertProjectClient(scope, value.type ?? current.type, value.clientId === undefined ? current.clientId : value.clientId);
return this.repositories.projects.update(scope, projectId, value) ?? this.throwNotFound("Proje"); const updated = this.repositories.projects.update(scope, projectId, value) ?? this.throwNotFound("Proje");
if (
updated.progressType === "auto"
&& (value.progressType === "auto" || value.progress !== undefined)
) {
this.recalculateProjectProgress(scope, projectId);
return this.repositories.projects.get(scope, projectId) ?? this.throwNotFound("Proje");
}
return updated;
} }
deleteProject(actor: DomainActor, id: string) { deleteProject(actor: DomainActor, id: string) {
@@ -118,6 +136,7 @@ export class DomainService {
listTasks(actor: DomainActor, projectId?: string) { listTasks(actor: DomainActor, projectId?: string) {
if (actor.role === "client") { if (actor.role === "client") {
const scope = requireClientScope(actor); const scope = requireClientScope(actor);
this.getClient(actor, scope.clientId);
if (projectId) this.getProject(actor, projectId); if (projectId) this.getProject(actor, projectId);
return this.repositories.tasks.listPublicForClient(scope, projectId); return this.repositories.tasks.listPublicForClient(scope, projectId);
} }
@@ -216,6 +235,16 @@ export class DomainService {
return this.repositories.journal.list(requireOwnerScope(actor)); return this.repositories.journal.list(requireOwnerScope(actor));
} }
updateJournalEntry(actor: DomainActor, entryId: string, input: unknown) {
const scope = requireOwnerScope(actor);
const value = parseDomainInput(journalEntrySchema.omit({ id: true }), input);
const conflicting = this.repositories.journal.getByDate(scope, value.entryDate);
if (conflicting && conflicting.id !== entryId) {
throw conflict("Bu tarih için zaten bir günlük kaydı var.");
}
return this.repositories.journal.update(scope, entryId, value) ?? this.throwNotFound("Günlük kaydı");
}
deleteJournalEntry(actor: DomainActor, entryId: string) { deleteJournalEntry(actor: DomainActor, entryId: string) {
return this.repositories.journal.remove(requireOwnerScope(actor), entryId) ?? this.throwNotFound("Günlük kaydı"); return this.repositories.journal.remove(requireOwnerScope(actor), entryId) ?? this.throwNotFound("Günlük kaydı");
} }
@@ -251,6 +280,7 @@ export class DomainService {
requestRevision(actor: DomainActor, input: unknown) { requestRevision(actor: DomainActor, input: unknown) {
const scope = requireClientScope(actor); const scope = requireClientScope(actor);
this.getClient(actor, scope.clientId);
const value = parseDomainInput(revisionCreateSchema, input); const value = parseDomainInput(revisionCreateSchema, input);
const revisionId = value.id ?? this.id(); const revisionId = value.id ?? this.id();
@@ -273,9 +303,16 @@ export class DomainService {
}, { behavior: "immediate" }); }, { behavior: "immediate" });
} }
updateRevisionStatus(actor: DomainActor, revisionId: string, statusInput: unknown) { updateRevisionStatus(actor: DomainActor, revisionId: string, statusInput: unknown, projectId?: string) {
const scope = requireOwnerScope(actor);
if (projectId) {
this.requireOwnedProject(scope, projectId);
if (!this.repositories.revisions.list(scope, projectId).some((revision) => revision.id === revisionId)) {
throw notFound("Revizyon");
}
}
const status = parseDomainInput(revisionStatusSchema, statusInput); const status = parseDomainInput(revisionStatusSchema, statusInput);
return this.repositories.revisions.updateStatus(requireOwnerScope(actor), revisionId, status) ?? this.throwNotFound("Revizyon"); return this.repositories.revisions.updateStatus(scope, revisionId, status) ?? this.throwNotFound("Revizyon");
} }
listRevisions(actor: DomainActor, projectId: string) { listRevisions(actor: DomainActor, projectId: string) {
@@ -289,6 +326,29 @@ export class DomainService {
return this.repositories.revisions.list(scope, projectId); return this.repositories.revisions.list(scope, projectId);
} }
listPortalRevisions(actor: DomainActor) {
const scope = requireClientScope(actor);
this.getClient(actor, scope.clientId);
return this.repositories.revisions.listAllForClient(scope);
}
getRevisionAllowance(actor: DomainActor, projectId: string) {
const scope = requireClientScope(actor);
const project = this.getProject(actor, projectId);
const used = this.repositories.revisions
.listForClient(scope, projectId)
.filter((revision) => revision.status !== "rejected")
.length;
const remaining = Math.max(project.revisionQuota - used, 0);
return {
quota: project.revisionQuota,
used,
remaining,
canRequest: project.status === "active" && remaining > 0,
};
}
createChatSession(actor: DomainActor, input: unknown) { createChatSession(actor: DomainActor, input: unknown) {
const scope = requireOwnerScope(actor); const scope = requireOwnerScope(actor);
const value = parseDomainInput(chatSessionCreateSchema, input); const value = parseDomainInput(chatSessionCreateSchema, input);
@@ -318,6 +378,88 @@ export class DomainService {
}; };
} }
getFreelancerDashboard(
actor: DomainActor,
range: { startDate: string; endDate: string; startAt: Date; endAt: Date },
) {
const scope = requireOwnerScope(actor);
const finance = this.repositories.finance.listInRange(scope, range.startDate, range.endDate);
const journal = this.repositories.journal.listInRange(scope, range.startDate, range.endDate);
const projectsByStatus = this.repositories.analytics.projectStatusCounts(scope);
const tasksByStatus = this.repositories.analytics.taskStatusCountsInRange(
scope,
range.startAt,
range.endAt,
);
const financeByDate = new Map<string, { income: number; expense: number }>();
let incomeMinor = 0;
let expenseMinor = 0;
for (const transaction of finance) {
if (transaction.paymentStatus !== "paid") continue;
const current = financeByDate.get(transaction.transactionDate) ?? { income: 0, expense: 0 };
if (transaction.type === "income") {
current.income += transaction.amountMinor;
incomeMinor += transaction.amountMinor;
} else {
current.expense += transaction.amountMinor;
expenseMinor += transaction.amountMinor;
}
financeByDate.set(transaction.transactionDate, current);
}
const moodValues = journal.flatMap((entry) =>
entry.moodScore == null ? [] : [entry.moodScore],
);
return {
metrics: {
netProfit: (incomeMinor - expenseMinor) / 100,
activeProjectsCount:
projectsByStatus.find((item) => item.status === "active")?.value ?? 0,
completedTasksCount:
tasksByStatus.find((item) => item.status === "done")?.value ?? 0,
avgMood: moodValues.length
? (moodValues.reduce((sum, value) => sum + value, 0) / moodValues.length).toFixed(1)
: "0.0",
financeTrend: Array.from(financeByDate, ([date, value]) => ({
date,
income: value.income / 100,
expense: value.expense / 100,
})),
moodTrend: journal.map((entry) => ({
date: entry.entryDate,
mood: entry.moodScore ?? 0,
energy: entry.energyScore ?? 0,
})),
},
projects: this.repositories.projects.recent(scope, 5),
clients: this.repositories.clients.recent(scope, 5),
};
}
getFreelancerAnalytics(
actor: DomainActor,
range: { startDate: string; endDate: string; startAt: Date; endAt: Date },
) {
const scope = requireOwnerScope(actor);
const tasksByStatus = this.repositories.analytics.taskStatusCountsInRange(
scope,
range.startAt,
range.endAt,
);
const taskCount = (status: string) =>
tasksByStatus.find((item) => item.status === status)?.value ?? 0;
return {
projectIncomeData: this.repositories.analytics
.projectIncomeInRange(scope, range.startDate, range.endDate)
.map((item) => ({ name: item.name, value: Number(item.amountMinor) / 100 })),
completedTasks: taskCount("done"),
activeTasks: taskCount("todo") + taskCount("in_progress"),
};
}
createProposal(actor: DomainActor, input: unknown) { createProposal(actor: DomainActor, input: unknown) {
const scope = requireOwnerScope(actor); const scope = requireOwnerScope(actor);
const value = parseDomainInput(proposalCreateSchema, input); const value = parseDomainInput(proposalCreateSchema, input);
+132
View File
@@ -0,0 +1,132 @@
import "server-only";
import { createCipheriv, createDecipheriv, createHash, randomBytes } from "node:crypto";
import { eq } from "drizzle-orm";
import { z } from "zod";
import { getServerConfig } from "../config";
import { getSqliteConnection } from "../db/client";
import { userAiSettings, type AiProvider } from "../db/schema/settings";
import { requireOwnerScope, type DomainActor } from "../domain/actor";
import { DomainError } from "../domain/errors";
const inputSchema = z.object({
provider: z.enum(["gemini", "openai", "groq", "ollama"]),
apiKey: z.string().trim().max(4_096).optional(),
});
export type PublicAiSettings = {
provider: AiProvider;
hasApiKey: boolean;
};
export function getPublicAiSettings(actor: DomainActor): PublicAiSettings {
const scope = requireOwnerScope(actor);
const row = getSqliteConnection().db
.select()
.from(userAiSettings)
.where(eq(userAiSettings.ownerUserId, scope.ownerUserId))
.get();
return {
provider: row?.provider ?? "gemini",
hasApiKey: Boolean(row?.encryptedApiKey),
};
}
export function updateAiSettings(actor: DomainActor, input: unknown): PublicAiSettings {
const scope = requireOwnerScope(actor);
const parsed = inputSchema.safeParse(input);
if (!parsed.success) {
throw new DomainError("VALIDATION_ERROR", "Yapay zeka ayarları geçersiz.");
}
const { db } = getSqliteConnection();
const current = db
.select()
.from(userAiSettings)
.where(eq(userAiSettings.ownerUserId, scope.ownerUserId))
.get();
const encryptedApiKey = parsed.data.provider === "ollama"
? null
: parsed.data.apiKey
? encryptSecret(parsed.data.apiKey)
: current?.encryptedApiKey ?? null;
db.insert(userAiSettings)
.values({
ownerUserId: scope.ownerUserId,
provider: parsed.data.provider,
model: null,
encryptedApiKey,
})
.onConflictDoUpdate({
target: userAiSettings.ownerUserId,
set: {
provider: parsed.data.provider,
model: null,
encryptedApiKey,
updatedAt: sqlNow(),
},
})
.run();
return { provider: parsed.data.provider, hasApiKey: Boolean(encryptedApiKey) };
}
export function getAiRuntimeSettings(actor: DomainActor): {
provider: AiProvider;
model: string | null;
apiKey: string | null;
} {
const scope = requireOwnerScope(actor);
const row = getSqliteConnection().db
.select()
.from(userAiSettings)
.where(eq(userAiSettings.ownerUserId, scope.ownerUserId))
.get();
return {
provider: row?.provider ?? "gemini",
model: row?.model ?? null,
apiKey: row?.encryptedApiKey ? decryptSecret(row.encryptedApiKey) : null,
};
}
function encryptionKey(): Buffer {
const secret = getServerConfig().betterAuthSecret
?? "neta-development-only-ai-settings-secret";
return createHash("sha256").update(`neta:ai-settings:${secret}`).digest();
}
function encryptSecret(value: string): string {
const iv = randomBytes(12);
const cipher = createCipheriv("aes-256-gcm", encryptionKey(), iv);
const ciphertext = Buffer.concat([cipher.update(value, "utf8"), cipher.final()]);
const tag = cipher.getAuthTag();
return `v1.${iv.toString("base64url")}.${tag.toString("base64url")}.${ciphertext.toString("base64url")}`;
}
function decryptSecret(value: string): string {
const [version, ivValue, tagValue, ciphertextValue] = value.split(".");
if (version !== "v1" || !ivValue || !tagValue || !ciphertextValue) {
throw new DomainError("INVARIANT_VIOLATION", "AI secret formatı geçersiz.");
}
try {
const decipher = createDecipheriv(
"aes-256-gcm",
encryptionKey(),
Buffer.from(ivValue, "base64url"),
);
decipher.setAuthTag(Buffer.from(tagValue, "base64url"));
return Buffer.concat([
decipher.update(Buffer.from(ciphertextValue, "base64url")),
decipher.final(),
]).toString("utf8");
} catch {
throw new DomainError("INVARIANT_VIOLATION", "AI secret çözülemedi.");
}
}
function sqlNow(): string {
return new Date().toISOString();
}
+45
View File
@@ -0,0 +1,45 @@
import "server-only";
import { DomainError } from "../domain/errors";
export function cleanText(value: FormDataEntryValue | null): string | null {
const text = typeof value === "string" ? value.trim() : "";
return text && text !== "__none" ? text : null;
}
export function requiredText(
value: FormDataEntryValue | null,
message: string,
): string {
const text = cleanText(value);
if (!text) throw new DomainError("VALIDATION_ERROR", message);
return text;
}
export function optionalDate(value: FormDataEntryValue | null): Date | null {
const text = cleanText(value);
if (!text) return null;
const date = new Date(text);
if (Number.isNaN(date.getTime())) {
throw new DomainError("VALIDATION_ERROR", "Geçerli bir tarih girilmelidir.");
}
return date;
}
export function decimalToMinor(value: FormDataEntryValue | null): number | null {
const normalized = typeof value === "string" ? value.trim().replace(",", ".") : "";
if (!normalized) return null;
const amount = Number(normalized);
if (!Number.isFinite(amount) || amount < 0) {
throw new DomainError("VALIDATION_ERROR", "Tutar sıfır veya daha büyük olmalıdır.");
}
return Math.round((amount + Number.EPSILON) * 100);
}
export function minorToDecimal(value: number | null | undefined): number | null {
return value == null ? null : value / 100;
}
export function dateToIso(value: Date | null | undefined): string | null {
return value ? value.toISOString() : null;
}
+15
View File
@@ -0,0 +1,15 @@
import "server-only";
import { domainActorFromSession } from "../auth/domain-actor";
import { requireFreelancer } from "../auth/session";
import { getDomainService } from "../services/runtime";
export async function requireFreelancerBackend() {
const context = await requireFreelancer();
return {
context,
actor: domainActorFromSession(context),
service: getDomainService(),
};
}
+15
View File
@@ -0,0 +1,15 @@
import "server-only";
import { domainActorFromSession } from "../auth/domain-actor";
import { requireClientUser } from "../auth/session";
import { getDomainService } from "../services/runtime";
export async function requirePortalBackend() {
const context = await requireClientUser();
return {
context,
actor: domainActorFromSession(context),
service: getDomainService(),
};
}