fix: companies
This commit is contained in:
+1
-1
@@ -16,7 +16,7 @@ import { rateLimitConfig } from "./configs/rateLimit.config";
|
||||
import { HTTPLogger } from "./core/middlewares/logger.middleware";
|
||||
import { AnnouncementsModule } from "./modules/announcements/announcement.module";
|
||||
import { AuthModule } from "./modules/auth/auth.module";
|
||||
import { BillsModule } from './modules/bills/bills.module';
|
||||
import { BillsModule } from "./modules/bills/bills.module";
|
||||
import { BusinessesModule } from "./modules/businesses/businesses.module";
|
||||
import { ChatbotModule } from "./modules/chatbot/chatbot.module";
|
||||
import { CompaniesModule } from "./modules/companies/companies.module";
|
||||
|
||||
@@ -20,5 +20,10 @@ export function getSwaggerDocument(app: NestFastifyApplication) {
|
||||
.build();
|
||||
|
||||
const swaggerDocument = SwaggerModule.createDocument(app, swaggerConfig);
|
||||
SwaggerModule.setup("api-docs", app, swaggerDocument);
|
||||
SwaggerModule.setup("api-docs", app, swaggerDocument, {
|
||||
swaggerOptions: {
|
||||
docExpansion: "none",
|
||||
defaultModelsExpandDepth: -1,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,13 +1,4 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
UseInterceptors,
|
||||
} from "@nestjs/common";
|
||||
import { Body, Controller, Delete, Get, Param, Post, Query, UseInterceptors } from "@nestjs/common";
|
||||
import { ApiOperation } from "@nestjs/swagger";
|
||||
|
||||
import { BillsService } from "./bills.service";
|
||||
|
||||
@@ -8,11 +8,7 @@ import { BusinessesModule } from "../businesses/businesses.module";
|
||||
import { CompaniesModule } from "../companies/companies.module";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MikroOrmModule.forFeature([Bill]),
|
||||
CompaniesModule,
|
||||
BusinessesModule,
|
||||
],
|
||||
imports: [MikroOrmModule.forFeature([Bill]), CompaniesModule, BusinessesModule],
|
||||
controllers: [BillsController],
|
||||
providers: [BillsService],
|
||||
})
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import dayjs from 'dayjs';
|
||||
import Decimal from 'decimal.js';
|
||||
import { EntityManager } from "@mikro-orm/postgresql";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import dayjs from "dayjs";
|
||||
import Decimal from "decimal.js";
|
||||
|
||||
import { BillListQueryDto } from './dto/bill-list-query.dto';
|
||||
import { CreateChargeBillDto } from './dto/create-charge-bill.dto';
|
||||
import { CreateWaterBillDto } from './dto/create-water-bill.dto';
|
||||
import { Bill } from './entities/bill.entity';
|
||||
import { BillType } from './enums/bill-type.enum';
|
||||
import { BillsRepository } from './repositories/bill.repository';
|
||||
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 { InvoiceItem } from '../invoices/entities/invoice-item.entity';
|
||||
import { Invoice } from '../invoices/entities/invoice.entity';
|
||||
import { BillListQueryDto } from "./dto/bill-list-query.dto";
|
||||
import { CreateChargeBillDto } from "./dto/create-charge-bill.dto";
|
||||
import { CreateWaterBillDto } from "./dto/create-water-bill.dto";
|
||||
import { Bill } from "./entities/bill.entity";
|
||||
import { BillType } from "./enums/bill-type.enum";
|
||||
import { BillsRepository } from "./repositories/bill.repository";
|
||||
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 { InvoiceItem } from "../invoices/entities/invoice-item.entity";
|
||||
import { Invoice } from "../invoices/entities/invoice.entity";
|
||||
|
||||
@Injectable()
|
||||
export class BillsService {
|
||||
@@ -33,7 +33,7 @@ export class BillsService {
|
||||
|
||||
const bill = em.create(Bill, { type: BillType.WATER_BILL, business });
|
||||
em.persist(bill);
|
||||
const dueDate = dayjs().add(dueDays, 'day').toDate();
|
||||
const dueDate = dayjs().add(dueDays, "day").toDate();
|
||||
|
||||
const uniqueCompanyIds = [...new Set(values.map((v) => v.companyId))];
|
||||
const companies = await this.companiesService.findByIds(uniqueCompanyIds, em);
|
||||
@@ -59,7 +59,7 @@ export class BillsService {
|
||||
});
|
||||
|
||||
const invoiceItem = em.create(InvoiceItem, {
|
||||
name: 'قبض آب',
|
||||
name: "قبض آب",
|
||||
count: value.waterUsage,
|
||||
unitPrice: new Decimal(waterRate),
|
||||
discount: new Decimal(0),
|
||||
@@ -90,7 +90,7 @@ export class BillsService {
|
||||
|
||||
const bill = em.create(Bill, { type: BillType.MONTHLY_CHARGE, business });
|
||||
em.persist(bill);
|
||||
const dueDate = dayjs().add(dueDays, 'day').toDate();
|
||||
const dueDate = dayjs().add(dueDays, "day").toDate();
|
||||
|
||||
const companies = await this.companiesService.findAllBybusinessId(business.id, em);
|
||||
|
||||
@@ -110,7 +110,7 @@ export class BillsService {
|
||||
});
|
||||
|
||||
const invoiceItem = em.create(InvoiceItem, {
|
||||
name: 'قبض شارژ',
|
||||
name: "قبض شارژ",
|
||||
count: 1,
|
||||
unitPrice: subtotal,
|
||||
discount: new Decimal(0),
|
||||
@@ -142,10 +142,7 @@ export class BillsService {
|
||||
}
|
||||
|
||||
async findOne(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");
|
||||
}
|
||||
|
||||
@@ -2,12 +2,11 @@ import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsInt } from "class-validator";
|
||||
|
||||
export class CreateChargeBillDto {
|
||||
@ApiProperty()
|
||||
@IsInt()
|
||||
chargeRate: number;
|
||||
|
||||
@ApiProperty()
|
||||
@IsInt()
|
||||
dueDays:number;
|
||||
@ApiProperty()
|
||||
@IsInt()
|
||||
chargeRate: number;
|
||||
|
||||
@ApiProperty()
|
||||
@IsInt()
|
||||
dueDays: number;
|
||||
}
|
||||
|
||||
@@ -3,30 +3,31 @@ import { Type } from "class-transformer";
|
||||
import { ArrayMinSize, IsArray, IsInt, IsNotEmpty, IsUUID, ValidateNested } from "class-validator";
|
||||
|
||||
export class ValueDto {
|
||||
@IsNotEmpty({ message: ' BillMessage.COMPANY_ID_REQUIRED' })
|
||||
@ApiProperty({ description: "شناسه شرکت", })
|
||||
@IsUUID("7")
|
||||
companyId: string;
|
||||
@IsNotEmpty({ message: " BillMessage.COMPANY_ID_REQUIRED" })
|
||||
@ApiProperty({ description: "شناسه شرکت" })
|
||||
@IsUUID("7")
|
||||
companyId: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsNotEmpty({ message: 'BillMessage.WATER_USAGE_REQUIRED' })
|
||||
@IsInt()
|
||||
@ApiProperty({ description: "مصرف آب", example: 100 })
|
||||
waterUsage: number;
|
||||
@ApiProperty()
|
||||
@IsNotEmpty({ message: "BillMessage.WATER_USAGE_REQUIRED" })
|
||||
@IsInt()
|
||||
@ApiProperty({ description: "مصرف آب", example: 100 })
|
||||
waterUsage: number;
|
||||
}
|
||||
|
||||
export class CreateWaterBillDto {
|
||||
@ApiProperty()
|
||||
@IsInt()
|
||||
waterRate:number;
|
||||
@ApiProperty()
|
||||
@IsInt()
|
||||
waterRate: number;
|
||||
|
||||
@ApiProperty()
|
||||
@IsInt()
|
||||
dueDays:number;
|
||||
@ApiProperty()
|
||||
@IsInt()
|
||||
dueDays: number;
|
||||
|
||||
@Type(() => ValueDto)
|
||||
@IsArray()
|
||||
@ArrayMinSize(1)
|
||||
@ValidateNested({ each: true })
|
||||
values: ValueDto[];
|
||||
@ApiProperty({ example: [{ companyId: "", waterUsage: 100 }] })
|
||||
@Type(() => ValueDto)
|
||||
@IsArray()
|
||||
@ArrayMinSize(1)
|
||||
@ValidateNested({ each: true })
|
||||
values: ValueDto[];
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { PartialType } from "@nestjs/swagger";
|
||||
|
||||
import { CreateWaterBillDto } from './create-water-bill.dto';
|
||||
import { CreateWaterBillDto } from "./create-water-bill.dto";
|
||||
|
||||
export class UpdateBillDto extends PartialType(CreateWaterBillDto) {}
|
||||
|
||||
@@ -8,15 +8,14 @@ import { BillsRepository } from "../repositories/bill.repository";
|
||||
|
||||
@Entity({ repository: () => BillsRepository })
|
||||
export class Bill extends BaseEntity {
|
||||
@Enum({ items: () => BillType })
|
||||
type: BillType;
|
||||
|
||||
@Enum({ items: () => BillType })
|
||||
type: BillType;
|
||||
@ManyToOne(() => Business, { nullable: false, deleteRule: "cascade" })
|
||||
business!: Business;
|
||||
|
||||
@ManyToOne(() => Business, { nullable: false, deleteRule: "cascade" })
|
||||
business!: Business;
|
||||
@OneToMany(() => Invoice, (invoice) => invoice.bill, { cascade: [Cascade.PERSIST, Cascade.REMOVE], orphanRemoval: true })
|
||||
invoices = new Collection<Invoice>(this);
|
||||
|
||||
@OneToMany(() => Invoice, (invoice) => invoice.bill, { cascade: [Cascade.PERSIST, Cascade.REMOVE], orphanRemoval: true })
|
||||
invoices = new Collection<Invoice>(this);
|
||||
|
||||
[EntityRepositoryType]?: BillsRepository;
|
||||
[EntityRepositoryType]?: BillsRepository;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,13 @@ export class CompaniesController {
|
||||
return this.companiesService.getCompaniesList(query, businessId);
|
||||
}
|
||||
|
||||
@Get("list/all")
|
||||
@AuthGuards()
|
||||
@ApiOperation({ summary: "get companies list without pagination (admin)" })
|
||||
getAllCompanies(@BusinessDec("id") businessId: string) {
|
||||
return this.companiesService.findAllBybusinessId2(businessId);
|
||||
}
|
||||
|
||||
@Get("list/public")
|
||||
@ApiOperation({ summary: "get companies list (public)" })
|
||||
getCompaniesListPublic(@Query() queryDto: CompanyListPublicQueryDto, @BusinessDec("id") businessId: string) {
|
||||
|
||||
@@ -29,7 +29,7 @@ export class CompaniesService {
|
||||
private readonly passwordService: PasswordService,
|
||||
private readonly em: EntityManager,
|
||||
private readonly companyRequestRepository: CompanyRequestRepository,
|
||||
) { }
|
||||
) {}
|
||||
|
||||
async create(createDto: CreateCompanyDto, business: Business) {
|
||||
const em = this.em.fork();
|
||||
@@ -461,7 +461,7 @@ export class CompaniesService {
|
||||
|
||||
async findByIds(comapnyIds: string[], em: EntityManager) {
|
||||
const companies = await em.find(Company, {
|
||||
id: { $in: comapnyIds }
|
||||
id: { $in: comapnyIds },
|
||||
});
|
||||
if (companies.length !== comapnyIds.length) throw new BadRequestException(CompanyMessage.COMPANY_NOT_FOUND);
|
||||
return companies;
|
||||
@@ -471,4 +471,8 @@ export class CompaniesService {
|
||||
const companies = await em.find(Company, { business: { id: businessId } });
|
||||
return companies;
|
||||
}
|
||||
async findAllBybusinessId2(businessId: string) {
|
||||
const companies = await this.em.find(Company, { business: { id: businessId } });
|
||||
return companies;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user