Skip to content

Commit

Permalink
version 1.29.0
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed May 10, 2021
2 parents 3a919ed + 0ffc7c6 commit 92c6e51
Show file tree
Hide file tree
Showing 49 changed files with 1,166 additions and 314 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci_ts_emit_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: check-emitted-types

on:
push:
branches:
- master
- development
- release
pull_request:

jobs:
check-emitted-types:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 14.6.x
uses: actions/setup-node@v1
with:
node-version: '14.6.x'
- name: Cache node modules
uses: actions/cache@v2
with:
path: ./node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
- name: install dependencies
run: yarn install
- run: yarn emit-types --check
33 changes: 27 additions & 6 deletions app/api/entities/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function denormalizeMetadata(metadata, entity, template, dictionariesByKey
}

const translation = (await translationsModel.get({ locale: entity.language }))[0];

const allTemplates = await templates.get();
const resolveProp = async (key, value) => {
if (!Array.isArray(value)) {
throw new Error('denormalizeMetadata received non-array prop!');
Expand Down Expand Up @@ -59,12 +59,25 @@ async function denormalizeMetadata(metadata, entity, template, dictionariesByKey
}

if (prop.type === 'relationship') {
const partner = await model.get({ sharedId: elem.value, language: entity.language });
const [partner] = await model.get({ sharedId: elem.value, language: entity.language });

if (partner && partner.title) {
elem.label = partner.title;
elem.icon = partner.icon;
elem.type = partner.file ? 'document' : 'entity';
}

if (prop.inherit && partner) {
const partnerTemplate = allTemplates.find(
t => t._id.toString() === partner.template.toString()
);

if (partner && partner[0] && partner[0].title) {
elem.label = partner[0].title;
elem.icon = partner[0].icon;
elem.type = partner[0].file ? 'document' : 'entity';
const inheritedProperty = partnerTemplate.properties.find(
p => p._id && p._id.toString() === prop.inheritProperty.toString()
);

elem.inheritedValue = partner.metadata[inheritedProperty.name];
elem.inheritedType = inheritedProperty.type;
}
}
return elem;
Expand Down Expand Up @@ -126,6 +139,7 @@ async function updateEntity(entity, _template) {
) {
await this.renameRelatedEntityInMetadata({ ...currentDoc, ...entity });
}

const toSave = { ...entity };
if (entity.metadata) {
toSave.metadata = await denormalizeMetadata(entity.metadata, entity, template);
Expand Down Expand Up @@ -323,6 +337,7 @@ export default {
const [entity] = await this.getWithRelationships({ sharedId, language });
if (updateRelationships) {
await relationships.saveEntityBasedReferences(entity, language);
await this.updateDenormalizedMetadataInRelatedEntities(entity);
}
if (index) {
await search.indexEntities({ sharedId }, '+fullText');
Expand All @@ -331,6 +346,12 @@ export default {
return entity;
},

async updateDenormalizedMetadataInRelatedEntities(entity) {
const related = await relationships.getByDocument(entity.sharedId, entity.language);
const sharedIds = related.map(r => r.entityData.sharedId);
await this.updateMetdataFromRelationships(sharedIds, entity.language);
},

async denormalize(_doc, { user, language }) {
await validateEntity(_doc);
const doc = _doc;
Expand Down
18 changes: 16 additions & 2 deletions app/api/entities/specs/entities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ describe('entities', () => {
};
const user = { _id: db.id() };

await entities.save(doc, { user, language: 'es' });
await entities.save(doc, { user, language: 'es' }, false);
await new Promise(resolve => setTimeout(resolve, 3000));
expect(entities.updateMetdataFromRelationships).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -730,7 +730,18 @@ describe('entities', () => {
const denormalized = await entities.denormalize(entity, { user: 'dummy', language: 'en' });
expect(denormalized.metadata.friends[0].label).toBe('shared2title');
});

it('should denormalize inherited metadata', async () => {
const entity = (await entities.get({ sharedId: 'shared', language: 'en' }))[0];

const denormalized = await entities.denormalize(entity, { user: 'dummy', language: 'en' });
expect(denormalized.metadata.enemies[0].inheritedValue).toEqual([
{ value: 'something to be inherited' },
]);
expect(denormalized.metadata.enemies[0].inheritedType).toBe('text');
});
});

describe('countByTemplate', () => {
it('should return how many entities using the template passed', async () => {
const count = await entities.countByTemplate(templateId);
Expand Down Expand Up @@ -832,7 +843,10 @@ describe('entities', () => {
['shared'],
{
diffMetadata: {
multiselect: { added: [{ value: 'country_two' }], removed: [{ value: 'country_one' }] },
multiselect: {
added: [{ value: 'country_two' }],
removed: [{ value: 'country_one' }],
},
},
},
{ language: 'en' }
Expand Down
9 changes: 7 additions & 2 deletions app/api/entities/specs/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const relationType3 = db.id();
const relationType4 = db.id();
const uploadId1 = db.id();
const uploadId2 = db.id();
const inheritedProperty = db.id();

export default {
files: [
Expand Down Expand Up @@ -312,7 +313,9 @@ export default {
sharedId: 'shared2',
language: 'en',
title: 'shared2title',
metadata: {},
metadata: {
property1: [{ value: 'something to be inherited' }],
},
},
{ sharedId: 'source2', language: 'en' },
{
Expand Down Expand Up @@ -358,7 +361,7 @@ export default {
name: 'template_test',
properties: [
{ type: 'text', name: 'text' },
{ type: 'text', name: 'property1' },
{ _id: inheritedProperty, type: 'text', name: 'property1' },
{ type: 'text', name: 'property2' },
{ type: 'text', name: 'description' },
{ type: 'select', name: 'select', content: dictionary },
Expand All @@ -377,6 +380,8 @@ export default {
name: 'enemies',
relationType: relationType4,
content: templateId.toString(),
inherit: true,
inheritProperty: inheritedProperty,
},
{ type: 'nested', name: 'field_nested' },
{ type: 'numeric', name: 'numeric' },
Expand Down
69 changes: 69 additions & 0 deletions app/api/migrations/migrations/38-denormalize-inherited/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* eslint-disable no-await-in-loop */
export default {
delta: 38,

name: 'denormalize-inherited',

description: 'Denormalize inherited metadata',

async up(db) {
const cursor = db.collection('entities').find({});
const templates = await db
.collection('templates')
.find({})
.toArray();

let index = 1;

while (await cursor.hasNext()) {
const entity = await cursor.next();
if (entity.template && entity.metadata) {
const template = templates.find(t => t._id.toString() === entity.template.toString());

await Promise.all(
template.properties.map(async prop => {
if (prop.type === 'relationship') {
const value = entity.metadata[prop.name] || [];
const denormalizedValue = await Promise.all(
value.map(async _elem => {
const elem = { ..._elem };
const [partner] = await db
.collection('entities')
.find({
sharedId: elem.value,
language: entity.language,
})
.toArray();

if (prop.inherit && partner) {
const partnerTemplate = templates.find(
t => t._id.toString() === partner.template.toString()
);

const inheritedProperty = partnerTemplate.properties.find(
p => p._id && p._id.toString() === prop.inheritProperty.toString()
);

elem.inheritedValue = partner.metadata[inheritedProperty.name] || [];
elem.inheritedType = inheritedProperty.type;
}
return elem;
})
);

entity.metadata[prop.name] = denormalizedValue.filter(v => v);
}
})
);

await db
.collection('entities')
.updateOne({ _id: entity._id }, { $set: { metadata: entity.metadata } });
}
process.stdout.write(`-> processed: ${index} \r`);
index += 1;
}

process.stdout.write('\r\n');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import testingDB from 'api/utils/testing_db';
import migration from '../index.js';
import fixtures from './fixtures.js';

describe('migration denormalize-inherited', () => {
beforeEach(async () => {
spyOn(process.stdout, 'write');
await testingDB.clearAllAndLoad(fixtures);
});

afterAll(async () => {
await testingDB.disconnect();
});

it('should have a delta number', () => {
expect(migration.delta).toBe(38);
});

it('should denormalize inherited data', async () => {
await migration.up(testingDB.mongodb);
const [denormalizedEntity] = await testingDB.mongodb
.collection('entities')
.find({ title: 'test_doc' })
.toArray();

expect(denormalizedEntity.metadata.friend).toEqual([
{
inheritedType: 'text',
inheritedValue: [{ value: 'Bocata Tun' }],
label: 'test_doc 2',
value: '456DEF',
},
{
inheritedType: 'text',
inheritedValue: [],
label: 'test_doc 3',
value: '789ZXY',
},
]);

const [denormalizedEntityWithoutValues] = await testingDB.mongodb
.collection('entities')
.find({ title: 'test_doc 4' })
.toArray();

expect(denormalizedEntityWithoutValues.metadata.friend).toEqual([
{
inheritedType: 'text',
inheritedValue: [],
label: 'test_doc 5',
value: '789ABC',
},
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import db from 'api/utils/testing_db';
const templateId = db.id();
const templateId2 = db.id();
const inheritPropertyId = db.id();
export default {
entities: [
{
template: templateId,
title: 'test_doc',
sharedId: '123ABC',
metadata: {
friend: [
{ value: '456DEF', label: 'test_doc 2' },
{ value: '789ZXY', label: 'test_doc 3' },
],
},
language: 'en',
},

{
template: templateId2,
title: 'test_doc 2',
sharedId: '456DEF',
metadata: { name: [{ value: 'Bocata Tun' }] },
language: 'en',
},
{
template: templateId2,
title: 'test_doc 3',
sharedId: '789ZXY',
metadata: { name: [] },
language: 'en',
},
{
template: templateId,
title: 'test_doc 4',
sharedId: '498ABC',
metadata: {
friend: [{ value: '789ABC', label: 'test_doc 5' }],
},
language: 'en',
},
{
template: templateId2,
title: 'test_doc 5',
sharedId: '789ABC',
metadata: {},
language: 'en',
},
{
title: 'Im gona break everything',
},
{
title: 'Try to break it harder',
template: 'whats a template?',
},
{
title: 'There is only zull',
template: templateId,
},
],
templates: [
{
_id: templateId,
properties: [
{
type: 'relationship',
relationType: 'something',
inherit: true,
content: templateId2,
inheritProperty: inheritPropertyId,
name: 'friend',
},
],
},
{
_id: templateId2,
properties: [{ _id: inheritPropertyId, type: 'text', name: 'name' }],
},
],
};
Loading

0 comments on commit 92c6e51

Please sign in to comment.