add auth roles and guards 050220

This commit is contained in:
2026-05-11 12:40:42 +03:30
parent 03ac02bee9
commit 3e5543a548
29 changed files with 924 additions and 30 deletions
+36 -1
View File
@@ -1,5 +1,6 @@
import {
ConflictException,
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
@@ -7,6 +8,8 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
import { BusinessEntity } from '../../entities/business.entity';
import { UserRole } from '../../entities/user.entity';
import { AuthUser } from '../auth/auth.types';
import { CreateBusinessDto } from './dto/create-business.dto';
import { UpdateBusinessDto } from './dto/update-business.dto';
@@ -80,8 +83,13 @@ export class BusinessesService {
return this.repo.save(entity);
}
async update(id: string, payload: UpdateBusinessDto): Promise<BusinessEntity> {
async update(
id: string,
payload: UpdateBusinessDto,
actor?: AuthUser,
): Promise<BusinessEntity> {
const business = await this.findOne(id);
this.assertCanUpdateBusiness(business, payload, actor);
if (payload.slug && payload.slug !== business.slug) {
const slugTaken = await this.repo.findOne({ where: { slug: payload.slug } });
@@ -111,4 +119,31 @@ export class BusinessesService {
throw new NotFoundException('Business category not found');
}
}
private assertCanUpdateBusiness(
business: BusinessEntity,
payload: UpdateBusinessDto,
actor?: AuthUser,
): void {
if (!actor || actor.role === UserRole.ADMIN) {
return;
}
if (
actor.role !== UserRole.BUSINESS_OWNER ||
business.ownerId !== actor.id
) {
throw new ForbiddenException('You can only manage your own business');
}
if (
payload.ownerId !== undefined ||
payload.categoryId !== undefined ||
payload.isActive !== undefined
) {
throw new ForbiddenException(
'Only the super admin can change owner, category, or active status',
);
}
}
}