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