@@ -2,7 +2,6 @@ const mongoose = require('mongoose')
22const logger = require ( '../../middleware/logger' )
33const { getConstants } = require ( '../../constants' )
44const RegistryOrg = require ( '../../model/registry-org' )
5- const RegistryUser = require ( '../../model/registry-user' )
65const errors = require ( './error' )
76const error = new errors . RegistryOrgControllerError ( )
87const validateUUID = require ( 'uuid' ) . validate
@@ -21,7 +20,10 @@ const validateUUID = require('uuid').validate
2120 */
2221async function getAllOrgs ( req , res , next ) {
2322 try {
23+ const session = await mongoose . startSession ( )
24+ const repo = req . ctx . repositories . getBaseOrgRepository ( )
2425 const CONSTANTS = getConstants ( )
26+ let returnValue
2527
2628 // temporary measure to allow tests to work after fixing #920
2729 // tests required changing the global limit to force pagination
@@ -32,29 +34,15 @@ async function getAllOrgs (req, res, next) {
3234 const options = CONSTANTS . PAGINATOR_OPTIONS
3335 options . sort = { short_name : 'asc' }
3436 options . page = req . ctx . query . page ? parseInt ( req . ctx . query . page ) : CONSTANTS . PAGINATOR_PAGE // if 'page' query parameter is not defined, set 'page' to the default page value
35- const repo = req . ctx . repositories . getRegistryOrgRepository ( )
36- const agt = setAggregateOrgObj ( { } )
37- const pg = await repo . aggregatePaginate ( agt , options )
38-
39- await RegistryOrg . populateOverseesAndReportsTo ( pg . itemsList )
40- await RegistryUser . populateUsers ( pg . itemsList )
41- await RegistryUser . populateAdditionalContactUsers ( pg . itemsList )
42- await RegistryUser . populateAdmins ( pg . itemsList )
43- // Update UUIDS to objects
44-
45- const payload = { orgs : pg . itemsList }
46-
47- if ( pg . itemCount >= CONSTANTS . PAGINATOR_OPTIONS . limit ) {
48- payload . totalCount = pg . itemCount
49- payload . itemsPerPage = pg . itemsPerPage
50- payload . pageCount = pg . pageCount
51- payload . currentPage = pg . currentPage
52- payload . prevPage = pg . prevPage
53- payload . nextPage = pg . nextPage
37+
38+ try {
39+ returnValue = await repo . getAllOrgs ( { ...options , session } )
40+ } finally {
41+ await session . endSession ( )
5442 }
5543
56- logger . info ( { uuid : req . ctx . uuid , message : 'The org information was sent to the secretariat user.' } )
57- return res . status ( 200 ) . json ( payload )
44+ logger . info ( { uuid : req . ctx . uuid , message : 'The orgs were sent to the user.' } )
45+ return res . status ( 200 ) . json ( returnValue )
5846 } catch ( err ) {
5947 next ( err )
6048 }
@@ -74,36 +62,39 @@ async function getAllOrgs (req, res, next) {
7462 */
7563async function getOrg ( req , res , next ) {
7664 try {
77- const repo = req . ctx . repositories . getRegistryOrgRepository ( )
65+ const session = await mongoose . startSession ( )
66+ const repo = req . ctx . repositories . getBaseOrgRepository ( )
7867 // User passed in parameter to filter for
7968 const identifier = req . ctx . params . identifier
80- const orgShortName = req . ctx . org
81- const isSecretariat = await repo . isSecretariat ( orgShortName )
82- const org = await repo . findOneByShortName ( orgShortName )
83- let requestingUserOrgIdentifier = orgShortName
84- let agt = setAggregateOrgObj ( { short_name : identifier } )
85-
86- if ( validateUUID ( identifier ) ) {
87- requestingUserOrgIdentifier = org . UUID
88- agt = setAggregateOrgObj ( { UUID : identifier } )
89- }
69+ const requesterOrgShortName = req . ctx . org
70+ const identifierIsUUID = validateUUID ( identifier )
71+ let returnValue
9072
91- if ( requestingUserOrgIdentifier !== identifier && ! isSecretariat ) {
92- logger . info ( { uuid : req . ctx . uuid , message : identifier + ' organization can only be viewed by the users of the same organization or the Secretariat.' } )
93- return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
94- }
73+ try {
74+ session . startTransaction ( )
75+ const requesterOrg = await repo . findOneByShortName ( requesterOrgShortName , { session } )
76+ const requesterOrgIdentifier = identifierIsUUID ? requesterOrg . UUID : requesterOrgShortName
77+ const isSecretariat = await repo . isSecretariat ( requesterOrg , { session } )
9578
96- let result = await repo . aggregate ( agt )
97- result = result . length > 0 ? result [ 0 ] : null
98- // TODO: We need real error messages here pls and thanks
79+ if ( requesterOrgIdentifier !== identifier && ! isSecretariat ) {
80+ logger . info ( { uuid : req . ctx . uuid , message : identifier + ' organization can only be viewed by the users of the same organization or the Secretariat.' } )
81+ return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
82+ }
9983
100- if ( ! result ) {
84+ returnValue = await repo . getOrg ( identifier , identifierIsUUID , { session } )
85+ } catch ( error ) {
86+ await session . abortTransaction ( )
87+ throw error
88+ } finally {
89+ await session . endSession ( )
90+ }
91+ if ( ! returnValue ) { // an empty result can only happen if the requestor is the Secretariat
10192 logger . info ( { uuid : req . ctx . uuid , message : identifier + ' organization does not exist.' } )
10293 return res . status ( 404 ) . json ( error . orgDne ( identifier , 'identifier' , 'path' ) )
10394 }
10495
105- logger . info ( { uuid : req . ctx . uuid , message : identifier + ' org was sent to the user.' , org : result } )
106- return res . status ( 200 ) . json ( result )
96+ logger . info ( { uuid : req . ctx . uuid , message : identifier + ' organization was sent to the user.' , org : returnValue } )
97+ return res . status ( 200 ) . json ( returnValue )
10798 } catch ( err ) {
10899 next ( err )
109100 }
0 commit comments