tipsEnabled
This commit is contained in:
@@ -13,6 +13,7 @@ import { resolveUniqueSlug, normalizeSlugInput } from '../../common/utils/slug.u
|
||||
import { BusinessAvailabilitySlotEntity } from '../../entities/business-availability-slot.entity';
|
||||
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
|
||||
import { BusinessEntity } from '../../entities/business.entity';
|
||||
import { BookingReviewEntity } from '../../entities/booking-review.entity';
|
||||
import { BusinessImageEntity } from '../../entities/business-image.entity';
|
||||
import { UserEntity, UserRole } from '../../entities/user.entity';
|
||||
import { AuthUser } from '../auth/auth.types';
|
||||
@@ -40,6 +41,8 @@ export class BusinessesService {
|
||||
private readonly imageRepo: Repository<BusinessImageEntity>,
|
||||
@InjectRepository(UserEntity)
|
||||
private readonly userRepo: Repository<UserEntity>,
|
||||
@InjectRepository(BookingReviewEntity)
|
||||
private readonly reviewRepo: Repository<BookingReviewEntity>,
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly images: BusinessImagesService,
|
||||
) {}
|
||||
@@ -64,6 +67,7 @@ export class BusinessesService {
|
||||
|
||||
const businesses = await query.getMany();
|
||||
await this.attachCoverImages(businesses);
|
||||
await this.attachReviewStats(businesses);
|
||||
return businesses;
|
||||
}
|
||||
|
||||
@@ -88,6 +92,7 @@ export class BusinessesService {
|
||||
throw new NotFoundException('Business not found');
|
||||
}
|
||||
business.images = this.sortImages(business.images ?? []);
|
||||
await this.attachReviewStats([business]);
|
||||
return business;
|
||||
}
|
||||
|
||||
@@ -398,6 +403,44 @@ export class BusinessesService {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private async attachReviewStats(businesses: BusinessEntity[]): Promise<void> {
|
||||
if (!businesses.length) return;
|
||||
|
||||
const ids = businesses.map((business) => business.id);
|
||||
const rows = await this.reviewRepo
|
||||
.createQueryBuilder('review')
|
||||
.select('review.businessId', 'businessId')
|
||||
.addSelect('AVG(review.rating)', 'averageRating')
|
||||
.addSelect('COUNT(review.id)', 'reviewCount')
|
||||
.where('review.businessId IN (:...ids)', { ids })
|
||||
.groupBy('review.businessId')
|
||||
.getRawMany<{
|
||||
businessId: string;
|
||||
averageRating: string;
|
||||
reviewCount: string;
|
||||
}>();
|
||||
|
||||
const statsById = new Map(
|
||||
rows.map((row) => [
|
||||
row.businessId,
|
||||
{
|
||||
averageRating: Number.parseFloat(row.averageRating),
|
||||
reviewCount: Number.parseInt(row.reviewCount, 10),
|
||||
},
|
||||
]),
|
||||
);
|
||||
|
||||
for (const business of businesses) {
|
||||
const stats = statsById.get(business.id);
|
||||
(business as BusinessEntity & {
|
||||
averageRating?: number | null;
|
||||
reviewCount?: number;
|
||||
}).averageRating = stats?.averageRating ?? null;
|
||||
(business as BusinessEntity & { reviewCount?: number }).reviewCount =
|
||||
stats?.reviewCount ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
private assertCanUpdateBusiness(
|
||||
business: BusinessEntity,
|
||||
payload: UpdateBusinessDto,
|
||||
|
||||
Reference in New Issue
Block a user