2026-06-12 20:37:33 +03:30
|
|
|
import {
|
|
|
|
|
BadRequestException,
|
|
|
|
|
ConflictException,
|
|
|
|
|
ForbiddenException,
|
|
|
|
|
Injectable,
|
|
|
|
|
NotFoundException,
|
|
|
|
|
} from '@nestjs/common';
|
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
import { DataSource, Repository } from 'typeorm';
|
|
|
|
|
import { deleteBusinessCascade } from '../../common/utils/business-cascade.util';
|
|
|
|
|
import { parseHmToMinute } from '../../common/utils/booking-schedule.util';
|
|
|
|
|
import { resolveUniqueSlug, normalizeSlugInput } from '../../common/utils/slug.util';
|
|
|
|
|
import { BusinessAvailabilitySlotEntity } from '../../entities/business-availability-slot.entity';
|
|
|
|
|
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
|
|
|
|
|
import { BusinessEntity } from '../../entities/business.entity';
|
|
|
|
|
import { BusinessImageEntity } from '../../entities/business-image.entity';
|
|
|
|
|
import { UserEntity, UserRole } from '../../entities/user.entity';
|
|
|
|
|
import { AuthUser } from '../auth/auth.types';
|
|
|
|
|
import { BusinessImagesService } from './business-images.service';
|
|
|
|
|
import { AssignBusinessOwnerDto } from './dto/assign-business-owner.dto';
|
|
|
|
|
import { CreateBusinessDto } from './dto/create-business.dto';
|
|
|
|
|
import { SetAvailabilityDto } from './dto/set-availability.dto';
|
|
|
|
|
import { UpdateBusinessDto } from './dto/update-business.dto';
|
2026-05-07 21:22:50 +03:30
|
|
|
|
2026-06-12 20:37:33 +03:30
|
|
|
type FindAllFilters = {
|
|
|
|
|
categoryId?: string;
|
|
|
|
|
isActive?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class BusinessesService {
|
|
|
|
|
constructor(
|
|
|
|
|
@InjectRepository(BusinessEntity)
|
|
|
|
|
private readonly repo: Repository<BusinessEntity>,
|
|
|
|
|
@InjectRepository(BusinessAvailabilitySlotEntity)
|
|
|
|
|
private readonly availabilityRepo: Repository<BusinessAvailabilitySlotEntity>,
|
|
|
|
|
@InjectRepository(BusinessCategoryEntity)
|
|
|
|
|
private readonly categoryRepo: Repository<BusinessCategoryEntity>,
|
|
|
|
|
@InjectRepository(BusinessImageEntity)
|
|
|
|
|
private readonly imageRepo: Repository<BusinessImageEntity>,
|
|
|
|
|
@InjectRepository(UserEntity)
|
|
|
|
|
private readonly userRepo: Repository<UserEntity>,
|
|
|
|
|
private readonly dataSource: DataSource,
|
|
|
|
|
private readonly images: BusinessImagesService,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const businesses = await query.getMany();
|
|
|
|
|
await this.attachCoverImages(businesses);
|
|
|
|
|
return businesses;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findMine(ownerId: string): Promise<BusinessEntity[]> {
|
|
|
|
|
const businesses = await this.repo.find({
|
|
|
|
|
where: { ownerId },
|
|
|
|
|
relations: { category: true },
|
|
|
|
|
order: { createdAt: 'DESC' },
|
|
|
|
|
});
|
|
|
|
|
await this.attachCoverImages(businesses);
|
|
|
|
|
return businesses;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(id: string): Promise<BusinessEntity> {
|
|
|
|
|
const business = await this.repo.findOne({
|
|
|
|
|
where: { id },
|
|
|
|
|
relations: { category: true, images: true },
|
|
|
|
|
});
|
|
|
|
|
if (!business) {
|
|
|
|
|
throw new NotFoundException('Business not found');
|
|
|
|
|
}
|
|
|
|
|
return business;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(payload: CreateBusinessDto): Promise<BusinessEntity> {
|
|
|
|
|
const explicitSlug = normalizeSlugInput(payload.slug);
|
|
|
|
|
if (explicitSlug) {
|
|
|
|
|
const taken = await this.repo.findOne({ where: { slug: explicitSlug } });
|
|
|
|
|
if (taken) {
|
|
|
|
|
throw new ConflictException('Slug already exists');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const slug = await resolveUniqueSlug(
|
|
|
|
|
payload.title,
|
|
|
|
|
async (candidate) => Boolean(await this.repo.findOne({ where: { slug: candidate } })),
|
|
|
|
|
payload.slug,
|
|
|
|
|
'business',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await this.ensureCategoryExists(payload.categoryId);
|
|
|
|
|
|
|
|
|
|
const entity = this.repo.create({
|
|
|
|
|
title: payload.title,
|
|
|
|
|
slug,
|
|
|
|
|
subtitle: payload.subtitle ?? null,
|
|
|
|
|
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,
|
|
|
|
|
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 } });
|
|
|
|
|
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> {
|
|
|
|
|
await this.findOne(id);
|
|
|
|
|
|
|
|
|
|
const imageUrls = await this.images.listUrlsForBusiness(id);
|
|
|
|
|
|
|
|
|
|
await this.dataSource.transaction(async (manager) => {
|
|
|
|
|
await deleteBusinessCascade(manager, id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.images.deleteFiles(imageUrls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getAvailability(businessId: string) {
|
|
|
|
|
await this.findOne(businessId);
|
|
|
|
|
const rows = await this.availabilityRepo.find({
|
|
|
|
|
where: { businessId },
|
|
|
|
|
order: { dayOfWeek: 'ASC', startMinute: 'ASC' },
|
|
|
|
|
});
|
|
|
|
|
return rows.map((row) => ({
|
|
|
|
|
id: row.id,
|
|
|
|
|
dayOfWeek: row.dayOfWeek,
|
|
|
|
|
startTime: this.minuteToHm(row.startMinute),
|
|
|
|
|
endTime: this.minuteToHm(row.endMinute),
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setAvailability(
|
|
|
|
|
businessId: string,
|
|
|
|
|
dto: SetAvailabilityDto,
|
|
|
|
|
actor?: AuthUser,
|
|
|
|
|
) {
|
|
|
|
|
const business = await this.findOne(businessId);
|
|
|
|
|
this.assertCanManageAvailability(business, actor);
|
|
|
|
|
|
|
|
|
|
const entities: BusinessAvailabilitySlotEntity[] = [];
|
|
|
|
|
for (const slot of dto.slots) {
|
|
|
|
|
const startMinute = parseHmToMinute(slot.startTime);
|
|
|
|
|
const endMinute = parseHmToMinute(slot.endTime);
|
|
|
|
|
if (endMinute <= startMinute) {
|
|
|
|
|
throw new BadRequestException('endTime must be after startTime');
|
|
|
|
|
}
|
|
|
|
|
entities.push(
|
|
|
|
|
this.availabilityRepo.create({
|
|
|
|
|
businessId,
|
|
|
|
|
dayOfWeek: slot.dayOfWeek,
|
|
|
|
|
startMinute,
|
|
|
|
|
endMinute,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.availabilityRepo.delete({ businessId });
|
|
|
|
|
if (entities.length) {
|
|
|
|
|
await this.availabilityRepo.save(entities);
|
|
|
|
|
}
|
|
|
|
|
return this.getAvailability(businessId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async assignOwner(
|
|
|
|
|
id: string,
|
|
|
|
|
dto: AssignBusinessOwnerDto,
|
|
|
|
|
): Promise<BusinessEntity> {
|
|
|
|
|
const business = await this.findOne(id);
|
|
|
|
|
const mobile = this.normalizeMobile(dto.mobile);
|
|
|
|
|
|
|
|
|
|
let user = await this.userRepo.findOne({ where: { mobile } });
|
|
|
|
|
if (!user) {
|
|
|
|
|
user = this.userRepo.create({
|
|
|
|
|
mobile,
|
|
|
|
|
fullName: dto.fullName ?? null,
|
|
|
|
|
role: UserRole.BUSINESS_OWNER,
|
|
|
|
|
isActive: true,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
if (user.role !== UserRole.ADMIN) {
|
|
|
|
|
user.role = UserRole.BUSINESS_OWNER;
|
|
|
|
|
}
|
|
|
|
|
if (dto.fullName !== undefined) {
|
|
|
|
|
user.fullName = dto.fullName;
|
|
|
|
|
}
|
|
|
|
|
user.isActive = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const savedUser = await this.userRepo.save(user);
|
|
|
|
|
business.ownerId = savedUser.id;
|
|
|
|
|
return this.repo.save(business);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async attachCoverImages(businesses: BusinessEntity[]): Promise<void> {
|
|
|
|
|
if (!businesses.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ids = businesses.map((b) => b.id);
|
|
|
|
|
const rows = (await this.imageRepo.query(
|
|
|
|
|
`SELECT DISTINCT ON ("businessId") id, "businessId", url, "originalName", "sortOrder", "isCover", "createdAt"
|
|
|
|
|
FROM business_images
|
|
|
|
|
WHERE "businessId" = ANY($1::uuid[])
|
|
|
|
|
ORDER BY "businessId", "isCover" DESC, "sortOrder" ASC`,
|
|
|
|
|
[ids],
|
|
|
|
|
)) as Array<{
|
|
|
|
|
id: string;
|
|
|
|
|
businessId: string;
|
|
|
|
|
url: string;
|
|
|
|
|
originalName: string | null;
|
|
|
|
|
sortOrder: number;
|
|
|
|
|
isCover: boolean;
|
|
|
|
|
createdAt: Date;
|
|
|
|
|
}>;
|
|
|
|
|
|
|
|
|
|
const coverByBusiness = new Map<string, BusinessImageEntity[]>();
|
|
|
|
|
for (const row of rows) {
|
|
|
|
|
coverByBusiness.set(row.businessId, [
|
|
|
|
|
this.imageRepo.create({
|
|
|
|
|
id: row.id,
|
|
|
|
|
businessId: row.businessId,
|
|
|
|
|
url: row.url,
|
|
|
|
|
originalName: row.originalName,
|
|
|
|
|
sortOrder: row.sortOrder,
|
|
|
|
|
isCover: row.isCover,
|
|
|
|
|
createdAt: row.createdAt,
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const business of businesses) {
|
|
|
|
|
business.images = coverByBusiness.get(business.id) ?? [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private minuteToHm(minute: number): string {
|
|
|
|
|
const h = Math.floor(minute / 60);
|
|
|
|
|
const m = minute % 60;
|
|
|
|
|
return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private assertCanManageAvailability(
|
|
|
|
|
business: BusinessEntity,
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private normalizeMobile(mobile: string): string {
|
|
|
|
|
const normalized = String(mobile ?? '').trim();
|
|
|
|
|
if (!/^(\+?\d{10,15}|09\d{9})$/.test(normalized)) {
|
|
|
|
|
throw new BadRequestException('Invalid mobile number');
|
|
|
|
|
}
|
|
|
|
|
return normalized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|