chore: complete the the invoice module - not tested

This commit is contained in:
mahyargdz
2025-02-11 14:25:49 +03:30
parent bbdc5996b5
commit 49c62018f0
24 changed files with 674 additions and 74 deletions
@@ -12,16 +12,16 @@ import { Roles } from "../../common/decorators/roles.decorator";
import { ParamDto } from "../../common/DTO/param.dto";
import { RoleEnum } from "../users/enums/role.enum";
@Controller("services")
@Controller("danak-services")
@ApiTags("Danak-Services")
export class DanakServicesController {
constructor(private readonly danakServicesService: DanakServicesService) {}
//
//------------------------ service categories ------------------------
@AuthGuards()
@Roles(RoleEnum.ADMIN)
@ApiOperation({ summary: "Create a new service category => admin route" })
@Post("category")
@Post("categories")
createCategory(@Body() createDto: CreateCategoryDto) {
return this.danakServicesService.createCategory(createDto);
}
@@ -29,7 +29,7 @@ export class DanakServicesController {
@AuthGuards()
@Roles(RoleEnum.ADMIN)
@ApiOperation({ summary: "Get all service categories => admin route" })
@Get("category")
@Get("categories")
getCategories(@Query() queryDto: CategorySearchQueryDto) {
return this.danakServicesService.getCategories(queryDto);
}
@@ -38,11 +38,27 @@ export class DanakServicesController {
@Roles(RoleEnum.ADMIN)
@ApiOperation({ summary: "Get all service categories => admin route" })
@Pagination()
@Get("category-list")
@Get("categories/list")
getCategoryList(@Query() queryDto: CategoryListSearchQueryDto) {
return this.danakServicesService.getCategoryList(queryDto);
}
@AuthGuards()
@Roles(RoleEnum.USER)
@ApiOperation({ summary: "Get all service categories user side" })
@Get("categories/public")
getCategoriesUserSide() {
return this.danakServicesService.getCategoriesUserSide();
}
@AuthGuards()
@Roles(RoleEnum.USER)
@ApiOperation({ summary: "get category services with category id" })
@Get("categories/:id/services")
getCategoryServices(@Param() paramDto: ParamDto) {
return this.danakServicesService.getCategoryServices(paramDto.id);
}
@AuthGuards()
@Roles(RoleEnum.ADMIN)
@ApiOperation({ summary: "toggle status of categories => admin route" })
@@ -51,7 +67,7 @@ export class DanakServicesController {
toggleCategoryStatus(@Param() paramDto: ParamDto) {
return this.danakServicesService.toggleCategoryStatus(paramDto);
}
//-------------------- service management --------------------------
@AuthGuards()
@Roles(RoleEnum.ADMIN)
@ApiOperation({ summary: "create new danak services => admin route" })
@@ -63,7 +79,7 @@ export class DanakServicesController {
@AuthGuards()
@ApiOperation({ summary: "get all danak services ==> admin route" })
@Roles(RoleEnum.ADMIN)
@Get("list")
@Get()
getServices(@Query() queryDto: DanakServicesSearchQueryDto) {
return this.danakServicesService.getServicesList(queryDto);
}
@@ -77,6 +93,22 @@ export class DanakServicesController {
return this.danakServicesService.toggleServiceStatus(paramDto);
}
@ApiOperation({ summary: "get services for user side" })
@AuthGuards()
@Roles(RoleEnum.USER)
@Get("suggested")
getDanakSuggestServices() {
return this.danakServicesService.getDanakSuggestServices();
}
@ApiOperation({ summary: "get danak service by id" })
@AuthGuards()
@Roles(RoleEnum.USER)
@Get(":id")
getDanakServiceById(@Param() paramDto: ParamDto) {
return this.danakServicesService.getDanakServiceByID(paramDto.id);
}
// @AuthGuards()
// @Roles(RoleEnum.USER)
// @ApiOperation({ summary: "get all user purchased danak services" })
@@ -1,5 +1,5 @@
import { BadRequestException, Injectable } from "@nestjs/common";
import { Brackets, FindOptionsWhere, IsNull } from "typeorm";
import { FindOptionsWhere, IsNull } from "typeorm";
import { ParamDto } from "../../../common/DTO/param.dto";
import { CategoryMessage, CommonMessage, ServiceMessage } from "../../../common/enums/message.enum";
@@ -79,33 +79,56 @@ export class DanakServicesService {
}
/******************************************** */
// async getUserServices(userId: string) {
// const userServices = await this.danakServicesRepository.find({
// where: { users: { id: userId } },
// });
async getCategoriesUserSide() {
const categories = await this.danakServicesCategoryRepository.find({
where: { isActive: true },
order: { createdAt: "DESC" },
});
return {
categories,
};
}
/******************************************** */
async getCategoryServices(categoryId: string) {
const category = await this.danakServicesCategoryRepository.findOne({
where: { id: categoryId },
relations: {
danakService: true,
},
order: { createdAt: "DESC" },
});
if (!category) throw new BadRequestException(CategoryMessage.CATEGORY_NOT_EXIST);
return {
category,
};
}
// return {
// userServices,
// };
// }
/******************************************** */
async toggleCategoryStatus(paramDto: ParamDto) {
const category = await this.danakServicesCategoryRepository.findOneById(paramDto.id);
if (!category) throw new BadRequestException(CategoryMessage.CATEGORY_NOT_EXIST);
//
category.isActive = !category.isActive;
//
await this.danakServicesCategoryRepository.save(category);
//
return {
message: CommonMessage.UPDATE_SUCCESS,
isActive: category.isActive,
};
}
/******************************************** */
async toggleServiceStatus(paramDto: ParamDto) {
const service = await this.danakServicesRepository.findServiceById(paramDto.id);
if (!service) throw new BadRequestException(ServiceMessage.SERVICE_NOT_EXIST);
//
service.isActive = !service.isActive;
//
await this.danakServicesRepository.save(service);
//
return {
message: CommonMessage.UPDATE_SUCCESS,
isActive: service.isActive,
@@ -133,42 +156,30 @@ export class DanakServicesService {
/******************************************** */
async getServicesList(queryDto: DanakServicesSearchQueryDto) {
const { limit, skip } = PaginationUtils(queryDto);
return this.danakServicesRepository.getServicesForAdmin(queryDto);
}
const queryBuilder = this.danakServicesRepository
.createQueryBuilder("service")
.leftJoinAndSelect("service.category", "category")
.orderBy("service.createdAt", "DESC")
.skip(skip)
.take(limit);
if (queryDto.isActive) {
queryBuilder.andWhere("service.isActive = :isActive", { isActive: queryDto.isActive === 1 });
}
if (queryDto.isDanakSuggest) {
queryBuilder.andWhere("service.isDanakSuggest = :isDanakSuggest", { isDanakSuggest: queryDto.isDanakSuggest === 1 });
}
if (queryDto.categoryId) {
queryBuilder.andWhere("service.categoryId = :categoryId", { categoryId: queryDto.categoryId });
}
if (queryDto.q) {
queryBuilder.andWhere(
new Brackets((qb) => {
qb.where("service.name LIKE :q", { q: `%${queryDto.q}%` })
.orWhere("service.description LIKE :q", { q: `%${queryDto.q}%` })
.orWhere("service.softwareLanguage LIKE :q", { q: `%${queryDto.q}%` })
.orWhere("service.author LIKE :q", { q: `%${queryDto.q}%` });
}),
);
}
const [services, count] = await queryBuilder.getManyAndCount();
/******************************************** */
async getDanakSuggestServices() {
const danakServices = await this.danakServicesRepository.find({
where: { isDanakSuggest: true, isActive: true },
order: { createdAt: "DESC" },
});
return {
services,
count,
danakServices,
};
}
/******************************************** */
async getDanakServiceByID(serviceId: string) {
const danakService = await this.danakServicesRepository.findOne({
where: { id: serviceId, isActive: true },
relations: { images: true, subscriptionPlans: true },
});
if (!danakService) throw new BadRequestException(ServiceMessage.SERVICE_NOT_FOUND_BY_ID);
return {
danakService,
};
}
@@ -1,7 +1,9 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Brackets, Repository } from "typeorm";
import { PaginationUtils } from "../../utils/providers/pagination.utils";
import { DanakServicesSearchQueryDto } from "../DTO/danak-services-search-query.dto";
import { DanakService } from "../entities/danak-service.entity";
@Injectable()
@@ -17,4 +19,43 @@ export class DanakServicesRepository extends Repository<DanakService> {
async findServiceById(id: string): Promise<DanakService | null> {
return this.findOneBy({ id });
}
async getServicesForAdmin(queryDto: DanakServicesSearchQueryDto) {
const { limit, skip } = PaginationUtils(queryDto);
const queryBuilder = this.createQueryBuilder("service")
.leftJoinAndSelect("service.category", "category")
.orderBy("service.createdAt", "DESC")
.skip(skip)
.take(limit);
if (queryDto.isActive) {
queryBuilder.andWhere("service.isActive = :isActive", { isActive: queryDto.isActive === 1 });
}
if (queryDto.isDanakSuggest) {
queryBuilder.andWhere("service.isDanakSuggest = :isDanakSuggest", { isDanakSuggest: queryDto.isDanakSuggest === 1 });
}
if (queryDto.categoryId) {
queryBuilder.andWhere("service.categoryId = :categoryId", { categoryId: queryDto.categoryId });
}
if (queryDto.q) {
queryBuilder.andWhere(
new Brackets((qb) => {
qb.where("service.name LIKE :q", { q: `%${queryDto.q}%` })
.orWhere("service.description LIKE :q", { q: `%${queryDto.q}%` })
.orWhere("service.softwareLanguage LIKE :q", { q: `%${queryDto.q}%` })
.orWhere("service.author LIKE :q", { q: `%${queryDto.q}%` });
}),
);
}
const [services, count] = await queryBuilder.getManyAndCount();
return {
services,
count,
};
}
}