From 786e588623393be49eeba58cc9f6db171ad99cbf Mon Sep 17 00:00:00 2001 From: Arthur Bassi Date: Tue, 16 Jun 2026 09:10:11 -0300 Subject: [PATCH] fix(accounts): align detail and mutation payloads with backend --- src/domain/accounts/api/accounts-api.ts | 39 ++++++++++++++++--- src/domain/accounts/mappers/account-mapper.ts | 25 ++++++------ src/domain/accounts/types/account.ts | 2 + .../accounts/mappers/account-mapper.test.ts | 25 ++++++++++++ 4 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 src/test/domain/accounts/mappers/account-mapper.test.ts diff --git a/src/domain/accounts/api/accounts-api.ts b/src/domain/accounts/api/accounts-api.ts index 7f713a31..f0d07328 100644 --- a/src/domain/accounts/api/accounts-api.ts +++ b/src/domain/accounts/api/accounts-api.ts @@ -20,6 +20,37 @@ function buildSearchParams(params: AccountsListParams): Record { + const targetId = String(id); + let page = 1; + const pageSize = 100; + + while (true) { + const data = await apiGet(API_PATHS.account.getList, { + searchParams: { search: "", page, pageSize }, + }); + const paginated = handlePaginatedResponse(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 => { const searchParams = buildSearchParams(params); @@ -37,12 +68,8 @@ export const accountsApi = { }; }, - getById: async (id: string | number): Promise => { - const data = await apiGet(API_PATHS.account.getById, { - searchParams: { id }, - }); - return mapAccount(handleApiResponse(data)); - }, + /** Backend has no GetById — resolve from paginated list. */ + getById: async (id: string | number): Promise => findAccountInList(id), create: async (payload: unknown): Promise => { const data = await apiPost( diff --git a/src/domain/accounts/mappers/account-mapper.ts b/src/domain/accounts/mappers/account-mapper.ts index 100e36d9..4927116a 100644 --- a/src/domain/accounts/mappers/account-mapper.ts +++ b/src/domain/accounts/mappers/account-mapper.ts @@ -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 { >; const payload: Record = { - 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; diff --git a/src/domain/accounts/types/account.ts b/src/domain/accounts/types/account.ts index bc3d531c..7d9823b0 100644 --- a/src/domain/accounts/types/account.ts +++ b/src/domain/accounts/types/account.ts @@ -22,6 +22,8 @@ export interface Account { State?: string; Zip?: string; Country?: string; + Phone?: string; + Email?: string; } export interface AccountsListParams { diff --git a/src/test/domain/accounts/mappers/account-mapper.test.ts b/src/test/domain/accounts/mappers/account-mapper.test.ts new file mode 100644 index 00000000..d65f2be0 --- /dev/null +++ b/src/test/domain/accounts/mappers/account-mapper.test.ts @@ -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"); + }); +});