@@ -39,6 +39,9 @@ module.exports = function (options, handler) {
3939 ( event . requestContext && event . requestContext . identity && event . requestContext . identity . user ) ||
4040 event . user ;
4141 } ;
42+
43+ options . identifyCompany = options . identifyCompany || function ( ) { } ;
44+
4245 options . getSessionToken = options . getSessionToken || function ( event , context ) {
4346 return ( event . requestContext && event . requestContext . identity && event . requestContext . identity . apiKey ) ;
4447 } ;
@@ -68,12 +71,12 @@ module.exports = function (options, handler) {
6871 var moesifMiddleware = function ( event , context , callback ) {
6972 logMessage ( options . debug , 'moesifMiddleware' , 'start' ) ;
7073
71- var next = function ( err , result ) {
72- logEvent ( event , context , err , result , options , moesifController ) ;
73- callback ( err , result )
74- } ;
74+ var next = function ( err , result ) {
75+ logEvent ( event , context , err , result , options , moesifController ) ;
76+ callback ( err , result )
77+ } ;
7578
76- handler ( event , context , next ) ;
79+ handler ( event , context , next ) ;
7780 } ;
7881
7982 moesifMiddleware . updateUser = function ( userModel , cb ) {
@@ -83,6 +86,13 @@ module.exports = function (options, handler) {
8386 moesifController . updateUser ( userModel , cb ) ;
8487 } ;
8588
89+ moesifMiddleware . updateCompany = function ( companyModel , cb ) {
90+ logMessage ( options . debug , 'updateCompany' , 'companyModel=' + JSON . stringify ( companyModel ) ) ;
91+ ensureValidCompanyModel ( companyModel ) ;
92+ logMessage ( options . debug , 'updateCompany' , 'companyModel valid' ) ;
93+ moesifController . updateCompany ( companyModel , cb ) ;
94+ } ;
95+
8696 logMessage ( options . debug , 'moesifInitiator' , 'returning moesifMiddleware Function' ) ;
8797 return moesifMiddleware ;
8898} ;
@@ -131,13 +141,16 @@ function logEvent(event, context, err, result, options, moesifController) {
131141
132142 if ( safeRes . body ) {
133143 if ( safeRes . isBase64Encoded ) {
144+ // does this flag exists from AWS?
134145 logData . response . transferEncoding = 'base64' ;
135146 logData . response . body = bodyToBase64 ( safeRes . body ) ;
136147 } else {
137148 try {
138149 logData . response . body = JSON . parse ( safeRes . body ) ;
139150 } catch ( err ) {
140- logData . response . body = safeRes . body ;
151+ // if JSON decode fails, we'll try to base64 encode the body.
152+ logData . response . transferEncoding = 'base64' ;
153+ logData . response . body = bodyToBase64 ( safeRes . body ) ;
141154 }
142155 }
143156 }
@@ -147,6 +160,7 @@ function logEvent(event, context, err, result, options, moesifController) {
147160 logData = options . maskContent ( logData ) ;
148161
149162 logData . userId = options . identifyUser ( event , context ) ;
163+ logData . companyId = options . identifyCompany ( event , context ) ;
150164 logData . sessionToken = options . getSessionToken ( event , context ) ;
151165 logData . tags = options . getTags ( event , context ) ;
152166
@@ -245,7 +259,13 @@ function ensureValidLogData(logData) {
245259}
246260
247261function ensureValidUserModel ( userModel ) {
248- if ( ! userModel . userId ) {
262+ if ( ! userModel || ! userModel . userId ) {
249263 throw new Error ( 'To update user, a userId field is required' ) ;
250264 }
251265}
266+
267+ function ensureValidCompanyModel ( companyModel ) {
268+ if ( ! companyModel || ! companyModel . companyId ) {
269+ throw new Error ( 'To update company, a companyId field is required' ) ;
270+ }
271+ }
0 commit comments