Skip to content

Commit 32a9005

Browse files
feat: Added Dynamic Filters
1 parent 55dfb30 commit 32a9005

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed

apps/queue-manager/src/consumers/get-auto-import-job-data.consumer.ts

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
ITemplateSchemaItem,
66
ColumnDelimiterEnum,
77
UserJobImportStatusEnum,
8+
IFilter,
9+
FilterOperationEnum,
810
} from '@impler/shared';
911

1012
import { SendImportJobDataConsumer } from './send-import-job-data.consumer';
@@ -88,8 +90,15 @@ export class SendAutoImportJobDataConsumer extends SendImportJobDataConsumer {
8890

8991
const validationResult = await this.validateData(_jobId, mappedData);
9092

91-
// Send data to webhook with validation status
92-
this.sendDataImportData(_jobId, mappedData, 1, undefined, validationResult.hasInvalidRecords);
93+
if (validationResult.filteredData.length > 0) {
94+
this.sendDataImportData(
95+
_jobId,
96+
validationResult.filteredData,
97+
1,
98+
undefined,
99+
validationResult.hasInvalidRecords
100+
);
101+
}
93102

94103
if (validationResult.hasInvalidRecords) {
95104
await this.userJobRepository.update({ _id: _jobId }, { $set: { isInvalidRecords: true } });
@@ -117,6 +126,7 @@ export class SendAutoImportJobDataConsumer extends SendImportJobDataConsumer {
117126
invalidRecords: number;
118127
validData: Record<string, unknown>[];
119128
invalidData: Record<string, unknown>[];
129+
filteredData: Record<string, unknown>[];
120130
}> {
121131
try {
122132
const userJob = await this.userJobRepository.findOne({ _id: _jobId });
@@ -134,6 +144,7 @@ export class SendAutoImportJobDataConsumer extends SendImportJobDataConsumer {
134144
invalidRecords: 0,
135145
validData: [],
136146
invalidData: [],
147+
filteredData: [],
137148
};
138149
}
139150

@@ -150,7 +161,19 @@ export class SendAutoImportJobDataConsumer extends SendImportJobDataConsumer {
150161
const validData: Record<string, unknown>[] = [];
151162
const invalidData: Record<string, unknown>[] = [];
152163

164+
const filters = userJob.filters || [];
165+
166+
const filteredData: Record<string, unknown>[] = [];
167+
153168
for (const recordData of mappedData) {
169+
const passesFilters = await this.applyDynamicFilters(recordData, filters);
170+
171+
if (passesFilters) {
172+
filteredData.push(recordData);
173+
}
174+
}
175+
176+
for (const recordData of filteredData) {
154177
const checkRecord: Record<string, unknown> = this.formatRecord({
155178
record: { record: recordData },
156179
multiSelectColumnHeadings,
@@ -181,6 +204,7 @@ export class SendAutoImportJobDataConsumer extends SendImportJobDataConsumer {
181204
invalidRecords,
182205
validData,
183206
invalidData,
207+
filteredData,
184208
};
185209
} catch (error) {
186210
return {
@@ -190,6 +214,7 @@ export class SendAutoImportJobDataConsumer extends SendImportJobDataConsumer {
190214
invalidRecords: 0,
191215
validData: [],
192216
invalidData: [],
217+
filteredData: [],
193218
};
194219
}
195220
}
@@ -287,6 +312,68 @@ export class SendAutoImportJobDataConsumer extends SendImportJobDataConsumer {
287312
}
288313
}
289314

315+
private async applyDynamicFilters(record: Record<string, unknown>, filters: IFilter[]): Promise<boolean> {
316+
try {
317+
if (!filters || filters.length === 0) {
318+
return true;
319+
}
320+
321+
for (const filter of filters) {
322+
const fieldValue = record[filter.field];
323+
324+
if (fieldValue === undefined || fieldValue === null) {
325+
return false;
326+
}
327+
328+
const stringValue = String(fieldValue);
329+
const filterValue = String(filter.value);
330+
331+
const operationType = filter.operation;
332+
333+
let operationPassed = false;
334+
335+
switch (operationType) {
336+
case FilterOperationEnum.CONTAINS:
337+
operationPassed = stringValue.includes(filterValue);
338+
break;
339+
340+
case FilterOperationEnum.EQUALS:
341+
operationPassed = stringValue === filterValue;
342+
break;
343+
344+
case FilterOperationEnum.STARTSWITH:
345+
operationPassed = stringValue.startsWith(filterValue);
346+
break;
347+
348+
case FilterOperationEnum.ENDSWITH:
349+
operationPassed = stringValue.endsWith(filterValue);
350+
break;
351+
352+
case FilterOperationEnum.MATCHES:
353+
try {
354+
const regex = new RegExp(filter.value, 'i');
355+
operationPassed = regex.test(stringValue);
356+
} catch (regexError) {
357+
operationPassed = false;
358+
}
359+
break;
360+
361+
default:
362+
operationPassed = false;
363+
break;
364+
}
365+
366+
if (!operationPassed) {
367+
return false;
368+
}
369+
}
370+
371+
return true;
372+
} catch (error) {
373+
return true;
374+
}
375+
}
376+
290377
private async finalizeAutoImportJob(
291378
_jobId: string,
292379
validationResult: IValidationResult,

libs/dal/src/repositories/user-job/user-job.entity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IFilter } from '@impler/shared';
2+
13
export class UserJobEntity {
24
_id?: string;
35

@@ -34,4 +36,6 @@ export class UserJobEntity {
3436
validRecords?: number;
3537

3638
invalidRecords?: number;
39+
40+
filters?: IFilter[];
3741
}

libs/dal/src/repositories/user-job/user-job.schema.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { model, models, Schema, Model } from 'mongoose';
22
import { schemaOptions } from '../schema-default.options';
33
import { UserJobEntity } from './user-job.entity';
4+
import { FilterOperationEnum } from '@impler/shared';
45

56
const userJobSchema = new Schema(
67
{
@@ -62,6 +63,19 @@ const userJobSchema = new Schema(
6263
type: Schema.Types.Number,
6364
default: 0,
6465
},
66+
filters: {
67+
type: [
68+
{
69+
field: { type: Schema.Types.String },
70+
operation: {
71+
type: Schema.Types.String,
72+
enum: Object.values(FilterOperationEnum),
73+
},
74+
value: { type: Schema.Types.String },
75+
},
76+
],
77+
default: [],
78+
},
6579
},
6680
{ ...schemaOptions }
6781
);

libs/shared/src/entities/UserJob/Userjob.interface.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
export enum FilterOperationEnum {
2+
CONTAINS = 'contains',
3+
EQUALS = 'equals',
4+
STARTSWITH = 'startsWith',
5+
ENDSWITH = 'endsWith',
6+
MATCHES = 'matches',
7+
}
8+
9+
export interface IFilter {
10+
field: string;
11+
operation: FilterOperationEnum;
12+
value: string;
13+
}
14+
115
export interface IUserJob {
216
_id: string;
317
url: string;
@@ -7,4 +21,5 @@ export interface IUserJob {
721
endsOn?: Date;
822
headings: string[];
923
_templateId: string;
24+
filters?: IFilter[];
1025
}

0 commit comments

Comments
 (0)