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

115 lines
3.4 KiB
TypeScript
Raw Normal View History

2026-05-07 21:22:50 +03:30
import {
ConflictException,
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';
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);
}
async update(id: string, payload: UpdateBusinessDto): Promise<BusinessEntity> {
const business = await this.findOne(id);
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');
}
}
}