Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3598 - Custom page for entity view #3617

Merged
merged 118 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
118 commits
Select commit Hold shift + click to select a range
b179aff
Initial test for front end component
Zasa-san Apr 20, 2021
f93127b
Added component with placeholder contents
Zasa-san Apr 20, 2021
7f1088d
Initial layout of frontend component
Zasa-san Apr 20, 2021
0a008db
fleshing out component
Zasa-san Apr 20, 2021
984610a
Migrated pagesModel to TS and json schema
RafaPolit Apr 21, 2021
a9af719
Foreign change due to emit types.
RafaPolit Apr 21, 2021
d0b374e
Changed pages spec to TS and async
RafaPolit Apr 21, 2021
3e19fba
Migrated pages.js to TS
RafaPolit Apr 21, 2021
800eb99
Moving routes to TS, lacking validation
RafaPolit Apr 21, 2021
3816663
Preparing to test via supertest route validation
RafaPolit Apr 22, 2021
31ec823
Added route validation
RafaPolit Apr 22, 2021
4fd786f
Naming fix
Zasa-san Apr 22, 2021
9586557
Test for new option
Zasa-san Apr 22, 2021
5b51b2a
Initial setup
Zasa-san Apr 22, 2021
128d8e7
Using controled component
Zasa-san Apr 22, 2021
666f0ba
Migrated PageCreator with test to TS
Zasa-san Apr 23, 2021
b1f7e08
Fix type erros
Zasa-san Apr 23, 2021
3ec64f1
Test for new page action
Zasa-san Apr 25, 2021
e522721
Action to update toggle button
Zasa-san Apr 25, 2021
ccbe60d
Updated pageCreator test props with new action
Zasa-san Apr 25, 2021
491b8e8
Added action to pageCreator to toggle entity view settng
Zasa-san Apr 25, 2021
8a8204c
pageActions test to TS
Zasa-san Apr 25, 2021
3bd9d11
Type fixes
Zasa-san Apr 26, 2021
9d0dfad
Added filter type.
RafaPolit Apr 26, 2021
aded7d2
Incomplete test
RafaPolit Apr 26, 2021
dddf1ff
Added loadPages to page selector in templates
RafaPolit Apr 26, 2021
4a068d3
Adapted viewTemplateAsPage test
Zasa-san Apr 26, 2021
4938511
Added check in case no page is set for template view
Zasa-san Apr 26, 2021
42108ab
Logic for no pages
Zasa-san Apr 26, 2021
3ca339a
Minor test error fix
Zasa-san Apr 26, 2021
9c36c7b
pageActions to TS
Zasa-san Apr 26, 2021
5fe1025
Added value for entity view page on Template model
Zasa-san Apr 27, 2021
42c6c43
Toggled base on entityViewPage value
Zasa-san Apr 27, 2021
9f3925c
Test fix
Zasa-san Apr 27, 2021
7a90504
Selected option and select functionality
Zasa-san Apr 27, 2021
efee17d
Logic to clear selecte page value
Zasa-san Apr 27, 2021
e8739a4
Prepared Entity actions for user selected tabs
RafaPolit Apr 27, 2021
1de5311
WIP Showing info or page view depending on temp.
RafaPolit Apr 27, 2021
0ca171c
type change
Zasa-san Apr 28, 2021
6ab156d
Encapsulated page assets
RafaPolit Apr 28, 2021
f680907
Added action to update value on toggle
Zasa-san Apr 28, 2021
5d18235
Changed select implementation
Zasa-san Apr 28, 2021
38e0136
Remove unused import
Zasa-san Apr 28, 2021
99d54bf
Changed so template uses sharedId instead of _id
Zasa-san Apr 28, 2021
130f682
Preparing EntityViewer and View to hold pages.
RafaPolit Apr 28, 2021
093a632
Styling of new components
Zasa-san Apr 28, 2021
f3344a4
Simpler rison type until better approach found
RafaPolit Apr 28, 2021
1776bc0
Fully functional proof of concept
RafaPolit Apr 28, 2021
6ed6ae3
Removed url and preview field on pages for entity view
Zasa-san Apr 30, 2021
57b129b
Fixed e2e selectors
Zasa-san Apr 30, 2021
e9ec2ee
Removed unused eslint remove max statements
Zasa-san Apr 30, 2021
b79647a
Improved params and fixed defaults
RafaPolit Apr 30, 2021
a9eae38
Merge remote-tracking branch 'origin/3598-custom-entity-view' into 35…
Zasa-san Apr 30, 2021
0af4112
Tested new markdown datsets options
RafaPolit May 3, 2021
7c54226
Merge remote-tracking branch 'origin/3598-custom-entity-view' into 35…
Zasa-san May 3, 2021
a451368
Added validation for page crud when they are used as entity views
Zasa-san May 3, 2021
1df92ba
unintended import fix
Zasa-san May 3, 2021
e7e373f
Remove unnecessary page get.
Zasa-san May 3, 2021
5d2bc1a
Condition to verify page sharedID before validating entity view use
Zasa-san May 3, 2021
5ae561d
Tested extracted component
RafaPolit May 3, 2021
cf7f860
Appeded component cleanup
RafaPolit May 3, 2021
38f9b81
Removed comment
RafaPolit May 3, 2021
8c138f8
Removed tests for extracted code.
RafaPolit May 3, 2021
5470cf8
Tested extra EntityView funcitonality
RafaPolit May 3, 2021
85bc654
Tested mapState, other visual changes untested
RafaPolit May 3, 2021
a16f00e
Test for pages schema
Zasa-san May 4, 2021
e9591c7
Small refactor
RafaPolit May 4, 2021
2340c25
Test for template schema entity view page validation
Zasa-san May 5, 2021
bf41ebb
Validate that selected page exists
Zasa-san May 5, 2021
51e96a7
Merge branch 'development' into 3598-custom-entity-view
Zasa-san May 5, 2021
dfcf509
Removed unnecessary test
Zasa-san May 5, 2021
4ba274f
Validation for selecting pages that are not entity view enabled
Zasa-san May 5, 2021
d4b8c74
Remove unused imports
Zasa-san May 6, 2021
7a19489
loadPages test
Zasa-san May 6, 2021
cbddf2b
Basic e2e
Zasa-san May 6, 2021
3db7650
Minor fix
Zasa-san May 6, 2021
007792b
Added currentTemplate to page datasets
RafaPolit May 7, 2021
478ae84
e2e update
Zasa-san May 7, 2021
d972ee6
Removed need for extra query
RafaPolit May 7, 2021
54b0e0b
Added datasets to script of page.
RafaPolit May 7, 2021
7f67936
Removed logs from proof of concept
RafaPolit May 7, 2021
991e9d4
Applied Thunk and mocked api call for tests
RafaPolit May 10, 2021
9a4742b
Excluded selector for relationTypes
RafaPolit May 10, 2021
367caa6
Updated e2e to include value components
Zasa-san May 10, 2021
028c5a2
Selected a more comprehensive template
Zasa-san May 10, 2021
d9795a9
Check for new component
Zasa-san May 11, 2021
830c424
Added test for when it should render template editor
Zasa-san May 11, 2021
4e3c1f4
Abstracted safeName
RafaPolit May 11, 2021
bd9666f
Further advanced CurrentEntity
RafaPolit May 11, 2021
ef1a5d4
extracted query building to reduce complexity (codeclimate)
Zasa-san May 11, 2021
cd88b16
Slight fix of previous commit
Zasa-san May 11, 2021
2f2ef43
More work on CurrentEntity
RafaPolit May 11, 2021
b00f196
Removed unneeded types
Zasa-san May 13, 2021
4ae6197
Fixed fixture typing
Zasa-san May 13, 2021
b3a547a
Changed component name
Zasa-san May 13, 2021
c4bdc13
Import + type fixing
Zasa-san May 13, 2021
ba92d47
Added optional in type
Zasa-san May 13, 2021
b49969e
Reintroduced missing line.
RafaPolit May 13, 2021
f6df093
Fixed codeclimate errors
RafaPolit May 13, 2021
fd35d2c
Removed changes pertaining to a future PR
RafaPolit May 13, 2021
95ad372
Added static content and classes to improve e2e test
Zasa-san May 14, 2021
68f4c56
Removed exposure of datasets in favor or #3670
RafaPolit May 17, 2021
0a74b42
Changed return of setPageAssets to only values
RafaPolit May 17, 2021
c881e04
use change of setPageAssests to getPageAssets
RafaPolit May 17, 2021
c16fc36
Updated snapshot
RafaPolit May 17, 2021
6210488
Awaited for async getPageAssets
RafaPolit May 17, 2021
46ac04a
Test that entityView is sync across all languages
Zasa-san May 17, 2021
0ef2c1b
entityView sync across all languages
Zasa-san May 17, 2021
e3d1c53
Better expect statement
Zasa-san May 20, 2021
4f1d27e
Only request the required data from the db to sync entityView
Zasa-san May 20, 2021
ebd2f96
Renamed entityView page datasets.
RafaPolit May 21, 2021
1b25c7a
Added formatted entity to page view of entity
RafaPolit May 21, 2021
b888708
Expanded e2e to cover formatted datasets
Zasa-san May 24, 2021
2b51690
Expanded e2e to cover value from thesauri selector
Zasa-san May 24, 2021
9fbd36a
Further fromatted entity dataset
RafaPolit May 26, 2021
ae2c551
Update to e2e
Zasa-san May 26, 2021
1ad6b5d
Rebase: fixed duplicated const during rebase
Zasa-san May 31, 2021
208beb4
Merge branch 'development' into 3598-custom-entity-view
daneryl Jun 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 0 additions & 84 deletions app/api/pages/pages.js

This file was deleted.

117 changes: 117 additions & 0 deletions app/api/pages/pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import ID from 'shared/uniqueID';
import { PageType } from 'shared/types/pageType';
import { validatePage } from 'shared/types/pageSchema';
import date from 'api/utils/date.js';
import templates from 'api/templates';
import { createError } from 'api/utils';
import { UwaziFilterQuery } from 'api/odm';
import { User } from 'api/users/usersModel';

import model from './pagesModel';
import settings from '../settings';

const assignUserAndDate = (page: PageType, user?: User) => {
if (!user) {
throw new Error('missing user');
}
return {
...page,
user: user._id,
creationDate: date.currentUTC(),
};
};

const entityViewSyncing = async (page: PageType) => {
const pageInAllLaguangues = await model.get({ sharedId: page.sharedId }, '_id entityView');
const updatedPages = pageInAllLaguangues.map(_id => ({
..._id,
entityView: page.entityView || false,
}));
await model.saveMultiple(updatedPages);
};

export default {
// eslint-disable-next-line max-statements
async save(_page: PageType, user?: User, language?: string) {
await validatePage(_page);
let page = { ..._page };

if (!page.sharedId) {
page = assignUserAndDate(page, user);
}

if (page.sharedId) {
await entityViewSyncing(page);
return model.save(page);
}

const { languages = [] } = await settings.get();
const sharedId = ID();
const pages = languages.map(lang => ({
...page,
language: lang.key,
sharedId,
}));
await model.saveMultiple(pages);
return this.getById(sharedId, language);
},

get(query: UwaziFilterQuery<PageType>, select?: string) {
return model.get(query, select);
},

async getById(sharedId: string, language?: string, select?: string) {
const results = await this.get({ sharedId, language }, select);
return results[0] ? results[0] : Promise.reject(createError('Page not found', 404));
},

async delete(sharedId: string) {
const templatesUsingPage = await templates.get({
entityViewPage: sharedId,
});
if (templatesUsingPage.length > 0) {
const templatesTitles = templatesUsingPage.map(template => template.name);
return Promise.reject(
createError(
`This page is in use by the following templates: ${templatesTitles.join(
', '
)}. Remove the page from the templates before trying again.`,
409
)
);
}
Zasa-san marked this conversation as resolved.
Show resolved Hide resolved
return model.delete({ sharedId });
},

async addLanguage(language: string) {
const [lanuageTranslationAlreadyExists] = await this.get({ locale: language });
if (lanuageTranslationAlreadyExists) {
return Promise.resolve();
}

const { languages } = await settings.get();

const defaultLanguage = languages?.find(l => l.default)?.key;

const duplicate = async () => {
const pages = await this.get({ language: defaultLanguage });
const savePages = pages.map(async _page => {
const page = { ..._page, language };
delete page._id;
delete page.__v;
return this.save(page);
});

return Promise.all(savePages);
};

return duplicate();
},

// TEST!!!
async removeLanguage(language: string) {
return model.delete({ language });
},

count: model.count.bind(model),
};
17 changes: 0 additions & 17 deletions app/api/pages/pagesModel.js

This file was deleted.

16 changes: 16 additions & 0 deletions app/api/pages/pagesModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mongoose from 'mongoose';
import { instanceModel } from 'api/odm';
import { PageType } from 'shared/types/pageType';

const propsWithDBSpecifics = {
creationDate: { type: Number, select: false },
user: { type: mongoose.Schema.Types.ObjectId, ref: 'users', select: false },
};

const mongoSchema = new mongoose.Schema(propsWithDBSpecifics, {
strict: false,
});

const Model = instanceModel<PageType>('pages', mongoSchema);

export default Model;
34 changes: 13 additions & 21 deletions app/api/pages/routes.js → app/api/pages/routes.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
import Joi from 'joi';
import { Application } from 'express';

import { PageSchema } from 'shared/types/pageSchema';
import { validation } from 'api/utils';

import needsAuthorization from '../auth/authMiddleware';
import pages from './pages';

export default app => {
export default (app: Application) => {
app.post(
'/api/pages',
needsAuthorization(['admin']),

needsAuthorization(),
validation.validateRequest({
type: 'object',
properties: {
body: PageSchema,
},
}),

validation.validateRequest(
Joi.object()
.keys({
_id: Joi.objectId(),
__v: Joi.number(),
sharedId: Joi.string(),
title: Joi.string().required(),
language: Joi.string(),
metadata: Joi.object().keys({
_id: Joi.objectId(),
content: Joi.string().allow(''),
script: Joi.string().allow(''),
}),
})
.required()
),

(req, res, next) =>
(req, res, next) => {
pages
.save(req.body, req.user, req.language)
.then(response => res.json(response))
.catch(next)
.catch(next);
}
);

app.get(
Expand Down
Loading