Add saved report views with personal/org filters and in-org share links.

This commit is contained in:
2026-09-27 11:11:33 +03:30
parent 6a9e864b07
commit a16c2f0a9b
7 changed files with 550 additions and 34 deletions
@@ -17,6 +17,7 @@ import {
IsString,
MaxLength,
} from 'class-validator';
import { SAVED_VIEW_SCOPES } from '@mento/shared';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { RolesGuard } from '../auth/roles.guard';
@@ -24,21 +25,13 @@ import { OrgModuleGuard } from '../billing/org-module.guard';
import { RequireOrgModule } from '../billing/org-module.decorator';
import { SavedViewsService } from './saved-views.service';
const SCOPES = [
'deals',
'people',
'companies',
'activities',
'project_cards',
] as const;
class CreateSavedViewDto {
@IsString()
@MaxLength(120)
name!: string;
@IsIn(SCOPES)
scope!: (typeof SCOPES)[number];
@IsIn([...SAVED_VIEW_SCOPES])
scope!: (typeof SAVED_VIEW_SCOPES)[number];
@IsObject()
filters!: Record<string, unknown>;
@@ -55,8 +48,8 @@ class UpdateSavedViewDto {
name?: string;
@IsOptional()
@IsIn(SCOPES)
scope?: (typeof SCOPES)[number];
@IsIn([...SAVED_VIEW_SCOPES])
scope?: (typeof SAVED_VIEW_SCOPES)[number];
@IsOptional()
@IsObject()
@@ -81,6 +74,14 @@ export class SavedViewsController {
return this.savedViews.list(user.organizationId, user.userId, scope);
}
@Get(':id')
get(
@CurrentUser() user: { organizationId: string; userId: string },
@Param('id') id: string,
) {
return this.savedViews.get(user.organizationId, user.userId, id);
}
@Post()
create(
@CurrentUser() user: { organizationId: string; userId: string },
@@ -9,17 +9,10 @@ import type {
SavedViewScope,
UpdateSavedViewRequest,
} from '@mento/shared';
import { SAVED_VIEW_SCOPES } from '@mento/shared';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
const SCOPES: SavedViewScope[] = [
'deals',
'people',
'companies',
'activities',
'project_cards',
];
@Injectable()
export class SavedViewsService {
constructor(private readonly prisma: PrismaService) {}
@@ -41,6 +34,25 @@ export class SavedViewsService {
return rows.map((row) => this.toDto(row));
}
async get(
organizationId: string,
userId: string,
id: string,
scope?: string,
): Promise<SavedViewDto> {
const row = await this.prisma.savedView.findFirst({
where: {
id,
organizationId,
...(scope ? { scope } : {}),
OR: [{ isShared: true }, { ownerUserId: userId }],
},
include: { owner: { select: { id: true, fullName: true } } },
});
if (!row) throw new NotFoundException('فیلتر پیدا نشد');
return this.toDto(row);
}
async create(
organizationId: string,
userId: string,
@@ -69,11 +81,15 @@ export class SavedViewsService {
id: string,
dto: UpdateSavedViewRequest,
isAdmin: boolean,
expectedScope?: string,
): Promise<SavedViewDto> {
const existing = await this.prisma.savedView.findFirst({
where: { id, organizationId },
});
if (!existing) throw new NotFoundException('فیلتر پیدا نشد');
if (expectedScope && existing.scope !== expectedScope) {
throw new NotFoundException('فیلتر پیدا نشد');
}
if (!isAdmin && existing.ownerUserId !== userId) {
throw new BadRequestException('فقط مالک فیلتر می‌تواند آن را ویرایش کند');
}
@@ -103,11 +119,15 @@ export class SavedViewsService {
userId: string,
id: string,
isAdmin: boolean,
expectedScope?: string,
): Promise<void> {
const existing = await this.prisma.savedView.findFirst({
where: { id, organizationId },
});
if (!existing) throw new NotFoundException('فیلتر پیدا نشد');
if (expectedScope && existing.scope !== expectedScope) {
throw new NotFoundException('فیلتر پیدا نشد');
}
if (!isAdmin && existing.ownerUserId !== userId) {
throw new BadRequestException('فقط مالک فیلتر می‌تواند آن را حذف کند');
}
@@ -115,7 +135,7 @@ export class SavedViewsService {
}
private assertScope(scope: string): SavedViewScope {
if (!SCOPES.includes(scope as SavedViewScope)) {
if (!SAVED_VIEW_SCOPES.includes(scope as SavedViewScope)) {
throw new BadRequestException('محدوده فیلتر معتبر نیست');
}
return scope as SavedViewScope;