request populate
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
export interface IAttachment { type: string, url: string }
|
export interface IAttachment { type: string, url: string }
|
||||||
|
|
||||||
export interface IField { fieldId: number, value: string }
|
export interface IField { fieldId: string; value: string }
|
||||||
@@ -7,6 +7,7 @@ import { RequestItem } from './entities/request-item.entity';
|
|||||||
import { RequestRepository } from './repositories/request.repository';
|
import { RequestRepository } from './repositories/request.repository';
|
||||||
import { UserModule } from '../user/user.module';
|
import { UserModule } from '../user/user.module';
|
||||||
import { ProductModule } from '../product/product.module';
|
import { ProductModule } from '../product/product.module';
|
||||||
|
import { FormBuilderModule } from '../form-builder/form-builder.module';
|
||||||
import { AuthModule } from '../auth/auth.module';
|
import { AuthModule } from '../auth/auth.module';
|
||||||
import { JwtModule } from '@nestjs/jwt';
|
import { JwtModule } from '@nestjs/jwt';
|
||||||
|
|
||||||
@@ -15,6 +16,7 @@ import { JwtModule } from '@nestjs/jwt';
|
|||||||
MikroOrmModule.forFeature([Request, RequestItem]),
|
MikroOrmModule.forFeature([Request, RequestItem]),
|
||||||
UserModule,
|
UserModule,
|
||||||
ProductModule,
|
ProductModule,
|
||||||
|
FormBuilderModule,
|
||||||
JwtModule,
|
JwtModule,
|
||||||
AuthModule
|
AuthModule
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { RequestItem } from './entities/request-item.entity';
|
|||||||
import { RequestStatusEnum } from './enum/request';
|
import { RequestStatusEnum } from './enum/request';
|
||||||
import { UserService } from '../user/providers/user.service';
|
import { UserService } from '../user/providers/user.service';
|
||||||
import { ProductService } from '../product/providers/product.service';
|
import { ProductService } from '../product/providers/product.service';
|
||||||
|
import { FieldRepository } from '../form-builder/repository/field.repository';
|
||||||
import { RequestRepository } from './repositories/request.repository';
|
import { RequestRepository } from './repositories/request.repository';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -17,6 +18,7 @@ export class RequestService {
|
|||||||
private readonly requestRepository: RequestRepository,
|
private readonly requestRepository: RequestRepository,
|
||||||
private readonly userService: UserService,
|
private readonly userService: UserService,
|
||||||
private readonly productService: ProductService,
|
private readonly productService: ProductService,
|
||||||
|
private readonly fieldRepository: FieldRepository,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async create(createRequestDto: CreateRequestDto, userId: string) {
|
async create(createRequestDto: CreateRequestDto, userId: string) {
|
||||||
@@ -65,6 +67,8 @@ export class RequestService {
|
|||||||
if (!request) {
|
if (!request) {
|
||||||
throw new NotFoundException('Request not found');
|
throw new NotFoundException('Request not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.enrichItemsWithFields(request.items.getItems());
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,9 +80,37 @@ export class RequestService {
|
|||||||
if (!request) {
|
if (!request) {
|
||||||
throw new NotFoundException('Request not found');
|
throw new NotFoundException('Request not found');
|
||||||
}
|
}
|
||||||
|
await this.enrichItemsWithFields(request.items.getItems());
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async enrichItemsWithFields(items: RequestItem[]) {
|
||||||
|
const fieldIds = [
|
||||||
|
...new Set(
|
||||||
|
items
|
||||||
|
.flatMap((item) => item.attributes ?? [])
|
||||||
|
.map((attr) => String(attr.fieldId))
|
||||||
|
.filter(Boolean),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
if (fieldIds.length === 0) return;
|
||||||
|
|
||||||
|
const fields = await this.fieldRepository.find(
|
||||||
|
{ id: { $in: fieldIds } },
|
||||||
|
{ populate: ['options'] },
|
||||||
|
);
|
||||||
|
const fieldMap = new Map(fields.map((f) => [f.id, f]));
|
||||||
|
|
||||||
|
for (const item of items) {
|
||||||
|
if (!item.attributes?.length) continue;
|
||||||
|
const enrichedAttributes = item.attributes.map((attr) => ({
|
||||||
|
...attr,
|
||||||
|
field: fieldMap.get(String(attr.fieldId)) ?? null,
|
||||||
|
}));
|
||||||
|
(item as unknown as { attributes: typeof enrichedAttributes }).attributes = enrichedAttributes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async update(id: string, updateRequestDto: UpdateRequestDto, userId: string) {
|
async update(id: string, updateRequestDto: UpdateRequestDto, userId: string) {
|
||||||
const request = await this.requestRepository.findOne(
|
const request = await this.requestRepository.findOne(
|
||||||
{ id, user: { id: userId } },
|
{ id, user: { id: userId } },
|
||||||
|
|||||||
Reference in New Issue
Block a user