import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; import { EntityManager } from '@mikro-orm/postgresql'; import { RequiredEntityData } from '@mikro-orm/core'; import { FieldRepository } from '../repository/field.repository'; import { Field } from '../entities/field.entity'; import { CreateFieldDto } from '../dto/create-field.dto'; import { UpdateFieldDto } from '../dto/update-field.dto'; import { EntityType } from '../interface/print'; import { Product } from 'src/modules/product/entities/product.entity'; import { PrintForm } from 'src/modules/print-form/entities/print-form.entity'; import { ProductService } from 'src/modules/product/providers/product.service'; import { PrintFormService } from 'src/modules/print-form/provider/print-form.service'; import { FindFieldsGroupDto } from '../dto/find-field-group.dto'; @Injectable() export class FieldService { constructor( private readonly fieldRepository: FieldRepository, private readonly productService: ProductService, private readonly printFormService: PrintFormService, private readonly em: EntityManager, ) { } async create(entityId: string, dto: CreateFieldDto) { const { entityType, isRequired, multiple, name, type, order } = dto let entity: null | Product | PrintForm = null if (entityType == EntityType.product) { entity = await this.productService.findOneOrFail(entityId) } else if (entityType == EntityType.printForm) { entity = await this.printFormService.finOrFail(entityId) } if (!entity) { throw new BadRequestException("Entity not Found") } const data: RequiredEntityData = { name, order, type, isRequired, multiple, entityId: entity.id, entityType }; const field = this.fieldRepository.create({ name, order, type, isRequired, multiple, entityId: entity.id, entityType }) await this.em.persistAndFlush(field) return field } async update(fieldId: string, dto: UpdateFieldDto) { const field = await this.fieldRepository.findOne({ id: fieldId }) if (!field) { throw new NotFoundException('Field not found'); } this.fieldRepository.assign(field, dto) this.em.persistAndFlush(field) return field } findAll(entityId: string) { return this.fieldRepository.find({ entityId, }, { populate: ['options'], orderBy: { order: 'ASC' } }); } async findAllByIds(dto: FindFieldsGroupDto) { const fields = await this.fieldRepository.find({ entityId: { $in: dto.entityIds } }, { populate: ['options'] }, ); // const fieldMap = new Map(fields.map(f => [f.entityId, f])) const result: { entityId: number, fields: Field[] }[] = [] fields.forEach(field => { if (!result.find((r => r.entityId == Number(field.entityId)))) { result.push({ entityId: Number(field.entityId), fields: fields.filter(f => f.entityId == field.entityId) }) } }) return result } async findById(fieldId: string): Promise { const field = await this.fieldRepository.findOne({ id: fieldId }, { populate: [] }); if (!field) throw new NotFoundException('Field not found'); return field; } async remove(id: string): Promise { const field = await this.findById(id); field.deletedAt = new Date(); return await this.em.persistAndFlush(field); } }