@@ -19,84 +19,38 @@ async function getAllConversations (req, res, next) {
1919 return res . status ( 200 ) . json ( response )
2020}
2121
22- async function getConversationsForOrg ( req , res , next ) {
23- const session = await mongoose . startSession ( )
24-
25- try {
26- session . startTransaction ( )
27-
28- const repo = req . ctx . repositories . getConversationRepository ( )
29- const orgRepo = req . ctx . repositories . getBaseOrgRepository ( )
30- const requesterOrg = req . ctx . org
31- const targetOrgUUID = req . params . uuid
32-
33- // Make sure target org matches user org if not secretariat
34- const isSecretariat = await orgRepo . isSecretariatByShortName ( requesterOrg , { session } )
35- const requesterOrgUUID = await orgRepo . getOrgUUID ( requesterOrg , { session } )
36- if ( ! isSecretariat && ( requesterOrgUUID !== targetOrgUUID ) ) {
37- return res . status ( 400 ) . json ( { message : 'User is not secretariat or admin for target org' } )
38- }
39-
40- // temporary measure to allow tests to work after fixing #920
41- // tests required changing the global limit to force pagination
42- if ( req . TEST_PAGINATOR_LIMIT ) {
43- CONSTANTS . PAGINATOR_OPTIONS . limit = req . TEST_PAGINATOR_LIMIT
44- }
45-
46- const options = CONSTANTS . PAGINATOR_OPTIONS
47- options . sort = { posted_at : 'desc' }
22+ async function getConversationsForTargetUUID ( req , res , next ) {
23+ const repo = req . ctx . repositories . getConversationRepository ( )
24+ const targetUUID = req . params . uuid
4825
49- const response = await repo . getAllByTargetUUID ( targetOrgUUID , options )
50- await session . commitTransaction ( )
51- return res . status ( 200 ) . json ( response )
52- } catch ( err ) {
53- if ( session && session . inTransaction ( ) ) {
54- await session . abortTransaction ( )
55- }
56- next ( err )
57- } finally {
58- if ( session && session . id ) { // Check if session is still valid before trying to end
59- try {
60- await session . endSession ( )
61- } catch ( sessionEndError ) {
62- logger . error ( { uuid : req . ctx . uuid , message : 'Error ending session in finally block' , error : sessionEndError } )
63- }
64- }
65- }
26+ const response = await repo . getAllByTargetUUID ( targetUUID )
27+ return res . status ( 200 ) . json ( response )
6628}
6729
68- async function createConversationForOrg ( req , res , next ) {
30+ async function createConversationForTargetUUID ( req , res , next ) {
6931 const session = await mongoose . startSession ( )
7032
7133 try {
7234 session . startTransaction ( )
7335
7436 const repo = req . ctx . repositories . getConversationRepository ( )
75- const orgRepo = req . ctx . repositories . getBaseOrgRepository ( )
7637 const userRepo = req . ctx . repositories . getBaseUserRepository ( )
7738 const requesterOrg = req . ctx . org
7839 const requesterUsername = req . ctx . user
79- const targetOrgUUID = req . params . uuid
40+ const targetUUID = req . params . uuid
8041 const body = req . body
8142
82- // Make sure target org matches user org if not secretariat
83- const isSecretariat = await orgRepo . isSecretariatByShortName ( requesterOrg , { session } )
84- const requesterOrgUUID = await orgRepo . getOrgUUID ( requesterOrg , { session } )
85- if ( ! isSecretariat && ( requesterOrgUUID !== targetOrgUUID ) ) {
86- return res . status ( 400 ) . json ( { message : 'User is not secretariat or admin for target org' } )
87- }
88-
8943 const user = await userRepo . findOneByUsernameAndOrgShortname ( requesterUsername , requesterOrg , { session } )
9044
9145 if ( ! body . body ) {
9246 return res . status ( 400 ) . json ( { message : 'Missing required field body' } )
9347 }
9448
9549 const conversationBody = {
96- target_uuid : targetOrgUUID ,
50+ target_uuid : targetUUID ,
9751 author_id : user . UUID ,
9852 author_name : [ user . name . first , user . name . last ] . join ( ' ' ) ,
99- author_role : isSecretariat ? 'Secretariat' : 'Partner ',
53+ author_role : 'Secretariat' ,
10054 visibility : body . visibility ? body . visibility . toLowerCase ( ) : 'private' ,
10155 body : body . body
10256 }
@@ -129,20 +83,20 @@ async function createConversationForOrg (req, res, next) {
12983
13084async function updateMessage ( req , res , next ) {
13185 const repo = req . ctx . repositories . getConversationRepository ( )
132- const targetOrgUUID = req . params . uuid
86+ const targetUUID = req . params . uuid
13387 const body = req . body
13488
13589 if ( ! body . body ) {
13690 return res . status ( 400 ) . json ( { message : 'Missing required field body' } )
13791 }
13892
139- const result = await repo . updateConversation ( body , targetOrgUUID )
93+ const result = await repo . updateConversation ( body , targetUUID )
14094 return res . status ( 200 ) . json ( result )
14195}
14296
14397module . exports = {
14498 getAllConversations,
145- getConversationsForOrg ,
146- createConversationForOrg ,
99+ getConversationsForTargetUUID ,
100+ createConversationForTargetUUID ,
147101 updateMessage
148102}
0 commit comments