update: the danak services module and add new route to fetch the danak service
This commit is contained in:
@@ -2,9 +2,11 @@ import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Brackets, IsNull, Not, Repository } from "typeorm";
|
||||
|
||||
import { SubscriptionStatus } from "../../subscriptions/enums/subscription-status.enum";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { DanakServicesQueryDto, DanakServicesSearchQueryDto } from "../DTO/danak-services-search-query.dto";
|
||||
import { DanakServicesGlobalSearchDto, DanakServicesQueryDto, DanakServicesSearchQueryDto } from "../DTO/danak-services-search-query.dto";
|
||||
import { DanakService } from "../entities/danak-service.entity";
|
||||
import { ReviewStatus } from "../enums/review-status.enum";
|
||||
|
||||
@Injectable()
|
||||
export class DanakServicesRepository extends Repository<DanakService> {
|
||||
@@ -86,4 +88,89 @@ export class DanakServicesRepository extends Repository<DanakService> {
|
||||
count,
|
||||
};
|
||||
}
|
||||
//+********************
|
||||
async getDanakServiceById(serviceId: string, isAdmin: boolean) {
|
||||
const serviceQueryBuilder = this.createQueryBuilder("service")
|
||||
.where("service.deletedAt IS NULL")
|
||||
.leftJoin("service.category", "category")
|
||||
.addSelect(["category.id", "category.title"])
|
||||
.leftJoinAndSelect("service.images", "images")
|
||||
.leftJoinAndSelect("service.subscriptionPlans", "subscriptionPlans")
|
||||
.leftJoin("service.reviews", "reviews", "reviews.status = :reviewStatus", { reviewStatus: ReviewStatus.APPROVED })
|
||||
.leftJoin("reviews.user", "user")
|
||||
.addSelect([
|
||||
"reviews.id",
|
||||
"reviews.comment",
|
||||
"reviews.rating",
|
||||
"reviews.title",
|
||||
"reviews.createdAt",
|
||||
"reviews.status",
|
||||
"user.id",
|
||||
"user.firstName",
|
||||
"user.lastName",
|
||||
"user.profilePic",
|
||||
])
|
||||
// .loadRelationCountAndMap("service.reviewCount", "service.reviews")
|
||||
// .addSelect(
|
||||
// (subQuery) =>
|
||||
// subQuery
|
||||
// .select("CAST(AVG(rating) AS DECIMAL(10,2))", "averageRating")
|
||||
// .from(DanakServiceReview, "r")
|
||||
// .where("r.serviceId = service.id")
|
||||
// .andWhere("r.status = :reviewStatus", { reviewStatus: ReviewStatus.APPROVED }),
|
||||
// "averageRating",
|
||||
// )
|
||||
.andWhere("service.id = :serviceId", { serviceId })
|
||||
.groupBy("service.id,category.id, images.id, subscriptionPlans.id, reviews.id, user.id");
|
||||
|
||||
if (!isAdmin) {
|
||||
serviceQueryBuilder
|
||||
.andWhere("service.isActive = :isActive", { isActive: true })
|
||||
.andWhere("subscriptionPlans.isActive = :planActive", { planActive: true });
|
||||
}
|
||||
|
||||
serviceQueryBuilder.orderBy("subscriptionPlans.price", "ASC");
|
||||
|
||||
const danakService = await serviceQueryBuilder.getOne();
|
||||
|
||||
return danakService;
|
||||
}
|
||||
//+********************
|
||||
async getUsersByService(serviceId: string) {
|
||||
const users = await this.createQueryBuilder("service")
|
||||
.innerJoin("service.subscriptionPlans", "subscriptionPlans")
|
||||
.innerJoin("subscriptionPlans.userSubscriptions", "userSubscriptions")
|
||||
.innerJoin("user", "user", "userSubscriptions.userId = user.id")
|
||||
.select([
|
||||
"DISTINCT user.id AS id",
|
||||
"user.firstName AS firstName",
|
||||
"user.lastName AS lastName",
|
||||
"user.email AS email",
|
||||
"user.profilePic AS profilePic",
|
||||
])
|
||||
.where("service.id = :serviceId", { serviceId })
|
||||
.andWhere("userSubscriptions.status = :status", { status: SubscriptionStatus.ACTIVE })
|
||||
.andWhere("userSubscriptions.endDate > NOW()")
|
||||
.getRawMany();
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
async searchServices(queryDto: DanakServicesGlobalSearchDto) {
|
||||
const servicesQuery = this.createQueryBuilder("service")
|
||||
.leftJoin("service.category", "category")
|
||||
.leftJoin("service.subscriptionPlans", "subscriptionPlans")
|
||||
.select(["service.id", "service.name", "service.description", "service.icon", "category.id", "category.title"])
|
||||
.where("service.isActive = :isActive", { isActive: true })
|
||||
.andWhere("service.deletedAt IS NULL")
|
||||
.andWhere("category.isActive = :categoryActive", { categoryActive: true })
|
||||
.andWhere("category.deletedAt IS NULL")
|
||||
.andWhere("subscriptionPlans.isActive = :planActive", { planActive: true });
|
||||
|
||||
if (queryDto.q) {
|
||||
servicesQuery.andWhere("service.name ILIKE :searchQuery", { searchQuery: `%${queryDto.q}%` });
|
||||
}
|
||||
|
||||
return servicesQuery.getMany();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user