Skip to content

refactor(google-contacts-export): reduce mapToPerson cyclomatic complexity (currently 33) #2831

@malek10xdev

Description

@malek10xdev

Context

backend/src/services/export/exports/google/contacts-api.ts:400mapToPerson has cyclomatic complexity 33 (DeepSource JS-R1005, "very-high" risk). The function is 130+ lines and handles 7+ different schema field types in one method.

The issue was flagged on PR #2830 (phone-only contacts) and intentionally not fixed in that PR because:

  • It is pre-existing on main (function is 129 lines on main; we added only 5 lines for the hasEmail guard in commit fcbcccd).
  • The refactor is non-trivial (~30-60 min) and carries regression risk without dedicated test coverage.
  • It is out of scope for the phone-only contacts feature.

Proposed approach

Split mapToPerson into smaller helper functions, one per schema field:

  • mapNames(contact, existing, updateEmptyOnly): Schema$Name[]
  • mapEmails(contact, existing, updateEmptyOnly): Schema$EmailAddress[]
  • mapPhones(contact, existing): Schema$PhoneNumber[]
  • mapOrgs(contact, existing): Schema$Organization[]
  • mapUrls(contact, existing): Schema$Url[]
  • mapAddresses(contact, existing): Schema$Address[]
  • mapMemberships(contact, existing, labels): Schema$Membership[]

Then mapToPerson becomes a ~10-line composer.

Existing test coverage

backend/test/unit/contact-export/googleContacts.test.ts:677 contains a describe('mapToPerson', ...) block that covers the happy path. May need additional branch coverage for the updateEmptyOnly flag and the per-field "is duplicate" checks.

Acceptance criteria

  • mapToPerson cyclomatic complexity ≤ 20
  • All existing googleContacts.test.ts tests pass
  • No regression in Google Contacts sync UX (manual QA on a small export)
  • DeepSource JS-R1005 no longer flags this file

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions