request populate
This commit is contained in:
@@ -8,6 +8,7 @@ import { RequestItem } from './entities/request-item.entity';
|
||||
import { RequestStatusEnum } from './enum/request';
|
||||
import { UserService } from '../user/providers/user.service';
|
||||
import { ProductService } from '../product/providers/product.service';
|
||||
import { FieldRepository } from '../form-builder/repository/field.repository';
|
||||
import { RequestRepository } from './repositories/request.repository';
|
||||
|
||||
@Injectable()
|
||||
@@ -17,6 +18,7 @@ export class RequestService {
|
||||
private readonly requestRepository: RequestRepository,
|
||||
private readonly userService: UserService,
|
||||
private readonly productService: ProductService,
|
||||
private readonly fieldRepository: FieldRepository,
|
||||
) { }
|
||||
|
||||
async create(createRequestDto: CreateRequestDto, userId: string) {
|
||||
@@ -65,6 +67,8 @@ export class RequestService {
|
||||
if (!request) {
|
||||
throw new NotFoundException('Request not found');
|
||||
}
|
||||
|
||||
await this.enrichItemsWithFields(request.items.getItems());
|
||||
return request;
|
||||
}
|
||||
|
||||
@@ -76,9 +80,37 @@ export class RequestService {
|
||||
if (!request) {
|
||||
throw new NotFoundException('Request not found');
|
||||
}
|
||||
await this.enrichItemsWithFields(request.items.getItems());
|
||||
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) {
|
||||
const request = await this.requestRepository.findOne(
|
||||
{ id, user: { id: userId } },
|
||||
|
||||
Reference in New Issue
Block a user