Skip to content

Commit 76dc0f6

Browse files
committed
correction filtre dans la corbeile bug #168
1 parent d7a8255 commit 76dc0f6

3 files changed

Lines changed: 45 additions & 19 deletions

File tree

apps/api/src/_common/abstracts/abstract.service.schema.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,16 @@ export abstract class AbstractServiceSchema<T extends AbstractSchema | Document
7878
}
7979

8080
public async trashAndCount<T extends AbstractSchema | Document>(
81+
filter?: FilterQuery<T>,
8182
projection?: ProjectionType<T> | null | undefined,
8283
options?: QueryOptions<T> | null | undefined,
8384
): Promise<[Array<T & Query<T, T, any, T>>, number]> {
84-
const filter = { deletedFlag: true }
85-
let count = await this._model.countDocuments(filter).exec()
86-
let data = await this._model.find<T & Query<T, T, any, T>>(filter, projection, options).exec()
85+
this.logger.debug(['trashAndCount', JSON.stringify(Object.values(arguments))].join(' '))
86+
const trashFilter = { deletedFlag: true }
87+
let trashedFilter = { ...(filter || {}), ...trashFilter } as FilterQuery<T>
88+
trashedFilter = normalizeMongoFilterValues(trashedFilter)
89+
let count = await this._model.countDocuments(trashedFilter).exec()
90+
let data = await this._model.find<T & Query<T, T, any, T>>(trashedFilter, projection, options).exec()
8791
return [data, count]
8892
}
8993
public async findAndCount<T extends AbstractSchema | Document>(

apps/api/src/management/identities/identities-crud.controller.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,32 @@ export class IdentitiesCrudController extends AbstractController {
109109
});
110110
}
111111

112+
/**
113+
* Construit le filtre Mongo commun aux listes d'identités (recherche plein texte + filtres `filters[...]`).
114+
* Le type `FilterSchema` est récursif (valeurs attendues) alors qu'on injecte ici des opérateurs Mongo,
115+
* d'où les casts `any` volontaires.
116+
*/
117+
protected buildIdentitiesSearchFilter(
118+
search: string,
119+
searchFilterSchema: FilterSchema,
120+
searchFields: string | string[],
121+
): any {
122+
const searchFilters: any[] = [];
123+
124+
if (search && search.trim().length > 0) {
125+
const effectiveSearchFields = mergeIdentitySearchFields(searchFields);
126+
searchFilters.push({
127+
$or: Object.keys(effectiveSearchFields).map((key) => ({
128+
[key]: { $regex: `^${search}`, $options: 'i' },
129+
})),
130+
});
131+
}
132+
133+
searchFilters.push(searchFilterSchema);
134+
135+
return searchFilters.length === 1 ? searchFilters[0] : { $and: searchFilters };
136+
}
137+
112138
@Get('getdeleted')
113139
@UseRoles({
114140
resource: '/management/identities',
@@ -119,7 +145,9 @@ export class IdentitiesCrudController extends AbstractController {
119145
public async getdeleted(
120146
@Res() res: Response,
121147
@Query('search') search: string,
148+
@SearchFilterSchema() searchFilterSchema: FilterSchema,
122149
@SearchFilterOptions() searchFilterOptions: FilterOptions,
150+
@Query('searchFields') searchFields: string | string[],
123151
): Promise<
124152
Response<
125153
{
@@ -132,7 +160,12 @@ export class IdentitiesCrudController extends AbstractController {
132160
any
133161
>
134162
> {
135-
const [data, total] = await this._service.trashAndCount(IdentitiesCrudController.projection, searchFilterOptions);
163+
const searchFilter = this.buildIdentitiesSearchFilter(search, searchFilterSchema, searchFields);
164+
const [data, total] = await this._service.trashAndCount(
165+
searchFilter,
166+
IdentitiesCrudController.projection,
167+
searchFilterOptions,
168+
);
136169
return res.status(HttpStatus.OK).json({
137170
statusCode: HttpStatus.OK,
138171
total,
@@ -163,7 +196,6 @@ export class IdentitiesCrudController extends AbstractController {
163196
validations?: MixedValue;
164197
}>
165198
> {
166-
const searchFilters = [];
167199
// Par défaut, on cache les identités "ne pas synchroniser" dans la recherche.
168200
// Si le client fournit déjà un filtre `state`, on ne l'écrase pas.
169201
// Le type `FilterSchema` est récursif (valeurs attendues), alors que pour Mongo on injecte parfois
@@ -173,19 +205,7 @@ export class IdentitiesCrudController extends AbstractController {
173205
effectiveSearchFilterSchema.state = { $ne: IdentityState.DONT_SYNC };
174206
}
175207

176-
if (search && search.trim().length > 0) {
177-
const effectiveSearchFields = mergeIdentitySearchFields(searchFields);
178-
const searchRequest = {};
179-
searchRequest['$or'] = Object.keys(effectiveSearchFields)
180-
.map((key) => {
181-
return { [key]: { $regex: `^${search}`, $options: 'i' } };
182-
})
183-
.filter((item) => item !== undefined);
184-
searchFilters.push(searchRequest);
185-
searchFilters.push(effectiveSearchFilterSchema);
186-
} else {
187-
searchFilters.push(effectiveSearchFilterSchema);
188-
}
208+
const searchFilters: any[] = [this.buildIdentitiesSearchFilter(search, effectiveSearchFilterSchema, searchFields)];
189209

190210
const expiredQuery = parseInitInvitationExpiredQuery(initInvitationExpired);
191211
if (expiredQuery !== null) {

apps/web/src/pages/identities/trash.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ export default defineNuxtComponent({
7474
const { getStateValue, fetchAllStateCount } = useIdentityStateStore()
7575
const { getStateName } = useIdentityStates()
7676
const { countFilters, hasFilters, getFilters, removeFilter } = useFiltersQuery(columns)
77-
const { buildSearchFieldsHint } = useIdentitySearchFields()
77+
const { getSearchFieldsQuery, buildSearchFieldsHint } = useIdentitySearchFields()
78+
const searchFieldsQuery = getSearchFieldsQuery()
7879
const searchFieldsHint = computed(() => buildSearchFieldsHint(columns.value))
7980
8081
const computedQuery = computed(() => {
8182
return {
8283
...getDefaults(),
84+
...searchFieldsQuery,
8385
...$route.query,
8486
}
8587
})

0 commit comments

Comments
 (0)