From bb500aa4ef67989965447b8e73fd720266cea0ed Mon Sep 17 00:00:00 2001 From: RohanSasne Date: Tue, 28 Mar 2023 22:38:42 +0530 Subject: [PATCH 1/9] BB-672: Rewrite entity folder using async/await --- src/server/routes/entity/publisher.js | 22 +++++----- src/server/routes/entity/work.js | 60 ++++++++++++++++----------- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/server/routes/entity/publisher.js b/src/server/routes/entity/publisher.js index d72634fdee..c99b10ea05 100644 --- a/src/server/routes/entity/publisher.js +++ b/src/server/routes/entity/publisher.js @@ -190,31 +190,31 @@ function _setPublisherTitle(res) { } -router.get('/:bbid', middleware.loadEntityRelationships, (req, res, next) => { - // Fetch editions +router.get('/:bbid', middleware.loadEntityRelationships, async (req, res, next) => { + try { + // Fetch editions const {Publisher} = req.app.locals.orm; const editionRelationsToFetch = [ 'defaultAlias', 'disambiguation', 'releaseEventSet.releaseEvents', 'identifierSet.identifiers.type', - 'editionFormat', - 'authorCredit.names' + 'editionFormat','authorCredit.names' ]; - const editionsPromise = - Publisher.forge({bbid: res.locals.entity.bbid}) + const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) .editions({withRelated: editionRelationsToFetch}); - return editionsPromise - .then((editions) => { + res.locals.entity.editions = editions.toJSON(); _setPublisherTitle(res); res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate); - return entityRoutes.displayEntity(req, res); - }) - .catch(next); + await entityRoutes.displayEntity(req, res); + } catch (err) { + next(err); + } }); + router.get('/:bbid/delete', auth.isAuthenticated, (req, res, next) => { if (!res.locals.entity.dataId) { return next(new ConflictError('This entity has already been deleted')); diff --git a/src/server/routes/entity/work.js b/src/server/routes/entity/work.js index 9337438376..a9c41b4fa5 100644 --- a/src/server/routes/entity/work.js +++ b/src/server/routes/entity/work.js @@ -92,37 +92,45 @@ router.get( '/create', auth.isAuthenticated, middleware.loadIdentifierTypes, middleware.loadLanguages, middleware.loadWorkTypes, middleware.loadRelationshipTypes, - (req, res, next) => { + async (req, res, next) => { const {Author, Edition} = req.app.locals.orm; let relationshipTypeId; let initialRelationshipIndex = 0; - const propsPromise = generateEntityProps( + const propsPromise = await generateEntityProps( 'work', req, res, {} ); if (req.query.author) { - propsPromise.author = - Author.forge({bbid: req.query.author}) - .fetch({require: false, withRelated: 'defaultAlias'}) - .then((data) => data && utils.entityToOption(data.toJSON())); + try { + const data = await Author.forge({bbid: req.query.author}) + .fetch({require: false, withRelated: 'defaultAlias'}); + propsPromise.author = data && utils.entityToOption(data.toJSON()); + } + catch (err) { + log.debug(err); + } } if (req.query.edition) { - propsPromise.edition = - Edition.forge({bbid: req.query.edition}) - .fetch({require: false, withRelated: 'defaultAlias'}) - .then((data) => data && utils.entityToOption(data.toJSON())); + try { + const data = await Edition.forge({bbid: req.query.edition}) + .fetch({require: false, withRelated: 'defaultAlias'}); + propsPromise.edition = data && utils.entityToOption(data.toJSON()); + } + catch (err) { + log.debug(err); + } } async function render(props) { if (props.author) { - // add initial ralationship with relationshipTypeId = 8 ( is written by ) + // add initial relationship with relationshipTypeId = 8 ( is written by ) relationshipTypeId = RelationshipTypes.AuthorWroteWork; addInitialRelationship(props, relationshipTypeId, initialRelationshipIndex++, props.author); } if (props.edition) { - // add initial ralationship with relationshipTypeId = 10 ( is contained in ) + // add initial relationship with relationshipTypeId = 10 ( is contained in ) relationshipTypeId = RelationshipTypes.EditionContainsWork; addInitialRelationship(props, relationshipTypeId, initialRelationshipIndex++, props.edition); } @@ -155,18 +163,24 @@ router.get( title: props.heading })); } - makePromiseFromObject(propsPromise) - .then(render) - .catch(next); + try { + const props = await makePromiseFromObject(propsPromise); + await render(props); + } + catch (err) { + next(err); + } } ); + router.post( '/create', entityRoutes.displayPreview, auth.isAuthenticatedForHandler, middleware.loadIdentifierTypes, middleware.loadLanguages, middleware.loadWorkTypes, middleware.loadRelationshipTypes, async (req, res, next) => { const {WorkType} = req.app.locals.orm; + try { const entity = await utils.parseInitialState(req, 'work'); if (entity.workSection?.type) { entity.workSection.type = await utils.getIdByField(WorkType, 'label', entity.workSection.type); @@ -174,26 +188,22 @@ router.post( if (entity.workSection) { entity.workSection = await utils.parseLanguages(entity.workSection, req.app.locals.orm); } - const propsPromise = generateEntityProps( + const props = await generateEntityProps( 'work', req, res, {}, () => entity ); - - function render(props) { const editorMarkup = entityEditorMarkup(props); const {markup} = editorMarkup; const updatedProps = editorMarkup.props; - - return res.send(target({ + res.send(target({ markup, props: escapeProps(updatedProps), script: '/js/entity-editor.js', title: props.heading })); - } - makePromiseFromObject(propsPromise) - .then(render) - .catch(next); - } + } catch (err) { + next(err); + } +} ); router.post('/create/handler', auth.isAuthenticatedForHandler, From 1855318ab73097aa2cc3b534da72e905d07290a1 Mon Sep 17 00:00:00 2001 From: Rohan Sasne Date: Tue, 28 Mar 2023 23:10:03 +0530 Subject: [PATCH 2/9] Update publisher.js --- src/server/routes/entity/publisher.js | 38 +++++++++++++-------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/server/routes/entity/publisher.js b/src/server/routes/entity/publisher.js index c99b10ea05..6ce83c98c8 100644 --- a/src/server/routes/entity/publisher.js +++ b/src/server/routes/entity/publisher.js @@ -191,27 +191,27 @@ function _setPublisherTitle(res) { router.get('/:bbid', middleware.loadEntityRelationships, async (req, res, next) => { - try { - // Fetch editions - const {Publisher} = req.app.locals.orm; - const editionRelationsToFetch = [ - 'defaultAlias', - 'disambiguation', - 'releaseEventSet.releaseEvents', - 'identifierSet.identifiers.type', - 'editionFormat','authorCredit.names' - ]; - const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) + try { + // Fetch editions + const {Publisher} = req.app.locals.orm; + const editionRelationsToFetch = [ + 'defaultAlias', + 'disambiguation', + 'releaseEventSet.releaseEvents', + 'identifierSet.identifiers.type', + 'editionFormat', 'authorCredit.names' + ]; + const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) .editions({withRelated: editionRelationsToFetch}); - - - res.locals.entity.editions = editions.toJSON(); - _setPublisherTitle(res); - res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate); - await entityRoutes.displayEntity(req, res); - } catch (err) { - next(err); + + res.locals.entity.editions = editions.toJSON(); + _setPublisherTitle(res); + res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate); + await entityRoutes.displayEntity(req, res); } + catch(err){ + next(err); + } }); From 6f4630a65c728e5e2c8e0367fb2698f9b88b74b2 Mon Sep 17 00:00:00 2001 From: Rohan Sasne Date: Tue, 28 Mar 2023 23:17:49 +0530 Subject: [PATCH 3/9] Update publisher.js --- src/server/routes/entity/publisher.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/server/routes/entity/publisher.js b/src/server/routes/entity/publisher.js index 6ce83c98c8..8af8e2c786 100644 --- a/src/server/routes/entity/publisher.js +++ b/src/server/routes/entity/publisher.js @@ -191,25 +191,24 @@ function _setPublisherTitle(res) { router.get('/:bbid', middleware.loadEntityRelationships, async (req, res, next) => { - try { - // Fetch editions - const {Publisher} = req.app.locals.orm; - const editionRelationsToFetch = [ - 'defaultAlias', - 'disambiguation', - 'releaseEventSet.releaseEvents', - 'identifierSet.identifiers.type', - 'editionFormat', 'authorCredit.names' - ]; - const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) - .editions({withRelated: editionRelationsToFetch}); - + try { + // Fetch editions + const {Publisher} = req.app.locals.orm; + const editionRelationsToFetch = [ + 'defaultAlias', + 'disambiguation', + 'releaseEventSet.releaseEvents', + 'identifierSet.identifiers.type', + 'editionFormat', 'authorCredit.names' + ]; + const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) + .editions({withRelated: editionRelationsToFetch}); res.locals.entity.editions = editions.toJSON(); _setPublisherTitle(res); res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate); await entityRoutes.displayEntity(req, res); } - catch(err){ + catch (err) { next(err); } }); From ab3d97aa1695a37999f009fc536880303f7138be Mon Sep 17 00:00:00 2001 From: Rohan Sasne Date: Tue, 28 Mar 2023 23:26:13 +0530 Subject: [PATCH 4/9] Update publisher.js --- src/server/routes/entity/publisher.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/routes/entity/publisher.js b/src/server/routes/entity/publisher.js index 8af8e2c786..415fc569ce 100644 --- a/src/server/routes/entity/publisher.js +++ b/src/server/routes/entity/publisher.js @@ -202,12 +202,12 @@ router.get('/:bbid', middleware.loadEntityRelationships, async (req, res, next) 'editionFormat', 'authorCredit.names' ]; const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) - .editions({withRelated: editionRelationsToFetch}); + .editions({withRelated: editionRelationsToFetch}); res.locals.entity.editions = editions.toJSON(); _setPublisherTitle(res); res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate); await entityRoutes.displayEntity(req, res); - } + } catch (err) { next(err); } From 9364f9f181c23fb77f75a37ed59e9fdabc63f924 Mon Sep 17 00:00:00 2001 From: Rohan Sasne Date: Tue, 28 Mar 2023 23:30:58 +0530 Subject: [PATCH 5/9] Update work.js --- src/server/routes/entity/work.js | 33 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/server/routes/entity/work.js b/src/server/routes/entity/work.js index a9c41b4fa5..726a0867bb 100644 --- a/src/server/routes/entity/work.js +++ b/src/server/routes/entity/work.js @@ -180,30 +180,31 @@ router.post( middleware.loadRelationshipTypes, async (req, res, next) => { const {WorkType} = req.app.locals.orm; - try { - const entity = await utils.parseInitialState(req, 'work'); - if (entity.workSection?.type) { - entity.workSection.type = await utils.getIdByField(WorkType, 'label', entity.workSection.type); - } - if (entity.workSection) { - entity.workSection = await utils.parseLanguages(entity.workSection, req.app.locals.orm); - } - const props = await generateEntityProps( - 'work', req, res, {}, () => entity - ); + try { + const entity = await utils.parseInitialState(req, 'work'); + if (entity.workSection?.type) { + entity.workSection.type = await utils.getIdByField(WorkType, 'label', entity.workSection.type); + } + if (entity.workSection) { + entity.workSection = await utils.parseLanguages(entity.workSection, req.app.locals.orm); + } + const props = await generateEntityProps( + 'work', req, res, {}, () => entity + ); const editorMarkup = entityEditorMarkup(props); const {markup} = editorMarkup; const updatedProps = editorMarkup.props; - res.send(target({ + res.send(target({ markup, props: escapeProps(updatedProps), script: '/js/entity-editor.js', title: props.heading })); - } catch (err) { - next(err); - } -} + } + catch (err) { + next(err); + } + } ); router.post('/create/handler', auth.isAuthenticatedForHandler, From 69f6468a48a8153b05f02fa631410c52820569b2 Mon Sep 17 00:00:00 2001 From: RohanSasne Date: Tue, 28 Mar 2023 23:37:20 +0530 Subject: [PATCH 6/9] Indentation Fixed --- src/server/routes/entity/publisher.js | 2 +- src/server/routes/entity/work.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/server/routes/entity/publisher.js b/src/server/routes/entity/publisher.js index 415fc569ce..6a4dd68601 100644 --- a/src/server/routes/entity/publisher.js +++ b/src/server/routes/entity/publisher.js @@ -209,7 +209,7 @@ router.get('/:bbid', middleware.loadEntityRelationships, async (req, res, next) await entityRoutes.displayEntity(req, res); } catch (err) { - next(err); + log.debug(err); } }); diff --git a/src/server/routes/entity/work.js b/src/server/routes/entity/work.js index 726a0867bb..b261efdb38 100644 --- a/src/server/routes/entity/work.js +++ b/src/server/routes/entity/work.js @@ -168,7 +168,7 @@ router.get( await render(props); } catch (err) { - next(err); + log.debug(err); } } ); @@ -202,7 +202,7 @@ router.post( })); } catch (err) { - next(err); + log.debug(err); } } ); From f2af26563ed6445f85286f41b95d26893c658296 Mon Sep 17 00:00:00 2001 From: Rohan Sasne Date: Thu, 18 May 2023 15:38:38 +0530 Subject: [PATCH 7/9] fix: Solve ESLint errors --- src/server/routes/entity/publisher.js | 38 +++++++++++++-------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/server/routes/entity/publisher.js b/src/server/routes/entity/publisher.js index 678b36f021..71306ef778 100644 --- a/src/server/routes/entity/publisher.js +++ b/src/server/routes/entity/publisher.js @@ -191,26 +191,24 @@ function _setPublisherTitle(res) { router.get('/:bbid', middleware.loadEntityRelationships, middleware.loadWikipediaExtract, async (req, res, next) => { - // Fetch editions - const {Publisher} = req.app.locals.orm; - const editionRelationsToFetch = [ - 'defaultAlias', - 'disambiguation', - 'releaseEventSet.releaseEvents', - 'identifierSet.identifiers.type', - 'editionFormat', - 'authorCredit.names' - ]; - const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) - .editions({withRelated: editionRelationsToFetch}); - - res.locals.entity.editions = editions.toJSON(); - _setPublisherTitle(res); - res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate); - await entityRoutes.displayEntity(req, res); -}) - - + // Fetch editions + const {Publisher} = req.app.locals.orm; + const editionRelationsToFetch = [ + 'defaultAlias', + 'disambiguation', + 'releaseEventSet.releaseEvents', + 'identifierSet.identifiers.type', + 'editionFormat', + 'authorCredit.names' + ]; + const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) + .editions({withRelated: editionRelationsToFetch}); + + res.locals.entity.editions = editions.toJSON(); + _setPublisherTitle(res); + res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate); + await entityRoutes.displayEntity(req, res); +}); router.get('/:bbid/delete', auth.isAuthenticated, (req, res, next) => { if (!res.locals.entity.dataId) { From 14084b0e079584e662cda9713dec8833fe1e2846 Mon Sep 17 00:00:00 2001 From: Rohan Sasne Date: Thu, 18 May 2023 15:52:47 +0530 Subject: [PATCH 8/9] fix: Solve ESLint errors --- src/server/routes/entity/publisher.js | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/server/routes/entity/publisher.js b/src/server/routes/entity/publisher.js index 71306ef778..36b7c3d095 100644 --- a/src/server/routes/entity/publisher.js +++ b/src/server/routes/entity/publisher.js @@ -191,23 +191,23 @@ function _setPublisherTitle(res) { router.get('/:bbid', middleware.loadEntityRelationships, middleware.loadWikipediaExtract, async (req, res, next) => { - // Fetch editions - const {Publisher} = req.app.locals.orm; - const editionRelationsToFetch = [ - 'defaultAlias', - 'disambiguation', - 'releaseEventSet.releaseEvents', - 'identifierSet.identifiers.type', - 'editionFormat', - 'authorCredit.names' - ]; - const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) - .editions({withRelated: editionRelationsToFetch}); - - res.locals.entity.editions = editions.toJSON(); - _setPublisherTitle(res); - res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate); - await entityRoutes.displayEntity(req, res); + // Fetch editions + const {Publisher} = req.app.locals.orm; + const editionRelationsToFetch = [ + 'defaultAlias', + 'disambiguation', + 'releaseEventSet.releaseEvents', + 'identifierSet.identifiers.type', + 'editionFormat', + 'authorCredit.names' + ]; + const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) + .editions({withRelated: editionRelationsToFetch}); + + res.locals.entity.editions = editions.toJSON(); + _setPublisherTitle(res); + res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate); + await entityRoutes.displayEntity(req, res); }); router.get('/:bbid/delete', auth.isAuthenticated, (req, res, next) => { From fc5f27a19b7a5368df43f65e3a7e407782877520 Mon Sep 17 00:00:00 2001 From: Rohan Sasne Date: Thu, 18 May 2023 15:58:48 +0530 Subject: [PATCH 9/9] fix: Solve ESLint errors --- src/server/routes/entity/publisher.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/server/routes/entity/publisher.js b/src/server/routes/entity/publisher.js index 36b7c3d095..bdff3b8a4e 100644 --- a/src/server/routes/entity/publisher.js +++ b/src/server/routes/entity/publisher.js @@ -192,18 +192,17 @@ function _setPublisherTitle(res) { router.get('/:bbid', middleware.loadEntityRelationships, middleware.loadWikipediaExtract, async (req, res, next) => { // Fetch editions - const {Publisher} = req.app.locals.orm; - const editionRelationsToFetch = [ + const {Publisher} = req.app.locals.orm; + const editionRelationsToFetch = [ 'defaultAlias', - 'disambiguation', - 'releaseEventSet.releaseEvents', - 'identifierSet.identifiers.type', - 'editionFormat', - 'authorCredit.names' + 'disambiguation', + 'releaseEventSet.releaseEvents', + 'identifierSet.identifiers.type', + 'editionFormat', + 'authorCredit.names' ]; const editions = await Publisher.forge({bbid: res.locals.entity.bbid}) - .editions({withRelated: editionRelationsToFetch}); - + .editions({withRelated: editionRelationsToFetch}); res.locals.entity.editions = editions.toJSON(); _setPublisherTitle(res); res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate);