Add fullName field metadata type (#2563)

* Add fullName field metadata type

* update seeds
This commit is contained in:
Weiko
2023-11-17 16:52:51 +01:00
committed by GitHub
parent f58e4263bc
commit 4870b0ac30
13 changed files with 147 additions and 92 deletions

View File

@ -11,8 +11,7 @@ export enum SeedPersonFieldMetadataIds {
CreatedAt = '20202020-bec0-4cf0-bf1c-8b2ed21f027a',
UpdatedAt = '20202020-2bf4-42b8-8718-a3e852bfa6a6',
FirstName = '20202020-9b56-4888-bfe3-f6f59aa999e3',
LastName = '20202020-b784-458a-869d-6c53fa85483d',
Name = '20202020-9b56-4888-bfe3-f6f59aa999e3',
Email = '20202020-8a96-4e4b-86fd-ea126530e0c1',
LinkedinLink = '20202020-dcf6-445a-b543-37e55de43c25',
@ -113,40 +112,23 @@ export const seedPersonFieldMetadata = async (
},
// Main Identifier
{
id: SeedPersonFieldMetadataIds.FirstName,
id: SeedPersonFieldMetadataIds.Name,
objectMetadataId: SeedObjectMetadataIds.Person,
isCustom: false,
workspaceId: SeedWorkspaceId,
isActive: true,
type: FieldMetadataType.TEXT,
name: 'firstName',
label: 'First name',
type: FieldMetadataType.FULL_NAME,
name: 'name',
label: 'Name',
targetColumnMap: {
value: 'firstName',
firstName: 'nameFirstName',
lastName: 'nameLastName',
},
description: 'Contacts first name',
description: 'Contacts name',
icon: 'IconUser',
isNullable: false,
isSystem: false,
defaultValue: { value: '' },
},
{
id: SeedPersonFieldMetadataIds.LastName,
objectMetadataId: SeedObjectMetadataIds.Person,
isCustom: false,
workspaceId: SeedWorkspaceId,
isActive: true,
type: FieldMetadataType.TEXT,
name: 'lastName',
label: 'Last name',
targetColumnMap: {
value: 'lastName',
},
description: 'Contacts last name',
icon: 'IconUser',
isNullable: false,
isSystem: false,
defaultValue: { value: '' },
defaultValue: { firstName: '', lastName: '' },
},
// Scalar Fields

View File

@ -11,8 +11,8 @@ export const seedPeople = async (
.insert()
.into(`${schemaName}.${tableName}`, [
'id',
'firstName',
'lastName',
'nameFirstName',
'nameLastName',
'phone',
'city',
'companyId',
@ -22,8 +22,8 @@ export const seedPeople = async (
.values([
{
id: '86083141-1c0e-494c-a1b6-85b1c6fefaa5',
firstName: 'Christoph',
lastName: 'Callisto',
nameFirstName: 'Christoph',
nameLastName: 'Callisto',
phone: '+33789012345',
city: 'Seattle',
companyId: 'fe256b39-3ec3-4fe3-8997-b76aa0bfa408',
@ -31,8 +31,8 @@ export const seedPeople = async (
},
{
id: '0aa00beb-ac73-4797-824e-87a1f5aea9e0',
firstName: 'Sylvie',
lastName: 'Palmer',
nameFirstName: 'Sylvie',
nameLastName: 'Palmer',
phone: '+33780123456',
city: 'Los Angeles',
companyId: 'fe256b39-3ec3-4fe3-8997-b76aa0bfa408',
@ -40,8 +40,8 @@ export const seedPeople = async (
},
{
id: '93c72d2e-f517-42fd-80ae-14173b3b70ae',
firstName: 'Christopher',
lastName: 'Gonzalez',
nameFirstName: 'Christopher',
nameLastName: 'Gonzalez',
phone: '+33789012345',
city: 'Seattle',
companyId: '118995f3-5d81-46d6-bf83-f7fd33ea6102',
@ -49,8 +49,8 @@ export const seedPeople = async (
},
{
id: 'eeeacacf-eee1-4690-ad2c-8619e5b56a2e',
firstName: 'Ashley',
lastName: 'Parker',
nameFirstName: 'Ashley',
nameLastName: 'Parker',
phone: '+33780123456',
city: 'Los Angeles',
companyId: '118995f3-5d81-46d6-bf83-f7fd33ea6102',
@ -58,8 +58,8 @@ export const seedPeople = async (
},
{
id: '9b324a88-6784-4449-afdf-dc62cb8702f2',
firstName: 'Nicholas',
lastName: 'Wright',
nameFirstName: 'Nicholas',
nameLastName: 'Wright',
phone: '+33781234567',
city: 'Seattle',
companyId: '460b6fb1-ed89-413a-b31a-962986e67bb4',
@ -67,8 +67,8 @@ export const seedPeople = async (
},
{
id: '1d151852-490f-4466-8391-733cfd66a0c8',
firstName: 'Isabella',
lastName: 'Scott',
nameFirstName: 'Isabella',
nameLastName: 'Scott',
phone: '+33782345678',
city: 'New York',
companyId: '460b6fb1-ed89-413a-b31a-962986e67bb4',
@ -76,8 +76,8 @@ export const seedPeople = async (
},
{
id: '98406e26-80f1-4dff-b570-a74942528de3',
firstName: 'Matthew',
lastName: 'Green',
nameFirstName: 'Matthew',
nameLastName: 'Green',
phone: '+33783456789',
city: 'Seattle',
companyId: '460b6fb1-ed89-413a-b31a-962986e67bb4',
@ -85,8 +85,8 @@ export const seedPeople = async (
},
{
id: 'a2e78a5f-338b-46df-8811-fa08c7d19d35',
firstName: 'Elizabeth',
lastName: 'Baker',
nameFirstName: 'Elizabeth',
nameLastName: 'Baker',
phone: '+33784567890',
city: 'New York',
companyId: '0d940997-c21e-4ec2-873b-de4264d89025',
@ -94,8 +94,8 @@ export const seedPeople = async (
},
{
id: 'ca1f5bf3-64ad-4b0e-bbfd-e9fd795b7016',
firstName: 'Christopher',
lastName: 'Nelson',
nameFirstName: 'Christopher',
nameLastName: 'Nelson',
phone: '+33785678901',
city: 'San Francisco',
companyId: '0d940997-c21e-4ec2-873b-de4264d89025',
@ -103,8 +103,8 @@ export const seedPeople = async (
},
{
id: '56955422-5d54-41b7-ba36-f0d20e1417ae',
firstName: 'Avery',
lastName: 'Carter',
nameFirstName: 'Avery',
nameLastName: 'Carter',
phone: '+33786789012',
city: 'New York',
companyId: '0d940997-c21e-4ec2-873b-de4264d89025',
@ -112,8 +112,8 @@ export const seedPeople = async (
},
{
id: '755035db-623d-41fe-92e7-dd45b7c568e1',
firstName: 'Ethan',
lastName: 'Mitchell',
nameFirstName: 'Ethan',
nameLastName: 'Mitchell',
phone: '+33787890123',
city: 'Los Angeles',
companyId: '0d940997-c21e-4ec2-873b-de4264d89025',
@ -121,8 +121,8 @@ export const seedPeople = async (
},
{
id: '240da2ec-2d40-4e49-8df4-9c6a049190ef',
firstName: 'Madison',
lastName: 'Perez',
nameFirstName: 'Madison',
nameLastName: 'Perez',
phone: '+33788901234',
city: 'Seattle',
companyId: '0d940997-c21e-4ec2-873b-de4264d89025',
@ -130,8 +130,8 @@ export const seedPeople = async (
},
{
id: '240da2ec-2d40-4e49-8df4-9c6a049190df',
firstName: 'Bertrand',
lastName: 'Voulzy',
nameFirstName: 'Bertrand',
nameLastName: 'Voulzy',
phone: '+33788901234',
city: 'Seattle',
companyId: '0d940997-c21e-4ec2-873b-de4264d89025',
@ -139,8 +139,8 @@ export const seedPeople = async (
},
{
id: '240da2ec-2d40-4e49-8df4-9c6a049191de',
firstName: 'Louis',
lastName: 'Duss',
nameFirstName: 'Louis',
nameLastName: 'Duss',
phone: '+33788901234',
city: 'Seattle',
companyId: 'a7bc68d5-f79e-40dd-bd06-c36e6abb4678',
@ -148,8 +148,8 @@ export const seedPeople = async (
},
{
id: '240da2ec-2d40-4e49-8df4-9c6a049191df',
firstName: 'Lorie',
lastName: 'Vladim',
nameFirstName: 'Lorie',
nameLastName: 'Vladim',
phone: '+33788901235',
city: 'Seattle',
companyId: 'a674fa6c-1455-4c57-afaf-dd5dc086361d',

View File

@ -30,6 +30,7 @@ export enum FieldMetadataType {
LINK = 'LINK',
CURRENCY = 'CURRENCY',
RELATION = 'RELATION',
FULL_NAME = 'FULL_NAME',
}
@Entity('fieldMetadata')

View File

@ -33,11 +33,17 @@ interface FieldMetadataDefaultValueCurrency {
currencyCode: string;
}
interface FieldMetadataDefaultValueFullName {
firstName: string;
lastName: string;
}
type AllFieldMetadataDefaultValueTypes =
| FieldMetadataScalarDefaultValue
| FieldMetadataDynamicDefaultValue
| FieldMetadataDefaultValueLink
| FieldMetadataDefaultValueCurrency;
| FieldMetadataDefaultValueCurrency
| FieldMetadataDefaultValueFullName;
type FieldMetadataDefaultValueMapping = {
[FieldMetadataType.UUID]: FieldMetadataDefaultValueString;
@ -51,6 +57,7 @@ type FieldMetadataDefaultValueMapping = {
[FieldMetadataType.ENUM]: FieldMetadataDefaultValueString;
[FieldMetadataType.LINK]: FieldMetadataDefaultValueLink;
[FieldMetadataType.CURRENCY]: FieldMetadataDefaultValueCurrency;
[FieldMetadataType.FULL_NAME]: FieldMetadataDefaultValueFullName;
};
type DefaultValueByFieldMetadata<T extends FieldMetadataType | 'default'> = [

View File

@ -14,6 +14,11 @@ export interface FieldMetadataTargetColumnMapCurrency {
currencyCode: string;
}
export interface FieldMetadataTargetColumnMapFullName {
firstName: string;
lastName: string;
}
type AllFieldMetadataTypes = {
[key: string]: string;
};
@ -21,6 +26,7 @@ type AllFieldMetadataTypes = {
type FieldMetadataTypeMapping = {
[FieldMetadataType.LINK]: FieldMetadataTargetColumnMapLink;
[FieldMetadataType.CURRENCY]: FieldMetadataTargetColumnMapCurrency;
[FieldMetadataType.FULL_NAME]: FieldMetadataTargetColumnMapFullName;
};
type TypeByFieldMetadata<T extends FieldMetadataType | 'default'> =

View File

@ -123,6 +123,25 @@ export function convertFieldMetadataToColumnActions(
},
];
}
case FieldMetadataType.FULL_NAME: {
const defaultValue =
fieldMetadata.defaultValue as FieldMetadataDefaultValue<FieldMetadataType.FULL_NAME>;
return [
{
action: WorkspaceMigrationColumnActionType.CREATE,
columnName: fieldMetadata.targetColumnMap.firstName,
columnType: 'varchar',
defaultValue: serializeDefaultValue(defaultValue?.firstName),
},
{
action: WorkspaceMigrationColumnActionType.CREATE,
columnName: fieldMetadata.targetColumnMap.lastName,
columnType: 'varchar',
defaultValue: serializeDefaultValue(defaultValue?.lastName),
},
];
}
default:
throw new Error(`Unknown type ${fieldMetadata.type}`);
}

View File

@ -39,6 +39,12 @@ export function generateTargetColumnMap(
amountMicros: `${columnName}AmountMicros`,
currencyCode: `${columnName}CurrencyCode`,
};
case FieldMetadataType.FULL_NAME:
return {
firstName: `${columnName}FirstName`,
lastName: `${columnName}LastName`,
};
default:
throw new BadRequestException(`Unknown type ${type}`);
}

View File

@ -72,6 +72,15 @@ export const validateDefaultValueBasedOnType = (
typeof defaultValue.currencyCode === 'string'
);
case FieldMetadataType.FULL_NAME:
return (
typeof defaultValue === 'object' &&
'firstName' in defaultValue &&
typeof defaultValue.firstName === 'string' &&
'lastName' in defaultValue &&
typeof defaultValue.lastName === 'string'
);
default:
return false;
}

View File

@ -13,12 +13,12 @@ export const addPersonTable: WorkspaceMigrationTableAction[] = [
action: 'alter',
columns: [
{
columnName: 'firstName',
columnName: 'nameFirstName',
columnType: 'varchar',
action: WorkspaceMigrationColumnActionType.CREATE,
},
{
columnName: 'lastName',
columnName: 'nameLastName',
columnType: 'varchar',
action: WorkspaceMigrationColumnActionType.CREATE,
},

File diff suppressed because one or more lines are too long

View File

@ -14,26 +14,14 @@ const personMetadata = {
{
isCustom: false,
isActive: true,
type: FieldMetadataType.TEXT,
name: 'firstName',
label: 'First name',
type: FieldMetadataType.FULL_NAME,
name: 'name',
label: 'Name',
targetColumnMap: {
value: 'firstName',
firstName: 'nameFirstName',
lastName: 'nameLastName',
},
description: 'Contacts first name',
icon: 'IconUser',
isNullable: false,
},
{
isCustom: false,
isActive: true,
type: FieldMetadataType.TEXT,
name: 'lastName',
label: 'Last name',
targetColumnMap: {
value: 'lastName',
},
description: 'Contacts last name',
description: 'Contacts name',
icon: 'IconUser',
isNullable: false,
},

View File

@ -0,0 +1,35 @@
import { ObjectMetadataInterface } from 'src/workspace/workspace-schema-builder/interfaces/object-metadata.interface';
import { FieldMetadataInterface } from 'src/workspace/workspace-schema-builder/interfaces/field-metadata.interface';
import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';
export const fullNameObjectDefinition = {
id: FieldMetadataType.FULL_NAME.toString(),
nameSingular: 'fullName',
namePlural: 'fullName',
labelSingular: 'FullName',
labelPlural: 'FullName',
targetTableName: '',
fields: [
{
id: 'firstName',
type: FieldMetadataType.TEXT,
objectMetadataId: FieldMetadataType.FULL_NAME.toString(),
name: 'firstName',
label: 'First Name',
targetColumnMap: { value: 'firstName' },
isNullable: true,
} satisfies FieldMetadataInterface,
{
id: 'lastName',
type: FieldMetadataType.TEXT,
objectMetadataId: FieldMetadataType.FULL_NAME.toString(),
name: 'lastName',
label: 'Last Name',
targetColumnMap: { value: 'lastName' },
isNullable: true,
} satisfies FieldMetadataInterface,
],
fromRelations: [],
toRelations: [],
} satisfies ObjectMetadataInterface;

View File

@ -2,6 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { FieldMetadataEntity } from 'src/metadata/field-metadata/field-metadata.entity';
import { customTableDefaultColumns } from 'src/workspace/workspace-migration-runner/utils/custom-table-default-column.util';
import { fullNameObjectDefinition } from 'src/workspace/workspace-schema-builder/object-definitions/full-name.object-definition';
import { TypeDefinitionsStorage } from './storages/type-definitions.storage';
import {
@ -63,6 +64,7 @@ export class TypeDefinitionsGenerator {
const staticObjectMetadataCollection = [
currencyObjectDefinition,
linkObjectDefinition,
fullNameObjectDefinition,
];
this.logger.log(