From 94367752a2d357a6f1cbf470f70b8addd600e96e Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Tue, 7 Jul 2026 20:24:56 +0330 Subject: [PATCH] fix: get salon id from token --- src/dashboard/dashboard.controller.ts | 17 +++++++------ src/dashboard/dashboard.module.ts | 5 ++-- src/dashboard/dashboard.service.ts | 36 +++++++++++++++++++-------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/dashboard/dashboard.controller.ts b/src/dashboard/dashboard.controller.ts index 2a7c5ce..d4a795a 100644 --- a/src/dashboard/dashboard.controller.ts +++ b/src/dashboard/dashboard.controller.ts @@ -1,4 +1,8 @@ -import { Controller, Get, Param, ParseUUIDPipe, UseGuards } from '@nestjs/common'; +import { + Controller, + Get, + UseGuards, +} from '@nestjs/common'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; import { CurrentUser } from '../auth/decorators/current-user.decorator'; import { Permissions } from '../auth/decorators/permissions.decorator'; @@ -24,14 +28,11 @@ import { SalonDashboardDto } from './dto/salon-dashboard.dto'; export class DashboardController { constructor(private readonly dashboardService: DashboardService) {} - @Roles(UserRole.ADMIN, UserRole.SALON) + @Roles(UserRole.SALON) @Permissions(PermissionCode.RESERVES_READ) @ApiStandardOkResponse(SalonDashboardDto) - @Get('salon/:salonId') - getSalonDashboard( - @Param('salonId', ParseUUIDPipe) salonId: string, - @CurrentUser() user: AuthUser, - ) { - return this.dashboardService.getSalonDashboard(salonId, user); + @Get() + getDashboard(@CurrentUser() user: AuthUser) { + return this.dashboardService.getDashboard(user); } } diff --git a/src/dashboard/dashboard.module.ts b/src/dashboard/dashboard.module.ts index ea119e0..327eabf 100644 --- a/src/dashboard/dashboard.module.ts +++ b/src/dashboard/dashboard.module.ts @@ -3,14 +3,13 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { AuthGuardsModule } from '../auth/auth-guards.module'; import { AuthorizationModule } from '../authorization/authorization.module'; import { Reserve } from '../reserves/entities/reserve.entity'; -import { SalonsModule } from '../salons/salons.module'; +import { Salon } from '../salons/entities/salon.entity'; import { DashboardController } from './dashboard.controller'; import { DashboardService } from './dashboard.service'; @Module({ imports: [ - TypeOrmModule.forFeature([Reserve]), - SalonsModule, + TypeOrmModule.forFeature([Reserve, Salon]), AuthorizationModule, AuthGuardsModule, ], diff --git a/src/dashboard/dashboard.service.ts b/src/dashboard/dashboard.service.ts index 1b1765e..c815370 100644 --- a/src/dashboard/dashboard.service.ts +++ b/src/dashboard/dashboard.service.ts @@ -1,12 +1,12 @@ -import { ForbiddenException, Injectable } from '@nestjs/common'; +import { ForbiddenException, Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { AuthUser } from '../auth/interfaces/auth-user.interface'; -import { SalonPolicy } from '../authorization/policies/salon.policy'; import { ReserveItem } from '../reserves/entities/reserve-item.entity'; import { Reserve } from '../reserves/entities/reserve.entity'; import { ReserveStatus } from '../reserves/enums/reserve-status.enum'; -import { SalonsService } from '../salons/salons.service'; +import { Salon } from '../salons/entities/salon.entity'; +import { UserRole } from '../users/enums/user-role.enum'; import { SalonDashboard, SalonDashboardStats, @@ -33,19 +33,33 @@ export class DashboardService { constructor( @InjectRepository(Reserve) private readonly reservesRepository: Repository, - private readonly salonsService: SalonsService, - private readonly salonPolicy: SalonPolicy, + @InjectRepository(Salon) + private readonly salonsRepository: Repository, ) {} - async getSalonDashboard( - salonId: string, - user: AuthUser, - ): Promise { - const salon = await this.salonsService.findOne(salonId); - if (!this.salonPolicy.canRead(user, salon)) { + async getDashboard(user: AuthUser): Promise { + const salon = await this.resolveSalonForUser(user); + return this.buildDashboard(salon.id); + } + + private async resolveSalonForUser(user: AuthUser): Promise { + if (user.role !== UserRole.SALON) { throw new ForbiddenException(); } + const salon = await this.salonsRepository.findOne({ + where: { ownerId: user.id }, + order: { createdAt: 'ASC' }, + }); + + if (!salon) { + throw new NotFoundException('Salon not found for this user'); + } + + return salon; + } + + private async buildDashboard(salonId: string): Promise { const completedReserves = await this.reservesRepository.find({ where: { salonId, status: ReserveStatus.COMPLETED }, relations: { items: { serviceSkill: true } },