@@ -143,10 +143,10 @@ export class ChatListeners {
|
||||
const order = await this.em.findOne(
|
||||
Order,
|
||||
{ id: refId },
|
||||
{ populate: ['designer'], fields: ['id', 'designer'] },
|
||||
{ populate: ['designers', 'designers.designer'] },
|
||||
);
|
||||
if (order?.designer?.id) {
|
||||
return [order?.designer?.id]
|
||||
if (order?.designers?.length) {
|
||||
return order.designers.getItems().map((od) => od.designer.id);
|
||||
}
|
||||
|
||||
return [];
|
||||
|
||||
@@ -99,7 +99,7 @@ export class OrderController {
|
||||
@Permissions(PermissionEnum.ASSIGN_DESIGNER)
|
||||
@ApiOperation({ summary: 'Assign Order Designer ' })
|
||||
assignDesigner(@Param('orderId') orderId: string, @Body() body: AssignDesignerDto) {
|
||||
return this.orderService.assignDesigner(orderId, body.designerId);
|
||||
return this.orderService.assignDesigner(orderId, body);
|
||||
}
|
||||
|
||||
// Permission handled inside service
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
import {
|
||||
IsString
|
||||
IsArray,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from 'class-validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class AssignDesignerDto {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
designerId: string;
|
||||
}
|
||||
@ApiPropertyOptional({ description: 'Single designer id (legacy)' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
designerId?: string;
|
||||
|
||||
@ApiPropertyOptional({ type: [String], description: 'One or more designer ids' })
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
designerIds?: string[];
|
||||
}
|
||||
|
||||
@@ -16,11 +16,17 @@ export class CreateOrderAsAdminDto {
|
||||
@IsOptional()
|
||||
userId?: string;
|
||||
|
||||
@ApiPropertyOptional({ example: '' })
|
||||
@ApiPropertyOptional({ example: '', description: 'Single designer id (legacy)' })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
designerId?: string;
|
||||
|
||||
@ApiPropertyOptional({ type: [String], description: 'One or more designer ids' })
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
designerIds?: string[];
|
||||
|
||||
@ApiPropertyOptional({ example: '' })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Entity, Index, ManyToOne, OptionalProps, Unique } from '@mikro-orm/core';
|
||||
import { BaseEntity } from 'src/common/entities/base.entity';
|
||||
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||
import { Order } from './order.entity';
|
||||
|
||||
@Entity({ tableName: 'order_designers' })
|
||||
@Unique({ properties: ['order', 'designer'] })
|
||||
@Index({ properties: ['order'] })
|
||||
@Index({ properties: ['designer'] })
|
||||
export class OrderDesigner extends BaseEntity {
|
||||
[OptionalProps]?: 'createdAt' | 'deletedAt';
|
||||
|
||||
@ManyToOne(() => Order)
|
||||
order!: Order;
|
||||
|
||||
@ManyToOne(() => Admin)
|
||||
designer!: Admin;
|
||||
}
|
||||
@@ -7,7 +7,9 @@ import {
|
||||
PrimaryKey,
|
||||
OptionalProps,
|
||||
OneToOne,
|
||||
Opt
|
||||
OneToMany,
|
||||
Collection,
|
||||
Opt,
|
||||
} from '@mikro-orm/core';
|
||||
import { User } from '../../user/entities/user.entity';
|
||||
import { ulid } from 'ulid';
|
||||
@@ -18,10 +20,10 @@ import { InvoiceItem } from 'src/modules/invoice/entities/invoice-item.entity';
|
||||
import { Product } from 'src/modules/product/entities/product.entity';
|
||||
import { Category } from 'src/modules/product/entities/category.entity';
|
||||
import { OrderPrint } from './order-print.entity';
|
||||
import { OrderDesigner } from './order-designer.entity';
|
||||
|
||||
@Entity({ tableName: 'orders' })
|
||||
@Index({ properties: ['user'] })
|
||||
@Index({ properties: ['designer'] })
|
||||
@Index({ properties: ['status'] })
|
||||
export class Order extends BaseEntity {
|
||||
[OptionalProps]?: 'createdAt' | 'deletedAt' | 'orderNumber'
|
||||
@@ -32,10 +34,12 @@ export class Order extends BaseEntity {
|
||||
@ManyToOne(() => Product, { nullable: true })
|
||||
product?: Product | null;
|
||||
|
||||
|
||||
@OneToOne(() => OrderPrint, (orderPrint) => orderPrint.order, { owner: true, nullable: true })
|
||||
print?: OrderPrint | null
|
||||
|
||||
@OneToMany(() => OrderDesigner, (orderDesigner) => orderDesigner.order, { orphanRemoval: true })
|
||||
designers = new Collection<OrderDesigner>(this);
|
||||
|
||||
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
|
||||
id: string = ulid()
|
||||
|
||||
@@ -48,9 +52,6 @@ export class Order extends BaseEntity {
|
||||
@ManyToOne(() => User)
|
||||
user!: User;
|
||||
|
||||
@ManyToOne(() => Admin, { nullable: true })
|
||||
designer?: Admin
|
||||
|
||||
@ManyToOne(() => Admin, { nullable: true })
|
||||
creator?: Admin
|
||||
|
||||
@@ -61,7 +62,6 @@ export class Order extends BaseEntity {
|
||||
})
|
||||
orderNumber: number & Opt;
|
||||
|
||||
|
||||
@Enum(() => OrderStatusEnum)
|
||||
status: OrderStatusEnum = OrderStatusEnum.CREATED;
|
||||
|
||||
@@ -70,6 +70,4 @@ export class Order extends BaseEntity {
|
||||
|
||||
@Property({ type: 'json' })
|
||||
attachments: IAttachment[] = []
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { OrderController } from './controllers/order.controller';
|
||||
import { Order } from './entities/order.entity';
|
||||
import { OrderPrint } from './entities/order-print.entity';
|
||||
import { OrderPrintValue } from './entities/order-print-value.entity';
|
||||
import { OrderDesigner } from './entities/order-designer.entity';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { PaymentModule } from '../payment/payments.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
@@ -22,7 +23,7 @@ import { OrderPrintValueRepository } from './repositories/order-print-value.repo
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MikroOrmModule.forFeature([Order, OrderPrint, OrderPrintValue]),
|
||||
MikroOrmModule.forFeature([Order, OrderPrint, OrderPrintValue, OrderDesigner]),
|
||||
AuthModule,
|
||||
forwardRef(() => PaymentModule),
|
||||
JwtModule,
|
||||
|
||||
@@ -22,6 +22,7 @@ import { CreateOrderPrintDto } from '../dto/create-order-print.dto';
|
||||
import { OrderPrintValueDto } from '../dto/order-print-value.dto';
|
||||
import { OrderPrint } from '../entities/order-print.entity';
|
||||
import { OrderPrintValue } from '../entities/order-print-value.entity';
|
||||
import { OrderDesigner } from '../entities/order-designer.entity';
|
||||
import { OrderPrintRepository } from '../repositories/order-print.repository';
|
||||
import { Field } from 'src/modules/form-builder/entities/field.entity';
|
||||
import { Request } from 'src/modules/request/entities/request.entity';
|
||||
@@ -29,6 +30,7 @@ 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';
|
||||
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||
|
||||
type AdminOrderTabCounts = {
|
||||
inProgress: number;
|
||||
@@ -116,15 +118,11 @@ export class OrderService {
|
||||
product = invoiceItem.product;
|
||||
}
|
||||
|
||||
let designer = null;
|
||||
if (dto.designerId) {
|
||||
designer = await this.adminService.findOrFail(dto.designerId);
|
||||
}
|
||||
const designerIds = this.resolveDesignerIds(dto) ?? [];
|
||||
|
||||
const order = this.em.create(Order, {
|
||||
user,
|
||||
creator,
|
||||
designer,
|
||||
type,
|
||||
product: product ?? null,
|
||||
title: dto.title,
|
||||
@@ -132,9 +130,9 @@ export class OrderService {
|
||||
estimatedDays: dto.estimatedDays,
|
||||
invoiceItem,
|
||||
attachments: dto.attachments,
|
||||
|
||||
});
|
||||
|
||||
await this.setOrderDesigners(order, designerIds);
|
||||
await this.em.persistAndFlush(order);
|
||||
|
||||
if (dto.keepRequestChats) {
|
||||
@@ -266,12 +264,9 @@ export class OrderService {
|
||||
order.product = product;
|
||||
}
|
||||
}
|
||||
if (dto.designerId !== undefined) {
|
||||
if (dto.designerId == null || dto.designerId === '') {
|
||||
order.designer = undefined;
|
||||
} else {
|
||||
order.designer = await this.adminService.findOrFail(dto.designerId);
|
||||
}
|
||||
const designerIds = this.resolveDesignerIds(dto);
|
||||
if (designerIds !== undefined) {
|
||||
await this.setOrderDesigners(order, designerIds);
|
||||
}
|
||||
|
||||
await this.em.flush();
|
||||
@@ -354,11 +349,14 @@ export class OrderService {
|
||||
return order;
|
||||
}
|
||||
|
||||
async assignDesigner(orderId: string, designerId: string) {
|
||||
async assignDesigner(orderId: string, dto: { designerId?: string; designerIds?: string[] }) {
|
||||
const order = await this.findOrderOrFail(orderId)
|
||||
const designer = await this.adminService.findOrFail(designerId)
|
||||
const designerIds = this.resolveDesignerIds(dto)
|
||||
if (designerIds === undefined) {
|
||||
throw new BadRequestException('designerIds or designerId is required')
|
||||
}
|
||||
|
||||
order.designer = designer
|
||||
await this.setOrderDesigners(order, designerIds)
|
||||
await this.em.flush()
|
||||
return order
|
||||
}
|
||||
@@ -433,11 +431,46 @@ export class OrderService {
|
||||
}
|
||||
}
|
||||
|
||||
// -----------Hrlper Methods -----------
|
||||
// -----------Helper Methods -----------
|
||||
private resolveDesignerIds(dto: {
|
||||
designerId?: string | null;
|
||||
designerIds?: string[];
|
||||
}): string[] | undefined {
|
||||
if (dto.designerIds !== undefined) {
|
||||
return [...new Set(dto.designerIds.filter(Boolean))];
|
||||
}
|
||||
if (dto.designerId !== undefined) {
|
||||
if (dto.designerId == null || dto.designerId === '') {
|
||||
return [];
|
||||
}
|
||||
return [dto.designerId];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private async setOrderDesigners(order: Order, designerIds: string[]): Promise<void> {
|
||||
await this.em.populate(order, ['designers']);
|
||||
|
||||
const designers: Admin[] = [];
|
||||
for (const designerId of designerIds) {
|
||||
designers.push(await this.adminService.findOrFail(designerId));
|
||||
}
|
||||
|
||||
order.designers.removeAll();
|
||||
for (const designer of designers) {
|
||||
order.designers.add(
|
||||
this.em.create(OrderDesigner, {
|
||||
order,
|
||||
designer,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async findOrderOrFail(id: string): Promise<Order> {
|
||||
const order = await this.em.findOne(Order, { id },
|
||||
{
|
||||
populate: ['user', 'type', 'product', 'creator', 'designer', 'invoiceItem'],
|
||||
populate: ['user', 'type', 'product', 'creator', 'designers', 'designers.designer', 'invoiceItem'],
|
||||
});
|
||||
if (!order) {
|
||||
throw new NotFoundException(`Order with ID ${id} not found.`);
|
||||
@@ -446,12 +479,12 @@ export class OrderService {
|
||||
}
|
||||
|
||||
async findOrderOrFailAssignedOrder(id: string, adminId: string): Promise<Order> {
|
||||
const order = await this.em.findOne(Order, { id }, { populate: ['user', 'type', 'product', 'creator'] });
|
||||
if (!order) {
|
||||
throw new NotFoundException(`Order with ID ${id} not found.`);
|
||||
}
|
||||
if (order?.designer?.id !== adminId) {
|
||||
throw new ForbiddenException("You don't have permission to view this order")
|
||||
const order = await this.findOrderOrFail(id);
|
||||
const isAssigned = order.designers
|
||||
.getItems()
|
||||
.some((orderDesigner) => orderDesigner.designer.id === adminId);
|
||||
if (!isAssigned) {
|
||||
throw new ForbiddenException("You don't have permission to view this order");
|
||||
}
|
||||
return order;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ export class OrderRepository extends EntityRepository<Order> {
|
||||
}
|
||||
|
||||
if (designerId) {
|
||||
where.designer = { id: designerId };
|
||||
where.designers = { designer: { id: designerId } };
|
||||
}
|
||||
|
||||
if (categoryId) {
|
||||
@@ -87,7 +87,7 @@ export class OrderRepository extends EntityRepository<Order> {
|
||||
limit,
|
||||
offset,
|
||||
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
|
||||
populate: ['user', 'product', 'designer', 'invoiceItem', 'invoiceItem.invoice'],
|
||||
populate: ['user', 'product', 'designers', 'designers.designer', 'invoiceItem', 'invoiceItem.invoice'],
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user