Skip to content

Commit

Permalink
issue #SB-941 chore: Merge pull request #115 from loganathan1989/rele…
Browse files Browse the repository at this point in the history
…ase-1.3

Issue #SB-941 feat: user services dependency
  • Loading branch information
gsbajaj72 authored Nov 30, 2017
2 parents a684a95 + d45fe4b commit e609b66
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/app/helpers/announcement/services/user/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @author Loganathan Shanmugam <[email protected]>
*/

/**
* Class provides user details for user related services
*/

class User {
constructor({
id,
firstName,
lastName,
email,
phone,
dob,
location,
rootOrg,
regOrgId,
organisations,
roles
} = {}) {

/**
* @property {string} userId - a unqiue id for the user
*/
this.id = id

/**
* @property {string} firstName - first name of the user
*/
this.firstName = firstName

/**
* @property {string} lastName - last name of the user
*/
this.lastName = lastName

/**
* @property {string} email - email id of the user
*/
this.email = email

/**
* @property {string} phone - contact number of the user
*/
this.phone = phone

/**
* @property {date} dob - date of birth of the user
*/
this.dob = dob

/**
* @property {string} location - place or location of the user
*/
this.location = location

/**
* @property {string} rootOrg - root organisation to which user belongs to
*/
this.rootOrg = rootOrg

/**
* @property {string} regOrgId - organisation id to which user belongs to
*/
this.regOrgId = regOrgId

/**
* @property {array} organisations - list of organisations to which user belongs to
*/
this.organisations = organisations

/**
* @property {array} roles - list of roles to which user assigned to
*/
this.roles = roles
}
}
module.exports = User
110 changes: 110 additions & 0 deletions src/app/helpers/announcement/services/user/UserService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @author Loganathan Shanmugam <[email protected]>
*/

let webService = require('request')
let envVariables = require('../../../environmentVariablesHelper.js')
let httpWrapper = require('../httpWrapper.js')
let async = require('async')
const _ = require('lodash')
let User = require('./User.js')
let AppError = require('../ErrorInterface.js')

let HttpStatus = require('http-status-codes')
/**
* Class provides services for user related requests */

class UserService {
/**
*
* Callers of the constructor can invoke as follows:
*
* let userService = new UserService({userAccessToken: 135-5435-6456,userId:5343434-67676-135-5, httpService:instance })
*/
constructor({
userAccessToken,
userId,
httpService
} = {}) {
/**
* @property {string} userAccessToken - A user authenticated user token [ TODO: Remove this property once session service is implemented ]
*/
this.userAccessToken = userAccessToken

/**
* @property {instance} httpService - Instance of httpservice which is used to make a http service call
*/
this.httpService = httpService || httpWrapper

/**
* @property {string} userId - An unique id of user
*/
this.userId = userId


/**
* @property {string} profileUri - router path used to store profile path uri
*/
this.profileUri = 'user/v1/read/'


}
/**
* Get user profile of given user
*/
getUserProfile() {
return new Promise((resolve, reject) => {
if (_.isEmpty(this.userId)) {
reject(new AppError({
message: 'user id is required!',
status: HttpStatus.BAD_REQUEST
}))
}
let options = {
method: 'GET',
uri: envVariables.DATASERVICE_URL + this.profileUri + this.userId,
headers: this.httpService.getRequestHeader(this.userAccessToken)
}
this.httpService.call(options).then((data) => {
let res = JSON.parse(data.body)
let profileData = res.result.response
let userData = {
id: profileData.identifier,
firstName: profileData.firstName,
lastName: profileData.lastName,
email: profileData.email,
phone: profileData.phone,
dob: profileData.dob,
location: profileData.location,
rootOrg: profileData.rootOrg,
regOrgId: profileData.regOrgId,
organisations: profileData.organisations,
roles: profileData.roles
}

let userObj = new User(userData)
resolve(userObj)
}).catch((error) => {
if (_.get(error, 'body.params.err') === 'USER_NOT_FOUND') {
reject(new AppError({
message: 'User not found',
status: HttpStatus.NOT_FOUND
}))
} else if (_.get(error, 'body.params.err') === 'UNAUTHORIZE_USER') {
reject(new AppError({
message: 'Unauthorised user',
status: HttpStatus.UNAUTHORIZED
}))
} else {
reject(new AppError({
message: 'Unknown error',
status: HttpStatus.INTERNAL_SERVER_ERROR
}))
}
})
})
}

}

module.exports = UserService

0 comments on commit e609b66

Please sign in to comment.