Skip to content

Commit

Permalink
Add new filters, fix missing terms on pagination and reload (#442)
Browse files Browse the repository at this point in the history
* Add new filters, fix missing terms on pagination and reload

Signed-off-by: Tomás Castillo <[email protected]>

* Add summit timezone, change action operator, fix placeholder

Signed-off-by: Tomás Castillo <[email protected]>

* Update PR with lint rules

Signed-off-by: Tomás Castillo <[email protected]>

* Remove warnings from actions

Signed-off-by: Tomás Castillo <[email protected]>

---------

Signed-off-by: Tomás Castillo <[email protected]>
  • Loading branch information
tomrndom authored and smarcet committed Jul 29, 2024
1 parent 0d0f5a1 commit 8060bb9
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 42 deletions.
79 changes: 60 additions & 19 deletions src/actions/audit-log-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
* */

import {
getRequest,
Expand All @@ -19,62 +19,103 @@ import {
authErrorHandler,
escapeFilterValue
} from "openstack-uicore-foundation/lib/utils/actions";
import { getAccessTokenSafely } from "../utils/methods";
import {
getAccessTokenSafely,
isNumericString,
parseDateRangeFilter
} from "../utils/methods";
import { DEFAULT_CURRENT_PAGE, DEFAULT_ORDER_DIR } from "../utils/constants";

export const CLEAR_LOG_PARAMS = "CLEAR_LOG_PARAMS";
export const REQUEST_LOG = "REQUEST_LOG";
export const RECEIVE_LOG = "RECEIVE_LOG";

const DEFAULT_PER_PAGE_AUDIT_LOG = 100;

const parseFilters = (filters, term = null) => {
const filter = [];

if (filters.created_date_filter) {
parseDateRangeFilter(filter, filters.created_date_filter, "created");
}

if (
filters.hasOwnProperty("user_id_filter") &&
Array.isArray(filters.user_id_filter) &&
filters.user_id_filter.length > 0
) {
filter.push(
`user_id==${filters.user_id_filter.map((t) => t.id).join("||")}`
);
}

if (term) {
const escapedTerm = escapeFilterValue(term);
let searchString = "";

if (isNumericString(term)) {
searchString += `entity_id==${term}`;
} else {
searchString += `action=@${escapedTerm}`;
}

filter.push(searchString);
}

return filter;
};

export const getAuditLog =
(
entityFilter = [],
term = null,
page = 1,
perPage = 100,
page = DEFAULT_CURRENT_PAGE,
perPage = DEFAULT_PER_PAGE_AUDIT_LOG,
order = null,
orderDir = 1
orderDir = DEFAULT_ORDER_DIR,
filters = {}
) =>
async (dispatch, getState) => {
const { currentSummitState } = getState();
const accessToken = await getAccessTokenSafely();
const { currentSummit } = currentSummitState;
const filter = [`summit_id==${currentSummit.id}`];
const summitTZ = currentSummit.time_zone.name;
const summitFilter = [`summit_id==${currentSummit.id}`];

dispatch(startLoading());

if (term) {
const escapedTerm = escapeFilterValue(term);
filter.push(
`user_email=@${escapedTerm},user_full_name=@${escapedTerm},action=@${escapedTerm}`
);
}

const params = {
page: page,
page,
per_page: perPage,
expand: "user",
access_token: accessToken
};

params["filter[]"] = [...filter, ...entityFilter];
const parsedFilters = [
...summitFilter,
...entityFilter,
...parseFilters(filters, term)
];

params["filter[]"] = parsedFilters;

// order
if (order != null && orderDir != null) {
const orderDirSign = orderDir === 1 ? "+" : "-";
params["order"] = `${orderDirSign}${order}`;
const orderDirSign = orderDir === DEFAULT_ORDER_DIR ? "+" : "-";
params.order = `${orderDirSign}${order}`;
}

return getRequest(
createAction(REQUEST_LOG),
createAction(RECEIVE_LOG),
`${window.API_BASE_URL}/api/v1/audit-logs`,
authErrorHandler,
{ page, perPage, order, orderDir, term }
{ page, perPage, order, orderDir, term, summitTZ, filters }
)(params)(dispatch).then(() => {
dispatch(stopLoading());
});
};

export const clearAuditLogParams = () => async (dispatch, getState) => {
export const clearAuditLogParams = () => async (dispatch) => {
dispatch(createAction(CLEAR_LOG_PARAMS)());
};
Loading

0 comments on commit 8060bb9

Please sign in to comment.