Remove Tenant columns anonymisation (#2404)
* Remove Tenant columns anonymisation * add tests * use _ instead of custom_ * put _ on all custom fields
This commit is contained in:
@ -83,7 +83,11 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadata> {
|
|||||||
|
|
||||||
const createdFieldMetadata = await super.createOne({
|
const createdFieldMetadata = await super.createOne({
|
||||||
...record,
|
...record,
|
||||||
targetColumnMap: generateTargetColumnMap(record.type),
|
targetColumnMap: generateTargetColumnMap(
|
||||||
|
record.type,
|
||||||
|
record.isCustom,
|
||||||
|
record.name,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.tenantMigrationService.createCustomMigration(
|
await this.tenantMigrationService.createCustomMigration(
|
||||||
|
|||||||
@ -0,0 +1,57 @@
|
|||||||
|
import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';
|
||||||
|
|
||||||
|
import {
|
||||||
|
generateTargetColumnMap,
|
||||||
|
convertFieldMetadataToColumnActions,
|
||||||
|
} from './field-metadata.util';
|
||||||
|
|
||||||
|
describe('generateTargetColumnMap', () => {
|
||||||
|
it('should generate a target column map for a given type', () => {
|
||||||
|
const textMap = generateTargetColumnMap(
|
||||||
|
FieldMetadataType.TEXT,
|
||||||
|
false,
|
||||||
|
'name',
|
||||||
|
);
|
||||||
|
expect(textMap).toEqual({ value: 'name' });
|
||||||
|
|
||||||
|
const urlMap = generateTargetColumnMap(
|
||||||
|
FieldMetadataType.URL,
|
||||||
|
false,
|
||||||
|
'website',
|
||||||
|
);
|
||||||
|
expect(urlMap).toEqual({ text: 'website_text', link: 'website_link' });
|
||||||
|
|
||||||
|
const moneyMap = generateTargetColumnMap(
|
||||||
|
FieldMetadataType.MONEY,
|
||||||
|
true,
|
||||||
|
'price',
|
||||||
|
);
|
||||||
|
expect(moneyMap).toEqual({
|
||||||
|
amount: '_price_amount',
|
||||||
|
currency: '_price_currency',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error for an unknown type', () => {
|
||||||
|
expect(() =>
|
||||||
|
generateTargetColumnMap('invalid' as FieldMetadataType, false, 'name'),
|
||||||
|
).toThrowError('Unknown type invalid');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('convertFieldMetadataToColumnActions', () => {
|
||||||
|
it('should convert field metadata to column actions', () => {
|
||||||
|
const fieldMetadata = {
|
||||||
|
type: FieldMetadataType.TEXT,
|
||||||
|
targetColumnMap: { value: 'name' },
|
||||||
|
} as any;
|
||||||
|
const columnActions = convertFieldMetadataToColumnActions(fieldMetadata);
|
||||||
|
expect(columnActions).toEqual([
|
||||||
|
{
|
||||||
|
action: 'CREATE',
|
||||||
|
columnName: 'name',
|
||||||
|
columnType: 'text',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,8 +1,5 @@
|
|||||||
import { v4 } from 'uuid';
|
|
||||||
|
|
||||||
import { FieldMetadataTargetColumnMap } from 'src/metadata/field-metadata/interfaces/field-metadata-target-column-map.interface';
|
import { FieldMetadataTargetColumnMap } from 'src/metadata/field-metadata/interfaces/field-metadata-target-column-map.interface';
|
||||||
|
|
||||||
import { uuidToBase36 } from 'src/metadata/data-source/data-source.util';
|
|
||||||
import {
|
import {
|
||||||
FieldMetadata,
|
FieldMetadata,
|
||||||
FieldMetadataType,
|
FieldMetadataType,
|
||||||
@ -12,16 +9,6 @@ import {
|
|||||||
TenantMigrationColumnActionType,
|
TenantMigrationColumnActionType,
|
||||||
} from 'src/metadata/tenant-migration/tenant-migration.entity';
|
} from 'src/metadata/tenant-migration/tenant-migration.entity';
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a column name from a field name removing unsupported characters.
|
|
||||||
*
|
|
||||||
* @param name string
|
|
||||||
* @returns string
|
|
||||||
*/
|
|
||||||
export function generateColumnName(name: string): string {
|
|
||||||
return name.toLowerCase().replace(/ /g, '_');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a target column map for a given type, this is used to map the field to the correct column(s) in the database.
|
* Generate a target column map for a given type, this is used to map the field to the correct column(s) in the database.
|
||||||
* This is used to support fields that map to multiple columns in the database.
|
* This is used to support fields that map to multiple columns in the database.
|
||||||
@ -31,7 +18,11 @@ export function generateColumnName(name: string): string {
|
|||||||
*/
|
*/
|
||||||
export function generateTargetColumnMap(
|
export function generateTargetColumnMap(
|
||||||
type: FieldMetadataType,
|
type: FieldMetadataType,
|
||||||
|
isCustomField: boolean,
|
||||||
|
fieldName: string,
|
||||||
): FieldMetadataTargetColumnMap {
|
): FieldMetadataTargetColumnMap {
|
||||||
|
const columnName = isCustomField ? `_${fieldName}` : fieldName;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case FieldMetadataType.TEXT:
|
case FieldMetadataType.TEXT:
|
||||||
case FieldMetadataType.PHONE:
|
case FieldMetadataType.PHONE:
|
||||||
@ -40,17 +31,17 @@ export function generateTargetColumnMap(
|
|||||||
case FieldMetadataType.BOOLEAN:
|
case FieldMetadataType.BOOLEAN:
|
||||||
case FieldMetadataType.DATE:
|
case FieldMetadataType.DATE:
|
||||||
return {
|
return {
|
||||||
value: `column_${uuidToBase36(v4())}`,
|
value: columnName,
|
||||||
};
|
};
|
||||||
case FieldMetadataType.URL:
|
case FieldMetadataType.URL:
|
||||||
return {
|
return {
|
||||||
text: `column_${uuidToBase36(v4())}`,
|
text: `${columnName}_text`,
|
||||||
link: `column_${uuidToBase36(v4())}`,
|
link: `${columnName}_link`,
|
||||||
};
|
};
|
||||||
case FieldMetadataType.MONEY:
|
case FieldMetadataType.MONEY:
|
||||||
return {
|
return {
|
||||||
amount: `column_${uuidToBase36(v4())}`,
|
amount: `${columnName}_amount`,
|
||||||
currency: `column_${uuidToBase36(v4())}`,
|
currency: `${columnName}_currency`,
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unknown type ${type}`);
|
throw new Error(`Unknown type ${type}`);
|
||||||
|
|||||||
@ -30,7 +30,7 @@ export class BeforeCreateOneObject<T extends ObjectMetadata>
|
|||||||
);
|
);
|
||||||
|
|
||||||
instance.input.dataSourceId = lastDataSourceMetadata.id;
|
instance.input.dataSourceId = lastDataSourceMetadata.id;
|
||||||
instance.input.targetTableName = instance.input.namePlural;
|
instance.input.targetTableName = `_${instance.input.namePlural}`;
|
||||||
instance.input.workspaceId = workspaceId;
|
instance.input.workspaceId = workspaceId;
|
||||||
instance.input.isActive = true;
|
instance.input.isActive = true;
|
||||||
instance.input.isCustom = true;
|
instance.input.isCustom = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user