add: company excel

This commit is contained in:
2026-02-17 10:29:26 +03:30
parent 42d78e15ff
commit 2cac3a4925
6 changed files with 431 additions and 16 deletions
+4 -4
View File
@@ -150,10 +150,10 @@ export class BillsService {
}
async remove(id: string, businessId: string) {
const bill = await this.billRepository.findOne({
id,
business: { id: businessId },
});
const bill = await this.billRepository.findOne(
{ id, business: { id: businessId } },
{ populate: ["invoices"] },
);
if (!bill) {
throw new BadRequestException("Bill not found");
}
+15 -2
View File
@@ -1,5 +1,6 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, UseInterceptors } from "@nestjs/common";
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, Res, UseInterceptors } from "@nestjs/common";
import { ApiHeader, ApiOperation } from "@nestjs/swagger";
import { FastifyReply } from "fastify";
import { CompanyListPublicQueryDto, CompanyListQueryDto } from "./DTO/company-list-query.dto";
import { ChangeCompanyRequestStatusDto, CreateCompanyDto, CreateCompanyRequestDto } from "./DTO/create-company.dto";
@@ -49,11 +50,23 @@ export class CompaniesController {
@Get("list/all")
@AuthGuards()
@ApiOperation({ summary: "get companies list without pagination (admin)" })
@ApiOperation({ summary: "get companies list without pagination (admin)" })
getAllCompanies(@BusinessDec("id") businessId: string) {
return this.companiesService.findAllBybusinessId2(businessId);
}
@Get("list/all/export")
@ApiHeader({ name: "x-business-id" })
@AuthGuards()
@ApiOperation({ summary: "export companies list as Excel (admin)" })
async exportAllCompanies(@BusinessDec("id") businessId: string, @Res() res: FastifyReply) {
const buffer = await this.companiesService.exportCompaniesToExcel(businessId);
const filename = "companies.xlsx";
res.header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
res.header("Content-Disposition", `attachment; filename="${filename}"`);
return res.send(buffer);
}
@Get("list/public")
@ApiOperation({ summary: "get companies list (public)" })
getCompaniesListPublic(@Query() queryDto: CompanyListPublicQueryDto, @BusinessDec("id") businessId: string) {
@@ -1,6 +1,7 @@
import { EntityManager } from "@mikro-orm/postgresql";
import { BadRequestException, Injectable } from "@nestjs/common";
import dayjs from "dayjs";
import { Workbook } from "exceljs";
import { CompanyMessage, IndustryMessage } from "../../../common/enums/message.enum";
import { Business } from "../../businesses/entities/business.entity";
@@ -475,4 +476,25 @@ export class CompaniesService {
const companies = await this.em.find(Company, { business: { id: businessId } });
return companies;
}
async exportCompaniesToExcel(businessId: string): Promise<Buffer> {
const companies = await this.em.find(Company, { business: { id: businessId } }, { fields: ["id", "name"] });
const workbook = new Workbook();
const sheet = workbook.addWorksheet("Companies", { headerFooter: { firstHeader: "Companies" } });
sheet.columns = [
{ header: "companyId", key: "companyId", width: 40 },
{ header: "company name", key: "companyName", width: 30 },
{ header: "waterUsage", key: "waterUsage", width: 15 },
];
sheet.getRow(1).font = { bold: true };
for (const company of companies) {
sheet.addRow({
companyId: company.id,
companyName: company.name,
waterUsage: "",
});
}
const buffer = await workbook.xlsx.writeBuffer();
return Buffer.from(buffer as ArrayBuffer);
}
}