Repository navigation
OUT-4208: collapse duplicate custom-fields requests on client pageload #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 4 additions & 14 deletions
18
src/features/custom-fields/lib/custom-fields.controller.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { CustomFieldEntityType, type CustomFieldOption, CustomFieldType } from '@assembly/types' | ||
| import type { IconType } from '@assembly-js/design-system' | ||
|
|
||
| const CUSTOM_FIELD_TYPE_ICON: Record<CustomFieldType, IconType> = { | ||
| [CustomFieldType.ADDRESS]: 'Location', | ||
| [CustomFieldType.EMAIL]: 'Email', | ||
| [CustomFieldType.PHONE_NUMBER]: 'MobileNumber', | ||
| [CustomFieldType.TEXT]: 'Text', | ||
| [CustomFieldType.NUMBER]: 'Number', | ||
| [CustomFieldType.URL]: 'Link', | ||
| [CustomFieldType.TAGS]: 'Tag', | ||
| } | ||
|
|
||
| export type CustomFieldItem = { | ||
| id: string | ||
| key: string | ||
| name: string | ||
| type: CustomFieldType | ||
| icon: IconType | ||
| options: CustomFieldOption[] | ||
| } | ||
|
|
||
| /** Nested map: { [entityType]: { [fieldKey]: { [optionKey]: optionLabel } } } */ | ||
| export type CustomFieldOptionsMap = Record<string, Record<string, Record<string, string>>> | ||
|
|
||
| export type CustomFieldItemWithEntity = CustomFieldItem & { entityType: CustomFieldEntityType } | ||
|
|
||
| /** Raw custom field as returned by the list endpoint, before UI mapping. */ | ||
| type RawCustomField = Omit<CustomFieldItem, 'icon'> & { | ||
| order: number | ||
| entityType: CustomFieldEntityType | ||
| } | ||
|
|
||
| // Sort by display order and attach the type-specific icon, keeping entityType | ||
| // so callers can split the combined response. | ||
| export const mapCustomFields = (fields: RawCustomField[]): CustomFieldItemWithEntity[] => | ||
| [...fields] | ||
| .sort((a, b) => a.order - b.order) | ||
| .map(({ id, key, name, type, options, entityType }) => ({ | ||
| id, | ||
| key, | ||
| name, | ||
| type, | ||
| options, | ||
| entityType, | ||
| icon: CUSTOM_FIELD_TYPE_ICON[type], | ||
| })) | ||
|
|
||
| // Split the combined list by entity type, since field keys are only unique | ||
| // within an entity type (client vs company). | ||
| export const splitCustomFieldsByEntity = ( | ||
| items: CustomFieldItemWithEntity[], | ||
| ): { clientCustomFields: CustomFieldItem[]; companyCustomFields: CustomFieldItem[] } => { | ||
| const clientCustomFields: CustomFieldItem[] = [] | ||
| const companyCustomFields: CustomFieldItem[] = [] | ||
| for (const { entityType, ...field } of items) { | ||
| if (entityType === CustomFieldEntityType.CLIENT) clientCustomFields.push(field) | ||
| else if (entityType === CustomFieldEntityType.COMPANY) companyCustomFields.push(field) | ||
| } | ||
| return { clientCustomFields, companyCustomFields } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { CustomFieldEntityType, CustomFieldType } from '@assembly/types' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| type CustomFieldItemWithEntity, | ||
| mapCustomFields, | ||
| splitCustomFieldsByEntity, | ||
| } from '@/features/custom-fields/utils/custom-field-mappers' | ||
|
|
||
| // A raw field as returned by the list endpoint (pre UI mapping). | ||
| const rawField = (overrides: Partial<Parameters<typeof mapCustomFields>[0][number]> = {}) => ({ | ||
| id: 'field-1', | ||
| key: 'field_1', | ||
| name: 'Field 1', | ||
| type: CustomFieldType.TEXT, | ||
| order: 0, | ||
| options: [], | ||
| entityType: CustomFieldEntityType.CLIENT, | ||
| ...overrides, | ||
| }) | ||
|
|
||
| describe('mapCustomFields', () => { | ||
| it('sorts by order and attaches the type-specific icon while keeping entityType', () => { | ||
| const result = mapCustomFields([ | ||
| rawField({ id: 'b', order: 2, type: CustomFieldType.EMAIL }), | ||
| rawField({ id: 'a', order: 1, type: CustomFieldType.TAGS }), | ||
| ]) | ||
|
|
||
| expect(result.map((f) => f.id)).toEqual(['a', 'b']) | ||
| expect(result[0]).toMatchObject({ id: 'a', icon: 'Tag', entityType: CustomFieldEntityType.CLIENT }) | ||
| expect(result[1]).toMatchObject({ id: 'b', icon: 'Email' }) | ||
| }) | ||
|
|
||
| it('does not mutate the input array', () => { | ||
| const input = [rawField({ id: 'b', order: 2 }), rawField({ id: 'a', order: 1 })] | ||
| mapCustomFields(input) | ||
| expect(input.map((f) => f.id)).toEqual(['b', 'a']) | ||
| }) | ||
| }) | ||
|
|
||
| describe('splitCustomFieldsByEntity', () => { | ||
| const items = [ | ||
| { entityType: CustomFieldEntityType.CLIENT, id: 'c1' }, | ||
| { entityType: CustomFieldEntityType.COMPANY, id: 'co1' }, | ||
| { entityType: CustomFieldEntityType.CLIENT, id: 'c2' }, | ||
| ] as unknown as CustomFieldItemWithEntity[] | ||
|
|
||
| it('splits fields into client and company lists, preserving order', () => { | ||
| const { clientCustomFields, companyCustomFields } = splitCustomFieldsByEntity(items) | ||
| expect(clientCustomFields.map((f) => f.id)).toEqual(['c1', 'c2']) | ||
| expect(companyCustomFields.map((f) => f.id)).toEqual(['co1']) | ||
| }) | ||
|
|
||
| it('strips entityType from the returned items', () => { | ||
| const { clientCustomFields } = splitCustomFieldsByEntity(items) | ||
| expect(clientCustomFields[0]).not.toHaveProperty('entityType') | ||
| }) | ||
| }) | ||
|
|
||
| describe('mapCustomFields + splitCustomFieldsByEntity', () => { | ||
| it('turns one mixed, unordered response into two ordered per-entity lists', () => { | ||
| const mapped = mapCustomFields([ | ||
| rawField({ id: 'company-late', order: 3, entityType: CustomFieldEntityType.COMPANY }), | ||
| rawField({ id: 'client-late', order: 2, entityType: CustomFieldEntityType.CLIENT }), | ||
| rawField({ id: 'client-early', order: 1, entityType: CustomFieldEntityType.CLIENT }), | ||
| ]) | ||
|
|
||
| const { clientCustomFields, companyCustomFields } = splitCustomFieldsByEntity(mapped) | ||
|
|
||
| expect(clientCustomFields.map((f) => f.id)).toEqual(['client-early', 'client-late']) | ||
| expect(companyCustomFields.map((f) => f.id)).toEqual(['company-late']) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GET /api/custom-fields, two ordered lists, and the unchanged options-map request. This protects the main reason for this change as well as the returned data.Knowledge Base Used: Content assets and templates
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 3c2dc57 — extracted the sort/icon mapping and entity split into
custom-field-mappers.tsand addedtests/unit/custom-field-mappers.test.ts, which asserts a mixed, unordered response yields two correctly ordered per-entity lists (client vs company). The single-request and unchanged options-map behavior is covered by the manual test steps in the PR description.