Skip to content

Commit

Permalink
fix(actionRecordMiddleware): issues/52 limit and data adjustment (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Jan 27, 2021
1 parent aaccff0 commit 0f7db07
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions src/redux/middleware/actionRecordMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
import { platformApiTypes } from '../../types';
import { userTypes } from '../types/userTypes';

/**
* Modify actions for privacy.
* Modify actions' payload for privacy.
*
* @param {object} action
* @param {string} action.type
* @param {object} action.payload
* @returns {object}
*/
const sanitizeActionHeaders = ({ type, payload, ...action }) => {
const sanitizeActionHeaders = ({ payload, ...action }) => {
if (payload) {
let updatedPayload = { ...payload, headers: {} };

if (Array.isArray(payload)) {
updatedPayload = payload.map(({ headers, ...obj }) => ({ ...obj, headers: {} }));
}

return { payload: updatedPayload, ...action };
}

return { ...action };
};

/**
* Modify actions' payload data for privacy.
*
* @param {object} action
* @param {string} action.type
* @param {object} action.payload
* @returns {object}
*/
const sanitizeData = ({ type, payload, ...action }) => {
if (payload && new RegExp(userTypes.USER_AUTH).test(type)) {
const updatedPayload = {
...payload,
data: {
...payload.data,
user: {
...payload.data.user,
[platformApiTypes.PLATFORM_API_RESPONSE_USER_IDENTITY]: {
...payload.data.user[platformApiTypes.PLATFORM_API_RESPONSE_USER_IDENTITY],
[platformApiTypes.PLATFORM_API_RESPONSE_USER_IDENTITY_TYPES.USER]: {}
}
}
}
};

return { type, payload: updatedPayload, ...action };
}

Expand All @@ -30,7 +62,7 @@ const sanitizeActionHeaders = ({ type, payload, ...action }) => {
const getActions = (id, limit) => {
const { sessionStorage } = window;
const item = sessionStorage.getItem(id);
let parsedItems = (item && (JSON.parse(item) || {})?.actions) || null;
let parsedItems = (item && (JSON.parse(item) || {})?.actions) || [];

if (parsedItems?.length && limit > 0) {
parsedItems = parsedItems.slice(limit * -1);
Expand All @@ -51,7 +83,7 @@ const recordAction = (action, { id, limit, ...config }) => {
const { navigator, sessionStorage } = window;
const items = getActions(id, limit) || [];
const priorItem = items[items.length - 1];
const updatedAction = sanitizeActionHeaders(action);
const updatedAction = sanitizeData(sanitizeActionHeaders(action));
const actionObj = {
diff: 0,
timestamp: Date.now(),
Expand All @@ -67,7 +99,7 @@ const recordAction = (action, { id, limit, ...config }) => {
id,
JSON.stringify({
browser: navigator.userAgent,
timestamp: new Date(),
timestamp: new Date().toLocaleString(),
...config,
actions: items
})
Expand All @@ -84,7 +116,7 @@ const actionRecordMiddleware = (config = {}) => {
return () => next => action => {
recordAction(action, {
id: 'actionRecordMiddleware/v1',
limitResults: 100,
limit: 100,
...config
});

Expand Down

0 comments on commit 0f7db07

Please sign in to comment.