import { BadRequestException } from '@nestjs/common';
import { Repository } from 'typeorm';
import { Organization } from '../entities/organization.entity';

export async function assertActiveOrganization(
  organizationRepo: Repository<Organization>,
  id: number,
): Promise<void> {
  const organization = await organizationRepo.findOne({ where: { id, status: 'active' } });
  if (!organization) throw new BadRequestException('所属机构不存在或已归档');
}

export async function getHostOrganizationId(
  organizationRepo: Repository<Organization>,
): Promise<number> {
  const organization = await organizationRepo.findOne({
    where: { isHost: true, status: 'active' },
  });
  if (!organization) throw new BadRequestException('尚未配置本机构');
  return organization.id;
}
