Files
serverMontime/src/modules/businesses/businesses.service.ts
T

150 lines
4.3 KiB
TypeScript
Raw Normal View History

2026-05-07 21:22:50 +03:30
import {
ConflictException,
2026-05-11 12:40:42 +03:30
ForbiddenException,
2026-05-07 21:22:50 +03:30
Injectable,
NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
import { BusinessEntity } from '../../entities/business.entity';
2026-05-11 12:40:42 +03:30
import { UserRole } from '../../entities/user.entity';
import { AuthUser } from '../auth/auth.types';
2026-05-07 21:22:50 +03:30
import { CreateBusinessDto } from './dto/create-business.dto';
import { UpdateBusinessDto } from './dto/update-business.dto';
type FindAllFilters = {
categoryId?: string;
isActive?: boolean;
};
@Injectable()
export class BusinessesService {
constructor(
@InjectRepository(BusinessEntity)
private readonly repo: Repository<BusinessEntity>,
@InjectRepository(BusinessCategoryEntity)
private readonly categoryRepo: Repository<BusinessCategoryEntity>,
) {}
async findAll(filters: FindAllFilters): Promise<BusinessEntity[]> {
const query = this.repo
.createQueryBuilder('business')
.leftJoinAndSelect('business.category', 'category')
.orderBy('business.createdAt', 'DESC');
if (filters.categoryId) {
query.andWhere('business.categoryId = :categoryId', {
categoryId: filters.categoryId,
});
}
if (typeof filters.isActive === 'boolean') {
query.andWhere('business.isActive = :isActive', {
isActive: filters.isActive,
});
}
return query.getMany();
}
async findOne(id: string): Promise<BusinessEntity> {
const business = await this.repo.findOne({
where: { id },
relations: { category: true },
});
if (!business) {
throw new NotFoundException('Business not found');
}
return business;
}
async create(payload: CreateBusinessDto): Promise<BusinessEntity> {
const slugTaken = await this.repo.findOne({ where: { slug: payload.slug } });
if (slugTaken) {
throw new ConflictException('Slug already exists');
}
await this.ensureCategoryExists(payload.categoryId);
const entity = this.repo.create({
title: payload.title,
slug: payload.slug,
categoryId: payload.categoryId,
description: payload.description ?? null,
contactPhone: payload.contactPhone ?? null,
address: payload.address ?? null,
latitude: payload.latitude ?? null,
longitude: payload.longitude ?? null,
ownerId: payload.ownerId ?? null,
isActive: payload.isActive ?? true,
});
return this.repo.save(entity);
}
2026-05-11 12:40:42 +03:30
async update(
id: string,
payload: UpdateBusinessDto,
actor?: AuthUser,
): Promise<BusinessEntity> {
2026-05-07 21:22:50 +03:30
const business = await this.findOne(id);
2026-05-11 12:40:42 +03:30
this.assertCanUpdateBusiness(business, payload, actor);
2026-05-07 21:22:50 +03:30
if (payload.slug && payload.slug !== business.slug) {
const slugTaken = await this.repo.findOne({ where: { slug: payload.slug } });
if (slugTaken) {
throw new ConflictException('Slug already exists');
}
}
if (payload.categoryId && payload.categoryId !== business.categoryId) {
await this.ensureCategoryExists(payload.categoryId);
}
Object.assign(business, payload);
return this.repo.save(business);
}
async remove(id: string): Promise<void> {
const result = await this.repo.delete(id);
if (!result.affected) {
throw new NotFoundException('Business not found');
}
}
private async ensureCategoryExists(categoryId: string): Promise<void> {
const category = await this.categoryRepo.findOne({ where: { id: categoryId } });
if (!category) {
throw new NotFoundException('Business category not found');
}
}
2026-05-11 12:40:42 +03:30
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',
);
}
}
2026-05-07 21:22:50 +03:30
}