legacy-api-to-salesforce-facilitator
v1.0.4
Published
tool used for existing API integrations to correctly interact with the correct data source when transitioning from a legacy API to Salesforce without having to make changes for the current integrated services.
Readme
legacy-api-to-salesforce-facilitator
Explanation
there may be is an interrim time where it is necessary to have both systems active while transitioning from a legacy db and restful API app to salesforce.
The aim of this package is to aid in this transition by encapulsating the salesforce restful apex apis and providing a switch function to allow the consumer of this package to switch which function (legacy or salesforce) is exected based off of supplied existence checks.
By including these functions into the existing legacy API, all currently integrated services connecting to the legacy API can continue working without making changes as the legacy API will correctly input the incoming data in the appropriate place (salesforce or legacy DB).
Steps To Use:
Provide env vars in the project
Import the restful api functions as needed
Define legacy and salesforce existence check fuctions
Define legacy and salesforce functions
Provide the previously defined functions in step 3 and 4 to the checkAndExecuteLegacyOrSalesforceFunction method as anonymous functions
Provided Functions:
- getSalesforceAuthToken:
returns the salesforce auth token. sets it in to local cache for future reuse to prevent multiple auth calls.
this method is exposed to allow the user to get an auth token on it's own if necessary.
By exposing this function, users can bypass the he local cache functionality and call this method directly to add the token to their respective rest calls (token is given as a optional param)
@returns token for salesforce Authentication
- checkAndExecuteLegacyOrSalesforceFunction:
This function is the main function used for this package. By defining the below parameter functions, current API's can still be utilitzed by all integrating services while transitioning data from a legacy system to salesforce.
The goal of this package is to slowly allow a legacy system to decouple from current integrated services seamlessly when transitioning to salesforce.
@param {*} legacyCheck a function that checks for object existence in legacy DB. pass in an anonymous function.
@param {*} salesForceCheck a function that checks for object existence in salesforce DB. pass in an anonymous function.
@param {*} legacyFunc a function that performs the legacy API interaction with the legacy DB. This function would contain all previous API interactions. pass in an anonymous function.
@param {*} salesForceFunc a function that performs the same API interaction as the legacy API interaction but with the salesforce DB. This function would contain all previous API interactions but interact with the salesforce DB. pass in an anonymous function.
@returns the output of the executed function.
- getSalesforceData:
provide sa GET endpoint wrapper to interact with salesforces endpoints in Node.
@param {*} endpoint pass in the salesforce endpoint that is expecting a GET. Do not include the base url as provided in env vars (see readMe).
@param {*} token optional parameter set to null. will check localCache to see if token is present. if not will retrieve and store in cache.
@returns the output object of the GET salesforce endpoint.
- putSalesforceData:
provides a PUT endpoint wrapper to interact with salesforces endpoints in Node.
@param {*} endpoint pass in the salesforce endpoint that is expecting a PUT. Do not include the base url as provided in env vars (see readMe). @param {*} body the body of the PUT call. expecting a json object that matches the expected salesforce input (verify in salesforce the expected body) @param {*} token optional parameter set to null. will check localCache to see if token is present. if not will retrieve and store in cache.
@returns sucessful or unsuccessful message
- postSalesforceData:
provides a POST endpoint wrapper to interact with salesforces endpoints in Node.
@param {*} endpoint pass in the salesforce endpoint that is expecting a POST. Do not include the base url as provided in env vars (see readMe).
@param {*} body the body of the POST call. expecting a json object that matches the expected salesforce input (verify in salesforce the expected body)
@param {*} token optional parameter set to null. will check localCache to see if token is present. if not will retrieve and store in cache.
@returns sucessful or unsuccessful message
- NOTE: delete function is not supplied.
the following environment variables are needed:
// sales force grant type (typically 'client_credentials')
SALESFORCE_GRANT_TYPE
// client id provided by salesforce
SALESFORCE_CLIENT_ID
// client secret provided by salesforce
SALESFORCE_CLIENT_SECRET
// salesforce base url to use with endpoints
SALESFORCE_BASE_URLExample Usage:
const legacyDBInteraction = async (params) => {
const data = await legacyQuery(params);
return data;
}
const salesforceDBInteraction = async (appId) => {
const url = `${process.env.SALESFORCE_BASE_URL}${PutNewInfoEndpoint}`;
const body = { app_id: `${appId}` };
const data = await putSalesforceData(url, body);
return data;
}
const legacyAppCheck = async (uuid) => {
const record = legacyDBAppLookup(uuid);
return record ? true : false;
}
const sfAppCheck = async (uuid) => {
const sfRecord = await getSalesforceData(`${process.env.SALESFORCE_BASE_URL}${SF_GET_BY_UUID(UUID)}`);
return sfRecord ? true : false;
}
//every service calling this api endpoint will have their data correctly put into the corresponding data source.
const apiCall = async (req, res, next) => {
try {
const { uuid } = req.params;
const legacyCheck = async () => { await legacyAppCheck(uuid); }
const sfCheck = async () => { await sfAppCheck(uuid); }
const legacyWrapper = async () => { await legacyDBInteraction(uuid); }
const salesforceWrapper = async () => { await salesforceDBInteraction(uuid); }
await checkAndExecuteLegacyOrSalesforceFunction(legacyCheck, sfCheck, legacyWrapper, salesforceWrapper);
return res.sendStatus(204);
} catch (error) {
return next(error);
}
};Recomendations:
- create a salesforce folder
- add an endpoints file there with each endpoint defined as either a string or a func that returns a string.
- add a util file there with the existance checks
