"use server";
import { revalidatePath } from "next/cache";
import { winofyFetch, type PaginatedResponse } from "./winofy";
import { callApi, type ActionResult } from "./result";
import type { Address, AddressLabel } from "@/types/api";
export async function getAddresses(): Promise
{
const res = await winofyFetch>("/v1/addresses/");
return res.results;
}
export interface AddressInput {
label: AddressLabel;
title: string;
full_address: string;
plaque: string;
floor: string;
unit: string;
recipient_name: string;
recipient_phone: string;
city_uuid: string;
neighborhood_uuid?: string;
latitude: number;
longitude: number;
is_default: boolean;
}
export async function createAddress(input: AddressInput): Promise> {
const result = await callApi(() => winofyFetch("/v1/addresses/", { method: "POST", body: input }));
if (result.ok) revalidatePath("/addresses");
return result;
}
export async function updateAddress(uuid: string, input: Partial): Promise> {
const result = await callApi(() =>
winofyFetch(`/v1/addresses/${uuid}/`, { method: "PATCH", body: input }),
);
if (result.ok) revalidatePath("/addresses");
return result;
}
export async function deleteAddress(uuid: string): Promise {
const result = await callApi(() => winofyFetch(`/v1/addresses/${uuid}/`, { method: "DELETE" }));
if (result.ok) revalidatePath("/addresses");
return result;
}