30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
import { Controller, Get, Request, Logger } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository, In } from 'typeorm';
|
|
import { Client } from '../database/entities/client.entity';
|
|
import { UserClient } from '../database/entities/user-client.entity';
|
|
|
|
@Controller('api/clients')
|
|
export class ClientsController {
|
|
private readonly logger = new Logger(ClientsController.name);
|
|
|
|
constructor(
|
|
@InjectRepository(Client) private readonly clientRepo: Repository<Client>,
|
|
@InjectRepository(UserClient) private readonly userClientRepo: Repository<UserClient>,
|
|
) {}
|
|
|
|
@Get()
|
|
async getMyClients(@Request() req: any) {
|
|
const userId = req.user.userId;
|
|
const mappings = await this.userClientRepo.find({ where: { UserId: userId } });
|
|
const clientIds = mappings.map((m) => m.ClientId);
|
|
|
|
if (clientIds.length === 0) {
|
|
// Fallback to match old C# behavior where it returned all clients
|
|
// or if admin users shouldn't be restricted initially.
|
|
return this.clientRepo.find();
|
|
}
|
|
return this.clientRepo.find({ where: { Id: In(clientIds) } });
|
|
}
|
|
}
|