chore: add notif service for new blog and service comment
This commit is contained in:
@@ -11,9 +11,15 @@ import { DanakServicesCategoryRepository } from "./repositories/danak-services-c
|
||||
import { DanakServicesImageRepository } from "./repositories/danak-services-image.repository";
|
||||
import { DanakServiceReviewRepository } from "./repositories/danak-services-review.repository";
|
||||
import { DanakServicesRepository } from "./repositories/danak-services.repository";
|
||||
import { NotificationModule } from "../notifications/notifications.module";
|
||||
import { UsersModule } from "../users/users.module";
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([DanakService, DanakServiceImage, DanakServiceCategory, DanakServiceReview])],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([DanakService, DanakServiceImage, DanakServiceCategory, DanakServiceReview]),
|
||||
UsersModule,
|
||||
NotificationModule,
|
||||
],
|
||||
providers: [
|
||||
DanakServicesService,
|
||||
DanakServicesRepository,
|
||||
|
||||
@@ -3,7 +3,10 @@ import { DataSource, FindOptionsWhere, In, IsNull, Not, QueryRunner } from "type
|
||||
|
||||
import { ParamDto } from "../../../common/DTO/param.dto";
|
||||
import { CategoryMessage, CommonMessage, ServiceMessage, SubscriptionMessage } from "../../../common/enums/message.enum";
|
||||
import { NotificationsService } from "../../notifications/providers/notifications.service";
|
||||
import { SubscriptionStatus } from "../../subscriptions/enums/subscription-status.enum";
|
||||
import { AdminsService } from "../../users/providers/admins.service";
|
||||
import { UsersService } from "../../users/providers/users.service";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { AddReviewDto } from "../DTO/add-review.dto";
|
||||
import { CategoryListSearchQueryDto, CategorySearchQueryDto } from "../DTO/category-search-query.dto";
|
||||
@@ -21,7 +24,6 @@ import { DanakServicesCategoryRepository } from "../repositories/danak-services-
|
||||
import { DanakServicesImageRepository } from "../repositories/danak-services-image.repository";
|
||||
import { DanakServiceReviewRepository } from "../repositories/danak-services-review.repository";
|
||||
import { DanakServicesRepository } from "../repositories/danak-services.repository";
|
||||
|
||||
@Injectable()
|
||||
export class DanakServicesService {
|
||||
private readonly logger = new Logger(DanakServicesService.name);
|
||||
@@ -31,6 +33,9 @@ export class DanakServicesService {
|
||||
private readonly danakServiceReviewRepository: DanakServiceReviewRepository,
|
||||
private readonly danakServicesImageRepository: DanakServicesImageRepository,
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly adminsService: AdminsService,
|
||||
private readonly notificationsService: NotificationsService,
|
||||
private readonly usersService: UsersService,
|
||||
) {}
|
||||
/******************************************** */
|
||||
|
||||
@@ -492,26 +497,58 @@ export class DanakServicesService {
|
||||
/******************************************** */
|
||||
|
||||
async addReview(userId: string, serviceId: string, addReviewDto: AddReviewDto) {
|
||||
const service = await this.findServiceById(serviceId);
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
const hasSubscription = await this.checkUserPurchasedService(userId, serviceId);
|
||||
if (!hasSubscription) throw new BadRequestException(SubscriptionMessage.NOT_PURCHASED_CANNOT_REVIEW);
|
||||
try {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
// // Check if user already reviewed this service
|
||||
// const existingReview = await this.danakServiceReviewRepository.findOne({
|
||||
// where: { user: { id: userId }, service: { id: serviceId } },
|
||||
// });
|
||||
const user = await this.usersService.findOneByIdWithQueryRunner(userId, queryRunner);
|
||||
|
||||
// if (existingReview) {
|
||||
// throw new BadRequestException(ServiceMessage.ALREADY_REVIEWED);
|
||||
// }
|
||||
const service = await queryRunner.manager.findOne(this.danakServicesRepository.target, {
|
||||
where: { id: serviceId, deletedAt: IsNull() },
|
||||
});
|
||||
|
||||
const review = this.danakServiceReviewRepository.create({ user: { id: userId }, ...addReviewDto, service });
|
||||
await this.danakServiceReviewRepository.save(review);
|
||||
return {
|
||||
message: ServiceMessage.REVIEW_ADDED,
|
||||
review,
|
||||
};
|
||||
if (!service) throw new BadRequestException(ServiceMessage.SERVICE_NOT_EXIST);
|
||||
|
||||
const hasSubscription = await this.checkUserPurchasedService(userId, serviceId);
|
||||
if (!hasSubscription) throw new BadRequestException(SubscriptionMessage.NOT_PURCHASED_CANNOT_REVIEW);
|
||||
|
||||
const review = queryRunner.manager.create(this.danakServiceReviewRepository.target, {
|
||||
user: { id: userId },
|
||||
...addReviewDto,
|
||||
service,
|
||||
});
|
||||
|
||||
await queryRunner.manager.save(this.danakServiceReviewRepository.target, review);
|
||||
|
||||
const superAdmins = await this.adminsService.getSuperAdmins();
|
||||
|
||||
for (const admin of superAdmins) {
|
||||
await this.notificationsService.createNewServiceReviewNotification(
|
||||
admin.id,
|
||||
{
|
||||
serviceName: service.name,
|
||||
userPhone: user.phone,
|
||||
userEmail: user.email,
|
||||
fullName: `${user.firstName} ${user.lastName}`,
|
||||
},
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
return {
|
||||
message: ServiceMessage.REVIEW_ADDED,
|
||||
review,
|
||||
};
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
this.logger.error("Error in add review", error);
|
||||
throw error;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************** */
|
||||
@@ -578,9 +615,10 @@ export class DanakServicesService {
|
||||
.replace(/-+/g, "-") // Replace multiple hyphens with single hyphen
|
||||
.trim();
|
||||
}
|
||||
/******************************************** */
|
||||
|
||||
private async checkUserPurchasedService(userId: string, serviceId: string) {
|
||||
const subscription = await this.danakServicesRepository
|
||||
private async checkUserPurchasedService(userId: string, serviceId: string, queryRunner?: QueryRunner) {
|
||||
const subscription = await (queryRunner ? queryRunner.manager : this.danakServicesRepository)
|
||||
.createQueryBuilder()
|
||||
.select("1")
|
||||
.from("user_subscription", "us")
|
||||
|
||||
Reference in New Issue
Block a user