This commit is contained in:
@@ -146,6 +146,10 @@ export class ChattService {
|
||||
await em.nativeDelete(Chat, { refId });
|
||||
}
|
||||
|
||||
async moveChatsRefId(fromRefId: string, toRefId: string, em: EntityManager = this.em) {
|
||||
await em.nativeUpdate(Chat, { refId: fromRefId }, { refId: toRefId });
|
||||
}
|
||||
|
||||
async updateChat(chat: Chat, dto: UpdateChatDto) {
|
||||
|
||||
this.chatRepository.assign(chat, dto, {
|
||||
|
||||
@@ -5,6 +5,7 @@ import { InvoiceController } from './invoice.controller';
|
||||
import { Invoice } from './entities/invoice.entity';
|
||||
import { InvoiceItem } from './entities/invoice-item.entity';
|
||||
import { ProductModule } from '../product/product.module';
|
||||
import { RequestModule } from '../request/request.module';
|
||||
import { InvoiceRepository } from './repositories/invoice.repository';
|
||||
import { InvoiceListeners } from './listeners/invoice.listeners';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
@@ -16,6 +17,7 @@ import { NotificationsModule } from '../notification/notifications.module';
|
||||
imports: [
|
||||
MikroOrmModule.forFeature([Invoice, InvoiceItem]),
|
||||
ProductModule,
|
||||
RequestModule,
|
||||
RolesModule,
|
||||
JwtModule,
|
||||
AuthModule,
|
||||
|
||||
@@ -9,6 +9,7 @@ import { InvoiceItem } from './entities/invoice-item.entity';
|
||||
import { Request } from '../request/entities/request.entity';
|
||||
import { User } from '../user/entities/user.entity';
|
||||
import { ProductService } from '../product/providers/product.service';
|
||||
import { RequestService } from '../request/request.service';
|
||||
import { InvoiceRepository } from './repositories/invoice.repository';
|
||||
import { InvoiceCreatedEvent } from './events/invoice.events';
|
||||
const TAX_RATE = 0.1;
|
||||
@@ -23,6 +24,7 @@ export class InvoiceService {
|
||||
constructor(
|
||||
private readonly em: EntityManager,
|
||||
private readonly productService: ProductService,
|
||||
private readonly requestService: RequestService,
|
||||
private readonly invoiceRepository: InvoiceRepository,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
) { }
|
||||
@@ -130,26 +132,9 @@ export class InvoiceService {
|
||||
invoice.items.add(invoiceItem);
|
||||
}
|
||||
|
||||
// const taxAmount = enableTax
|
||||
// ? Math.round((subTotal - discount) * TAX_RATE)
|
||||
// : 0;
|
||||
// const total = Math.max(0, subTotal - discount + taxAmount);
|
||||
|
||||
// invoice.subTotal = subTotal;
|
||||
// invoice.attachments = dto.attachments;
|
||||
// invoice.taxAmount = taxAmount;
|
||||
// invoice.total = total;
|
||||
// invoice.balance = total - invoice.paidAmount;
|
||||
|
||||
if (dto.requestId) {
|
||||
const requestEntity = await em.findOne(
|
||||
Request,
|
||||
{ id: dto.requestId },
|
||||
);
|
||||
if (!requestEntity) {
|
||||
throw new NotFoundException('Request not found');
|
||||
}
|
||||
requestEntity.deletedAt = new Date();
|
||||
await this.requestService.hardDeleteRequestAndItems(dto.requestId, em);
|
||||
}
|
||||
|
||||
await em.flush();
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
} from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IAttachment } from '../interface/order.interface';
|
||||
@@ -54,5 +55,8 @@ export class CreateOrderAsAdminDto {
|
||||
@IsString()
|
||||
description?: string;
|
||||
|
||||
@ApiProperty({ description: 'When true, reassign request chats to the created order' })
|
||||
@IsBoolean()
|
||||
keepRequestChats: boolean;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import { Request } from 'src/modules/request/entities/request.entity';
|
||||
import { Invoice } from 'src/modules/invoice/entities/invoice.entity';
|
||||
import { OrderCreatedEvent } from '../events/order.events';
|
||||
import { InvoiceConfirmStatusEnum } from 'src/modules/invoice/enum/invoice-confirm-status.enum';
|
||||
import { ChattService } from 'src/modules/chat/providers/chat.service';
|
||||
|
||||
@Injectable()
|
||||
export class OrderService {
|
||||
@@ -40,6 +41,7 @@ export class OrderService {
|
||||
private readonly adminService: AdminService,
|
||||
private readonly orderPrintRepository: OrderPrintRepository,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly chatService: ChattService,
|
||||
) {}
|
||||
|
||||
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto): Promise<Order> {
|
||||
@@ -114,6 +116,13 @@ export class OrderService {
|
||||
|
||||
await this.em.persistAndFlush(order);
|
||||
|
||||
if (dto.keepRequestChats) {
|
||||
const requestId = invoiceItem?.invoice?.requestId;
|
||||
if (requestId) {
|
||||
await this.chatService.moveChatsRefId(requestId, order.id);
|
||||
}
|
||||
}
|
||||
|
||||
this.eventEmitter.emit(
|
||||
OrderCreatedEvent.name,
|
||||
new OrderCreatedEvent(order.id, user.id, order.orderNumber),
|
||||
|
||||
@@ -29,5 +29,6 @@ import { ChatModule } from '../chat/chat.module';
|
||||
],
|
||||
controllers: [RequestController],
|
||||
providers: [RequestService, RequestRepository, RequestListeners],
|
||||
exports: [RequestService],
|
||||
})
|
||||
export class RequestModule {}
|
||||
|
||||
@@ -219,10 +219,14 @@ export class RequestService {
|
||||
});
|
||||
}
|
||||
|
||||
private async hardDeleteRequest(id: string, em: EntityManager) {
|
||||
await this.chatService.deleteChatsByRefId(id, em);
|
||||
await em.nativeUpdate(Invoice, { requestId: id }, { requestId: null });
|
||||
async hardDeleteRequestAndItems(id: string, em: EntityManager) {
|
||||
await em.nativeDelete(RequestItem, { request: { id } });
|
||||
await em.nativeDelete(Request, { id });
|
||||
}
|
||||
|
||||
private async hardDeleteRequest(id: string, em: EntityManager) {
|
||||
await this.chatService.deleteChatsByRefId(id, em);
|
||||
await em.nativeUpdate(Invoice, { requestId: id }, { requestId: null });
|
||||
await this.hardDeleteRequestAndItems(id, em);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user