fix: improve plugins functionality with server management
This commit is contained in:
@@ -1,11 +1,28 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { RoomEntity } from '../../../entities';
|
||||
import { RoomEntity, RoomOwnerEntity } from '../../../entities';
|
||||
import { rowToRoom } from '../../mappers';
|
||||
import { loadRoomRelationsMap } from '../../relations';
|
||||
import { getCurrentUserScope } from '../../current-user-scope';
|
||||
|
||||
export async function handleGetAllRooms(dataSource: DataSource) {
|
||||
const currentUserId = await getCurrentUserScope(dataSource);
|
||||
|
||||
if (!currentUserId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const repo = dataSource.getRepository(RoomEntity);
|
||||
const rows = await repo.find();
|
||||
const ownershipRows = await dataSource.getRepository(RoomOwnerEntity).find({ where: { userId: currentUserId } });
|
||||
const roomIds = ownershipRows.map((owner) => owner.roomId);
|
||||
|
||||
if (roomIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const rows = await repo
|
||||
.createQueryBuilder('room')
|
||||
.where('room.id IN (:...roomIds)', { roomIds })
|
||||
.getMany();
|
||||
const relationsByRoomId = await loadRoomRelationsMap(dataSource, rows.map((row) => row.id));
|
||||
|
||||
return rows.map((row) => rowToRoom(row, relationsByRoomId.get(row.id)));
|
||||
|
||||
Reference in New Issue
Block a user