Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apps/braze/src/fields/ExternalResourceField.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 I feel like this class and richText have a lot in common. Perhaps there is a way to avoid duplication. On the other hand, I like that it is explicit on how we handle ExternalResourceField. So I leave it up to you if you want to remove the duplicates

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that rich text is supported in one of the versions, that's why I kept them separate

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Field } from './Field';
import { FieldRegistry } from './fieldRegistry';

export class ExternalResourceField extends Field {
constructor(id: string, name: string, entryContentTypeId: string, localized: boolean) {
super(id, name, entryContentTypeId, localized);
}

get type(): string {
return 'ExternalResourceField';
}

public override set selected(value: boolean) {
return;
}

static fromSerialized(serializedField: any): ExternalResourceField {
const field = new ExternalResourceField(
serializedField.id,
serializedField.name,
serializedField.entryContentTypeId,
serializedField.localized
);
return field;
}

generateQuery(): string {
throw new Error('External resource not supported');
}

generateLiquidTagForType(template: string): string[] {
throw new Error('External resource not supported');
}

isEnabledForGenerate(): boolean {
return false;
}

isEnabledForCreate(): boolean {
return false;
}
}

FieldRegistry.registerFieldType('ExternalResourceField', ExternalResourceField.fromSerialized);
3 changes: 3 additions & 0 deletions apps/braze/src/fields/FieldsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ReferenceItem } from './ReferenceItem';
import { RichTextField } from './RichTextField';
import { TextArrayField } from './TextArrayField';
import resolveResponse from 'contentful-resolve-response';
import { ExternalResourceField } from './ExternalResourceField';

export class FieldsFactory {
private contentTypes: { [key: string]: ContentTypeProps };
Expand Down Expand Up @@ -134,6 +135,8 @@ export class FieldsFactory {
fieldClass = RichTextField;
} else if (fieldInfo.type === 'Location') {
fieldClass = LocationField;
} else if (fieldInfo.type === 'ResourceLink') {
fieldClass = ExternalResourceField;
} else {
fieldClass = BasicField;
}
Expand Down
9 changes: 0 additions & 9 deletions apps/braze/src/fields/RichTextField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class RichTextField extends Field {
serializedField.entryContentTypeId,
serializedField.localized
);
field.selected = serializedField.selected;
return field;
}

Expand All @@ -37,14 +36,6 @@ export class RichTextField extends Field {
isEnabledForGenerate(): boolean {
return false;
}

displayNameForGenerate(): string {
return `${this.name} (Support for rich text fields coming soon)`;
}

displayNameForCreate(): string {
return this.name;
}
}

FieldRegistry.registerFieldType('RichTextField', RichTextField.fromSerialized);
29 changes: 29 additions & 0 deletions apps/braze/test/fields/FieldsFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ vi.mock('contentful-resolve-response', () => {

// Import the mocked function for use in tests
import resolveResponse from 'contentful-resolve-response';
import { ExternalResourceField } from '../../src/fields/ExternalResourceField';

describe('FieldsFactory', () => {
const mockCma = {
Expand Down Expand Up @@ -475,6 +476,34 @@ describe('FieldsFactory', () => {
'English Title'
);
});

it('should create an ExternalResourceField instance with correct properties', async () => {
const mockEntry = [
{
fields: {
externalReference: {
sys: { type: 'ResourceLink', linkType: 'Contentful:Entry', urn: 'an-id' },
},
},
},
];
(resolveResponse as any).mockReturnValue(mockEntry);

mockCma.contentType.get.mockResolvedValue({
fields: [{ id: 'externalReference', type: 'ResourceLink', localized: false }],
sys: {
id: 'externalArticle',
},
});

const result = await createFields(entryId, entryContentTypeId, mockCma);
expect(result).toHaveLength(1);
expect(result[0]).toBeInstanceOf(ExternalResourceField);
const fieldInstance = result[0] as ExternalResourceField;
expect(fieldInstance.id).toBe('externalReference');
expect(fieldInstance.entryContentTypeId).toBe('externalArticle');
expect(fieldInstance.localized).toBe(false);
});
});
});

Expand Down
Loading