barname bug
This commit is contained in:
@@ -15,42 +15,44 @@ import { Business } from "../businesses/entities/business.entity";
|
||||
import { UserDec } from "../../common/decorators/user.decorator";
|
||||
import { RoleEnum } from "../users/enums/role.enum";
|
||||
import { UpdateBarnameAsWatcherDto } from "./dto/update-barname-watcher.dto";
|
||||
import { User } from "../users/entities/user.entity";
|
||||
|
||||
@Controller("barname")
|
||||
@UseInterceptors(BusinessInterceptor)
|
||||
@AuthGuards()
|
||||
export class BarnameController {
|
||||
constructor(private readonly billsService: BarnameService) { }
|
||||
constructor(private readonly barnameService: BarnameService) { }
|
||||
|
||||
@ApiOperation({ summary: "Create barname" })
|
||||
@Post()
|
||||
@ApiHeader({ name: "x-business-id" })
|
||||
createWaterBill(@Body() dto: CreateBarnameDto, @BusinessDec() business: Business, @UserDec("id") userId: string,) {
|
||||
return this.billsService.createBarname(dto, business, userId);
|
||||
return this.barnameService.createBarname(dto, business, userId);
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation({ summary: "Get all barname with filters and pagination" })
|
||||
@Get()
|
||||
@ApiHeader({ name: "x-business-id" })
|
||||
findAll(@Query() dto: BillListQueryDto, @BusinessDec("id") businessId: string) {
|
||||
return this.billsService.findAll(dto, businessId);
|
||||
findAllAsUSer(@Query() dto: BillListQueryDto, @BusinessDec() business: Business, @UserDec() user: User) {
|
||||
return this.barnameService.findAll(dto, business, user);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Get a barname by id" })
|
||||
@Get(":id")
|
||||
findOne(@Param() paramDto: ParamDto, @BusinessDec("id") businessId: string) {
|
||||
return this.billsService.findOne(paramDto.id, businessId);
|
||||
return this.barnameService.findOne(paramDto.id, businessId);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Update Barname by watcher" })
|
||||
@Get(":id/watcher")
|
||||
update(@Param('id') id: string, @BusinessDec() business: Business, @Body() dto: UpdateBarnameAsWatcherDto, @UserDec("role") role: RoleEnum) {
|
||||
return this.billsService.updateBarnameAsWatcher(id, dto, business, role);
|
||||
return this.barnameService.updateBarnameAsWatcher(id, dto, business, role);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Remove a barname" })
|
||||
@Delete(":id")
|
||||
remove(@Param() paramDto: ParamDto, @BusinessDec("id") businessId: string) {
|
||||
return this.billsService.remove(paramDto.id, businessId);
|
||||
return this.barnameService.remove(paramDto.id, businessId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import { Company } from "../companies/entities/company.entity";
|
||||
import { BarnameStatus } from "./enums/bill-type.enum";
|
||||
import { UpdateBarnameAsWatcherDto } from "./dto/update-barname-watcher.dto";
|
||||
import { RoleEnum } from "../users/enums/role.enum";
|
||||
import { User } from "../users/entities/user.entity";
|
||||
// import { CompaniesService } from "../companies/services/companies.service";
|
||||
|
||||
|
||||
@@ -22,7 +23,7 @@ export class BarnameService {
|
||||
constructor(
|
||||
private readonly em: EntityManager,
|
||||
// private readonly companiesService: CompaniesService,
|
||||
private readonly billRepository: BarnameRepository,
|
||||
private readonly barnameRepository: BarnameRepository,
|
||||
) { }
|
||||
|
||||
async createBarname(dto: CreateBarnameDto, business: Business, userId: string) {
|
||||
@@ -74,10 +75,17 @@ export class BarnameService {
|
||||
}
|
||||
|
||||
|
||||
async findAll(queryDto: BillListQueryDto, businessId: string) {
|
||||
const [bills, count] = await this.billRepository.getBillsList(queryDto, businessId);
|
||||
async findAll(queryDto: BillListQueryDto, business: Business, user: User) {
|
||||
const companyId = undefined
|
||||
|
||||
if (user.role.name == RoleEnum.USER) {
|
||||
const company = await this.em.findOne(Company, { business, user: { id: user.id }, deletedAt: null });
|
||||
if (!company) throw new BadRequestException(CompanyMessage.COMPANY_NOT_FOUND);
|
||||
}
|
||||
|
||||
const [barname, count] = await this.barnameRepository.getList({ ...queryDto, companyId }, business.id);
|
||||
return {
|
||||
bills,
|
||||
barname,
|
||||
count,
|
||||
paginate: true,
|
||||
};
|
||||
@@ -92,7 +100,7 @@ export class BarnameService {
|
||||
}
|
||||
|
||||
async remove(id: string, businessId: string) {
|
||||
const bill = await this.billRepository.findOne({ id, business: { id: businessId } }, { populate: [] });
|
||||
const bill = await this.barnameRepository.findOne({ id, business: { id: businessId } }, { populate: [] });
|
||||
if (!bill) {
|
||||
throw new BadRequestException("Bill not found");
|
||||
}
|
||||
@@ -105,7 +113,7 @@ export class BarnameService {
|
||||
}
|
||||
|
||||
async findOneOrFail(id: string) {
|
||||
const barname = await this.billRepository.findOne({ id }, { populate: ['company', 'business'] });
|
||||
const barname = await this.barnameRepository.findOne({ id }, { populate: ['company', 'business'] });
|
||||
if (!barname) {
|
||||
throw new BadRequestException("barname not found");
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsDateString, IsEnum, IsOptional } from "class-validator";
|
||||
import { IsDateString, IsEnum, IsOptional, IsString } from "class-validator";
|
||||
|
||||
import { PaginationDto } from "../../../common/DTO/pagination.dto";
|
||||
import { BarnameStatus } from "../enums/bill-type.enum";
|
||||
@@ -15,6 +15,11 @@ export class BillListQueryDto extends PaginationDto {
|
||||
@ApiPropertyOptional({ description: "Filter bills created since this date", example: "2025-01-01" })
|
||||
since?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiPropertyOptional()
|
||||
companyId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
@ApiPropertyOptional({ description: "Filter bills created until this date", example: "2025-12-31" })
|
||||
|
||||
@@ -1,58 +1,40 @@
|
||||
import { EntityRepository } from "@mikro-orm/postgresql";
|
||||
import { EntityRepository, FilterQuery } from "@mikro-orm/postgresql";
|
||||
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { BillListQueryDto } from "../dto/bill-list-query.dto";
|
||||
import { Barname } from "../entities/barname.entity";
|
||||
|
||||
export class BarnameRepository extends EntityRepository<Barname> {
|
||||
async getBillsList(queryDto: BillListQueryDto, businessId: string) {
|
||||
async getList(queryDto: BillListQueryDto, businessId: string) {
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
|
||||
const knex = this.em.getKnex();
|
||||
const query = knex
|
||||
.select("b.*")
|
||||
.select(knex.raw("COUNT(i.id) as invoice_count"))
|
||||
.from("bill as b")
|
||||
.leftJoin("invoice as i", "i.bill_id", "b.id")
|
||||
.where("b.business_id", businessId)
|
||||
.groupBy("b.id")
|
||||
.orderBy("b.created_at", "desc")
|
||||
.limit(limit)
|
||||
.offset(skip);
|
||||
const where: FilterQuery<Barname> = {
|
||||
deletedAt: null,
|
||||
business: { id: businessId },
|
||||
};
|
||||
|
||||
// Add filters
|
||||
if (queryDto.status) {
|
||||
query.andWhere("b.status", queryDto.status);
|
||||
where.status = queryDto.status;
|
||||
}
|
||||
|
||||
if (queryDto.since) {
|
||||
query.andWhere("b.created_at", ">=", new Date(queryDto.since));
|
||||
where.createdAt = { $gte: queryDto.since }
|
||||
}
|
||||
|
||||
if (queryDto.companyId) {
|
||||
where.company = { id: queryDto.companyId }
|
||||
}
|
||||
|
||||
if (queryDto.to) {
|
||||
query.andWhere("b.created_at", "<=", new Date(queryDto.to));
|
||||
where.createdAt = { $lte: queryDto.to }
|
||||
}
|
||||
|
||||
// Execute the query
|
||||
const barname = await query;
|
||||
return this.findAndCount(where, {
|
||||
populate: ["company"],
|
||||
limit,
|
||||
offset: skip,
|
||||
orderBy: { createdAt: "DESC" }
|
||||
});
|
||||
|
||||
// Get total count for pagination
|
||||
const countQuery = knex("bill").count("* as total").where("business_id", businessId);
|
||||
|
||||
|
||||
if (queryDto.since) {
|
||||
countQuery.andWhere("created_at", ">=", new Date(queryDto.since));
|
||||
}
|
||||
|
||||
if (queryDto.to) {
|
||||
countQuery.andWhere("created_at", "<=", new Date(queryDto.to));
|
||||
}
|
||||
|
||||
const totalResult = await countQuery;
|
||||
const total = Number(totalResult[0].total);
|
||||
|
||||
|
||||
|
||||
return [barname, total];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user