From d9a0edcc3ecb0a0a481562b245ef2b73bb3e11ac Mon Sep 17 00:00:00 2001 From: Davo00 Date: Wed, 22 Nov 2023 09:44:22 +0100 Subject: [PATCH] filtering in SQL --- .../resource-query.component.html | 4 +- .../resource-query.component.ts | 42 +++++++++++++++++ .../src/app/common/services/query.service.ts | 46 +------------------ 3 files changed, 45 insertions(+), 47 deletions(-) diff --git a/workbench/frontend/src/app/common/components/resource-query/resource-query.component.html b/workbench/frontend/src/app/common/components/resource-query/resource-query.component.html index 6638315..0bc0d0f 100755 --- a/workbench/frontend/src/app/common/components/resource-query/resource-query.component.html +++ b/workbench/frontend/src/app/common/components/resource-query/resource-query.component.html @@ -25,8 +25,8 @@

Query Templates:

- Crowd - Internal + Crowd + Internal
diff --git a/workbench/frontend/src/app/common/components/resource-query/resource-query.component.ts b/workbench/frontend/src/app/common/components/resource-query/resource-query.component.ts index abf8886..4d1476b 100644 --- a/workbench/frontend/src/app/common/components/resource-query/resource-query.component.ts +++ b/workbench/frontend/src/app/common/components/resource-query/resource-query.component.ts @@ -59,6 +59,8 @@ export class ResourceQueryComponent implements OnInit, OnDestroy { public selectedSkills: string[] = []; public selectedTemplate = 'default'; + public crowdChecked = false; + public internalChecked = false; @Input() selectedMandatorySkills: string[]; @@ -173,6 +175,7 @@ export class ResourceQueryComponent implements OnInit, OnDestroy { public applyTmpl(t: keyof typeof TEMPLATES): void { this.selectedTemplate = t; this.form.patchValue({ query: TEMPLATES[t] }); + this.updateSqlCode(); } private updateResources(skills): void { @@ -191,4 +194,43 @@ export class ResourceQueryComponent implements OnInit, OnDestroy { } } + public updateSqlCode(): void { + const updateCondition = (conditionToAdd: string, include: boolean): void => { + const query = this.form.value.query; + + if (query.trim() !== '') { + if (include) { + // If including, add the new condition + const updatedQuery = this.insertTextAfterWhere(query, conditionToAdd); + this.form.patchValue({ query: updatedQuery }); + } else { + // If excluding, remove the existing condition + const modifiedQuery = this.removeCondition(query, conditionToAdd); + this.form.patchValue({ query: modifiedQuery }); + } + } + }; + + updateCondition('resource.crowdType LIKE \'Crowd\'\n\tAND ', this.crowdChecked); + updateCondition('resource.crowdType LIKE \'Non_Crowd\'\n\tAND ', this.internalChecked); + } + + private insertTextAfterWhere(input: string, newText: string): string { + const whereRegex = /\bWHERE\b/i; // case-insensitive match for WHERE + const whereMatch = whereRegex.exec(input); + + if (whereMatch) { + const insertionIndex = whereMatch.index + whereMatch[0].length; + + return `${input.slice(0, insertionIndex)} ${newText}${input.slice(insertionIndex)}`; + } else { + return `${input}\n${newText}`; + } + } + + private removeCondition(query: string, conditionToRemove: string): string { + return query.replace(conditionToRemove, ''); + } + + } diff --git a/workbench/frontend/src/app/common/services/query.service.ts b/workbench/frontend/src/app/common/services/query.service.ts index 401d7a0..fbb1d29 100644 --- a/workbench/frontend/src/app/common/services/query.service.ts +++ b/workbench/frontend/src/app/common/services/query.service.ts @@ -110,52 +110,8 @@ export class QueryService { ); } - public queryResourceSkills, item extends { - id: string, - firstName: string, - lastName: string - }>(query: string): Observable { - return combineLatest([ - this._query(query), - this._query, TagObj>(`SELECT tag.id as id, tag.name as name - FROM Tag tag - WHERE tag.inactive = false`), - this._query, SkillObj>(`SELECT skill.id as id, - skill.tag as tag, - skill.person as person, - skill.startDate as startDate, - skill.endDate as endDate - FROM Skill skill - WHERE skill.inactive = false`) - ]) - .pipe( - map(([resources, tags, skills]) => { - const tagMap = tags.data.reduce((m, { id, name }) => m.set(id, name), new Map()); - const skillMap = skills.data.reduce((m, skill) => { - const currentItem = { - name: (tagMap.get(skill.tag) || skill.tag), - start: skill.startDate === 'null' ? null : skill.startDate, - end: skill.endDate === 'null' ? null : skill.endDate - }; - if (m.has(skill.person)) { - m.get(skill.person)?.push(currentItem); - } else { - m.set(skill.person, [currentItem]); - } - return m; - }, new Map()); - - return resources.data.map(it => ({ - ...it, - skills: skillMap.has(it.id) ? skillMap.get(it.id).sort((a, b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0)) : [] - })); - }), - tap((currentList) => this.addToCache('resource', currentList)) - ); - } - - public queryResourceSkills_new, item extends { + public queryResourceSkills, item extends { id: string, firstName: string, lastName: string,