@@ -20,10 +20,9 @@ const validateUUID = require('uuid').validate
2020 */
2121async function getAllOrgs ( req , res , next ) {
2222 try {
23- const session = await mongoose . startSession ( )
2423 const repo = req . ctx . repositories . getBaseOrgRepository ( )
2524 const conversationRepo = req . ctx . repositories . getConversationRepository ( )
26- const isSecretariat = await repo . isSecretariatByShortName ( req . ctx . org , { session } )
25+ const isSecretariat = await repo . isSecretariatByShortName ( req . ctx . org )
2726 const CONSTANTS = getConstants ( )
2827 let returnValue
2928
@@ -38,14 +37,17 @@ async function getAllOrgs (req, res, next) {
3837 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
3938
4039 try {
41- returnValue = await repo . getAllOrgs ( { ...options , session } )
40+ returnValue = await repo . getAllOrgs ( { ...options } )
4241 // fetch conversations
4342 for ( let i = 0 ; i < returnValue . organizations . length ; i ++ ) {
44- const conversation = await conversationRepo . getAllByTargetUUID ( returnValue . organizations [ i ] . UUID , isSecretariat , { session } )
43+ const conversation = await conversationRepo . getAllByTargetUUID ( returnValue . organizations [ i ] . UUID , isSecretariat )
4544 returnValue . organizations [ i ] . conversation = conversation ?. length ? conversation : undefined
4645 }
47- } finally {
48- await session . endSession ( )
46+ } catch ( error ) {
47+ // Handle the specific error thrown by BaseOrgRepository.createOrg
48+ if ( error . message && error . message . includes ( 'Unknown Org type requested' ) ) {
49+ return res . status ( 400 ) . json ( { message : error . message } )
50+ }
4951 }
5052
5153 logger . info ( { uuid : req . ctx . uuid , message : 'The orgs were sent to the user.' } )
@@ -69,7 +71,6 @@ async function getAllOrgs (req, res, next) {
6971 */
7072async function getOrg ( req , res , next ) {
7173 try {
72- const session = await mongoose . startSession ( )
7374 const repo = req . ctx . repositories . getBaseOrgRepository ( )
7475 const conversationRepo = req . ctx . repositories . getConversationRepository ( )
7576 // User passed in parameter to filter for
@@ -79,32 +80,28 @@ async function getOrg (req, res, next) {
7980 let returnValue
8081
8182 try {
82- session . startTransaction ( )
83- const requesterOrg = await repo . findOneByShortName ( requesterOrgShortName , { session } )
83+ const requesterOrg = await repo . findOneByShortName ( requesterOrgShortName )
8484 const requesterOrgIdentifier = identifierIsUUID ? requesterOrg . UUID : requesterOrgShortName
85- const isSecretariat = await repo . isSecretariat ( requesterOrg , { session } )
85+ const isSecretariat = await repo . isSecretariat ( requesterOrg )
8686
8787 if ( requesterOrgIdentifier !== identifier && ! isSecretariat ) {
8888 logger . info ( { uuid : req . ctx . uuid , message : identifier + ' organization can only be viewed by the users of the same organization or the Secretariat.' } )
8989 return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
9090 }
9191
92- returnValue = await repo . getOrg ( identifier , identifierIsUUID , { session } )
92+ returnValue = await repo . getOrg ( identifier , identifierIsUUID )
9393
9494 if ( returnValue ) {
9595 // fetch conversation
96- const conversation = await conversationRepo . getAllByTargetUUID ( returnValue . UUID , isSecretariat , { session } )
96+ const conversation = await conversationRepo . getAllByTargetUUID ( returnValue . UUID , isSecretariat )
9797 returnValue . conversation = conversation ?. length ? _ . map ( conversation , c => _ . omit ( c , [ '__v' , '_id' , 'UUID' , 'previous_conversation_uuid' , 'next_conversation_uuid' , 'target_uuid' , 'visibility' ] ) ) : undefined
9898 }
9999 } catch ( error ) {
100- await session . abortTransaction ( )
101100 // Handle the specific error thrown by BaseOrgRepository.createOrg
102101 if ( error . message && error . message . includes ( 'Unknown Org type requested' ) ) {
103102 return res . status ( 400 ) . json ( { message : error . message } )
104103 }
105104 throw error
106- } finally {
107- await session . endSession ( )
108105 }
109106 if ( ! returnValue ) { // an empty result can only happen if the requestor is the Secretariat
110107 logger . info ( { uuid : req . ctx . uuid , message : identifier + ' organization does not exist.' } )
@@ -132,7 +129,7 @@ async function getOrg (req, res, next) {
132129 */
133130async function createOrg ( req , res , next ) {
134131 try {
135- const session = await mongoose . startSession ( )
132+ const session = await mongoose . startSession ( { causalConsistency : false } )
136133 const repo = req . ctx . repositories . getBaseOrgRepository ( )
137134 const body = req . ctx . body
138135 const isSecretariat = await repo . isSecretariatByShortName ( req . ctx . org , { session } )
@@ -230,7 +227,7 @@ async function createOrg (req, res, next) {
230227 */
231228async function updateOrg ( req , res , next ) {
232229 try {
233- const session = await mongoose . startSession ( )
230+ const session = await mongoose . startSession ( { causalConsistency : false } )
234231 const shortName = req . ctx . params . shortname
235232 const repo = req . ctx . repositories . getBaseOrgRepository ( )
236233 const userRepo = req . ctx . repositories . getBaseUserRepository ( )
@@ -316,15 +313,18 @@ async function updateOrg (req, res, next) {
316313 }
317314 }
318315
316+ // Update Org full will cause a write to the Conversations collection, to avoid a read-after-write issue, we need to get the previous conversation data first
317+ const previousConversation = await conversationRepo . getAllByTargetUUID ( await repo . getOrgUUID ( shortName , { session } ) , isSecretariat , { session } ) || [ ]
318+
319319 updatedOrg = await repo . updateOrgFull ( shortName , req . ctx . body , { session } , false , requestingUser . UUID , isAdmin , isSecretariat )
320320 jointApprovalRequired = _ . get ( updatedOrg , 'joint_approval_required' , false )
321321 _ . unset ( updatedOrg , 'joint_approval_required' )
322-
323- await session . commitTransaction ( )
324- session . startTransaction ( )
325- // Checking for existing Conversations
326- const existingConversations = await conversationRepo . getAllByTargetUUID ( updatedOrg . UUID , isSecretariat , { session } ) || [ ]
327- updatedOrg . conversation = existingConversations . map ( c => _ . omit ( c , [ '__v' , '_id' , 'previous_conversation_uuid' , 'next_conversation_uuid' ] ) )
322+ // append previous conversations to any conversations that are in the org already
323+ const currentConversations = Array . isArray ( updatedOrg ?. conversation ) ? updatedOrg . conversation : [ ]
324+ const prevConversations = Array . isArray ( previousConversation ) ? previousConversation : [ ]
325+ if ( updatedOrg ) {
326+ updatedOrg . conversation = [ ... currentConversations , ... prevConversations ] . map ( c => _ . omit ( c , [ '__v' , '_id' , 'previous_conversation_uuid' , 'next_conversation_uuid' ] ) )
327+ }
328328
329329 await session . commitTransaction ( )
330330 } catch ( updateErr ) {
@@ -385,7 +385,7 @@ async function updateOrg (req, res, next) {
385385 */
386386async function deleteOrg ( req , res , next ) {
387387 try {
388- const session = await mongoose . startSession ( )
388+ const session = await mongoose . startSession ( { causalConsistency : false } )
389389 const repo = req . ctx . repositories . getBaseOrgRepository ( )
390390 const shortName = req . ctx . params . identifier
391391
@@ -498,7 +498,7 @@ async function getUsers (req, res, next) {
498498 * Called by POST /api/registryOrg/:shortname/user
499499 */
500500async function createUserByOrg ( req , res , next ) {
501- const session = await mongoose . startSession ( )
501+ const session = await mongoose . startSession ( { causalConsistency : false } )
502502 try {
503503 const body = req . ctx . body
504504 const userRepo = req . ctx . repositories . getBaseUserRepository ( )
0 commit comments