Skip to content

Commit

Permalink
fix issue with room visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
N7Remus committed Dec 23, 2024
1 parent cc9d372 commit d8515c9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
44 changes: 44 additions & 0 deletions src/hooks/filterByRoomOwnership.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { HookContext } from '@feathersjs/feathers';

const filterByRoomOwnership = async (context: HookContext): Promise<void> => {
const { app, params } = context;

// Ensure we have an authenticated user
const userId = params.user?.id;

if (!userId) {
throw new Error('User is not authenticated.');
}

// Get the roomOwners service
const roomOwnersService = app.service('roomOwners');

// Query roomOwners to get room IDs for the current user
const roomOwners = await roomOwnersService.find({
paginate: false, // Fetch all relevant records
query: {
userId // Filter by the authenticated user's ID
}
});

if (roomOwners) {
// Extract the list of room IDs
const validRoomIds: string[] = roomOwners.map((owner: any) => owner.roomId);

// If no valid room IDs are found, return an empty result
if (validRoomIds.length === 0) {
context.result = { total: 0, data: [], limit: 0, skip: 0 };
}

// Modify the query to filter rooms by the valid room IDs
context.params.query = {
...params.query,
id: { $in: validRoomIds } // FeathersJS query operator for "in" array
};

}


};

export default filterByRoomOwnership;
8 changes: 5 additions & 3 deletions src/services/rooms/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { isRoomOwnerOrAdmin } from '../../hooks/isRoomOwnerOrAdmin';
import { addRoomOwner } from '../../hooks/addRoomOwner';
import { iff } from 'feathers-hooks-common';
import { notSuperAdmin } from '../../hooks/notSuperAdmin';
import filterByRoomOwnership from '../../hooks/filterByRoomOwnership';

export * from './rooms.class';
export * from './rooms.schema';
Expand All @@ -40,16 +41,16 @@ export const room = (app: Application) => {
all: [
authenticate('jwt'),
schemaHooks.resolveExternal(roomExternalResolver),
schemaHooks.resolveResult(roomResolver)
schemaHooks.resolveResult(roomResolver)
]
},
before: {
all: [
schemaHooks.validateQuery(roomQueryValidator),
iff(notSuperAdmin(), schemaHooks.resolveQuery(roomQueryResolver))
],
find: [],
get: [],
find: [ iff(notSuperAdmin(), filterByRoomOwnership) ],
get: [ iff(notSuperAdmin(), filterByRoomOwnership) ],
create: [
schemaHooks.validateData(roomDataValidator),
schemaHooks.resolveData(roomDataResolver)
Expand All @@ -63,6 +64,7 @@ export const room = (app: Application) => {
},
after: {
all: [],
get: [],
create: [ addRoomOwner ],
},
error: {
Expand Down

0 comments on commit d8515c9

Please sign in to comment.