All files / src/components/tek-grid/tek-datasource tek-rest-datasource.ts

100% Statements 77/77
100% Branches 43/43
100% Functions 19/19
100% Lines 75/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225112x 112x   112x                   37x         37x         37x             37x   37x 35x 35x   37x   37x 37x 37x       5x 5x       65x 5x 5x 5x 5x 5x       10x       3x 3x 2x   3x 2x   3x       3x 3x   3x 3x 2x         3x 3x 2x         3x                   2x 1x 1x   1x                 1x 1x                 7x 7x 12x 7x   5x     7x               1x 1x               12x 12x 12x 12x 2x 1x       1x         12x                 27x           26x                       28x 56x   28x   15x   15x     28x 11x     28x 2x   28x       3x       1x                 112x  
import { DatasourceFactory, IDatasource, IDictionary, RestDatasource, URL } from '@zeedhi/core';
import { DynamicFilterOperations, DynamicFilterRelations, IDynamicFilterItem, ITekRestDatasource } from './interfaces';
 
export class TekRestDatasource extends RestDatasource implements ITekRestDatasource {
	/** Dynamic filter data */
	public dynamicFilter!: IDictionary<IDynamicFilterItem[]>;
 
	/** Search Join data */
	public searchJoin!: IDictionary<Array<string | number>>;
 
	/**
	 * Dynamic Filter Operations
	 */
	public dynamicFilterOperations = DynamicFilterOperations;
 
	/**
	 * Dynamic Filter Relations
	 */
	public dynamicFilterRelations = DynamicFilterRelations;
 
	/**
	 * Dynamic Filter applied flag
	 */
	protected dynamicFilterApplied = '';
 
	/**
	 * Create new datasource
	 * @param props Datasource properties
	 */
	constructor(props: ITekRestDatasource) {
		super({ ...props, lazyLoad: true });
 
		if (!this.watchUrl) {
			this.dynamicFilter = this.getInitValue('dynamicFilter', props.dynamicFilter, {});
			this.searchJoin = this.getInitValue('searchJoin', props.searchJoin, {});
		}
		this.lazyLoad = this.getInitValue('lazyLoad', props.lazyLoad, this.lazyLoad);
 
		this.createAccessors();
		this.createObjAccessors(this.dynamicFilter, 'dynamicFilter');
		this.createObjAccessors(this.searchJoin, 'searchJoin');
	}
 
	protected updateReservedKeys() {
		this.reservedKeys.dynamic_filter = true;
		this.reservedKeys.search_join = true;
	}
 
	protected updateInternalProperties(datasource: IDatasource = {}) {
		if (!this.watchUrl) return;
		this.updateReservedKeys();
		super.updateInternalProperties(datasource);
		const queryString = URL.getParsedQueryStringFromUrl();
		this.dynamicFilter = this.getEncodedParam(queryString.dynamic_filter, datasource.dynamicFilter);
		this.searchJoin = this.getEncodedParam(queryString.search_join, datasource.searchJoin);
	}
 
	protected getEncodedParam(urlParam: string, datasourceParam: IDictionary<any> = {}): IDictionary<any> {
		return urlParam ? JSON.parse(atob(urlParam)) : datasourceParam;
	}
 
	protected getQueryStringValues(): IDictionary<any> {
		const values = super.getQueryStringValues();
		if (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
			values.dynamic_filter = btoa(JSON.stringify(this.dynamicFilter));
		}
		if (this.searchJoin && Object.keys(this.searchJoin).length) {
			values.search_join = btoa(JSON.stringify(this.searchJoin));
		}
		return values;
	}
 
	protected getUrlQueryString() {
		const superQueryString = super.getUrlQueryString();
		const query = URL.getParsedQueryStringFromUrl();
 
		let dynamicFilterQuerystring = '';
		if (query.dynamic_filter) {
			dynamicFilterQuerystring = `&${URL.getFormattedQueryString({
				dynamic_filter: query.dynamic_filter,
			})}`;
		}
 
		let searchJoinQuerystring = '';
		if (query.search_join) {
			searchJoinQuerystring = `&${URL.getFormattedQueryString({
				search_join: query.search_join,
			})}`;
		}
 
		return superQueryString + dynamicFilterQuerystring + searchJoinQuerystring;
	}
 
	/**
	 * Adds a new dynamic filter position or replace if exists
	 * @param column Dynamic Filter column name
	 * @param value Dynamic Filter value
	 * @returns Promise with data collection
	 */
	public addDynamicFilter(column: string, value: any) {
		if (this.isValidDynamicFilterValue(column, value)) {
			this.dynamicFilter[column] = value;
			return this.updateDynamicFilter();
		}
		return this.removeDynamicFilter(column);
	}
 
	/**
	 * Removes a dynamic filter position
	 * @param column Dynamic Filter column name
	 * @returns Promise with data collection
	 */
	public removeDynamicFilter(column: string) {
		delete this.dynamicFilter[column];
		return this.updateDynamicFilter();
	}
 
	/**
	 * Sets new dynamic filter value
	 * @param filter Dynamic Filter value
	 * @returns Promise with data collection
	 */
	public setDynamicFilter(filter: IDictionary<any>) {
		this.dynamicFilter = {};
		Object.keys(filter).forEach((column: string) => {
			if (this.isValidDynamicFilterValue(column, filter[column])) {
				this.dynamicFilter[column] = filter[column];
			} else {
				delete this.dynamicFilter[column];
			}
		});
		return this.updateDynamicFilter();
	}
 
	/**
	 * Clears Dynamic filter value
	 * @returns Promise with data collection
	 */
	public clearDynamicFilter() {
		this.dynamicFilter = {};
		return this.updateDynamicFilter();
	}
 
	/**
	 * Resets page and selected rows and tries to update the url
	 * @returns Promise with data collection
	 */
	public async updateDynamicFilter() {
		this.page = this.firstPage;
		this.selectedRows = [];
		this.visibleSelectedRows = [];
		if (this.watchUrl) {
			if (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
				URL.updateQueryString({
					dynamic_filter: btoa(JSON.stringify(this.dynamicFilter)),
				});
			} else {
				URL.updateQueryString({
					dynamic_filter: undefined,
				});
			}
		}
		return this.get();
	}
 
	/**
	 * Checks if a filter value is valid
	 * @param value Filter value
	 * @returns Is valid filter value
	 */
	protected isValidDynamicFilterValue(column: string, value?: IDictionary<any>[]) {
		return (
			!this.reservedKeys[column] &&
			value &&
			value.length > 0 &&
			value.every(
				(filterValue) =>
					this.dynamicFilterOperations[filterValue.operation] &&
					this.dynamicFilterRelations[filterValue.relation] &&
					filterValue.value !== '' &&
					filterValue.value !== null,
			)
		);
	}
 
	/**
	 * Retrieves request params
	 */
	protected getRequestParams(): any {
		const requestParams = super.getRequestParams();
		const isNotEmptyObj = (obj?: IDictionary) => !!(obj && Object.keys(obj).length);
		const isValid =
			this.dynamicFilter &&
			Object.keys(this.dynamicFilter).every((column) => {
				const value = this.dynamicFilter[column];
 
				return value && value.length > 0 && this.isValidDynamicFilterValue(column, value);
			});
 
		if (isNotEmptyObj(this.dynamicFilter) && isValid) {
			requestParams.dynamic_filter = btoa(JSON.stringify(this.dynamicFilter));
		}
 
		if (isNotEmptyObj(this.searchJoin)) {
			requestParams.search_join = btoa(JSON.stringify(this.searchJoin));
		}
		return requestParams;
	}
 
	public hasFilter(columnName: string): boolean {
		return this.dynamicFilter[columnName] && this.dynamicFilter[columnName].length > 0;
	}
 
	public clone() {
		return {
			...super.clone(),
			dynamicFilter: this.dynamicFilter,
			searchJoin: this.searchJoin,
			type: 'tek-rest',
		};
	}
}
 
DatasourceFactory.register('tek-rest', TekRestDatasource);