import { Inject, Injectable } from '@nestjs/common'; import type { CustomerDirectory } from '../../common/security/scope/customer-directory'; import type { PrismaService } from '../../infra/prisma/prisma.service'; import { PRISMA } from '../../infra/tokens'; /** * 真实客户目录(A3 替换 A2 的 EmptyCustomerDirectory)。 * 只读,供 ScopeResolver 解析六类数据范围;不参与业务写操作。 */ @Injectable() export class PrismaCustomerDirectory implements CustomerDirectory { constructor(@Inject(PRISMA) private readonly prisma: PrismaService) {} async findOwnedCustomerIds(userId: string): Promise { const rows = await this.prisma.customer.findMany({ where: { ownerUserId: userId, deletedAt: null }, select: { id: true }, }); return rows.map((row) => row.id); } async findCustomerGroupCustomerIds(customerGroupIds: readonly string[]): Promise { if (customerGroupIds.length === 0) return []; const rows = await this.prisma.customerGroupMember.findMany({ where: { groupId: { in: [...customerGroupIds] }, customer: { deletedAt: null } }, select: { customerId: true }, }); return [...new Set(rows.map((row) => row.customerId))].sort(); } async findDepartmentCustomerIds(departmentIds: readonly string[]): Promise { if (departmentIds.length === 0) return []; const rows = await this.prisma.customer.findMany({ where: { deletedAt: null, owner: { departmentId: { in: [...departmentIds] } }, }, select: { id: true }, }); return rows.map((row) => row.id); } async findCreatedCustomerIds(userId: string): Promise { const rows = await this.prisma.customer.findMany({ where: { createdBy: userId, deletedAt: null }, select: { id: true }, }); return rows.map((row) => row.id); } }