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 { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||||
import { CurrentUser } from '../auth/decorators/current-user.decorator';
|
import { CurrentUser } from '../auth/decorators/current-user.decorator';
|
||||||
import { Permissions } from '../auth/decorators/permissions.decorator';
|
import { Permissions } from '../auth/decorators/permissions.decorator';
|
||||||
@@ -24,14 +28,11 @@ import { SalonDashboardDto } from './dto/salon-dashboard.dto';
|
|||||||
export class DashboardController {
|
export class DashboardController {
|
||||||
constructor(private readonly dashboardService: DashboardService) {}
|
constructor(private readonly dashboardService: DashboardService) {}
|
||||||
|
|
||||||
@Roles(UserRole.ADMIN, UserRole.SALON)
|
@Roles(UserRole.SALON)
|
||||||
@Permissions(PermissionCode.RESERVES_READ)
|
@Permissions(PermissionCode.RESERVES_READ)
|
||||||
@ApiStandardOkResponse(SalonDashboardDto)
|
@ApiStandardOkResponse(SalonDashboardDto)
|
||||||
@Get('salon/:salonId')
|
@Get()
|
||||||
getSalonDashboard(
|
getDashboard(@CurrentUser() user: AuthUser) {
|
||||||
@Param('salonId', ParseUUIDPipe) salonId: string,
|
return this.dashboardService.getDashboard(user);
|
||||||
@CurrentUser() user: AuthUser,
|
|
||||||
) {
|
|
||||||
return this.dashboardService.getSalonDashboard(salonId, user);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,13 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
|||||||
import { AuthGuardsModule } from '../auth/auth-guards.module';
|
import { AuthGuardsModule } from '../auth/auth-guards.module';
|
||||||
import { AuthorizationModule } from '../authorization/authorization.module';
|
import { AuthorizationModule } from '../authorization/authorization.module';
|
||||||
import { Reserve } from '../reserves/entities/reserve.entity';
|
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 { DashboardController } from './dashboard.controller';
|
||||||
import { DashboardService } from './dashboard.service';
|
import { DashboardService } from './dashboard.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forFeature([Reserve]),
|
TypeOrmModule.forFeature([Reserve, Salon]),
|
||||||
SalonsModule,
|
|
||||||
AuthorizationModule,
|
AuthorizationModule,
|
||||||
AuthGuardsModule,
|
AuthGuardsModule,
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { ForbiddenException, Injectable } from '@nestjs/common';
|
import { ForbiddenException, Injectable, NotFoundException } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { AuthUser } from '../auth/interfaces/auth-user.interface';
|
import { AuthUser } from '../auth/interfaces/auth-user.interface';
|
||||||
import { SalonPolicy } from '../authorization/policies/salon.policy';
|
|
||||||
import { ReserveItem } from '../reserves/entities/reserve-item.entity';
|
import { ReserveItem } from '../reserves/entities/reserve-item.entity';
|
||||||
import { Reserve } from '../reserves/entities/reserve.entity';
|
import { Reserve } from '../reserves/entities/reserve.entity';
|
||||||
import { ReserveStatus } from '../reserves/enums/reserve-status.enum';
|
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 {
|
import {
|
||||||
SalonDashboard,
|
SalonDashboard,
|
||||||
SalonDashboardStats,
|
SalonDashboardStats,
|
||||||
@@ -33,19 +33,33 @@ export class DashboardService {
|
|||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Reserve)
|
@InjectRepository(Reserve)
|
||||||
private readonly reservesRepository: Repository<Reserve>,
|
private readonly reservesRepository: Repository<Reserve>,
|
||||||
private readonly salonsService: SalonsService,
|
@InjectRepository(Salon)
|
||||||
private readonly salonPolicy: SalonPolicy,
|
private readonly salonsRepository: Repository<Salon>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async getSalonDashboard(
|
async getDashboard(user: AuthUser): Promise<SalonDashboard> {
|
||||||
salonId: string,
|
const salon = await this.resolveSalonForUser(user);
|
||||||
user: AuthUser,
|
return this.buildDashboard(salon.id);
|
||||||
): Promise<SalonDashboard> {
|
}
|
||||||
const salon = await this.salonsService.findOne(salonId);
|
|
||||||
if (!this.salonPolicy.canRead(user, salon)) {
|
private async resolveSalonForUser(user: AuthUser): Promise<Salon> {
|
||||||
|
if (user.role !== UserRole.SALON) {
|
||||||
throw new ForbiddenException();
|
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({
|
const completedReserves = await this.reservesRepository.find({
|
||||||
where: { salonId, status: ReserveStatus.COMPLETED },
|
where: { salonId, status: ReserveStatus.COMPLETED },
|
||||||
relations: { items: { serviceSkill: true } },
|
relations: { items: { serviceSkill: true } },
|
||||||
|
|||||||
Reference in New Issue
Block a user