Skip to content

Commit

Permalink
same relationship and different inherit case
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Jun 14, 2021
1 parent 4d32f68 commit c55eef5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
12 changes: 9 additions & 3 deletions app/api/entities/denormalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ interface Changes {
interface Params {
id: string;
language?: string;
template?: string;
}

export const updateDenormalization = async (
{ id, language }: Params,
{ id, language, template }: Params,
changes: Changes,
properties: PropertySchema[]
) =>
Promise.all(
properties.map(async property =>
model.updateMany(
{ ...(language ? { language } : {}), [`metadata.${property.name}.value`]: id },
{
...(template ? { template } : {}),
...(language ? { language } : {}),
[`metadata.${property.name}.value`]: id,
},
{
$set: Object.keys(changes).reduce(
(set, prop) => ({
Expand Down Expand Up @@ -68,7 +73,7 @@ export const denormalizeRelated = async (
throw new Error('denormalization requires an entity with title, sharedId and language');
}

const transitiveProperties = await templates.propsThatNeedInheritDenormalization(
const transitiveProperties = await templates.propsThatNeedTransitiveDenormalization(
template._id.toString()
);
const properties = await templates.propsThatNeedDenormalization(template._id.toString());
Expand All @@ -89,6 +94,7 @@ export const denormalizeRelated = async (
// @ts-ignore we have a sharedId guard, why ts does not like this ? bug ?
id: entity.sharedId,
language: entity.language,
template: prop.template,
},
{
...(inheritProperty ? { inheritedValue: entity.metadata?.[inheritProperty.name] } : {}),
Expand Down
2 changes: 1 addition & 1 deletion app/api/entities/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ export default {

await updateDenormalization({ id: valueId, language }, { label: newLabel }, properties);

const transitiveProps = await templates.propsThatNeedInheritDenormalization(
const transitiveProps = await templates.propsThatNeedTransitiveDenormalization(
thesaurusId.toString()
);

Expand Down
34 changes: 21 additions & 13 deletions app/api/entities/specs/denormalization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,31 @@ describe('Denormalize relationships', () => {
it('should update title and text property denormalized on related entities from 2 different templates', async () => {
const fixtures: DBFixture = {
templates: [
factory.template('templateA', [factory.property('text')]),
factory.template('templateB', [factory.inherit('relationship_b', 'templateA', 'text')]),
factory.template('templateC', [factory.inherit('relationship_c', 'templateA', 'text')]),
factory.template('templateA', [
factory.property('text'),
factory.property('another_text'),
]),
factory.template('templateB', [factory.inherit('relationship', 'templateA', 'text')]),
factory.template('templateC', [
factory.inherit('relationship', 'templateA', 'another_text'),
]),
],
entities: [
factory.entity('A1', 'templateA'),
factory.entity('B1', 'templateB', { relationship_b: [factory.metadataValue('A1')] }),
factory.entity('B2', 'templateB', { relationship_b: [factory.metadataValue('A1')] }),
factory.entity('C1', 'templateC', { relationship_c: [factory.metadataValue('A1')] }),
factory.entity('B1', 'templateB', { relationship: [factory.metadataValue('A1')] }),
factory.entity('B2', 'templateB', { relationship: [factory.metadataValue('A1')] }),
factory.entity('C1', 'templateC', { relationship: [factory.metadataValue('A1')] }),
],
};

await load(fixtures);

await modifyEntity('A1', {
title: 'new A1',
metadata: { text: [{ value: 'text 1 changed' }] },
metadata: {
text: [{ value: 'text changed' }],
another_text: [{ value: 'another_text changed' }],
},
});

const [relatedB1, relatedB2, relatedC] = [
Expand All @@ -157,16 +165,16 @@ describe('Denormalize relationships', () => {
await entities.getById('C1', 'en'),
];

expect(relatedB1?.metadata?.relationship_b).toMatchObject([
{ label: 'new A1', inheritedValue: [{ value: 'text 1 changed' }] },
expect(relatedB1?.metadata?.relationship).toMatchObject([
{ label: 'new A1', inheritedValue: [{ value: 'text changed' }] },
]);

expect(relatedB2?.metadata?.relationship_b).toMatchObject([
{ label: 'new A1', inheritedValue: [{ value: 'text 1 changed' }] },
expect(relatedB2?.metadata?.relationship).toMatchObject([
{ label: 'new A1', inheritedValue: [{ value: 'text changed' }] },
]);

expect(relatedC?.metadata?.relationship_c).toMatchObject([
{ label: 'new A1', inheritedValue: [{ value: 'text 1 changed' }] },
expect(relatedC?.metadata?.relationship).toMatchObject([
{ label: 'new A1', inheritedValue: [{ value: 'another_text changed' }] },
]);
});

Expand Down
18 changes: 14 additions & 4 deletions app/api/templates/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,22 @@ export default {
await model.get({
$or: [{ 'properties.content': templateId }, { 'properties.content': '' }],
})
)
.reduce<PropertySchema[]>((m, t) => m.concat(t.properties || []), [])
.filter(p => templateId === p.content?.toString() || p.content === '');
).reduce<{ [k: string]: string | undefined }[]>(
(m, t) =>
m.concat(
t.properties
?.filter(p => templateId === p.content?.toString() || p.content === '')
.map(p => ({
name: p.name,
inheritProperty: p.inheritProperty,
template: t._id,
})) || []
),
[]
);
},

async propsThatNeedInheritDenormalization(contentId: string) {
async propsThatNeedTransitiveDenormalization(contentId: string) {
const properties = (await model.get({ 'properties.content': contentId }))
.reduce<PropertySchema[]>((m, t) => m.concat(t.properties || []), [])
.filter(p => contentId === p.content?.toString());
Expand Down

0 comments on commit c55eef5

Please sign in to comment.