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> => {
const data = await apiGet<unknown>(API_PATHS.contact.getById, {
searchParams: { id },
});
return mapContact(handleApiResponse(data));
const data = await apiGet<unknown>(API_PATHS.contact.getContacts);
const contacts = handleApiResponse<unknown[]>(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<Contact> => {

View file

@ -71,35 +71,25 @@ export function mapContact(raw: unknown): Contact {
export function mapContactToBackend(contact: unknown): Record<string, unknown> {
const item = asRecord(contact);
const payload: Record<string, unknown> = {
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;
}

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