Skip to content

Commit

Permalink
If an object is disabled, then the relationships to that object shoul…
Browse files Browse the repository at this point in the history
…d 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](#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 <[email protected]>
Co-authored-by: Marie Stoppa <[email protected]>
  • Loading branch information
3 people authored Aug 21, 2024
1 parent 08e07ac commit 6caa780
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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 },
);
}
}
}

0 comments on commit 6caa780

Please sign in to comment.