125 lines
3.3 KiB
TypeScript
125 lines
3.3 KiB
TypeScript
import { apiRequest } from "@/lib/meshkee-api";
|
|
|
|
export interface MeshkeeCategory {
|
|
id: string;
|
|
parentId: string | null;
|
|
name: string;
|
|
nameFa: string | null;
|
|
slug: string;
|
|
description: string | null;
|
|
icon: string | null;
|
|
sortOrder: number;
|
|
tags: string[];
|
|
}
|
|
|
|
export type UpsertMeshkeeCategoryInput = {
|
|
parentId?: string | null;
|
|
name: string;
|
|
nameFa?: string;
|
|
description?: string;
|
|
slug?: string;
|
|
sortOrder?: number;
|
|
icon?: string;
|
|
};
|
|
|
|
function categoryLabel(category: {
|
|
name: string;
|
|
nameFa?: string | null;
|
|
slug: string;
|
|
}) {
|
|
if (category.nameFa && category.nameFa !== category.name) {
|
|
return `${category.name} / ${category.nameFa}`;
|
|
}
|
|
return category.name;
|
|
}
|
|
|
|
function mapCategory(row: Record<string, unknown>): MeshkeeCategory {
|
|
return {
|
|
id: String(row.id),
|
|
parentId:
|
|
row.parentId == null && row.parent_id == null
|
|
? null
|
|
: String(row.parentId ?? row.parent_id),
|
|
name: String(row.name ?? ""),
|
|
nameFa:
|
|
row.nameFa == null && row.name_fa == null
|
|
? null
|
|
: String(row.nameFa ?? row.name_fa),
|
|
slug: String(row.slug ?? ""),
|
|
description: row.description == null ? null : String(row.description),
|
|
icon: row.icon == null ? null : String(row.icon),
|
|
sortOrder: Number(row.sortOrder ?? row.sort_order ?? 0),
|
|
tags: Array.isArray(row.tags) ? (row.tags as string[]) : [],
|
|
};
|
|
}
|
|
|
|
export async function searchMeshkeeCategories(
|
|
q: string,
|
|
signal?: AbortSignal,
|
|
) {
|
|
const params = new URLSearchParams();
|
|
if (q.trim()) params.set("q", q.trim());
|
|
|
|
const data = await apiRequest<{ items: Record<string, unknown>[] }>(
|
|
`/business-categories?${params}`,
|
|
{ auth: true, signal },
|
|
);
|
|
|
|
return (data.items ?? []).map((row) => mapCategory(row));
|
|
}
|
|
|
|
export async function createMeshkeeCategory(body: UpsertMeshkeeCategoryInput) {
|
|
const data = await apiRequest<{ item: Record<string, unknown> }>(
|
|
`/business-categories`,
|
|
{
|
|
method: "POST",
|
|
auth: true,
|
|
body: {
|
|
name: body.name.trim(),
|
|
nameFa: body.nameFa?.trim() || undefined,
|
|
description: body.description?.trim() || undefined,
|
|
parentId: body.parentId || undefined,
|
|
slug: body.slug?.trim() || undefined,
|
|
sortOrder: body.sortOrder,
|
|
icon: body.icon?.trim() || undefined,
|
|
},
|
|
},
|
|
);
|
|
return mapCategory(data.item ?? (data as unknown as Record<string, unknown>));
|
|
}
|
|
|
|
export async function updateMeshkeeCategory(
|
|
categoryId: string,
|
|
body: UpsertMeshkeeCategoryInput,
|
|
) {
|
|
const data = await apiRequest<{ item: Record<string, unknown> }>(
|
|
`/business-categories/${categoryId}`,
|
|
{
|
|
method: "PATCH",
|
|
auth: true,
|
|
body: {
|
|
name: body.name.trim(),
|
|
nameFa: body.nameFa?.trim() || null,
|
|
description: body.description?.trim() || null,
|
|
parentId: body.parentId === undefined ? undefined : body.parentId || null,
|
|
slug: body.slug?.trim() || undefined,
|
|
sortOrder: body.sortOrder,
|
|
icon: body.icon?.trim() || null,
|
|
},
|
|
},
|
|
);
|
|
return mapCategory(data.item ?? (data as unknown as Record<string, unknown>));
|
|
}
|
|
|
|
export async function deleteMeshkeeCategory(categoryId: string) {
|
|
return apiRequest<{ deletedIds?: string[] }>(
|
|
`/business-categories/${categoryId}`,
|
|
{
|
|
method: "DELETE",
|
|
auth: true,
|
|
},
|
|
);
|
|
}
|
|
|
|
export { categoryLabel };
|