Skip to content

Commit 413c4ac

Browse files
committed
fix(api): improve user query handling for schoolId filtering
1 parent 670a832 commit 413c4ac

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

modules/services/api/src/edu/services/users.service.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,29 @@ export class UsersService extends ScopedEntitiesService<User, Scope> {
1111

1212
constructor(@InjectRepository(User) repository: Repository<User>) {
1313
super(repository, (query, scope) => {
14-
// TODO add support for query.where as array
15-
const q = query?.where as any;
16-
const userId = q?.id;
17-
const schoolId = q?.roles?.schoolId;
14+
if (query?.where instanceof Array) {
15+
// TODO add support for query.where as array
16+
throw new Error('Array where clauses are not supported');
17+
}
1818

19-
// Get all user scopes and filter whem by query params if provided
20-
const userScopes = scope.permissions
19+
const where = query?.where as any;
20+
const schoolId = where?.roles?.schoolId;
21+
22+
// Get all schools permitted for user
23+
const schoolIds = scope.permissions
2124
.getScopes(['users:read'])
22-
.filter((s) => !schoolId || s.schoolId === schoolId);
25+
.filter((s) => !schoolId || s.schoolId === schoolId)
26+
.map((s) => s.schoolId);
2327

2428
// Create a scoped query for the user
25-
const scopedQuery =
26-
userScopes.length > 0
27-
? {
28-
where: [
29-
...userScopes.map((s) => ({
30-
id: userId,
31-
roles: {
32-
schoolId: s.schoolId,
33-
},
34-
})),
35-
],
36-
}
37-
: { where: { id: In([]) } };
29+
const scopedQuery = {
30+
where: {
31+
id: where?.id,
32+
roles: {
33+
schoolId: In(schoolIds),
34+
},
35+
},
36+
};
3837
this.logger.debug(`Scoped Query: ${JSON.stringify(scopedQuery)}`);
3938
return scopedQuery;
4039
});

0 commit comments

Comments
 (0)