Skip to content

Commit

Permalink
Merge pull request #57 from bcgov/ofmcc-729-update-user
Browse files Browse the repository at this point in the history
Ofmcc 729 update user
  • Loading branch information
jgstorey authored Dec 13, 2023
2 parents 630a995 + fea7f79 commit 34c6404
Show file tree
Hide file tree
Showing 13 changed files with 561 additions and 129 deletions.
29 changes: 20 additions & 9 deletions backend/src/components/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const HttpStatus = require('http-status-codes')
const _ = require('lodash')
const cache = require('memory-cache')
const { PROGRAM_YEAR_STATUS_CODES, ORGANIZATION_PROVIDER_TYPES, CHANGE_REQUEST_TYPES } = require('../util/constants')
const { ProgramYearMappings, SystemMessagesMappings, RequestCategoryMappings } = require('../util/mapping/Mappings')
const { ProgramYearMappings, SystemMessagesMappings, RequestCategoryMappings, UserRoleMappings } = require('../util/mapping/Mappings')
const { MappableObjectForFront } = require('../util/mapping/MappableObject')
const log = require('../components/logger')

Expand Down Expand Up @@ -125,21 +125,32 @@ async function getRequestCategories() {
return requestCategories
}

async function getUserRoles() {
let userRoles = lookupCache.get('userRoles')
if (!userRoles) {
userRoles = []
let response = await getOperation('GlobalOptionSetDefinitions(Name=%27ofm_portal_role%27)?$Select=Options')
userRoles = response?.Options?.map((item) => ({
id: Number(item.Value),
description: item.Label && item.Label.LocalizedLabels ? item.Label.LocalizedLabels[0].Label : null,
}))
lookupCache.put('userRoles', userRoles, 60 * 60 * 1000)
}
return userRoles
}

/**
* Look ups from Dynamics365.
*/
async function getLookupInfo(req, res) {
/**
* Look ups from Dynamics365.
* status code values are:
* 1 - Current
* 2 - Inactive
* 3 - Future
* 4 - Historica
*/
try {
let resData = lookupCache.get('lookups')
if (!resData) {
let requestCategories = await getRequestCategories()
let userRoles = await getUserRoles()
resData = {
requestCategories: requestCategories,
userRoles: userRoles,
}
lookupCache.put('lookups', resData, 60 * 60 * 1000)
}
Expand Down
63 changes: 54 additions & 9 deletions backend/src/components/user.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
'use strict'
const { getSessionUser, getUserName, getBusinessName, getHttpHeader, minify, getUserGuid, isIdirUser, getOperation } = require('./utils')
const { getSessionUser, getUserName, getBusinessName, getHttpHeader, minify, getUserGuid, isIdirUser, getOperation, postOperation } = require('./utils')
const config = require('../config/index')
const ApiError = require('./error')
const axios = require('axios')
const HttpStatus = require('http-status-codes')
const log = require('../components/logger')
// TODO... const { ORGANIZATION_PROVIDER_TYPES} = require('../util/constants')
const {
FacilityMappings,
UserFacilityMappings,
UserFacilityDetailMappings,
UserMappings,
UserProfileFacilityMappings,
UserProfileMappings,
UserProfileOrganizationMappings,
UserProfileFacilityPermissionMappings,
UserProfileFacilityMappings,
UserPermissionMappings,
FacilityMappings,
} = require('../util/mapping/Mappings')

const { MappableObjectForFront } = require('../util/mapping/MappableObject')
const { MappableObjectForFront, MappableObjectForBack } = require('../util/mapping/MappableObject')
const _ = require('lodash')

const USER_NOT_FOUND = 'User not found.'
Expand Down Expand Up @@ -165,7 +166,7 @@ function parseFacilityPermissions(userResponse) {
}

function mapUsersPermissionsFacilitiesObjectForFront(data) {
const usersPermissionsFacilities = new MappableObjectForFront(data, UserPermissionMappings).toJSON()
const usersPermissionsFacilities = new MappableObjectForFront(data, UserMappings).toJSON()
if (usersPermissionsFacilities?.facilities) {
usersPermissionsFacilities.facilities = usersPermissionsFacilities.facilities.map((facility) => {
let facilityData = new MappableObjectForFront(facility, FacilityMappings).toJSON()
Expand All @@ -174,22 +175,66 @@ function mapUsersPermissionsFacilitiesObjectForFront(data) {
return facilityData
})
}
return usersPermissionsFacilities
return {
...usersPermissionsFacilities,
role: Number(usersPermissionsFacilities.role),
}
}

async function getUsersPermissionsFacilities(req, res) {
try {
let usersPermissionsFacilities = []
const operation = `contacts?$select=ccof_userid,ccof_username,contactid,emailaddress1,ofm_first_name,ofm_is_primary_contact,ofm_last_name,ofm_portal_role,telephone1,ofm_is_expense_authority,statecode&$expand=ofm_facility_business_bceid($select=_ofm_bceid_value,ofm_bceid_facilityid,_ofm_facility_value,ofm_name,ofm_portal_access,statecode,statuscode;$filter=(ofm_portal_access eq true);$expand=ofm_facility($select=address1_line1,address1_line2,address1_line3,address1_city))&$filter=(_parentcustomerid_value eq ${req.params.organizationId})`
const response = await getOperation(operation)
response?.value?.forEach((item) => usersPermissionsFacilities.push(mapUsersPermissionsFacilitiesObjectForFront(item)))
response?.value?.forEach((item) => {
usersPermissionsFacilities.push(mapUsersPermissionsFacilitiesObjectForFront(item))
})
return res.status(HttpStatus.OK).json(usersPermissionsFacilities)
} catch (e) {
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(e.data ? e.data : e?.status)
}
}

function mapUserFacilityObjectForFront(data) {
let userFacilities = new MappableObjectForFront(data, UserFacilityMappings).toJSON()
delete userFacilities.ofmFacility
const userFacilityDetails = new MappableObjectForFront(data.ofm_facility, UserFacilityDetailMappings).toJSON()
userFacilities = { ...userFacilities, ...userFacilityDetails }
return userFacilities
}

async function getUserFacilities(req, res) {
try {
let userFacilities = []
const operation = `ofm_bceid_facilities?$expand=ofm_facility($select=accountnumber,address1_composite,name)&$filter=(statecode eq 0 and _ofm_bceid_value eq ${req.params.contactId}) and (ofm_facility/statecode eq 0)`
const response = await getOperation(operation)
response?.value?.forEach((item) => userFacilities.push(mapUserFacilityObjectForFront(item)))
return res.status(HttpStatus.OK).json(userFacilities)
} catch (e) {
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(e.data ? e.data : e?.status)
}
}

function mapUserObjectForBack(data) {
let newUser = new MappableObjectForBack(data, UserMappings).toJSON()
newUser.ofm_portal_role = String(newUser.ofm_portal_role)
newUser['[email protected]'] = `/accounts(${data?.organizationId})`
return newUser
}

async function createUser(req, res) {
try {
const payload = mapUserObjectForBack(req.body)
let response = await postOperation('contacts', JSON.stringify(payload))
return res.status(HttpStatus.OK).json(response)
} catch (e) {
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(e.data ? e.data : e?.status)
}
}

module.exports = {
createUser,
getUserFacilities,
getUserInfo,
getUsersPermissionsFacilities,
}
62 changes: 59 additions & 3 deletions backend/src/routes/user.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,73 @@
'use strict'

const passport = require('passport')
const express = require('express')
const router = express.Router()
const auth = require('../components/auth')
const isValidBackendToken = auth.isValidBackendToken()
const { createUser, getUserFacilities, getUserInfo, getUsersPermissionsFacilities } = require('../components/user')
const { param, validationResult, checkSchema } = require('express-validator')

const { getUserInfo, getUsersPermissionsFacilities } = require('../components/user')
const createUserSchema = {
userName: {
in: ['body'],
exists: { errorMessage: '[userName] is required' },
},
firstName: {
in: ['body'],
exists: { errorMessage: '[firstName] is required' },
},
lastName: {
in: ['body'],
exists: { errorMessage: '[lastName] is required' },
},
email: {
in: ['body'],
exists: { errorMessage: '[email] is required' },
},
role: {
in: ['body'],
exists: { errorMessage: '[role] is required' },
},
}

/**
* Get profile information for the logged in user
*/
router.get('/', passport.authenticate('jwt', { session: false }), isValidBackendToken, getUserInfo)

/**
* Get profile information for a given user name
*/
router.get('/:queryUserName', passport.authenticate('jwt', { session: false }), isValidBackendToken, getUserInfo)

router.get('/facilities/:organizationId', passport.authenticate('jwt', { session: false }), isValidBackendToken, getUsersPermissionsFacilities)
/**
* Get all users, permissions, and facilities for an organization.
*/
router.get(
'/permissions/facilities/:organizationId',
passport.authenticate('jwt', { session: false }),
isValidBackendToken,
[param('organizationId', 'URL param: [organizationId] is required').not().isEmpty()],
(req, res) => {
validationResult(req).throw()
return getUsersPermissionsFacilities(req, res)
},
)

/**
* Get all facilities for a contact. i.e. a contact is also referred to as a user
*/
router.get('/facilities/:contactId', passport.authenticate('jwt', { session: false }), isValidBackendToken, [param('contactId', 'URL param: [contactId] is required').not().isEmpty()], (req, res) => {
validationResult(req).throw()
return getUserFacilities(req, res)
})

/**
* Create a new user/contact
*/
router.post('/create', passport.authenticate('jwt', { session: false }), isValidBackendToken, [checkSchema(createUserSchema)], (req, res) => {
validationResult(req).throw()
return createUser(req, res)
})

module.exports = router
42 changes: 25 additions & 17 deletions backend/src/util/mapping/Mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ const UserProfileOrganizationMappings = [
{ back: 'statuscode', front: 'organizationStatus' },
]

// TODO (weskubo-cgi) Remove this
const UserProfileFacilityPermissionMappings = [
{ back: 'statecode', front: 'stateCode' },
{ back: 'statuscode', front: 'statusCode' },
]

const UserProfileFacilityMappings = [
{ back: 'accountid', front: 'facilityId' },
{ back: 'accountnumber', front: 'facilityAccountNumber' },
Expand Down Expand Up @@ -77,21 +71,34 @@ const NotificationMappings = [
{ back: 'ofm_is_read', front: 'isRead' },
]

const UserPermissionMappings = [
const UserMappings = [
{ back: 'contactid', front: 'contactId' },
{ back: 'ofm_first_name', front: 'firstName' },
{ back: 'ofm_last_name', front: 'lastName' },
{ back: 'emailaddress1', front: 'email' },
{ back: 'telephone1', front: 'phone' },
{ back: 'ccof_username', front: 'userName' },
{ back: 'ofm_portal_role', front: 'roles' },
{ back: 'ofm_portal_role', front: 'role' },
{ back: 'ofm_is_expense_authority', front: 'isExpenseAuthority' },
{ back: 'statecode', front: 'stateCode' },
{ back: 'ofm_facility_business_bceid', front: 'facilities' },
]

const UserFacilityMappings = [
{ back: 'ofm_bceid_facilityid', front: 'facilityId' },
{ back: 'ofm_facility', front: 'ofmFacility' },
]

const UserFacilityDetailMappings = [
{ back: 'accountid', front: 'accountId' },
{ back: 'accountnumber', front: 'accountNumber' },
{ back: 'name', front: 'facilityName' },
{ back: 'address1_composite', front: 'address' },
]

const FacilityMappings = [
{ back: 'ofm_bceid_facilityid', front: 'facilityId' },
{ back: '[email protected]', front: 'name' },
{ back: '[email protected]', front: 'facilityName' },
{ back: 'statecode', front: 'stateCode' },
{ back: 'statuscode', front: 'statusCode' },
{ back: 'ofm_facility', front: 'address' },
Expand All @@ -111,16 +118,17 @@ const ApplicationMappings = [
]

module.exports = {
NotificationMappings,
UserProfileMappings,
UserProfileOrganizationMappings,
UserProfileFacilityPermissionMappings,
UserProfileFacilityMappings,
RequestCategoryMappings,
ApplicationMappings,
AssistanceRequestMappings,
AssistanceRequestFacilityMappings,
AssistanceRequestConversationMappings,
UserPermissionMappings,
FacilityMappings,
ApplicationMappings,
NotificationMappings,
UserFacilityMappings,
UserFacilityDetailMappings,
UserMappings,
UserProfileFacilityMappings,
UserProfileMappings,
UserProfileOrganizationMappings,
RequestCategoryMappings,
}
1 change: 0 additions & 1 deletion frontend/src/components/TheHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export default {
return this.$vuetify.display.xs
},
messageNotificationCount() {
console.log('mnc', this.actionRequiredAndUnreadMessageCount + this.unreadNotificationCount)
return this.actionRequiredAndUnreadMessageCount + this.unreadNotificationCount
},
// count of requests that are unread or are in the status of “Action required”
Expand Down
Loading

0 comments on commit 34c6404

Please sign in to comment.