This commit is contained in:
@@ -2,35 +2,52 @@ import { BadRequestException, Injectable, NotFoundException } from '@nestjs/comm
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { RequiredEntityData } from '@mikro-orm/core';
|
||||
import { FieldRepository } from '../repository/field.repository';
|
||||
import { SectionRepository } from 'src/modules/print/repository/section.repository';
|
||||
import { PrintRepository } from 'src/modules/print/repository/print.repository';
|
||||
import { Field } from '../entities/field.entity';
|
||||
import { CreateFieldDto } from '../dto/create-field.dto';
|
||||
import { UpdateFieldDto } from '../dto/update-field.dto';
|
||||
import { ProductRepository } from 'src/modules/product/repositories/product.repository';
|
||||
import { EntityType } from '../interface/print';
|
||||
import { Product } from 'src/modules/product/entities/product.entity';
|
||||
import { Print } from 'src/modules/print/entities/print.entity';
|
||||
import { ProductService } from 'src/modules/product/providers/product.service';
|
||||
import { PrintService } from 'src/modules/print/provider/Print.service';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class FieldService {
|
||||
|
||||
constructor(
|
||||
private readonly sectionRepository: SectionRepository,
|
||||
private readonly sectionRepository: PrintRepository,
|
||||
private readonly fieldRepository: FieldRepository,
|
||||
private readonly productService: ProductService,
|
||||
private readonly printService: PrintService,
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async create(sectionId: string, dto: CreateFieldDto) {
|
||||
const section = await this.sectionRepository.findOne({ id: sectionId })
|
||||
async create(entityId: number, dto: CreateFieldDto) {
|
||||
const { entityType, isRequired, multiple, name, type, order } = dto
|
||||
|
||||
if (!section) {
|
||||
throw new BadRequestException("Section not found !")
|
||||
let entity: null | Product | Print = null
|
||||
|
||||
if (entityType == EntityType.product) {
|
||||
entity = await this.productService.findOneOrFail(entityId)
|
||||
} else if (entityType == EntityType.print) {
|
||||
entity = await this.printService.findOneOrFail(entityId)
|
||||
}
|
||||
|
||||
if(!entity){
|
||||
throw new BadRequestException("Entity not Found")
|
||||
}
|
||||
|
||||
const data: RequiredEntityData<Field> = {
|
||||
name: dto.name,
|
||||
order: dto.order,
|
||||
type: dto.type,
|
||||
section,
|
||||
isRequired: dto.isRequired,
|
||||
multiple: dto.multiple
|
||||
name,
|
||||
order,
|
||||
type,
|
||||
isRequired,
|
||||
multiple,
|
||||
entityId:entity.id,
|
||||
entityType
|
||||
};
|
||||
|
||||
const field = this.fieldRepository.create(data)
|
||||
@@ -57,9 +74,9 @@ export class FieldService {
|
||||
|
||||
}
|
||||
|
||||
findAll(sectionId: number) {
|
||||
findAll(entityId: number) {
|
||||
return this.fieldRepository.find({
|
||||
section: { id: sectionId }
|
||||
entityId
|
||||
},
|
||||
{ populate: ['options', 'options.value'] });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user