|
| 1 | +const mongoose = require('mongoose') |
| 2 | +const logger = require('../../middleware/logger') |
| 3 | +const getConstants = require('../../../src/constants').getConstants |
| 4 | +const CONSTANTS = getConstants() |
| 5 | + |
| 6 | +async function getAllConversations (req, res, next) { |
| 7 | + const repo = req.ctx.repositories.getConversationRepository() |
| 8 | + |
| 9 | + // temporary measure to allow tests to work after fixing #920 |
| 10 | + // tests required changing the global limit to force pagination |
| 11 | + if (req.TEST_PAGINATOR_LIMIT) { |
| 12 | + CONSTANTS.PAGINATOR_OPTIONS.limit = req.TEST_PAGINATOR_LIMIT |
| 13 | + } |
| 14 | + |
| 15 | + const options = CONSTANTS.PAGINATOR_OPTIONS |
| 16 | + options.sort = { posted_at: 'desc' } |
| 17 | + |
| 18 | + const response = await repo.getAll(options) |
| 19 | + return res.status(200).json(response) |
| 20 | +} |
| 21 | + |
| 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' } |
| 48 | + |
| 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 | + } |
| 66 | +} |
| 67 | + |
| 68 | +async function createConversationForOrg (req, res, next) { |
| 69 | + const session = await mongoose.startSession() |
| 70 | + |
| 71 | + try { |
| 72 | + session.startTransaction() |
| 73 | + |
| 74 | + const repo = req.ctx.repositories.getConversationRepository() |
| 75 | + const orgRepo = req.ctx.repositories.getBaseOrgRepository() |
| 76 | + const userRepo = req.ctx.repositories.getBaseUserRepository() |
| 77 | + const requesterOrg = req.ctx.org |
| 78 | + const requesterUsername = req.ctx.user |
| 79 | + const targetOrgUUID = req.params.uuid |
| 80 | + const body = req.body |
| 81 | + |
| 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 | + |
| 89 | + const user = await userRepo.findOneByUsernameAndOrgShortname(requesterUsername, requesterOrg, { session }) |
| 90 | + |
| 91 | + if (!body.body) { |
| 92 | + return res.status(400).json({ message: 'Missing required field body' }) |
| 93 | + } |
| 94 | + |
| 95 | + const conversationBody = { |
| 96 | + target_uuid: targetOrgUUID, |
| 97 | + author_id: user.UUID, |
| 98 | + author_name: [user.name.first, user.name.last].join(' '), |
| 99 | + author_role: isSecretariat ? 'Secretariat' : 'Partner', |
| 100 | + visibility: body.visibility ? body.visibility.toLowerCase() : 'private', |
| 101 | + body: body.body |
| 102 | + } |
| 103 | + const result = await repo.createConversation(conversationBody, { session }) |
| 104 | + await session.commitTransaction() |
| 105 | + if (!result) { |
| 106 | + return res.status(500).json({ message: 'Failed to create conversation' }) |
| 107 | + } |
| 108 | + return res.status(200).json(result) |
| 109 | + } catch (err) { |
| 110 | + if (session && session.inTransaction()) { |
| 111 | + await session.abortTransaction() |
| 112 | + } |
| 113 | + next(err) |
| 114 | + } finally { |
| 115 | + if (session && session.id) { |
| 116 | + // Check if session is still valid before trying to end |
| 117 | + try { |
| 118 | + await session.endSession() |
| 119 | + } catch (sessionEndError) { |
| 120 | + logger.error({ |
| 121 | + uuid: req.ctx.uuid, |
| 122 | + message: 'Error ending session in finally block', |
| 123 | + error: sessionEndError |
| 124 | + }) |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +async function updateMessage (req, res, next) { |
| 131 | + const repo = req.ctx.repositories.getConversationRepository() |
| 132 | + const targetOrgUUID = req.params.uuid |
| 133 | + const body = req.body |
| 134 | + |
| 135 | + if (!body.body) { |
| 136 | + return res.status(400).json({ message: 'Missing required field body' }) |
| 137 | + } |
| 138 | + |
| 139 | + const result = await repo.updateConversation(body, targetOrgUUID) |
| 140 | + return res.status(200).json(result) |
| 141 | +} |
| 142 | + |
| 143 | +module.exports = { |
| 144 | + getAllConversations, |
| 145 | + getConversationsForOrg, |
| 146 | + createConversationForOrg, |
| 147 | + updateMessage |
| 148 | +} |
0 commit comments