chore: add update and delete for services and category and ads
This commit is contained in:
@@ -11,8 +11,11 @@ import { CreateCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateServiceDto } from "../DTO/create-service.dto";
|
||||
import { DanakServiceReviewQueryDto } from "../DTO/danak-service-review-query.dto";
|
||||
import { DanakServicesQueryDto, DanakServicesSearchQueryDto } from "../DTO/danak-services-search-query.dto";
|
||||
import { UpdateCategoryDto } from "../DTO/update-category.dto";
|
||||
import { UpdateReviewStatusDto } from "../DTO/update-review-status.dto";
|
||||
import { UpdateServiceDto } from "../DTO/update-service.dto";
|
||||
import { DanakServiceCategory } from "../entities/danak-service-category.entity";
|
||||
import { DanakServiceImage } from "../entities/danak-service-image.entity";
|
||||
import { ReviewStatus } from "../enums/review-status.enum";
|
||||
import { DanakServicesCategoryRepository } from "../repositories/danak-services-category.repository";
|
||||
import { DanakServiceReviewRepository } from "../repositories/danak-services-review.repository";
|
||||
@@ -39,6 +42,32 @@ export class DanakServicesService {
|
||||
};
|
||||
}
|
||||
/******************************************** */
|
||||
|
||||
async updateCategory(categoryId: string, updateDto: UpdateCategoryDto) {
|
||||
const category = await this.danakServicesCategoryRepository.findOneById(categoryId);
|
||||
if (!category) throw new BadRequestException(CategoryMessage.CATEGORY_NOT_EXIST);
|
||||
|
||||
if (updateDto.title) {
|
||||
const existCategory = await this.danakServicesCategoryRepository.findOneByTitle(updateDto.title, categoryId);
|
||||
if (existCategory) throw new BadRequestException(CategoryMessage.TITLE_EXIST);
|
||||
}
|
||||
|
||||
await this.danakServicesCategoryRepository.save({ ...category, ...updateDto });
|
||||
return {
|
||||
message: CommonMessage.UPDATE_SUCCESS,
|
||||
};
|
||||
}
|
||||
|
||||
async deleteCategoryById(categoryId: string) {
|
||||
const category = await this.danakServicesCategoryRepository.findOneById(categoryId);
|
||||
if (!category) throw new BadRequestException(CategoryMessage.CATEGORY_NOT_EXIST);
|
||||
//
|
||||
await this.danakServicesCategoryRepository.softDelete(categoryId);
|
||||
return {
|
||||
message: CommonMessage.DELETED,
|
||||
};
|
||||
}
|
||||
/******************************************** */
|
||||
async getCategories(queryDto: CategorySearchQueryDto) {
|
||||
const where: FindOptionsWhere<DanakServiceCategory> = {
|
||||
isActive: true,
|
||||
@@ -64,6 +93,8 @@ export class DanakServicesService {
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
const queryBuilder = this.danakServicesCategoryRepository
|
||||
.createQueryBuilder("category")
|
||||
.where("service.deletedAt = :deletedAt", { deletedAt: IsNull() })
|
||||
// .where("category.deletedAt IS NULL")
|
||||
.loadRelationCountAndMap("category.servicesCount", "category.danakServices")
|
||||
.loadRelationCountAndMap("category.childrenCount", "category.children");
|
||||
|
||||
@@ -88,7 +119,7 @@ export class DanakServicesService {
|
||||
|
||||
async getCategoriesUserSide() {
|
||||
const categories = await this.danakServicesCategoryRepository.find({
|
||||
where: { isActive: true },
|
||||
where: { isActive: true, deletedAt: IsNull() },
|
||||
order: { createdAt: "DESC" },
|
||||
});
|
||||
return {
|
||||
@@ -99,7 +130,7 @@ export class DanakServicesService {
|
||||
|
||||
async getCategoryServices(categoryId?: string) {
|
||||
const category = await this.danakServicesCategoryRepository.findOne({
|
||||
where: { id: categoryId, isActive: true, danakServices: { isActive: true } },
|
||||
where: { id: categoryId, isActive: true, deletedAt: IsNull(), danakServices: { isActive: true } },
|
||||
relations: {
|
||||
danakServices: true,
|
||||
},
|
||||
@@ -161,6 +192,47 @@ export class DanakServicesService {
|
||||
};
|
||||
}
|
||||
/******************************************** */
|
||||
async updateDanakService(serviceId: string, updateDto: UpdateServiceDto) {
|
||||
const service = await this.findServiceById(serviceId);
|
||||
|
||||
let images: DanakServiceImage[] = [];
|
||||
|
||||
if (updateDto.categoryId) {
|
||||
const category = await this.danakServicesCategoryRepository.findOneById(updateDto.categoryId);
|
||||
if (!category) throw new BadRequestException(CategoryMessage.CATEGORY_NOT_EXIST);
|
||||
service.category = category;
|
||||
}
|
||||
//
|
||||
if (updateDto.name) {
|
||||
const existService = await this.danakServicesRepository.findOneByName(updateDto.name, serviceId);
|
||||
if (existService) throw new BadRequestException(ServiceMessage.NAME_EXIST);
|
||||
}
|
||||
//
|
||||
if (updateDto.images) {
|
||||
images = updateDto.images.map((image) => {
|
||||
const serviceImage = new DanakServiceImage();
|
||||
serviceImage.imageUrl = image;
|
||||
serviceImage.danakService = service;
|
||||
return serviceImage;
|
||||
});
|
||||
}
|
||||
|
||||
await this.danakServicesRepository.save({ ...service, ...updateDto, images: images.length ? images : service.images });
|
||||
return {
|
||||
message: CommonMessage.UPDATE_SUCCESS,
|
||||
service,
|
||||
};
|
||||
}
|
||||
/******************************************** */
|
||||
|
||||
async deleteServiceById(serviceId: string) {
|
||||
await this.findServiceById(serviceId);
|
||||
await this.danakServicesRepository.softDelete(serviceId);
|
||||
return {
|
||||
message: CommonMessage.DELETED,
|
||||
};
|
||||
}
|
||||
/******************************************** */
|
||||
|
||||
async getServicesList(queryDto: DanakServicesSearchQueryDto) {
|
||||
return this.danakServicesRepository.getServicesForAdmin(queryDto);
|
||||
@@ -175,7 +247,7 @@ export class DanakServicesService {
|
||||
|
||||
async getDanakSuggestServices() {
|
||||
const danakServices = await this.danakServicesRepository.find({
|
||||
where: { isDanakSuggest: true, isActive: true },
|
||||
where: { isDanakSuggest: true, isActive: true, deletedAt: IsNull() },
|
||||
order: { createdAt: "DESC" },
|
||||
});
|
||||
return {
|
||||
@@ -187,6 +259,7 @@ export class DanakServicesService {
|
||||
async getDanakServiceByIdWithSubs(serviceId: string, isAdmin: boolean, userId: string) {
|
||||
const serviceQueryBuilder = this.danakServicesRepository
|
||||
.createQueryBuilder("service")
|
||||
.where("service.deletedAt = :deletedAt", { deletedAt: IsNull() })
|
||||
.leftJoinAndSelect("service.images", "images")
|
||||
.leftJoinAndSelect("service.subscriptionPlans", "subscriptionPlans")
|
||||
.leftJoin("service.reviews", "reviews", "reviews.status = :reviewStatus", { reviewStatus: ReviewStatus.APPROVED })
|
||||
@@ -213,7 +286,7 @@ export class DanakServicesService {
|
||||
// .andWhere("r.status = :reviewStatus", { reviewStatus: ReviewStatus.APPROVED }),
|
||||
// "averageRating",
|
||||
// )
|
||||
.where("service.id = :serviceId", { serviceId })
|
||||
.andWhere("service.id = :serviceId", { serviceId })
|
||||
.groupBy("service.id, images.id, subscriptionPlans.id, reviews.id, user.id");
|
||||
|
||||
if (!isAdmin) {
|
||||
@@ -314,7 +387,7 @@ export class DanakServicesService {
|
||||
/******************************************** */
|
||||
|
||||
async findServiceById(id: string) {
|
||||
const service = await this.danakServicesRepository.findOneBy({ id });
|
||||
const service = await this.danakServicesRepository.findOneBy({ id, deletedAt: IsNull() });
|
||||
if (!service) throw new BadRequestException(ServiceMessage.SERVICE_NOT_EXIST);
|
||||
return service;
|
||||
}
|
||||
@@ -322,7 +395,7 @@ export class DanakServicesService {
|
||||
/******************************************** */
|
||||
|
||||
async findServicesByIds(ids: string[]) {
|
||||
const services = await this.danakServicesRepository.findBy({ id: In(ids) });
|
||||
const services = await this.danakServicesRepository.findBy({ id: In(ids), deletedAt: IsNull() });
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -396,6 +469,7 @@ export class DanakServicesService {
|
||||
const count = await this.danakServicesRepository.count({
|
||||
where: {
|
||||
isActive: true,
|
||||
deletedAt: IsNull(),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user