-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from bcgov/ofmcc-729-update-user
Ofmcc 729 update user
- Loading branch information
Showing
13 changed files
with
561 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
|
@@ -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() | ||
|
@@ -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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' }, | ||
|
@@ -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' }, | ||
|
@@ -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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.