1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 07:43:35 +00:00
Files
browser/apps/web/src/app/tools/reports/report-utils.ts
aj-rosado ea76760782 [AC-2508][AC-2511] member access report view and export logic (#10011)
* Added new report card and FeatureFlag for MemberAccessReport

* Add new "isEnterpriseOrgGuard"

* Add member access icon

* Show upgrade organization dialog for enterprise on member access report click

* verify member access featureflag on enterprise org guard

* add comment with TODO information for follow up task

* Initial member access report component

* Improved readability, removed path to wrong component and refactored buildReports to use the productType

* finished MemberAccessReport layout and added temporary service to provide mock data

* Moved member-access-report files to bitwarden_license/
Removed unnecessary files

* Added new tools path on bitwarden_license to the CODEOWNERS file

* added member access description to the messages.json

* layout changes to member access report

* Created new reports-routing under bitwarden_license
Moved member-access-report files to corresponding subfolder

* Added search logic

* Removed routing from member-access-report BL component on OSS.
Added member-access-report navigation to organizations-routing on BL

* removed unnecessary ng-container

* Added OrganizationPermissionsGuard and canAccessReports validation to member-access-report navigation

* replaced deprecated search code with searchControl

* Address PR feedback

* removed unnecessary canAccessReports method

* Added report-utils class with generic functions to support report operations

* Added member access report mock

* Added member access report specific logic

* Splitted code into different classes that explained their objective.

* fixed member access report service test cases

* Addressed PR feedback

* Creating a service to return the data for the member access report

* added missing ExportHelper on index.ts

* Corrected property names on member access report component view

* removed duplicated service
2024-07-11 17:11:05 +01:00

72 lines
2.4 KiB
TypeScript

import * as papa from "papaparse";
/**
* Returns an array of unique items from a collection based on a specified key.
*
* @param {T[]} items The array of items to process.
* @param {(item: T) => K} keySelector A function that selects the key to identify uniqueness.
* @returns {T[]} An array of unique items.
*/
export function getUniqueItems<T, K>(items: T[], keySelector: (item: T) => K): T[] {
const uniqueKeys = new Set<K>();
const uniqueItems: T[] = [];
items.forEach((item) => {
const key = keySelector(item);
if (!uniqueKeys.has(key)) {
uniqueKeys.add(key);
uniqueItems.push(item);
}
});
return uniqueItems;
}
/**
* Sums all the values of a specified numeric property in an array of objects.
*
* @param {T[]} array - The array of objects containing the property to be summed.
* @param {(item: T) => number} getProperty - A function that returns the numeric property value for each object.
* @returns {number} - The total sum of the specified property values.
*/
export function sumValue<T>(values: T[], getProperty: (item: T) => number): number {
return values.reduce((sum, item) => sum + getProperty(item), 0);
}
/**
* Collects a specified property from an array of objects.
*
* @param array The array of objects to collect from.
* @param property The property to collect.
* @returns An array of aggregated values from the specified property.
*/
export function collectProperty<T, K extends keyof T, V>(array: T[], property: K): V[] {
const collected: V[] = array
.map((i) => i[property])
.filter((value) => Array.isArray(value))
.flat() as V[];
return collected;
}
/**
* Exports an array of objects to a CSV string.
*
* @param {T[]} data - An array of objects to be exported.
* @param {[key in keyof T]: string } headers - A mapping of keys of type T to their corresponding header names.
* @returns A string in csv format from the input data.
*/
export function exportToCSV<T>(data: T[], headers?: Partial<{ [key in keyof T]: string }>): string {
const mappedData = data.map((item) => {
const mappedItem: { [key: string]: string } = {};
for (const key in item) {
if (headers != null && headers[key as keyof T]) {
mappedItem[headers[key as keyof T]] = String(item[key as keyof T]);
} else {
mappedItem[key] = String(item[key as keyof T]);
}
}
return mappedItem;
});
return papa.unparse(mappedData);
}