@@ -71,7 +71,6 @@ async function getAllOrgs (req, res, next) {
7171 */
7272async function getOrg ( req , res , next ) {
7373 try {
74- const session = await mongoose . startSession ( )
7574 const repo = req . ctx . repositories . getBaseOrgRepository ( )
7675 const conversationRepo = req . ctx . repositories . getConversationRepository ( )
7776 // User passed in parameter to filter for
@@ -81,32 +80,28 @@ async function getOrg (req, res, next) {
8180 let returnValue
8281
8382 try {
84- session . startTransaction ( )
85- const requesterOrg = await repo . findOneByShortName ( requesterOrgShortName , { session } )
83+ const requesterOrg = await repo . findOneByShortName ( requesterOrgShortName )
8684 const requesterOrgIdentifier = identifierIsUUID ? requesterOrg . UUID : requesterOrgShortName
87- const isSecretariat = await repo . isSecretariat ( requesterOrg , { session } )
85+ const isSecretariat = await repo . isSecretariat ( requesterOrg )
8886
8987 if ( requesterOrgIdentifier !== identifier && ! isSecretariat ) {
9088 logger . info ( { uuid : req . ctx . uuid , message : identifier + ' organization can only be viewed by the users of the same organization or the Secretariat.' } )
9189 return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
9290 }
9391
94- returnValue = await repo . getOrg ( identifier , identifierIsUUID , { session } )
92+ returnValue = await repo . getOrg ( identifier , identifierIsUUID )
9593
9694 if ( returnValue ) {
9795 // fetch conversation
98- const conversation = await conversationRepo . getAllByTargetUUID ( returnValue . UUID , isSecretariat , { session } )
96+ const conversation = await conversationRepo . getAllByTargetUUID ( returnValue . UUID , isSecretariat )
9997 returnValue . conversation = conversation ?. length ? _ . map ( conversation , c => _ . omit ( c , [ '__v' , '_id' , 'UUID' , 'previous_conversation_uuid' , 'next_conversation_uuid' , 'target_uuid' , 'visibility' ] ) ) : undefined
10098 }
10199 } catch ( error ) {
102- await session . abortTransaction ( )
103100 // Handle the specific error thrown by BaseOrgRepository.createOrg
104101 if ( error . message && error . message . includes ( 'Unknown Org type requested' ) ) {
105102 return res . status ( 400 ) . json ( { message : error . message } )
106103 }
107104 throw error
108- } finally {
109- await session . endSession ( )
110105 }
111106 if ( ! returnValue ) { // an empty result can only happen if the requestor is the Secretariat
112107 logger . info ( { uuid : req . ctx . uuid , message : identifier + ' organization does not exist.' } )
@@ -134,7 +129,7 @@ async function getOrg (req, res, next) {
134129 */
135130async function createOrg ( req , res , next ) {
136131 try {
137- const session = await mongoose . startSession ( )
132+ const session = await mongoose . startSession ( { causalConsistency : false } )
138133 const repo = req . ctx . repositories . getBaseOrgRepository ( )
139134 const body = req . ctx . body
140135 const isSecretariat = await repo . isSecretariatByShortName ( req . ctx . org , { session } )
@@ -232,7 +227,7 @@ async function createOrg (req, res, next) {
232227 */
233228async function updateOrg ( req , res , next ) {
234229 try {
235- const session = await mongoose . startSession ( )
230+ const session = await mongoose . startSession ( { causalConsistency : false } )
236231 const shortName = req . ctx . params . shortname
237232 const repo = req . ctx . repositories . getBaseOrgRepository ( )
238233 const userRepo = req . ctx . repositories . getBaseUserRepository ( )
@@ -318,15 +313,18 @@ async function updateOrg (req, res, next) {
318313 }
319314 }
320315
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+
321319 updatedOrg = await repo . updateOrgFull ( shortName , req . ctx . body , { session } , false , requestingUser . UUID , isAdmin , isSecretariat )
322320 jointApprovalRequired = _ . get ( updatedOrg , 'joint_approval_required' , false )
323321 _ . unset ( updatedOrg , 'joint_approval_required' )
324-
325- await session . commitTransaction ( )
326- session . startTransaction ( )
327- // Checking for existing Conversations
328- const existingConversations = await conversationRepo . getAllByTargetUUID ( updatedOrg . UUID , isSecretariat , { session } ) || [ ]
329- 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+ }
330328
331329 await session . commitTransaction ( )
332330 } catch ( updateErr ) {
@@ -387,7 +385,7 @@ async function updateOrg (req, res, next) {
387385 */
388386async function deleteOrg ( req , res , next ) {
389387 try {
390- const session = await mongoose . startSession ( )
388+ const session = await mongoose . startSession ( { causalConsistency : false } )
391389 const repo = req . ctx . repositories . getBaseOrgRepository ( )
392390 const shortName = req . ctx . params . identifier
393391
@@ -500,7 +498,7 @@ async function getUsers (req, res, next) {
500498 * Called by POST /api/registryOrg/:shortname/user
501499 */
502500async function createUserByOrg ( req , res , next ) {
503- const session = await mongoose . startSession ( )
501+ const session = await mongoose . startSession ( { causalConsistency : false } )
504502 try {
505503 const body = req . ctx . body
506504 const userRepo = req . ctx . repositories . getBaseUserRepository ( )
0 commit comments