65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
import type { AdapterResult } from "../types";
|
|
import {
|
|
commercialDelete,
|
|
commercialGet,
|
|
commercialPatch,
|
|
commercialPost,
|
|
commercialPut,
|
|
extractItems,
|
|
failResult,
|
|
listResult,
|
|
} from "../adapters/http";
|
|
import type { CommercialAdminResourceDef } from "../admin/resources";
|
|
|
|
export type RegistryRecord = Record<string, unknown>;
|
|
|
|
export async function listAdminRegistry(
|
|
resource: CommercialAdminResourceDef
|
|
): Promise<AdapterResult<RegistryRecord[]>> {
|
|
const res = await commercialGet<unknown>(resource.listPath);
|
|
if (!res.ok) {
|
|
return failResult(res.availability, res.message, []);
|
|
}
|
|
const items = extractItems<RegistryRecord>(res.data, resource.envelopeKeys);
|
|
return listResult(items, `admin.${resource.key}`, "موردی در رجیستری نیست");
|
|
}
|
|
|
|
export async function createAdminRegistryItem(
|
|
resource: CommercialAdminResourceDef,
|
|
body: RegistryRecord
|
|
): Promise<{ ok: true; data: RegistryRecord } | { ok: false; message: string }> {
|
|
const res = await commercialPost<RegistryRecord>(resource.createPath, body);
|
|
if (!res.ok) return { ok: false, message: res.message };
|
|
return { ok: true, data: res.data };
|
|
}
|
|
|
|
export async function updateAdminRegistryItem(
|
|
resource: CommercialAdminResourceDef,
|
|
code: string,
|
|
body: RegistryRecord
|
|
): Promise<{ ok: true; data: RegistryRecord } | { ok: false; message: string }> {
|
|
const path = `${resource.listPath}/${encodeURIComponent(code)}`;
|
|
const patch = await commercialPatch<RegistryRecord>(path, body);
|
|
if (patch.ok) return { ok: true, data: patch.data };
|
|
const put = await commercialPut<RegistryRecord>(path, body);
|
|
if (put.ok) return { ok: true, data: put.data };
|
|
return { ok: false, message: patch.message || put.message };
|
|
}
|
|
|
|
export async function deleteAdminRegistryItem(
|
|
resource: CommercialAdminResourceDef,
|
|
code: string
|
|
): Promise<{ ok: true } | { ok: false; message: string }> {
|
|
const path = `${resource.listPath}/${encodeURIComponent(code)}`;
|
|
const res = await commercialDelete(path);
|
|
if (!res.ok) return { ok: false, message: res.message };
|
|
return { ok: true };
|
|
}
|
|
|
|
export async function simulateRecommendations(answers: Record<string, unknown>) {
|
|
return commercialPost<unknown>("/api/v1/commercial/recommendations", {
|
|
answers,
|
|
version: "recommendation.v1",
|
|
});
|
|
}
|