Skip to content

Commit af394eb

Browse files
committed
feat(api): enhance SchoolsService query to handle permissions and scopes
1 parent 24631b4 commit af394eb

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,30 @@ import { Scope, ScopedEntitiesService } from './entities.service';
99
export class SchoolsService extends ScopedEntitiesService<School, Scope> {
1010
constructor(@InjectRepository(School) repository: Repository<School>) {
1111
super(repository, (query, scope) => {
12-
const orgIds = scope.permissions.getOrganizations(['orgs:read']);
13-
const schoolIds = scope.permissions.getSchools(['schools:read']);
14-
return {
15-
where: {
16-
...query?.where,
17-
id: schoolIds.length > 0 ? In(schoolIds) : undefined,
18-
organizationId: In(orgIds),
19-
},
20-
};
12+
// HACK: For some reason FindOptionsWhere<Organization> doesn't
13+
// work here, so we have to cast it to any. It doesn't contain
14+
// any fields like id, name, etc. But it should.
15+
const where = query?.where as any;
16+
17+
// TODO Have't tested yet
18+
19+
// Get all scopes that have the required permission
20+
// and match the organization and school ids in the query
21+
// if they are provided
22+
const scopes = scope.permissions
23+
.getScopes(['schools:read'])
24+
.filter((s) => !where?.id || s.schoolId === where?.id);
25+
26+
// Return the query with the scopes applied or an empty query
27+
// if no scopes were found for the user permissions
28+
return scopes.length > 0
29+
? {
30+
where: scopes.map((s) => ({
31+
...query?.where,
32+
id: s.schoolId,
33+
})),
34+
}
35+
: { where: { id: In([]) } };
2136
});
2237
}
2338
}

0 commit comments

Comments
 (0)