Skip to content

Commit

Permalink
filtering in SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Davo00 committed Nov 22, 2023
1 parent 2d0db31 commit d9a0edc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ <h2>Query Templates:</h2>

<div style="display: inline-flex; flex-wrap: wrap; margin-top: 16px;">
<div style="display: flex; flex-direction: row;">
<mat-checkbox style="margin-right: 16px;">Crowd</mat-checkbox>
<mat-checkbox>Internal</mat-checkbox>
<mat-checkbox style="margin-right: 16px;" [(ngModel)]="crowdChecked" (change)="updateSqlCode()">Crowd</mat-checkbox>
<mat-checkbox [(ngModel)]="internalChecked" (change)="updateSqlCode()">Internal</mat-checkbox>
</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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 {
Expand All @@ -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, '');
}


}
46 changes: 1 addition & 45 deletions workbench/frontend/src/app/common/services/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,52 +110,8 @@ export class QueryService {
);
}

public queryResourceSkills<list extends QueryResponse<item>, item extends {
id: string,
firstName: string,
lastName: string
}>(query: string): Observable<any> {
return combineLatest([
this._query<list, item>(query),
this._query<QueryResponse<TagObj>, TagObj>(`SELECT tag.id as id, tag.name as name
FROM Tag tag
WHERE tag.inactive = false`),
this._query<QueryResponse<SkillObj>, 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<string, string>());
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<string, { name: string, start: string, end: string }[]>());

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<list extends QueryResponse<item>, item extends {
public queryResourceSkills<list extends QueryResponse<item>, item extends {
id: string,
firstName: string,
lastName: string,
Expand Down

0 comments on commit d9a0edc

Please sign in to comment.