-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathadd-note-to-case.js
More file actions
118 lines (104 loc) · 4.01 KB
/
add-note-to-case.js
File metadata and controls
118 lines (104 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* 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.
*/
/**
* A Lambda function that creates a case.
*/
const app = require('../core/app.js');
/**
* Util classes to write logs to Cloudwatch
*/
const loggerUtils = require('../utils/logger-utils');
const strUtils = require('../utils/string-utils');
/**
* Defined Constants
*/
const { HttpStatus } = require('../constants/http-status');
const ErrorMessages = require('../constants/messages/error');
const SuccessMessages = require('../constants/messages/success');
/**
* Sugar Instance URL
*/
const baseUrl = process.env.sugarUrl;
/**
* Lambda function to add a note to a given case based on case number and note text sent via Contact Flow's Invoke Lambda
* Function block.
* @param {Object} event
*/
const addNoteToCaseHandler = async (event) => {
loggerUtils.logContactFlowEvent(event);
let statusCode = HttpStatus.error;
let caseId = '';
let noteName = '';
let body = '';
// input from the contact flow provided by the user
const caseNumber = event.Details.Parameters.caseNumber || '';
const contactId = event.Details.Parameters.contactId || '';
if (!caseNumber || !contactId) {
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
});
}
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);
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) {
noteName = 'Note from customer';
} else {
noteName = `Note from ${contactName}`;
}
// if case id is successfully obtained
if (caseBean && caseBean.id !== '') {
// payload to be sent with the request for note creation
const notePayload = {
'parent_type': 'Cases',
'parent_id': caseBean.id,
'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);
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 loggerUtils.logReturnValue({
statusCode: statusCode,
caseId: caseId,
body: body
});
};
exports.handler = addNoteToCaseHandler;