Requirement:
When a customer logs in to RepairDesk, we need to identify if the customer is an authorized NetSuite customer using their email address.
Solution:
We have developed a Restlet script that takes customer email address and password as request body and return a success response if a customer with this email address exists in Netsuite
/**
* @NApiVersion 2.1
* @NScriptType Restlet
*/
/*************************************************************************************************************************
* CLIENTNAME: Balaji Trading, inc.
* BTIN-2123
* Restlet script to check if a customer exists in netsuite using the email id that RD sends
*********************************************************************************************************************
*
* Author:Jobin & Jismi
* Script Description :This Restlet script to check if a customer exists in netsuite using the email id that RD sends
*
* REVISION HISTORY
*
* Revision 1.0
**************************************************************************************************************************/
define(["N/https", "N/record", "N/runtime", "N/search", "N/url", "N/http"],
/**
* @param{https} https
* @param{record} record
* @param{runtime} runtime
* @param{search} search
* @param{url} url
*/
(https, record, runtime, search, url, http) => {
'use strict';
/**
* The object that contains the response messages
* @type {{emailNotValid: {summary: {reason: string, status: string}}, success: {summary: {reason: string, status: string}, result: {customerToken: string}}, error: {summary: {reason: string, status: string}, result: {customerToken: string}}, invalidParameter: {summary: {reason: string, status: string}}, notExist: {summary: {reason: string, status: string}, result: {customerToken: string}}}}
*/
let ResponseSet = {
"success": {
statusCode: "200",
summary: {
status: "SUCCESS",
reason: "CUSTOMER_AUTHENTICATION_SUCCESSFUL"
},
result: {customerToken: "customerID"}
},
"failure": {
statusCode: "401",
summary: {
status: "FAILURE",
reason: "CUSTOMER_AUTHENTICATION_FAILED"
},
result: {reason: "CUSTOMER_DOES_NOT_EXIST"}
},
"invalidParameter": {
statusCode: "400",
summary: {
status: "FAILURE",
reason: "PARAMETER_IS_INVALID"
},
result: {reason: "THE_GIVEN_PARAMETER_IS_INVALID"}
}
};
let main = {
/**
* @description the function to check whether a value exists in parameter
* @param parameter -passing parameter
* @param parameterName - passing parameter name
* @returns{Boolean}
*/
checkForParameter: function (parameter, parameterName) {
if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false') {
return true;
} else {
if (parameterName)
log.debug('Empty Value found', 'Empty Value for parameter ' + parameterName);
return false;
}
},
/**
* Function that checks if a customer with an email id exists or not in netsuite
* @param email
* @returns {boolean|*}
* @constructor
*/
authenticateCustomerData: function (email) {
try {
let customerSearchObj = search.create({
type: "customer",
filters:
[
["stage", "anyof", "CUSTOMER", "LEAD", "PROSPECT"],
"AND",
["isinactive", "any", ""],
"AND",
["formulatext: NVL({email}, {altemail})", "is", email]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"})
]
});
let searchResultCount = customerSearchObj.runPaged().count;
log.debug("customerSearchObj result count", searchResultCount);
if (searchResultCount > 0) {
let internalId;
customerSearchObj.run().each(function (result) {
internalId = result.getValue({name: "internalid", label: "Internal ID"})
return false;
});
return internalId;
} else
return false;
}catch (e) {
return false;
}
},
/**
* Function to create a custom record entry of type RepairDesk Enabled for authenticated customers
* @param customer
* @param payload
*/
repairDeskEnabledCustomer: function (customer, payload){
try {
let authCustomer = record.create({
type: 'customrecord_jj_auth_customer_btin2123',
isDynamic: true
});
authCustomer.setValue({fieldId: 'custrecord_jj_auth_customer_name', value: customer});
authCustomer.setValue({fieldId: 'custrecord_jj_auth_customer_token', value: customer});
authCustomer.setValue({fieldId: 'custrecord_jj_auth_payload', value: JSON.stringify(payload)});
authCustomer.setValue({fieldId: 'custrecord_jj_auth_email', value: payload.email});
authCustomer.save({ignoreMandatoryFields: true, enableSourcing: false});
}catch (e) {
log.debug("Error @ repairDeskEnabledCustomer", e.message)
}
},
/**
* Search to check if a custom record for a customer already exists
* @param customer
* @param email
* @returns {boolean}
*/
customRecordExists: function (customer, email) {
try{
let customrecord_jj_auth_customer_btin2123SearchObj = search.create({
type: "customrecord_jj_auth_customer_btin2123",
filters:
[
["custrecord_jj_auth_customer_token","is",customer],
"AND",
["custrecord_jj_auth_email","is",email],
"AND",
["custrecord_jj_auth_customer_name.internalid","anyof",customer]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"})
]
});
var searchResultCount = customrecord_jj_auth_customer_btin2123SearchObj.runPaged().count;
log.debug("customrecord_jj_auth_customer_btin2123SearchObj result count",searchResultCount);
if (searchResultCount > 0)
return true;
else
return false;
}catch (e) {
return false;
}
},
/**
* Function to post the customer internal ID to repairdesk
* @param requestBody
* @returns {string}
*/
post: function (requestBody) {
let requestObj = requestBody;
log.debug ("requestObj", requestObj);
/** If no parameter passed **/
if (!main.checkForParameter (requestObj) || (main.checkForParameter (requestObj) && !main.checkForParameter (requestObj.email))) {
return JSON.stringify (ResponseSet.invalidParameter);
} else if (main.checkForParameter (requestObj.email)) // if emailid is provided
{
try
{
let email = requestObj.email;
// check if a customer with the email id exists in netsuite
let customerID = main.authenticateCustomerData (email);
if (customerID)
{ //An authenticated customer
let customRecord = main.customRecordExists(customerID, email)
if (!main.checkForParameter(customRecord)) {
main.repairDeskEnabledCustomer(customerID, requestBody) //create a custom record entry in netsuite for authenticate customer
}
ResponseSet.success.result.customerToken = customerID
return JSON.stringify (ResponseSet.success);
}
else
{ //Not an authenticated customer
return JSON.stringify (ResponseSet.failure);
}
}
catch (e)
{
log.debug("Error@ data processing", e.message);
return JSON.stringify (ResponseSet.failure);
}
}
}
}
return main;
});