Transform record phone field metadata (#12706)

# Introduction
close https://github.com/twentyhq/twenty/issues/12343

Adding a transform step for any field phone in order to infer country
code and calling code from the number if they're provided

## Edges cases
```ts
RecordTransformerExceptionCode.INVALID_PHONE_NUMBER:
RecordTransformerExceptionCode.INVALID_PHONE_COUNTRY_CODE:
RecordTransformerExceptionCode.CONFLICTING_PHONE_COUNTRY_CODE:
RecordTransformerExceptionCode.CONFLICTING_PHONE_CALLING_CODE:
RecordTransformerExceptionCode.CONFLICTING_PHONE_CALLING_CODE_AND_COUNTRY_CODE:
RecordTransformerExceptionCode.INVALID_PHONE_CALLING_CODE:
RecordTransformerExceptionCode.INVALID_URL:
```

## Coverage
Note: Will handle REST api integration testing pivot and UPDATE
operation later in the afternoon, critical bug appeared that I prefer
handling before improving this PR coverage, also would be too many
updates
Note2: Haven't fuzzed all of the string inputs, would seem overkill for
such a use case, to be debated
```ts
 PASS  test/integration/metadata/suites/field-metadata/phone/create-one-field-metadata-phone.integration-spec.ts (23.609 s)
  Phone field metadata tests suite
    ✓ It should succeed create primary phone field (1397 ms)
    ✓ It should succeed create primary phone field with number and other information (930 ms)
    ✓ It should succeed create primary phone field with full international format and other information (893 ms)
    ✓ It should succeed create primary phone field with full international and infer other information from it but not the countryCode as its shared (825 ms)
    ✓ It should succeed create primary phone field with full international and infer other information from it (818 ms)
    ✓ It should succeed create primary phone field with empty payload (827 ms)
    ✓ It should succeed create additional phone field with number and other information (894 ms)
    ✓ It should succeed create additional phone field with full international format and other information (1024 ms)
    ✓ It should succeed create additional phone field with full international and infer other information from it but not the countryCode as its shared (808 ms)
    ✓ It should succeed create additional phone field with full international and infer other information from it (751 ms)
    ✓ It should succeed create additional phone field with empty payload (739 ms)
    ✓ It should fail to create primary phone field without country or calling code at all (776 ms)
    ✓ It should fail to create primary phone field with invalid country code (782 ms)
    ✓ It should fail to create primary phone field with invalid calling code (858 ms)
    ✓ It should fail to create primary phone field with conflicting country code and calling code (872 ms)
    ✓ It should fail to create primary phone field with invalid phone number format (1489 ms)
    ✓ It should fail to create primary phone field with conflicting phone number country code (1425 ms)
    ✓ It should fail to create primary phone field with conflicting phone number calling code (1553 ms)
    ✓ It should fail to create primary phone field without country or calling code at all (814 ms)
    ✓ It should fail to create primary phone field with invalid country code (813 ms)
    ✓ It should fail to create primary phone field with invalid calling code (742 ms)
    ✓ It should fail to create primary phone field with conflicting country code and calling code (783 ms)
    ✓ It should fail to create primary phone field with invalid phone number format (731 ms)
    ✓ It should fail to create primary phone field with conflicting phone number country code (947 ms)
    ✓ It should fail to create primary phone field with conflicting phone number calling code (822 ms)

Test Suites: 1 passed, 1 total
Tests:       25 passed, 25 total
Snapshots:   14 passed, 14 total
Time:        23.627 s
```
This commit is contained in:
Paul Rastoin
2025-06-19 16:39:58 +02:00
committed by GitHub
parent 1d1718a8a8
commit e1393c4887
12 changed files with 1049 additions and 13 deletions

View File

@ -0,0 +1,46 @@
import { makeGraphqlAPIRequest } from 'test/integration/graphql/utils/make-graphql-api-request.util';
import { CommonResponseBody } from 'test/integration/metadata/types/common-response-body.type';
import { PerformMetadataQueryParams } from 'test/integration/metadata/types/perform-metadata-query.type';
import { warnIfNoErrorButExpectedToFail } from 'test/integration/metadata/utils/warn-if-no-error-but-expected-to-fail.util';
import { capitalize } from 'twenty-shared/utils';
import { updateOneOperationFactory } from 'test/integration/graphql/utils/update-one-operation-factory.util';
import { ObjectRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
type UpdateOneOperationArgs<T> = PerformMetadataQueryParams<T> & {
objectMetadataSingularName: string;
recordId: string;
};
export const updateOneOperation = async <T = object>({
input,
gqlFields = 'id',
objectMetadataSingularName,
expectToFail = false,
recordId,
}: UpdateOneOperationArgs<T>): CommonResponseBody<{
updateOneResponse: ObjectRecord;
}> => {
const graphqlOperation = updateOneOperationFactory({
data: input as object, // TODO default generic does not work
objectMetadataSingularName,
gqlFields,
recordId,
});
const response = await makeGraphqlAPIRequest(graphqlOperation);
if (expectToFail) {
warnIfNoErrorButExpectedToFail({
response,
errorMessage: 'Update one operation should have failed but did not',
});
}
return {
data: {
updateOneResponse:
response.body.data[`update${capitalize(objectMetadataSingularName)}`],
},
errors: response.body.errors,
};
};

View File

@ -0,0 +1,155 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Phone field metadata tests suite It should fail to create primary phone field with conflicting country code and calling code 1`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Provided country code and calling code are conflicting",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with conflicting country code and calling code 2`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Provided country code and calling code are conflicting",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with conflicting phone number calling code 1`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Provided and inferred calling code are conflicting",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with conflicting phone number calling code 2`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Provided and inferred calling code are conflicting",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with conflicting phone number country code 1`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Provided and inferred country code are conflicting",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with conflicting phone number country code 2`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Provided and inferred country code are conflicting",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with invalid calling code 1`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Invalid calling code +999",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with invalid calling code 2`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Invalid calling code +999",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with invalid country code 1`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Invalid country code XX",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with invalid country code 2`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Invalid country code XX",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with invalid phone number format 1`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Provided phone number is invalid not-a-number",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field with invalid phone number format 2`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Provided phone number is invalid not-a-number",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field without country or calling code at all 1`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Provided phone number is invalid 123456789",
},
]
`;
exports[`Phone field metadata tests suite It should fail to create primary phone field without country or calling code at all 2`] = `
[
{
"extensions": {
"code": "BAD_USER_INPUT",
},
"message": "Provided phone number is invalid 123456789",
},
]
`;

View File

@ -0,0 +1,417 @@
import { faker } from '@faker-js/faker';
import { CountryCode } from 'libphonenumber-js';
import { createOneOperation } from 'test/integration/graphql/utils/create-one-operation.util';
import { createOneFieldMetadata } from 'test/integration/metadata/suites/field-metadata/utils/create-one-field-metadata.util';
import { deleteOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/delete-one-object-metadata.util';
import { forceCreateOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/force-create-one-object-metadata.util';
import { EachTestingContext } from 'twenty-shared/testing';
import { FieldMetadataType } from 'twenty-shared/types';
import {
AdditionalPhoneMetadata,
PhonesMetadata,
} from 'src/engine/metadata-modules/field-metadata/composite-types/phones.composite-type';
const FIELD_NAME = 'phonenumber';
type TestCaseInputAndExpected = Partial<
Omit<PhonesMetadata, 'additionalPhones'>
> & {
additionalPhones?: Array<Partial<AdditionalPhoneMetadata>> | null;
};
type CreatePhoneFieldMetadataTestCase = {
input: TestCaseInputAndExpected;
expected?: TestCaseInputAndExpected;
};
const SUCCESSFUL_TEST_CASES: EachTestingContext<CreatePhoneFieldMetadataTestCase>[] =
[
{
title: 'create primary phone field',
context: {
input: {
primaryPhoneNumber: '123456789',
primaryPhoneCallingCode: '+33',
primaryPhoneCountryCode: 'FR',
additionalPhones: [],
},
expected: {
primaryPhoneNumber: '123456789',
primaryPhoneCallingCode: '+33',
primaryPhoneCountryCode: 'FR',
additionalPhones: [],
},
},
},
{
title: 'create primary phone field with number and other information',
context: {
input: {
primaryPhoneNumber: '123456789',
primaryPhoneCallingCode: '+33',
primaryPhoneCountryCode: 'FR',
additionalPhones: [],
},
expected: {
primaryPhoneNumber: '123456789',
primaryPhoneCallingCode: '+33',
primaryPhoneCountryCode: 'FR',
additionalPhones: [],
},
},
},
{
title:
'create primary phone field with full international format and other information',
context: {
input: {
primaryPhoneNumber: '+1123456789',
primaryPhoneCountryCode: 'US',
primaryPhoneCallingCode: '+1',
additionalPhones: null,
},
expected: {
primaryPhoneNumber: '123456789',
primaryPhoneCountryCode: 'US',
primaryPhoneCallingCode: '+1',
additionalPhones: null,
},
},
},
{
title:
'create primary phone field with full international and infer other information from it but not the countryCode as its shared',
context: {
input: { primaryPhoneNumber: '+1123456789' },
expected: {
primaryPhoneNumber: '123456789',
primaryPhoneCountryCode: '' as CountryCode,
primaryPhoneCallingCode: '+1',
additionalPhones: null,
},
},
},
{
title:
'create primary phone field with full international and infer other information from it',
context: {
input: { primaryPhoneNumber: '+33123456789' },
expected: {
primaryPhoneNumber: '123456789',
primaryPhoneCountryCode: 'FR',
primaryPhoneCallingCode: '+33',
additionalPhones: null,
},
},
},
{
title: 'create primary phone field with empty payload',
context: {
input: {},
expected: {
primaryPhoneNumber: '',
primaryPhoneCountryCode: '' as CountryCode,
primaryPhoneCallingCode: '',
additionalPhones: null,
},
},
},
{
title: 'create additional phone field with number and other information',
context: {
input: {
additionalPhones: [
{ callingCode: '+33', countryCode: 'FR', number: '123456789' },
],
},
expected: {
primaryPhoneCallingCode: '',
primaryPhoneCountryCode: '' as CountryCode,
primaryPhoneNumber: '',
additionalPhones: [
{ callingCode: '+33', countryCode: 'FR', number: '123456789' },
],
},
},
},
{
title:
'create additional phone field with full international format and other information',
context: {
input: {
additionalPhones: [
{ callingCode: '+1', countryCode: 'US', number: '+1123456789' },
],
},
expected: {
primaryPhoneCallingCode: '',
primaryPhoneCountryCode: '' as CountryCode,
primaryPhoneNumber: '',
additionalPhones: [
{ callingCode: '+1', countryCode: 'US', number: '123456789' },
],
},
},
},
{
title:
'create additional phone field with full international and infer other information from it but not the countryCode as its shared',
context: {
input: { additionalPhones: [{ number: '+1123456789' }] },
expected: {
primaryPhoneCallingCode: '',
primaryPhoneCountryCode: '' as CountryCode,
primaryPhoneNumber: '',
additionalPhones: [
{
callingCode: '+1',
number: '123456789',
},
],
},
},
},
{
title:
'create additional phone field with full international and infer other information from it',
context: {
input: { additionalPhones: [{ number: '+33123456789' }] },
expected: {
primaryPhoneCallingCode: '',
primaryPhoneCountryCode: '' as CountryCode,
primaryPhoneNumber: '',
additionalPhones: [
{ callingCode: '+33', countryCode: 'FR', number: '123456789' },
],
},
},
},
{
title: 'create additional phone field with empty payload',
context: {
input: { additionalPhones: [{}] },
expected: {
primaryPhoneCallingCode: '',
primaryPhoneCountryCode: '' as CountryCode,
primaryPhoneNumber: '',
additionalPhones: [{}],
},
},
},
];
const FAILING_TEST_INPUTS: { input: Partial<PhonesMetadata>; label: string }[] =
[
{
label: 'phone field without country or calling code at all',
input: {
primaryPhoneNumber: '123456789',
additionalPhones: [],
},
},
{
label: 'phone field with invalid country code',
input: {
primaryPhoneNumber: '123456789',
primaryPhoneCallingCode: '+33',
primaryPhoneCountryCode: 'XX' as CountryCode,
additionalPhones: [],
},
},
{
label: 'phone field with invalid calling code',
input: {
primaryPhoneNumber: '123456789',
primaryPhoneCallingCode: '+999',
primaryPhoneCountryCode: 'FR',
additionalPhones: [],
},
},
{
label: 'phone field with conflicting country code and calling code',
input: {
primaryPhoneNumber: '123456789',
primaryPhoneCallingCode: '+33',
primaryPhoneCountryCode: 'US',
additionalPhones: [],
},
},
{
label: 'phone field with invalid phone number format',
input: {
primaryPhoneNumber: 'not-a-number',
additionalPhones: [],
},
},
{
label: 'phone field with conflicting phone number country code',
input: {
primaryPhoneNumber: '+33123456789',
primaryPhoneCountryCode: 'US',
additionalPhones: [],
},
},
{
label: 'phone field with conflicting phone number calling code',
input: {
primaryPhoneNumber: '+33123456789',
primaryPhoneCallingCode: '+1',
additionalPhones: [],
},
},
];
const primaryFailingTests = FAILING_TEST_INPUTS.map<
EachTestingContext<CreatePhoneFieldMetadataTestCase>
>(({ input, label }) => ({
title: `create primary ${label}`,
context: {
input: {
...input,
additionalPhones: [],
},
},
}));
const additionalPhonesNumberFailingTests = FAILING_TEST_INPUTS.map<
EachTestingContext<CreatePhoneFieldMetadataTestCase>
>(
({
input: {
primaryPhoneCallingCode,
primaryPhoneCountryCode,
primaryPhoneNumber,
},
label,
}) => ({
title: `create primary ${label}`,
context: {
input: {
additionalPhones: [
{
callingCode: primaryPhoneCallingCode,
countryCode: primaryPhoneCountryCode,
number: primaryPhoneNumber,
},
],
},
},
}),
);
const FAILING_TEST_CASES: EachTestingContext<CreatePhoneFieldMetadataTestCase>[] =
[...primaryFailingTests, ...additionalPhonesNumberFailingTests];
describe('Phone field metadata tests suite', () => {
let createdObjectMetadataId: string;
beforeEach(async () => {
const { data } = await forceCreateOneObjectMetadata({
input: {
nameSingular: 'myTestObject',
namePlural: 'myTestObjects',
labelSingular: 'My Test Object',
labelPlural: 'My Test Objects',
icon: 'Icon123',
isLabelSyncedWithName: false,
},
});
createdObjectMetadataId = data.createOneObject.id;
await createOneFieldMetadata({
input: {
name: FIELD_NAME,
label: 'Phone number',
type: FieldMetadataType.PHONES,
objectMetadataId: createdObjectMetadataId,
isLabelSyncedWithName: false,
},
gqlFields: `
id
name
label
isLabelSyncedWithName
`,
});
});
afterEach(async () => {
await deleteOneObjectMetadata({
input: { idToDelete: createdObjectMetadataId },
});
});
test.each(SUCCESSFUL_TEST_CASES)(
'It should succeed $title',
async ({ context: { input, expected } }) => {
const {
data: { createOneResponse },
errors,
} = await createOneOperation<{
id: string;
[FIELD_NAME]: any;
}>({
objectMetadataSingularName: 'myTestObject',
input: {
id: faker.string.uuid(),
[FIELD_NAME]: input,
},
gqlFields: `
id
${FIELD_NAME} {
primaryPhoneNumber
primaryPhoneCountryCode
primaryPhoneCallingCode
additionalPhones
__typename
}
`,
});
expect(errors).toBeUndefined();
const { id: _id, ...rest } = createOneResponse;
expect(rest[FIELD_NAME]).toStrictEqual({
...expected,
__typename: 'Phones',
});
},
);
test.each(FAILING_TEST_CASES)(
'It should fail to $title',
async ({ context: { input } }) => {
const {
data: { createOneResponse },
errors,
} = await createOneOperation<{
id: string;
[FIELD_NAME]: any;
}>({
objectMetadataSingularName: 'myTestObject',
input: {
id: faker.string.uuid(),
[FIELD_NAME]: input,
},
gqlFields: `
id
${FIELD_NAME} {
primaryPhoneNumber
primaryPhoneCountryCode
primaryPhoneCallingCode
additionalPhones
__typename
}
`,
});
expect(createOneResponse).toBeNull();
expect(errors).toBeDefined();
expect(errors).toMatchSnapshot();
},
);
});