-
Notifications
You must be signed in to change notification settings - Fork 10
CS-1264 Improved Logging to debug Lambda Functions #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Your installation or use of this SugarCRM file is subject to the applicable | ||
| * terms available at | ||
| * http://support.sugarcrm.com/Resources/Master_Subscription_Agreements/. | ||
| * If you do not agree to all of the applicable terms or do not have the | ||
| * authority to bind the entity as an authorized representative, then do not | ||
| * install or use this SugarCRM file. | ||
| * | ||
| * Copyright (C) SugarCRM Inc. All rights reserved. | ||
| */ | ||
| const strUtils = require('../../../src/utils/string-utils.js'); | ||
|
|
||
| describe('generateMessage', function() { | ||
| test.each([ | ||
| { | ||
| template: 'Single ${} test', | ||
| filler: 'dog', | ||
| expected: 'Single dog test' | ||
| }, | ||
| { | ||
| template: 'Multiples ${} ${} test', | ||
| filler: ['dog', 'cat'], | ||
| expected: 'Multiples dog cat test' | ||
| }, | ||
| { | ||
| template: 'Mismatched ${} ${} ${} too few fillers', | ||
| filler: ['dog', 'cat'], | ||
| expected: 'Mismatched dog cat ${} too few fillers' | ||
| }, | ||
| { | ||
| template: 'Mismatch ${} too many fillers', | ||
| filler: ['dog', 'cat'], | ||
| expected: 'Mismatch dog too many fillers' | ||
| } | ||
| ])('should fill the template string as expected', (values) => { | ||
| let actual = strUtils.generateMessage(values.template, values.filler); | ||
| expect(actual).toEqual(values.expected); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * Your installation or use of this SugarCRM file is subject to the applicable | ||
| * terms available at | ||
| * http://support.sugarcrm.com/Resources/Master_Subscription_Agreements/. | ||
| * If you do not agree to all of the applicable terms or do not have the | ||
| * authority to bind the entity as an authorized representative, then do not | ||
| * install or use this SugarCRM file. | ||
| * | ||
| * Copyright (C) SugarCRM Inc. All rights reserved. | ||
| */ | ||
|
|
||
| module.exports = { | ||
| /** | ||
| * Success messages | ||
| */ | ||
| LAMBDA_FUNCTION_SUCCESS: 'Lambda Function Completed Successfully' | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| const axios = require('axios'); | ||
|
|
||
| const { HttpStatus } = require('../constants/http-status.js'); | ||
| const loggerUtils = require('../utils/logger-utils'); | ||
| const { Secrets } = require('../utils/aws/secrets'); | ||
|
|
||
| const methodToRequest = { | ||
|
|
@@ -61,7 +62,9 @@ module.exports = () => { | |
| request.params = params; | ||
| } | ||
| response = await axios(request); | ||
|
|
||
| if (response.data) { | ||
| loggerUtils.logSugarApiResponse(response.data); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log every return from the Sugar API for debugging purposes |
||
| return { | ||
| status: HttpStatus.ok, | ||
| data: response.data | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,13 +14,16 @@ | |
| */ | ||
| const app = require('../core/app.js'); | ||
| /** | ||
| * Util class to log JSOn to Cloudwatch | ||
| * Util classes to write logs to Cloudwatch | ||
| */ | ||
| const loggerUtils = require('../utils/logger-utils'); | ||
| const strUtils = require('../utils/string-utils'); | ||
| /** | ||
| * HTTP status codes used in this function | ||
| * Defined Constants | ||
| */ | ||
| const { HttpStatus } = require('../constants/http-status'); | ||
| const ErrorMessages = require('../constants/messages/error'); | ||
| const SuccessMessages = require('../constants/messages/success'); | ||
| /** | ||
| * Sugar Instance URL | ||
| */ | ||
|
|
@@ -32,27 +35,44 @@ const baseUrl = process.env.sugarUrl; | |
| * @param {Object} event | ||
| */ | ||
| const addNoteToCaseHandler = async (event) => { | ||
| loggerUtils.logContactFlowEvent(event); | ||
|
|
||
| let statusCode = HttpStatus.error; | ||
| let caseId = ''; | ||
| let noteName = ''; | ||
| let body = ''; | ||
|
Comment on lines
+40
to
+43
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just moved these to the top to use them in every early return path for consistency |
||
|
|
||
| // input from the contact flow provided by the user | ||
| const caseNumber = event.Details.Parameters.caseNumber || ''; | ||
| const contactId = event.Details.Parameters.contactId || ''; | ||
|
|
||
| if (!caseNumber || !contactId) { | ||
| return { | ||
| statusCode: HttpStatus.preconditionFailed | ||
| }; | ||
| let filler = caseNumber ? '' : 'caseNumber '; | ||
| filler += contactId ? '' : 'contactId'; | ||
| body = strUtils.generateMessage(ErrorMessages.TPL_MISSING_REQUIRED_PARAMETERS, filler); | ||
| return loggerUtils.logReturnValue({ | ||
| statusCode: HttpStatus.preconditionFailed, | ||
| caseId: caseId, | ||
| body: body | ||
| }); | ||
|
Comment on lines
+53
to
+57
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern is used to log return values to cloudwatch for debugging. |
||
| } | ||
|
|
||
| const noteDescription = event.Details.Parameters.noteDescription; | ||
| const contactName = event.Details.Parameters.contactName; | ||
|
|
||
| // Use the given case number to get the relavant case id | ||
| const filterUrl = encodeURI(`${baseUrl}/rest/v11_10/Contact/${contactId}/Cases?filter[0][case_number]=${caseNumber}&fields=id`); | ||
| const idResponse = await app.api.call('read', filterUrl, null, null); | ||
| const caseBean = idResponse.data.records[0]; | ||
|
|
||
| loggerUtils.logSugarApiResponse(idResponse); | ||
|
|
||
| let statusCode = HttpStatus.error; | ||
| let caseId = ''; | ||
| let noteName = '' | ||
| if (idResponse.data.records.length > 1) { | ||
| body = strUtils.generateMessage(ErrorMessages.TPL_MULTIPLE_RECORDS_MATCHED, 'Case'); | ||
| return loggerUtils.logReturnValue({ | ||
| statusCode: statusCode, | ||
| caseId: caseId, | ||
| body: body | ||
| }); | ||
| } | ||
| const caseBean = idResponse.data.records[0]; | ||
|
|
||
| // if contact name is empty then default it to 'customer' | ||
| if (contactName === '' || contactName === undefined) { | ||
|
|
@@ -70,26 +90,29 @@ const addNoteToCaseHandler = async (event) => { | |
| 'description': noteDescription, | ||
| 'name': noteName | ||
| }; | ||
|
|
||
| // add note to the case | ||
| const filterUrl = encodeURI(`${baseUrl}/rest/v11_10/Cases/${caseBean.id}/link/notes`); | ||
| const noteResponse = await app.api.call('create', filterUrl, null, notePayload); | ||
|
|
||
| loggerUtils.logSugarApiResponse(noteResponse); | ||
|
|
||
| const updateCaseBean = noteResponse.data.record; | ||
|
|
||
| // if the case was successfully updated | ||
| if (updateCaseBean && updateCaseBean.id !== '') { | ||
| statusCode = HttpStatus.ok; | ||
| caseId = updateCaseBean.id; | ||
| body = SuccessMessages.LAMBDA_FUNCTION_SUCCESS; | ||
| } else { | ||
| body = ErrorMessages.ERROR_NOTE_CREATE_FAILED; | ||
| } | ||
| } else { | ||
| body = strUtils.generateMessage(ErrorMessages.TPL_NO_RECORDS_MATCHED, 'Case'); | ||
| } | ||
|
|
||
| return { | ||
| return loggerUtils.logReturnValue({ | ||
| statusCode: statusCode, | ||
| caseId: caseId | ||
| }; | ||
| caseId: caseId, | ||
| body: body | ||
| }); | ||
| }; | ||
|
|
||
| exports.handler = addNoteToCaseHandler; | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this allows us to auto-fix style errors before committing with
npm run lint. All of the changes in this commit were from running this command