fix(C116): editor de Contato convive com o shape phones do C112 (primary phone via reorderWithPrimaryPhone)

This commit is contained in:
Francisco Solla 2026-08-11 14:01:01 -03:00
parent a83bedd7a8
commit 04d9940317
2 changed files with 25 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import { revalidatePath } from 'next/cache'
import { toggleMunicipalityAdvisorMembership } from '@/app/(campaign)/campanha/actions/municipality'
import { nextLeadershipAdvisorIdsAfterMembership } from '@/lib/leadershipAdvisorMembership'
import { reorderWithPrimaryPhone } from '@/lib/phone'
import { uniqueRelationshipIds } from '@/lib/relationship'
import { contactFieldUpdateSchema, type ContactFieldUpdateInput } from '@/lib/schemas/contact'
import {
@ -123,13 +124,32 @@ export const updatePersonContactRecord = async (
await assertPersonContactEditable(payload, currentActor, data.id, req)
const contactData: Partial<Pick<Contact, 'name' | 'phone' | 'email' | 'city'>> = {}
const contactData: Partial<Pick<Contact, 'name' | 'email' | 'city'>> & {
phones?: { value: string }[]
} = {}
if (data.field === 'name') {
contactData.name = data.name
} else if (data.field === 'email') {
contactData.email = data.email ?? null
} else if (data.field === 'phone') {
contactData.phone = data.phone
// Inline cell edit = set the PRIMARY phone, keeping the rest of the
// list untouched (C112 shape — same contract as the leadership and
// dobradinha siblings): the new number goes first, clearing removes
// the primary and the rest shifts up.
const current = await payload.findByID({
collection: 'contact',
id: data.id,
depth: 0,
select: { phones: true },
// Intentional bypass: the scope check above established the actor's
// right over the person's ficha; this read only resolves the current
// primary-phone list to reorder.
overrideAccess: true,
req,
})
contactData.phones = reorderWithPrimaryPhone(current.phones, data.phone).map((value) => ({
value,
}))
} else if (data.field === 'city') {
contactData.city = data.city
}

View file

@ -9,6 +9,7 @@ import {
setPersonAssessoraMembershipRecord,
updatePersonContactRecord,
} from '@/app/(campaign)/campanha/actions/person'
import { primaryPhoneOf } from '@/lib/phone'
import { uniqueRelationshipIds } from '@/lib/relationship'
import {
PERSON_ADVISORS_UNRESTRICTED_MESSAGE,
@ -67,7 +68,8 @@ describe('C116 — cell edits of the people list', () => {
})
expect(updated.name).toBe('Maria Nova da Silva')
expect(updated.city).toBe('Salvador')
expect(updated.phone).toBe('71999998888')
// C112 shape: the cell edit set the PRIMARY phone, the ficha list carries it first.
expect(primaryPhoneOf(updated.phones)).toBe('71999998888')
})
it('lets an advisor edit a person anchored in his carteira', async () => {