add: migration
This commit is contained in:
@@ -32,11 +32,7 @@ export class BillsController {
|
||||
@UseInterceptors(FileInterceptor("file"))
|
||||
@ApiBody({ type: CreateWaterBillFromExcelDto })
|
||||
@Post("water/excel")
|
||||
createWaterBillByExcel(
|
||||
@UploadedFile() file: IFile,
|
||||
@Body() dto: CreateWaterBillFromExcelDto,
|
||||
@BusinessDec() business: Business,
|
||||
) {
|
||||
createWaterBillByExcel(@UploadedFile() file: IFile, @Body() dto: CreateWaterBillFromExcelDto, @BusinessDec() business: Business) {
|
||||
return this.billsService.createWaterBillByExcel(file, dto, business);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { MikroOrmModule } from "@mikro-orm/nestjs";
|
||||
import { BullModule } from "@nestjs/bullmq";
|
||||
import { Module } from "@nestjs/common";
|
||||
|
||||
import { BillsController } from "./bills.controller";
|
||||
@@ -6,17 +7,20 @@ import { BillsService } from "./bills.service";
|
||||
import { Bill } from "./entities/bill.entity";
|
||||
import { BusinessesModule } from "../businesses/businesses.module";
|
||||
import { CompaniesModule } from "../companies/companies.module";
|
||||
import { NotificationModule } from "../notifications/notifications.module";
|
||||
import { InvoicesModule } from "../invoices/invoices.module";
|
||||
import { BullModule } from "@nestjs/bullmq";
|
||||
import { INVOICE } from "../invoices/constants";
|
||||
import { InvoicesModule } from "../invoices/invoices.module";
|
||||
import { NotificationModule } from "../notifications/notifications.module";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
BullModule.registerQueue({ name: INVOICE.QUEUE_NAME }),
|
||||
MikroOrmModule.forFeature([Bill]), CompaniesModule, BusinessesModule
|
||||
, NotificationModule, InvoicesModule],
|
||||
MikroOrmModule.forFeature([Bill]),
|
||||
CompaniesModule,
|
||||
BusinessesModule,
|
||||
NotificationModule,
|
||||
InvoicesModule,
|
||||
],
|
||||
controllers: [BillsController],
|
||||
providers: [BillsService],
|
||||
})
|
||||
export class BillsModule { }
|
||||
export class BillsModule {}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { EntityManager } from "@mikro-orm/postgresql";
|
||||
import { InjectQueue } from "@nestjs/bullmq";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { Queue } from "bullmq";
|
||||
import dayjs from "dayjs";
|
||||
import Decimal from "decimal.js";
|
||||
import { Workbook } from "exceljs";
|
||||
|
||||
import { IFile } from "../utils/interfaces/IFile";
|
||||
|
||||
import { BillListQueryDto } from "./dto/bill-list-query.dto";
|
||||
import { CreateChargeBillDto } from "./dto/create-charge-bill.dto";
|
||||
import { CreateWaterBillDto, CreateWaterBillFromExcelDto } from "./dto/create-water-bill.dto";
|
||||
@@ -16,13 +16,11 @@ import { CompanyMessage } from "../../common/enums/message.enum";
|
||||
import { Business } from "../businesses/entities/business.entity";
|
||||
import { Company } from "../companies/entities/company.entity";
|
||||
import { CompaniesService } from "../companies/services/companies.service";
|
||||
import { INVOICE } from "../invoices/constants";
|
||||
import { InvoiceItem } from "../invoices/entities/invoice-item.entity";
|
||||
import { Invoice } from "../invoices/entities/invoice.entity";
|
||||
import { NotificationQueue } from "../notifications/queue/notification.queue";
|
||||
import { InjectQueue } from "@nestjs/bullmq";
|
||||
import { INVOICE } from "../invoices/constants";
|
||||
import { Queue } from "bullmq";
|
||||
|
||||
import { IFile } from "../utils/interfaces/IFile";
|
||||
|
||||
@Injectable()
|
||||
export class BillsService {
|
||||
@@ -33,8 +31,7 @@ export class BillsService {
|
||||
@InjectQueue(INVOICE.QUEUE_NAME)
|
||||
private readonly invoiceQueue: Queue,
|
||||
private readonly notificationQueue: NotificationQueue,
|
||||
|
||||
) { }
|
||||
) {}
|
||||
|
||||
async createWaterBill(dto: CreateWaterBillDto, business: Business) {
|
||||
const { values, waterRate, dueDays } = dto;
|
||||
@@ -95,8 +92,8 @@ export class BillsService {
|
||||
}
|
||||
}
|
||||
|
||||
private async addInvoiceReminderJobs(billId: string) {
|
||||
const invoices = await this.em.find(Invoice, { bill: { id: billId } }, { populate: ["items", "company","company.user"] });
|
||||
private async addInvoiceReminderJobs(billId: string) {
|
||||
const invoices = await this.em.find(Invoice, { bill: { id: billId } }, { populate: ["items", "company", "company.user"] });
|
||||
|
||||
for (const invoice of invoices) {
|
||||
await this.notificationQueue.addInvoiceCreationNotification(invoice.company.user.id, {
|
||||
@@ -196,7 +193,7 @@ export class BillsService {
|
||||
await em.flush();
|
||||
await em.commit();
|
||||
|
||||
await this.addInvoiceReminderJobs(bill.id)
|
||||
await this.addInvoiceReminderJobs(bill.id);
|
||||
|
||||
return bill;
|
||||
} catch (error) {
|
||||
@@ -223,10 +220,7 @@ export class BillsService {
|
||||
}
|
||||
|
||||
async remove(id: string, businessId: string) {
|
||||
const bill = await this.billRepository.findOne(
|
||||
{ id, business: { id: businessId } },
|
||||
{ populate: ["invoices"] },
|
||||
);
|
||||
const bill = await this.billRepository.findOne({ id, business: { id: businessId } }, { populate: ["invoices"] });
|
||||
if (!bill) {
|
||||
throw new BadRequestException("Bill not found");
|
||||
}
|
||||
|
||||
@@ -21,6 +21,5 @@ export class Bill extends BaseEntity {
|
||||
@Property({ persist: false })
|
||||
invoiceCount?: number;
|
||||
|
||||
|
||||
[EntityRepositoryType]?: BillsRepository;
|
||||
}
|
||||
|
||||
@@ -5,65 +5,62 @@ import { BillListQueryDto } from "../dto/bill-list-query.dto";
|
||||
import { Bill } from "../entities/bill.entity";
|
||||
|
||||
export class BillsRepository extends EntityRepository<Bill> {
|
||||
|
||||
async getBillsList(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')
|
||||
.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);
|
||||
|
||||
|
||||
// Add filters
|
||||
if (queryDto.type) {
|
||||
query.andWhere('b.type', queryDto.type);
|
||||
query.andWhere("b.type", queryDto.type);
|
||||
}
|
||||
|
||||
|
||||
if (queryDto.since) {
|
||||
query.andWhere('b.created_at', '>=', new Date(queryDto.since));
|
||||
query.andWhere("b.created_at", ">=", new Date(queryDto.since));
|
||||
}
|
||||
|
||||
|
||||
if (queryDto.to) {
|
||||
query.andWhere('b.created_at', '<=', new Date(queryDto.to));
|
||||
query.andWhere("b.created_at", "<=", new Date(queryDto.to));
|
||||
}
|
||||
|
||||
|
||||
// Execute the query
|
||||
const results = await query;
|
||||
|
||||
|
||||
// Get total count for pagination
|
||||
const countQuery = knex('bill')
|
||||
.count('* as total')
|
||||
.where('business_id', businessId);
|
||||
|
||||
const countQuery = knex("bill").count("* as total").where("business_id", businessId);
|
||||
|
||||
if (queryDto.type) {
|
||||
countQuery.andWhere('type', queryDto.type);
|
||||
countQuery.andWhere("type", queryDto.type);
|
||||
}
|
||||
|
||||
|
||||
if (queryDto.since) {
|
||||
countQuery.andWhere('created_at', '>=', new Date(queryDto.since));
|
||||
countQuery.andWhere("created_at", ">=", new Date(queryDto.since));
|
||||
}
|
||||
|
||||
|
||||
if (queryDto.to) {
|
||||
countQuery.andWhere('created_at', '<=', new Date(queryDto.to));
|
||||
countQuery.andWhere("created_at", "<=", new Date(queryDto.to));
|
||||
}
|
||||
|
||||
|
||||
const totalResult = await countQuery;
|
||||
const total = Number(totalResult[0].total);
|
||||
|
||||
|
||||
// Map results back to Bill entities (invoice_count from knex may be string/bigint)
|
||||
const bills = results.map((row) => {
|
||||
const bill = this.em.map(Bill, row);
|
||||
bill.invoiceCount = Number(row.invoice_count ?? 0);
|
||||
return bill;
|
||||
});
|
||||
|
||||
|
||||
return [bills, total];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ export class CreateCompanyRequestDto extends PickType(CreateCompanyDto, [
|
||||
"industryId",
|
||||
"products",
|
||||
"services",
|
||||
"metrage"
|
||||
"metrage",
|
||||
]) {
|
||||
@IsNotEmpty({ message: CompanyMessage.CHIEF_EXECUTIVE_REQUIRED })
|
||||
@IsString({ message: CompanyMessage.CHIEF_EXECUTIVE_MUST_BE_STRING })
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Module } from "@nestjs/common";
|
||||
|
||||
import { CompaniesController } from "./companies.controller";
|
||||
import { CompanyProduct } from "./entities/company-product.entity";
|
||||
import { CompanyRequest } from "./entities/company-request.enitiy";
|
||||
import { CompanyRequest } from "./entities/company-request.entity";
|
||||
import { CompanyService } from "./entities/company-service.entity";
|
||||
import { Company } from "./entities/company.entity";
|
||||
import { CompaniesService } from "./services/companies.service";
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { EntityRepository } from "@mikro-orm/postgresql";
|
||||
|
||||
import { CompanyRequest } from "../entities/company-request.enitiy";
|
||||
import { CompanyRequest } from "../entities/company-request.entity";
|
||||
|
||||
export class CompanyRequestRepository extends EntityRepository<CompanyRequest> {}
|
||||
|
||||
@@ -14,7 +14,7 @@ import { CompanyListPublicQueryDto, CompanyListQueryDto } from "../DTO/company-l
|
||||
import { ChangeCompanyRequestStatusDto, CreateCompanyDto, CreateCompanyRequestDto } from "../DTO/create-company.dto";
|
||||
import { UpdateCompanyDto, UpdateUserCompanyDto } from "../DTO/update-company.dto";
|
||||
import { CompanyProduct } from "../entities/company-product.entity";
|
||||
import { CompanyRequest } from "../entities/company-request.enitiy";
|
||||
import { CompanyRequest } from "../entities/company-request.entity";
|
||||
import { CompanyService } from "../entities/company-service.entity";
|
||||
import { Company } from "../entities/company.entity";
|
||||
import { CompanyRequestStatus } from "../enums/company-request-status.enum";
|
||||
|
||||
Reference in New Issue
Block a user