From b4873cf1f8a307c6edbf133600f9d5a67fe3e3e5 Mon Sep 17 00:00:00 2001 From: Arthur Bassi Date: Tue, 16 Jun 2026 09:10:38 -0300 Subject: [PATCH] fix(contacts): resolve detail via GetContacts and map address DTOs --- src/domain/contacts/api/contacts-api.ts | 23 ++++++++-- src/domain/contacts/mappers/contact-mapper.ts | 42 +++++++------------ .../contacts/mappers/contact-mapper.test.ts | 29 +++++++++++++ 3 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 src/test/domain/contacts/mappers/contact-mapper.test.ts diff --git a/src/domain/contacts/api/contacts-api.ts b/src/domain/contacts/api/contacts-api.ts index 8de0848e..9c191e6e 100644 --- a/src/domain/contacts/api/contacts-api.ts +++ b/src/domain/contacts/api/contacts-api.ts @@ -38,10 +38,25 @@ export const contactsApi = { }, getById: async (id: string | number): Promise => { - const data = await apiGet(API_PATHS.contact.getById, { - searchParams: { id }, - }); - return mapContact(handleApiResponse(data)); + const data = await apiGet(API_PATHS.contact.getContacts); + const contacts = handleApiResponse(data); + const targetId = String(id); + const match = Array.isArray(contacts) + ? contacts.find((item) => { + const record = (typeof item === "object" && item !== null ? item : {}) as Record< + string, + unknown + >; + const itemId = record.id ?? record.Id; + return String(itemId) === targetId; + }) + : undefined; + + if (!match) { + throw new Error("Contact not found"); + } + + return mapContact(match); }, create: async (payload: unknown): Promise => { diff --git a/src/domain/contacts/mappers/contact-mapper.ts b/src/domain/contacts/mappers/contact-mapper.ts index 9216ee6a..48f5a64c 100644 --- a/src/domain/contacts/mappers/contact-mapper.ts +++ b/src/domain/contacts/mappers/contact-mapper.ts @@ -71,35 +71,25 @@ export function mapContact(raw: unknown): Contact { export function mapContactToBackend(contact: unknown): Record { const item = asRecord(contact); const payload: Record = { - firstName: item.firstName ?? "", - middleName: item.middleName ?? "", - lastName: item.lastName ?? "", - title: item.title ?? "", - contactType: item.contactType ?? "", - gender: item.gender ?? "", - dateOfBirth: item.dateOfBirth ?? "", - territory: item.territory ?? "", - owner: item.owner ?? "", - privacySetting: item.privacySetting ?? "PublicReadWrite", - about: item.about ?? "", - tags: item.tags ?? "", - street: item.street ?? "", - streetLineTwo: item.streetLineTwo ?? "", - city: item.city ?? "", - zip: item.zip ?? "", - country: item.country ?? "US", - state: item.state ?? "", - county: item.county ?? "", - timeZone: item.timeZone ?? "Eastern Standard Time", - smsAddress: item.smsAddress ?? "", - website: item.website ?? "", - facebookUrl: item.facebookUrl ?? "", - linkedInUrl: item.linkedInUrl ?? "", - twitterUrl: item.twitterUrl ?? "", + FirstName: item.firstName ?? item.FirstName ?? "", + MiddleName: item.middleName ?? item.MiddleName ?? "", + LastName: item.lastName ?? item.LastName ?? "", + Title: item.title ?? item.Title ?? "", + ContactType: item.contactType ?? item.ContactType ?? "", + PhoneNumber: item.phoneNumber ?? item.PhoneNumber ?? item.phone ?? item.Phone ?? "", + Email: item.email ?? item.Email ?? "", + Address1: item.street ?? item.Address1 ?? "", + Address2: item.streetLineTwo ?? item.Address2 ?? "", + City: item.city ?? item.City ?? "", + State: item.state ?? item.State ?? "", + Zip: item.zip ?? item.Zip ?? "", + FacebookUrl: item.facebookUrl ?? item.FacebookUrl ?? "", + LinkedInUrl: item.linkedInUrl ?? item.LinkedInUrl ?? "", + TwitterUrl: item.twitterUrl ?? item.TwitterUrl ?? "", }; 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/test/domain/contacts/mappers/contact-mapper.test.ts b/src/test/domain/contacts/mappers/contact-mapper.test.ts new file mode 100644 index 00000000..50253a7b --- /dev/null +++ b/src/test/domain/contacts/mappers/contact-mapper.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from "vitest"; +import { mapContactToBackend } from "@/domain/contacts/mappers/contact-mapper"; + +describe("mapContactToBackend", () => { + it("maps street fields to Address1 and Address2", () => { + const payload = mapContactToBackend({ + firstName: "Jane", + lastName: "Doe", + street: "123 Main St", + streetLineTwo: "Suite 4", + }); + + expect(payload.Address1).toBe("123 Main St"); + expect(payload.Address2).toBe("Suite 4"); + expect(payload.FirstName).toBe("Jane"); + expect(payload.LastName).toBe("Doe"); + expect(payload).not.toHaveProperty("street"); + }); + + it("includes Id for updates", () => { + const payload = mapContactToBackend({ + id: 7, + firstName: "Jane", + lastName: "Doe", + }); + + expect(payload.Id).toBe(7); + }); +});