Skip to content

Commit

Permalink
Validate relationship types on bulk save (#7554)
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl authored Dec 18, 2024
1 parent e016734 commit c89c686
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/api/entities/specs/denormalization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('Denormalize relationships', () => {
describe('title and basic property (text)', () => {
it('should update denormalized title and icon', async () => {
const fixtures: DBFixture = {
relationtypes: [factory.relationType('rel1')],
templates: [
factory.template('templateA', [
factory.relationshipProp('relationship', 'templateB', 'text'),
Expand Down Expand Up @@ -232,6 +233,7 @@ describe('Denormalize relationships', () => {

describe('when the relationship property has no content', () => {
const fixtures: DBFixture = {
relationtypes: [factory.relationType('rel1')],
templates: [
factory.template('templateA', [
factory.relationshipProp('relationship', '', { content: '' }),
Expand Down Expand Up @@ -289,6 +291,7 @@ describe('Denormalize relationships', () => {
beforeEach(async () => {
jest.spyOn(translations, 'updateContext').mockImplementation(async () => 'ok');
const fixtures: DBFixture = {
relationtypes: [factory.relationType('rel1')],
templates: [
factory.template('templateA', [
factory.inherit('relationship', 'templateB', 'multiselect'),
Expand Down Expand Up @@ -383,6 +386,7 @@ describe('Denormalize relationships', () => {
describe('inherited relationship', () => {
beforeEach(async () => {
const fixtures: DBFixture = {
relationtypes: [factory.relationType('rel1')],
templates: [
factory.template('templateA', [
factory.inherit('relationship', 'templateB', 'relationshipB'),
Expand Down Expand Up @@ -444,6 +448,7 @@ describe('Denormalize relationships', () => {
beforeEach(async () => {
await load(
{
relationtypes: [factory.relationType('rel1')],
templates: [
factory.template('templateA', [
factory.inherit('relationshipA', 'templateB', 'relationshipB'),
Expand Down
6 changes: 6 additions & 0 deletions app/api/entities/specs/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,12 @@ export default {
name: 'templateForGetWithRelationships',
},
],
relationtypes: [
{ _id: relationType1 },
{ _id: relationType2 },
{ _id: relationType3 },
{ _id: relationType4 },
],
connections: [
{ _id: referenceId, entity: 'shared', template: null, hub: hub1, entityData: {} },
{ _id: db.id(), entity: 'shared2', template: relationType1, hub: hub1, entityData: {} },
Expand Down
14 changes: 14 additions & 0 deletions app/api/relationships/relationships.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,20 @@ export default {
).map(r => r.sharedId)
);

const relTypesToSave = new Set(
relsFlat
.map(r => (r.template && Object.hasOwn(r.template, '_id') ? r.template._id : r.template))
.filter(r => r)
);

const existingRelationshipTypes = await relationtypes.count({
_id: { $in: Array.from(relTypesToSave) },
});

if (relTypesToSave.size !== existingRelationshipTypes) {
throw new Error('Some relationship types do not exist');
}

const relationships = rels.map(_group => {
let group = _group.filter(r => existingEntities.has(r.entity));
if (group.length === 1 && !group[0].hub) {
Expand Down
24 changes: 23 additions & 1 deletion app/api/relationships/specs/relationships.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ describe('relationships', () => {
});
});

describe('when creating relationships using non existent relationship types', () => {
it('should throw an error', async () => {
const nonExistentRelationshipType = db.id();
await expect(async () =>
relationships.save(
[
{ entity: 'entity3', template: relation2 },
{ entity: 'entity2', template: nonExistentRelationshipType },
{ entity: 'entity4', template: null },
],
'en'
)
).rejects.toBeInstanceOf(Error);
});
});

describe('when creating relationships to non existent entities', () => {
it('should not create them', async () => {
const relations = await relationships.save(
Expand Down Expand Up @@ -373,11 +389,17 @@ describe('relationships', () => {
});

it('should update correctly if template is null', async () => {
const reference = await relationships.getById(connectionID1);
let reference = await relationships.getById(connectionID1);
reference.template = { _id: null };
const [savedReference] = await relationships.save(reference, 'en');
expect(savedReference.entity).toBe('entity_id');
expect(savedReference.template).toBe(null);

reference = await relationships.getById(connectionID1);
reference.template = null;
const [savedRef2] = await relationships.save(reference, 'en');
expect(savedRef2.entity).toBe('entity_id');
expect(savedRef2.template).toBe(null);
});
});

Expand Down
4 changes: 4 additions & 0 deletions app/api/relationtypes/relationtypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export default {
return model.get(query);
},

count(query) {
return model.count(query);
},

getById(id) {
return model.getById(id);
},
Expand Down
1 change: 1 addition & 0 deletions app/api/search.v2/specs/sorting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Sorting', () => {

await setupTestingEnviroment(
{
relationtypes: [factory.relationType('rel1')],
templates: [
factory.template('templateA', [
factory.property('textProperty', 'text'),
Expand Down

0 comments on commit c89c686

Please sign in to comment.