fix: use proper variable name (#2938)

This commit is contained in:
Jérémy M 2023-12-12 14:57:42 +01:00 committed by GitHub
parent 032894e448
commit 44f1fe54e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 80 deletions

View File

@ -43,14 +43,14 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
}
override async createOne(
record: CreateFieldInput,
fieldMetadataInput: CreateFieldInput,
): Promise<FieldMetadataEntity> {
const objectMetadata =
await this.objectMetadataService.findOneWithinWorkspace(
record.workspaceId,
fieldMetadataInput.workspaceId,
{
where: {
id: record.objectMetadataId,
id: fieldMetadataInput.objectMetadataId,
},
},
);
@ -61,9 +61,9 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
const fieldAlreadyExists = await this.fieldMetadataRepository.findOne({
where: {
name: record.name,
objectMetadataId: record.objectMetadataId,
workspaceId: record.workspaceId,
name: fieldMetadataInput.name,
objectMetadataId: fieldMetadataInput.objectMetadataId,
workspaceId: fieldMetadataInput.workspaceId,
},
});
@ -72,10 +72,14 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
}
const createdFieldMetadata = await super.createOne({
...record,
targetColumnMap: generateTargetColumnMap(record.type, true, record.name),
options: record.options
? record.options.map((option) => ({
...fieldMetadataInput,
targetColumnMap: generateTargetColumnMap(
fieldMetadataInput.type,
true,
fieldMetadataInput.name,
),
options: fieldMetadataInput.options
? fieldMetadataInput.options.map((option) => ({
...option,
id: uuidV4(),
}))
@ -85,7 +89,7 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
});
await this.workspaceMigrationService.createCustomMigration(
record.workspaceId,
fieldMetadataInput.workspaceId,
[
{
name: objectMetadata.targetTableName,
@ -99,13 +103,13 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
);
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
record.workspaceId,
fieldMetadataInput.workspaceId,
);
// TODO: Move viewField creation to a cdc scheduler
const dataSourceMetadata =
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceIdOrFail(
record.workspaceId,
fieldMetadataInput.workspaceId,
);
const workspaceDataSource = await this.typeORMService.connectToDataSource(
@ -146,12 +150,12 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
override async updateOne(
id: string,
record: UpdateFieldInput,
fieldMetadataInput: UpdateFieldInput,
): Promise<FieldMetadataEntity> {
const existingFieldMetadata = await this.fieldMetadataRepository.findOne({
where: {
id,
workspaceId: record.workspaceId,
workspaceId: fieldMetadataInput.workspaceId,
},
});
@ -161,16 +165,16 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
if (existingFieldMetadata.isCustom === false) {
// We can only update the isActive field for standard fields
record = {
id: record.id,
isActive: record.isActive,
workspaceId: record.workspaceId,
fieldMetadataInput = {
id: fieldMetadataInput.id,
isActive: fieldMetadataInput.isActive,
workspaceId: fieldMetadataInput.workspaceId,
};
}
const objectMetadata =
await this.objectMetadataService.findOneWithinWorkspace(
record.workspaceId,
fieldMetadataInput.workspaceId,
{
where: {
id: existingFieldMetadata?.objectMetadataId,
@ -183,17 +187,17 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
}
// Check if the id of the options has been provided
if (record.options) {
for (const option of record.options) {
if (fieldMetadataInput.options) {
for (const option of fieldMetadataInput.options) {
if (!option.id) {
throw new BadRequestException('Option id is required');
}
}
}
const updatedFieldMetadata = await super.updateOne(id, record);
const updatedFieldMetadata = await super.updateOne(id, fieldMetadataInput);
if (record.options || record.defaultValue) {
if (fieldMetadataInput.options || fieldMetadataInput.defaultValue) {
await this.workspaceMigrationService.createCustomMigration(
existingFieldMetadata.workspaceId,
[

View File

@ -48,23 +48,26 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
}
override async createOne(
record: CreateObjectInput,
objectMetadataInput: CreateObjectInput,
): Promise<ObjectMetadataEntity> {
const lastDataSourceMetadata =
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceIdOrFail(
record.workspaceId,
objectMetadataInput.workspaceId,
);
if (record.nameSingular.toLowerCase() === record.namePlural.toLowerCase()) {
if (
objectMetadataInput.nameSingular.toLowerCase() ===
objectMetadataInput.namePlural.toLowerCase()
) {
throw new Error(
'The singular and plural name cannot be the same for an object',
);
}
const createdObjectMetadata = await super.createOne({
...record,
...objectMetadataInput,
dataSourceId: lastDataSourceMetadata.id,
targetTableName: createCustomColumnName(record.nameSingular),
targetTableName: createCustomColumnName(objectMetadataInput.nameSingular),
isActive: true,
isCustom: true,
isSystem: false,
@ -86,7 +89,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
isActive: true,
isCustom: false,
isSystem: true,
workspaceId: record.workspaceId,
workspaceId: objectMetadataInput.workspaceId,
defaultValue: { type: 'uuid' },
},
{
@ -101,7 +104,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
isNullable: true,
isActive: true,
isCustom: false,
workspaceId: record.workspaceId,
workspaceId: objectMetadataInput.workspaceId,
defaultValue: { value: 'Untitled' },
},
{
@ -116,7 +119,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
isNullable: true,
isActive: true,
isCustom: false,
workspaceId: record.workspaceId,
workspaceId: objectMetadataInput.workspaceId,
defaultValue: { type: 'now' },
},
{
@ -132,7 +135,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
isActive: true,
isCustom: false,
isSystem: true,
workspaceId: record.workspaceId,
workspaceId: objectMetadataInput.workspaceId,
defaultValue: { type: 'now' },
},
],
@ -141,7 +144,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
const activityTargetObjectMetadata =
await this.objectMetadataRepository.findOneByOrFail({
nameSingular: 'activityTarget',
workspaceId: record.workspaceId,
workspaceId: objectMetadataInput.workspaceId,
});
const activityTargetRelationFieldMetadata =
@ -149,44 +152,44 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
// FROM
{
objectMetadataId: createdObjectMetadata.id,
workspaceId: record.workspaceId,
workspaceId: objectMetadataInput.workspaceId,
isCustom: true,
isActive: true,
type: FieldMetadataType.RELATION,
name: 'activityTargets',
label: 'Activities',
targetColumnMap: {},
description: `Activities tied to the ${record.labelSingular}`,
description: `Activities tied to the ${objectMetadataInput.labelSingular}`,
icon: 'IconCheckbox',
isNullable: true,
},
// TO
{
objectMetadataId: activityTargetObjectMetadata.id,
workspaceId: record.workspaceId,
workspaceId: objectMetadataInput.workspaceId,
isCustom: true,
isActive: true,
type: FieldMetadataType.RELATION,
name: record.nameSingular,
label: record.labelSingular,
name: objectMetadataInput.nameSingular,
label: objectMetadataInput.labelSingular,
targetColumnMap: {
value: `${createdObjectMetadata.targetTableName}Id`,
},
description: `ActivityTarget ${record.labelSingular}`,
description: `ActivityTarget ${objectMetadataInput.labelSingular}`,
icon: 'IconBuildingSkyscraper',
isNullable: true,
},
// Foreign key
{
objectMetadataId: activityTargetObjectMetadata.id,
workspaceId: record.workspaceId,
workspaceId: objectMetadataInput.workspaceId,
isCustom: true,
isActive: true,
type: FieldMetadataType.UUID,
name: `${createdObjectMetadata.targetTableName}Id`,
label: `${record.labelSingular} ID (foreign key)`,
label: `${objectMetadataInput.labelSingular} ID (foreign key)`,
targetColumnMap: {},
description: `ActivityTarget ${record.labelSingular} id foreign key`,
description: `ActivityTarget ${objectMetadataInput.labelSingular} id foreign key`,
icon: undefined,
isNullable: true,
isSystem: true,
@ -208,7 +211,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
await this.relationMetadataRepository.save([
{
workspaceId: record.workspaceId,
workspaceId: objectMetadataInput.workspaceId,
relationType: RelationMetadataType.ONE_TO_MANY,
fromObjectMetadataId: createdObjectMetadata.id,
toObjectMetadataId: activityTargetObjectMetadata.id,

View File

@ -64,9 +64,11 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
}
override async createOne(
record: CreateRelationInput,
relationMetadataInput: CreateRelationInput,
): Promise<RelationMetadataEntity> {
if (record.relationType === RelationMetadataType.MANY_TO_MANY) {
if (
relationMetadataInput.relationType === RelationMetadataType.MANY_TO_MANY
) {
throw new BadRequestException(
'Many to many relations are not supported yet',
);
@ -87,10 +89,13 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
const objectMetadataEntries =
await this.objectMetadataService.findManyWithinWorkspace(
record.workspaceId,
relationMetadataInput.workspaceId,
{
where: {
id: In([record.fromObjectMetadataId, record.toObjectMetadataId]),
id: In([
relationMetadataInput.fromObjectMetadataId,
relationMetadataInput.toObjectMetadataId,
]),
},
},
);
@ -102,16 +107,18 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
}, {} as { [key: string]: ObjectMetadataEntity });
if (
objectMetadataMap[record.fromObjectMetadataId] === undefined ||
objectMetadataMap[record.toObjectMetadataId] === undefined
objectMetadataMap[relationMetadataInput.fromObjectMetadataId] ===
undefined ||
objectMetadataMap[relationMetadataInput.toObjectMetadataId] === undefined
) {
throw new NotFoundException(
'Can\t find an existing object matching fromObjectMetadataId or toObjectMetadataId',
);
}
const baseColumnName = `${camelCase(record.toName)}Id`;
const isToCustom = objectMetadataMap[record.toObjectMetadataId].isCustom;
const baseColumnName = `${camelCase(relationMetadataInput.toName)}Id`;
const isToCustom =
objectMetadataMap[relationMetadataInput.toObjectMetadataId].isCustom;
const foreignKeyColumnName = isToCustom
? createCustomColumnName(baseColumnName)
: baseColumnName;
@ -119,40 +126,40 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
const createdFields = await this.fieldMetadataService.createMany([
// FROM
{
name: record.fromName,
label: record.fromLabel,
description: record.fromDescription,
icon: record.fromIcon,
name: relationMetadataInput.fromName,
label: relationMetadataInput.fromLabel,
description: relationMetadataInput.fromDescription,
icon: relationMetadataInput.fromIcon,
isCustom: true,
targetColumnMap: {},
isActive: true,
type: FieldMetadataType.RELATION,
objectMetadataId: record.fromObjectMetadataId,
workspaceId: record.workspaceId,
objectMetadataId: relationMetadataInput.fromObjectMetadataId,
workspaceId: relationMetadataInput.workspaceId,
},
// TO
{
name: record.toName,
label: record.toLabel,
description: record.toDescription,
icon: record.toIcon,
name: relationMetadataInput.toName,
label: relationMetadataInput.toLabel,
description: relationMetadataInput.toDescription,
icon: relationMetadataInput.toIcon,
isCustom: true,
targetColumnMap: {
value: isToCustom
? createCustomColumnName(record.toName)
: record.toName,
? createCustomColumnName(relationMetadataInput.toName)
: relationMetadataInput.toName,
},
isActive: true,
type: FieldMetadataType.RELATION,
objectMetadataId: record.toObjectMetadataId,
workspaceId: record.workspaceId,
objectMetadataId: relationMetadataInput.toObjectMetadataId,
workspaceId: relationMetadataInput.workspaceId,
},
// FOREIGN KEY
{
name: baseColumnName,
label: `${record.toLabel} Foreign Key`,
description: record.toDescription
? `${record.toDescription} Foreign Key`
label: `${relationMetadataInput.toLabel} Foreign Key`,
description: relationMetadataInput.toDescription
? `${relationMetadataInput.toDescription} Foreign Key`
: undefined,
icon: undefined,
isCustom: true,
@ -163,8 +170,8 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
// Should not be visible on the front side
isSystem: true,
type: FieldMetadataType.UUID,
objectMetadataId: record.toObjectMetadataId,
workspaceId: record.workspaceId,
objectMetadataId: relationMetadataInput.toObjectMetadataId,
workspaceId: relationMetadataInput.workspaceId,
},
]);
@ -177,17 +184,18 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
}, {});
const createdRelationMetadata = await super.createOne({
...record,
fromFieldMetadataId: createdFieldMap[record.fromName].id,
toFieldMetadataId: createdFieldMap[record.toName].id,
...relationMetadataInput,
fromFieldMetadataId: createdFieldMap[relationMetadataInput.fromName].id,
toFieldMetadataId: createdFieldMap[relationMetadataInput.toName].id,
});
await this.workspaceMigrationService.createCustomMigration(
record.workspaceId,
relationMetadataInput.workspaceId,
[
// Create the column
{
name: objectMetadataMap[record.toObjectMetadataId].targetTableName,
name: objectMetadataMap[relationMetadataInput.toObjectMetadataId]
.targetTableName,
action: 'alter',
columns: [
{
@ -199,16 +207,20 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
},
// Create the foreignKey
{
name: objectMetadataMap[record.toObjectMetadataId].targetTableName,
name: objectMetadataMap[relationMetadataInput.toObjectMetadataId]
.targetTableName,
action: 'alter',
columns: [
{
action: WorkspaceMigrationColumnActionType.RELATION,
columnName: foreignKeyColumnName,
referencedTableName:
objectMetadataMap[record.fromObjectMetadataId].targetTableName,
objectMetadataMap[relationMetadataInput.fromObjectMetadataId]
.targetTableName,
referencedTableColumnName: 'id',
isUnique: record.relationType === RelationMetadataType.ONE_TO_ONE,
isUnique:
relationMetadataInput.relationType ===
RelationMetadataType.ONE_TO_ONE,
},
],
},
@ -216,7 +228,7 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
);
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
record.workspaceId,
relationMetadataInput.workspaceId,
);
return createdRelationMetadata;