If an object is disabled, then the relationships to that object should be disabled (#6690)

This PR was created by [GitStart](https://gitstart.com/) to address the
requirements from this ticket:
[TWNTY-5370](https://clients.gitstart.com/twenty/5449/tickets/TWNTY-5370).
This ticket was imported from:
[TWNTY-5370](https://github.com/twentyhq/twenty/issues/5370)

 --- 


### Description

- We updated the logic in
packages/twenty-server/src/engine/metadata-modules/object-metadata/object-metadata.service.ts
  
  Test cases:
1. Ensure that when an object is disabled, all related relationships are
also disabled.
         a. Example disable the people object
b. Check the company object and verify that the people field has been
disabled too
c. Check the opportunity object and check that the point of contact
field has been disabled too
2. Verify that when a previously disabled object is restored, the
relationships are also restored.
3. Ensure that previously disabled relationships remain disabled when
the object is disabled and later restored.
4. Verify that relationships of a disabled object are not visible in the
UI.
5. Ensure that relationships to a disabled object are marked as inactive
in the data models screen

 ### Refs

#5370

  
### Demo
  

<https://www.loom.com/share/2b0a91f463ca4e02a6963f9a8796a0d9?sid=1e9c4fb8-8fb9-4c6c-b43a-c50f3776e1d3>

Fixes #5370

---------

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: Marie Stoppa <marie.stoppa@essec.edu>
This commit is contained in:
gitstart-app[bot]
2024-08-21 11:19:21 +02:00
committed by GitHub
parent 08e07ac2d5
commit 6caa78008f

View File

@ -5,7 +5,7 @@ import console from 'console';
import { Query, QueryOptions } from '@ptc-org/nestjs-query-core';
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
import { FindManyOptions, FindOneOptions, Repository } from 'typeorm';
import { FindManyOptions, FindOneOptions, In, Repository } from 'typeorm';
import { FieldMetadataSettings } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata-settings.interface';
@ -369,6 +369,10 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
const updatedObject = await super.updateOne(input.id, input.update);
if (input.update.isActive !== undefined) {
await this.updateObjectRelationships(input.id, input.update.isActive);
}
await this.workspaceMetadataVersionService.incrementMetadataVersion(
workspaceId,
);
@ -1238,4 +1242,32 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
],
);
}
private async updateObjectRelationships(
objectMetadataId: string,
isActive: boolean,
) {
const affectedRelations = await this.relationMetadataRepository.find({
where: [
{ fromObjectMetadataId: objectMetadataId },
{ toObjectMetadataId: objectMetadataId },
],
});
const affectedFieldIds = affectedRelations.reduce(
(acc, { fromFieldMetadataId, toFieldMetadataId }) => {
acc.push(fromFieldMetadataId, toFieldMetadataId);
return acc;
},
[] as string[],
);
if (affectedFieldIds.length > 0) {
await this.fieldMetadataRepository.update(
{ id: In(affectedFieldIds) },
{ isActive: isActive },
);
}
}
}