Files
neta/app/(dashboard)/clients/actions.ts
T

67 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use server";
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 PIPELINE_STAGES = ["lead", "contacted", "proposal_sent", "won", "lost"] as const;
function enumValue<T extends readonly string[]>(
value: FormDataEntryValue | string | null,
values: T,
fallback: T[number],
): T[number] {
return typeof value === "string" && values.includes(value) ? value as T[number] : fallback;
}
function cleanWebsite(value: FormDataEntryValue | null) {
const website = cleanText(value)?.replace(/\s/g, "") ?? null;
return website && !/^https?:\/\//i.test(website) ? `https://${website}` : website;
}
function readPayload(formData: FormData) {
return {
name: requiredText(formData.get("name"), "Müşteri adı zorunludur."),
companyName: cleanText(formData.get("company_name")),
email: cleanText(formData.get("email")),
phone: cleanText(formData.get("phone")),
website: cleanWebsite(formData.get("website")),
status: enumValue(formData.get("status"), CLIENT_STATUSES, "active"),
notes: cleanText(formData.get("notes")),
pipelineStage: enumValue(formData.get("pipeline_stage"), PIPELINE_STAGES, "lead"),
nextFollowUpDate: cleanText(formData.get("next_follow_up_date")),
};
}
export async function createClientRecord(formData: FormData) {
const { actor, service } = await requireFreelancerBackend();
service.createClient(actor, readPayload(formData));
revalidatePath("/clients");
}
export async function updateClientRecord(formData: FormData) {
const { actor, service } = await requireFreelancerBackend();
const id = requiredText(formData.get("id"), "Müşteri kaydı bulunamadı.");
service.updateClient(actor, id, readPayload(formData));
revalidatePath("/clients");
revalidatePath(`/clients/${id}`);
}
export async function archiveClientRecord(formData: FormData) {
const { actor, service } = await requireFreelancerBackend();
const id = requiredText(formData.get("id"), "Arşivlenecek müşteri bulunamadı.");
service.updateClient(actor, id, { status: "archived" });
revalidatePath("/clients");
revalidatePath(`/clients/${id}`);
}
export async function updateClientPipelineStage(id: string, stage: string) {
const { actor, service } = await requireFreelancerBackend();
service.updateClient(actor, id, {
pipelineStage: enumValue(stage, PIPELINE_STAGES, "lead"),
});
revalidatePath("/clients");
revalidatePath(`/clients/${id}`);
}