fix: get salon id from token
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
],
|
||||
|
||||
@@ -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<Reserve>,
|
||||
private readonly salonsService: SalonsService,
|
||||
private readonly salonPolicy: SalonPolicy,
|
||||
@InjectRepository(Salon)
|
||||
private readonly salonsRepository: Repository<Salon>,
|
||||
) {}
|
||||
|
||||
async getSalonDashboard(
|
||||
salonId: string,
|
||||
user: AuthUser,
|
||||
): Promise<SalonDashboard> {
|
||||
const salon = await this.salonsService.findOne(salonId);
|
||||
if (!this.salonPolicy.canRead(user, salon)) {
|
||||
async getDashboard(user: AuthUser): Promise<SalonDashboard> {
|
||||
const salon = await this.resolveSalonForUser(user);
|
||||
return this.buildDashboard(salon.id);
|
||||
}
|
||||
|
||||
private async resolveSalonForUser(user: AuthUser): Promise<Salon> {
|
||||
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<SalonDashboard> {
|
||||
const completedReserves = await this.reservesRepository.find({
|
||||
where: { salonId, status: ReserveStatus.COMPLETED },
|
||||
relations: { items: { serviceSkill: true } },
|
||||
|
||||
Reference in New Issue
Block a user