fix(contacts): resolve detail via GetContacts and map address DTOs

This commit is contained in:
Arthur Bassi 2026-06-16 09:10:38 -03:00
parent 786e588623
commit b4873cf1f8
3 changed files with 64 additions and 30 deletions

View file

@ -38,10 +38,25 @@ export const contactsApi = {
}, },
getById: async (id: string | number): Promise<Contact> => { getById: async (id: string | number): Promise<Contact> => {
const data = await apiGet<unknown>(API_PATHS.contact.getById, { const data = await apiGet<unknown>(API_PATHS.contact.getContacts);
searchParams: { id }, const contacts = handleApiResponse<unknown[]>(data);
}); const targetId = String(id);
return mapContact(handleApiResponse(data)); 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<Contact> => { create: async (payload: unknown): Promise<Contact> => {

View file

@ -71,35 +71,25 @@ export function mapContact(raw: unknown): Contact {
export function mapContactToBackend(contact: unknown): Record<string, unknown> { export function mapContactToBackend(contact: unknown): Record<string, unknown> {
const item = asRecord(contact); const item = asRecord(contact);
const payload: Record<string, unknown> = { const payload: Record<string, unknown> = {
firstName: item.firstName ?? "", FirstName: item.firstName ?? item.FirstName ?? "",
middleName: item.middleName ?? "", MiddleName: item.middleName ?? item.MiddleName ?? "",
lastName: item.lastName ?? "", LastName: item.lastName ?? item.LastName ?? "",
title: item.title ?? "", Title: item.title ?? item.Title ?? "",
contactType: item.contactType ?? "", ContactType: item.contactType ?? item.ContactType ?? "",
gender: item.gender ?? "", PhoneNumber: item.phoneNumber ?? item.PhoneNumber ?? item.phone ?? item.Phone ?? "",
dateOfBirth: item.dateOfBirth ?? "", Email: item.email ?? item.Email ?? "",
territory: item.territory ?? "", Address1: item.street ?? item.Address1 ?? "",
owner: item.owner ?? "", Address2: item.streetLineTwo ?? item.Address2 ?? "",
privacySetting: item.privacySetting ?? "PublicReadWrite", City: item.city ?? item.City ?? "",
about: item.about ?? "", State: item.state ?? item.State ?? "",
tags: item.tags ?? "", Zip: item.zip ?? item.Zip ?? "",
street: item.street ?? "", FacebookUrl: item.facebookUrl ?? item.FacebookUrl ?? "",
streetLineTwo: item.streetLineTwo ?? "", LinkedInUrl: item.linkedInUrl ?? item.LinkedInUrl ?? "",
city: item.city ?? "", TwitterUrl: item.twitterUrl ?? item.TwitterUrl ?? "",
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 ?? "",
}; };
const id = item.id ?? item.Id; const id = item.id ?? item.Id;
if (typeof id === "string" || typeof id === "number") { if (typeof id === "string" || typeof id === "number") {
payload.id = id; payload.Id = id;
} }
return payload; return payload;
} }

View file

@ -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);
});
});