Add deal co-assignees and saved filters on people/pipeline lists.

Deals keep a primary owner plus shared assignees with matching visibility; Contacts and Pipeline can save and reuse filter presets.
This commit is contained in:
2026-09-27 14:25:32 +03:30
parent 00f8b91d78
commit c06c92b333
19 changed files with 553 additions and 20 deletions
+51 -5
View File
@@ -21,6 +21,7 @@ import { WebhooksService } from '../public-api/webhooks.service';
import {
type Actor,
canReassignOwner,
dealAccessWhere,
ownerWhere,
resolveOwnerId,
} from '../common/record-scope';
@@ -29,10 +30,14 @@ const dealInclude = {
person: { select: { id: true, firstName: true, lastName: true } },
company: { select: { id: true, name: true } },
owner: { select: { id: true, fullName: true } },
assignees: { select: { id: true, fullName: true } },
lineItems: { orderBy: { position: 'asc' as const } },
} satisfies Prisma.DealInclude;
type DealActor = Pick<Actor, 'userId' | 'role'>;
type DealActor = Pick<
Actor,
'userId' | 'role' | 'dataVisibility' | 'visibleUserIds'
>;
@Injectable()
export class DealsService {
@@ -93,6 +98,10 @@ export class DealsService {
if (dto.lineItems?.length) {
await this.assertProducts(organizationId, dto.lineItems);
}
const assigneeIds = await this.normalizeAssigneeIds(
organizationId,
dto.assigneeIds,
);
const maxPos = await this.prisma.deal.aggregate({
where: { organizationId, stageId: stage.id },
@@ -133,6 +142,9 @@ export class DealsService {
companyId: emptyToNull(dto.companyId ?? null),
ownerId: resolveOwnerId(actor, dto.ownerId),
position: (maxPos._max.position ?? -1) + 1,
...(assigneeIds.length
? { assignees: { connect: assigneeIds.map((id) => ({ id })) } }
: {}),
...(dto.lineItems?.length
? {
lineItems: {
@@ -219,6 +231,15 @@ export class DealsService {
data.ownerId = resolveOwnerId(actor, dto.ownerId);
}
let assigneeConnect: { id: string }[] | undefined;
if (dto.assigneeIds !== undefined) {
const assigneeIds = await this.normalizeAssigneeIds(
organizationId,
dto.assigneeIds,
);
assigneeConnect = assigneeIds.map((uid) => ({ id: uid }));
}
if (dto.lineItems !== undefined) {
const total = sumLineItems(dto.lineItems);
data.amount =
@@ -248,7 +269,15 @@ export class DealsService {
});
}
}
await tx.deal.update({ where: { id }, data });
await tx.deal.update({
where: { id },
data: {
...data,
...(assigneeConnect
? { assignees: { set: assigneeConnect } }
: {}),
},
});
});
const updated = await this.get(organizationId, id, actor);
@@ -271,7 +300,7 @@ export class DealsService {
dto: MoveDealRequest,
): Promise<DealDto> {
const deal = await this.prisma.deal.findFirst({
where: { id, organizationId, ...ownerWhere(actor) },
where: { id, organizationId, ...dealAccessWhere(actor) },
select: {
id: true,
title: true,
@@ -377,7 +406,7 @@ export class DealsService {
actor: DealActor,
) {
const found = await this.prisma.deal.findFirst({
where: { id, organizationId, ...ownerWhere(actor) },
where: { id, organizationId, ...dealAccessWhere(actor) },
select: { id: true },
});
if (!found) throw new NotFoundException('معامله پیدا نشد');
@@ -389,13 +418,30 @@ export class DealsService {
actor: DealActor,
) {
const row = await this.prisma.deal.findFirst({
where: { id, organizationId, ...ownerWhere(actor) },
where: { id, organizationId, ...dealAccessWhere(actor) },
include: dealInclude,
});
if (!row) throw new NotFoundException('معامله پیدا نشد');
return row;
}
private async normalizeAssigneeIds(
organizationId: string,
raw?: string[] | null,
): Promise<string[]> {
const ids = [
...new Set((raw ?? []).map((id) => id.trim()).filter(Boolean)),
];
if (!ids.length) return [];
const count = await this.prisma.user.count({
where: { organizationId, id: { in: ids }, isActive: true },
});
if (count !== ids.length) {
throw new BadRequestException('یکی از مسئولان انتخاب‌شده معتبر نیست');
}
return ids;
}
private async assertProducts(
organizationId: string,
items: DealLineItemInput[],