fix(accounts): align detail and mutation payloads with backend

This commit is contained in:
Arthur Bassi 2026-06-16 09:10:11 -03:00
parent a1ed14538c
commit 786e588623
4 changed files with 74 additions and 17 deletions

View file

@ -20,6 +20,37 @@ function buildSearchParams(params: AccountsListParams): Record<string, string |
};
}
async function findAccountInList(id: string | number): Promise<Account> {
const targetId = String(id);
let page = 1;
const pageSize = 100;
while (true) {
const data = await apiGet<unknown>(API_PATHS.account.getList, {
searchParams: { search: "", page, pageSize },
});
const paginated = handlePaginatedResponse<unknown>(data, { page, pageSize });
const match = paginated.items.find((item) => {
const record = (typeof item === "object" && item !== null ? item : {}) as Record<
string,
unknown
>;
const itemId = record.id ?? record.Id;
return String(itemId) === targetId;
});
if (match) {
return mapAccount(match);
}
if (paginated.items.length === 0 || page * pageSize >= paginated.totalCount) {
throw new Error("Account not found");
}
page += 1;
}
}
export const accountsApi = {
getList: async (params: AccountsListParams = {}): Promise<AccountsListResult> => {
const searchParams = buildSearchParams(params);
@ -37,12 +68,8 @@ export const accountsApi = {
};
},
getById: async (id: string | number): Promise<Account> => {
const data = await apiGet<unknown>(API_PATHS.account.getById, {
searchParams: { id },
});
return mapAccount(handleApiResponse(data));
},
/** Backend has no GetById — resolve from paginated list. */
getById: async (id: string | number): Promise<Account> => findAccountInList(id),
create: async (payload: unknown): Promise<Account> => {
const data = await apiPost<unknown>(

View file

@ -56,6 +56,8 @@ export function mapAccount(raw: unknown): Account {
State: readString(item, "State", "state") || undefined,
Zip: readString(item, "Zip", "zip", "zipCode") || undefined,
Country: readString(item, "Country", "country") || undefined,
Phone: readString(item, "Phone", "phone", "phoneNumber", "PhoneNumber") || undefined,
Email: readString(item, "Email", "email") || undefined,
};
}
@ -66,22 +68,23 @@ export function mapAccountToBackend(account: unknown): Record<string, unknown> {
>;
const payload: Record<string, unknown> = {
AccountName: item.AccountName ?? "",
AccountNumber: item.AccountNumber ?? "",
Industry: item.Industry ?? "",
Name: item.AccountName ?? item.Name ?? "",
Website: item.Website ?? "",
Notes: item.Notes ?? "",
Address1: item.Address1 ?? "",
Address2: item.Address2 ?? "",
City: item.City ?? "",
State: item.State ?? "",
Zip: item.Zip ?? "",
Country: item.Country ?? "US",
};
const phone = item.Phone ?? item.phoneNumber ?? item.PhoneNumber;
if (typeof phone === "string" && phone) {
payload.Phone = phone;
}
const email = item.Email ?? item.email;
if (typeof email === "string" && email) {
payload.Email = email;
}
const id = item.id ?? item.Id;
if (typeof id === "string" || typeof id === "number") {
payload.id = id;
payload.Id = id;
}
return payload;

View file

@ -22,6 +22,8 @@ export interface Account {
State?: string;
Zip?: string;
Country?: string;
Phone?: string;
Email?: string;
}
export interface AccountsListParams {

View file

@ -0,0 +1,25 @@
import { describe, expect, it } from "vitest";
import { mapAccountToBackend } from "@/domain/accounts/mappers/account-mapper";
describe("mapAccountToBackend", () => {
it("maps AccountName to backend Name field", () => {
const payload = mapAccountToBackend({
AccountName: "Acme Corp",
Website: "https://acme.example",
});
expect(payload.Name).toBe("Acme Corp");
expect(payload.Website).toBe("https://acme.example");
expect(payload).not.toHaveProperty("AccountName");
});
it("includes Id for updates", () => {
const payload = mapAccountToBackend({
id: 42,
AccountName: "Acme Corp",
});
expect(payload.Id).toBe(42);
expect(payload.Name).toBe("Acme Corp");
});
});