Skip to content

Commit

Permalink
Merge pull request #39 from bcgov/ofmcc-522-view-conversation-thread
Browse files Browse the repository at this point in the history
Ofmcc 522 view conversation thread
  • Loading branch information
jgstorey authored Nov 17, 2023
2 parents e49db45 + a101668 commit cacb3e1
Show file tree
Hide file tree
Showing 15 changed files with 964 additions and 206 deletions.
49 changes: 41 additions & 8 deletions backend/src/components/message.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'
const { getOperation, patchOperationWithObjectId, postOperation } = require('./utils')
const { MappableObjectForFront, MappableObjectForBack } = require('../util/mapping/MappableObject')
const { AssistanceRequestMappings, AssistanceRequestFacilityMappings } = require('../util/mapping/Mappings')
const { AssistanceRequestMappings, AssistanceRequestFacilityMappings, AssistanceRequestConversationMappings } = require('../util/mapping/Mappings')
const HttpStatus = require('http-status-codes')
const { ASSISTANCE_REQUEST_STATUS_CODES } = require('../util/constants')
const log = require('./logger')
Expand Down Expand Up @@ -61,7 +61,7 @@ function mapAssistanceRequestObjectForBack(data) {

async function createAssistanceRequest(req, res) {
try {
let payload = mapAssistanceRequestObjectForBack(req.body)
const payload = mapAssistanceRequestObjectForBack(req.body)
let response = await postOperation('ofm_assistance_requests?$select=ofm_name,ofm_assistance_requestid', payload)
response = new MappableObjectForFront(response, AssistanceRequestMappings).toJSON()
return res.status(HttpStatus.OK).json(response)
Expand All @@ -70,12 +70,26 @@ async function createAssistanceRequest(req, res) {
}
}

async function replyToAssistanceRequest(req, res) {
try {
const payload = `{
"ofm_message": "${req.body?.message}",
"[email protected]": "/ofm_assistance_requests(${req.body?.assistanceRequestId})"
}`
const response = await postOperation('ofm_conversations', payload)
return res.status(HttpStatus.OK).json(response)
} catch (e) {
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(e.data ? e.data : e?.status)
}
}

async function getAssistanceRequests(req, res) {
try {
log.debug('getAssistanceRequests: ', req.params.contactId)
let assistanceRequests = []
let operation = `ofm_assistance_requests?$select=modifiedon,ofm_assistance_requestid,ofm_last_opened_time,ofm_name,_ofm_request_category_value,ofm_subject,statecode,statuscode,ofm_is_read&$expand=ofm_facility_request_request($select=_ofm_facility_value),ofm_conversation_request($select=modifiedon)&$filter=(_ofm_contact_value eq ${req.params.contactId}) and (ofm_facility_request_request/any(o1:(o1/ofm_facility_requestid ne null))) and (ofm_conversation_request/any(o2:(o2/ofm_conversationid ne null)))`
log.info('operation: ', operation)
let response = await getOperation(operation)
const operation = `ofm_assistance_requests?$select=modifiedon,ofm_assistance_requestid,ofm_last_opened_time,ofm_name,_ofm_request_category_value,ofm_subject,statecode,statuscode,ofm_is_read&$expand=ofm_facility_request_request($select=_ofm_facility_value),ofm_conversation_request($select=modifiedon)&$filter=(_ofm_contact_value eq ${req.params.contactId}) and (ofm_facility_request_request/any(o1:(o1/ofm_facility_requestid ne null))) and (ofm_conversation_request/any(o2:(o2/ofm_conversationid ne null)))`
log.debug('operation: ', operation)
const response = await getOperation(operation)
response?.value?.forEach((item) => assistanceRequests.push(mapAssistanceRequestObjectForFront(item)))
return res.status(HttpStatus.OK).json(assistanceRequests)
} catch (e) {
Expand All @@ -86,7 +100,7 @@ async function getAssistanceRequests(req, res) {
async function getAssistanceRequest(req, res) {
try {
let operation = `ofm_assistance_requests(${req.params.assistanceRequestId})?$select=modifiedon,ofm_assistance_requestid,ofm_last_opened_time,ofm_name,_ofm_request_category_value,ofm_subject,statecode,statuscode,ofm_is_read&$expand=ofm_facility_request_request($select=_ofm_facility_value),ofm_conversation_request($select=modifiedon)`
log.info('operation: ', operation)
log.debug('operation: ', operation)
let response = await getOperation(operation)
return res.status(HttpStatus.OK).json(mapAssistanceRequestObjectForFront(response))
} catch (e) {
Expand All @@ -96,17 +110,36 @@ async function getAssistanceRequest(req, res) {

async function updateAssistanceRequest(req, res) {
try {
let payload = new MappableObjectForBack(req.body, AssistanceRequestMappings).toJSON()
let response = await patchOperationWithObjectId('ofm_assistance_requests', req.params.assistanceRequestId, payload)
const payload = new MappableObjectForBack(req.body, AssistanceRequestMappings).toJSON()
const response = await patchOperationWithObjectId('ofm_assistance_requests', req.params.assistanceRequestId, payload)
return res.status(HttpStatus.OK).json(response)
} catch (e) {
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(e.data ? e.data : e?.status)
}
}

async function getAssistanceRequestConversation(req, res) {
try {
const operation = `ofm_conversations?$select=ofm_conversationid,ofm_name,createdon,ofm_message,ofm_source_system,_ofm_request_value,_ownerid_value,statecode,statuscode&$expand=createdby($select=firstname,lastname)&$filter=(_ofm_request_value eq ${req.params.assistanceRequestId})&$orderby=createdon desc`
log.debug('operation: ', operation)
const response = await getOperation(operation)
const messages = []

for (const item of response.value) {
let assistanceRequestConversation = new MappableObjectForFront(item, AssistanceRequestConversationMappings).toJSON()
messages.push(assistanceRequestConversation)
}
return res.status(HttpStatus.OK).json(messages)
} catch (e) {
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(e.data ? e.data : e?.status)
}
}

module.exports = {
createAssistanceRequest,
getAssistanceRequests,
getAssistanceRequest,
updateAssistanceRequest,
getAssistanceRequestConversation,
replyToAssistanceRequest,
}
34 changes: 33 additions & 1 deletion backend/src/routes/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const passport = require('passport')
const router = express.Router()
const auth = require('../components/auth')
const isValidBackendToken = auth.isValidBackendToken()
const { updateAssistanceRequest, createAssistanceRequest, getAssistanceRequests, getAssistanceRequest } = require('../components/message')
const { updateAssistanceRequest, createAssistanceRequest, getAssistanceRequests, getAssistanceRequest, getAssistanceRequestConversation, replyToAssistanceRequest } = require('../components/message')
const { param, validationResult, checkSchema } = require('express-validator')

module.exports = router
Expand Down Expand Up @@ -35,6 +35,17 @@ const newAssistanceRequestSchema = {
},
}

const replyAssistanceRequestSchema = {
assistanceRequestId: {
in: ['body'],
exists: { errorMessage: '[assistanceRequestId] is required' },
},
message: {
in: ['body'],
exists: { errorMessage: '[message] is required' },
},
}

/**
* Update an existing Assistance Request
*/
Expand Down Expand Up @@ -79,4 +90,25 @@ router.get(
},
)

/**
* Get the list of conversations for a assistanceRequestId
*/
router.get(
'/conversations/:assistanceRequestId',
passport.authenticate('jwt', { session: false }),
isValidBackendToken,
[param('assistanceRequestId', 'URL param: [assistanceRequestId] is required').not().isEmpty()],
(req, res) => {
validationResult(req).throw()
return getAssistanceRequestConversation(req, res)
},
)

/**
* Create a reply to a Assistance Request
*/
router.post('/replyToAssistanceRequest', passport.authenticate('jwt', { session: false }), isValidBackendToken, [checkSchema(replyAssistanceRequestSchema)], (req, res) => {
validationResult(req).throw()
return replyToAssistanceRequest(req, res)
})
module.exports = router
12 changes: 12 additions & 0 deletions backend/src/util/mapping/Mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ const AssistanceRequestMappings = [
{ back: 'ofm_is_read', front: 'isRead' },
]

const AssistanceRequestConversationMappings = [
{ back: 'ofm_conversationid', front: 'conversationId' },
{ back: 'ofm_name', front: 'subject' },
{ back: '_ownerid_value', front: 'from' },
{ back: 'createdon', front: 'sentDate' },
{ back: 'ofm_message', front: 'message' },
{ back: 'ofm_source_system', front: 'ofmSourceSystem' },
{ back: '_ofm_request_value', front: 'requestValue' },
{ back: 'statuscode', front: 'statusCode' },
]

const AssistanceRequestFacilityMappings = [
{ back: '_ofm_facility_value', front: 'facilityId' },
{ back: '[email protected]', front: 'facilityName' },
Expand All @@ -75,4 +86,5 @@ module.exports = {
RequestCategoryMappings,
AssistanceRequestMappings,
AssistanceRequestFacilityMappings,
AssistanceRequestConversationMappings,
}
Loading

0 comments on commit cacb3e1

Please sign in to comment.