diff --git a/src/modules/product/dto/create-attribute-value.dto.ts b/src/modules/product/dto/create-attribute-value.dto.ts deleted file mode 100644 index 451101f..0000000 --- a/src/modules/product/dto/create-attribute-value.dto.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { IsString, IsOptional, IsInt, Min } from 'class-validator'; -import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger'; -import { Type } from 'class-transformer'; - -export class CreateAttributeValueDto { - @IsString() - @ApiProperty({ example: 'سلفون' }) - value: string; - - - @IsOptional() - @IsInt() - @Min(0) - @Type(() => Number) - @ApiPropertyOptional({ example: 1 }) - order?: number; -} diff --git a/src/modules/product/dto/create-attribute.dto.ts b/src/modules/product/dto/create-attribute.dto.ts deleted file mode 100644 index 655ede3..0000000 --- a/src/modules/product/dto/create-attribute.dto.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { IsString, IsOptional, IsBoolean, IsInt, Min, IsEnum, IsNotEmpty } from 'class-validator'; -import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger'; -import { Type } from 'class-transformer'; -import { FieldType } from 'src/modules/form-builder/interface/print'; - -export class CreateAttributeDto { - @IsString() - @ApiProperty({ example: 'نوع روکش' }) - name: string; - - @IsOptional() - @IsInt() - @ApiPropertyOptional() - parentId?: number; - - - @IsBoolean() - @Type(() => Boolean) - @ApiPropertyOptional({ example: true }) - isRequired: boolean; - - @IsNotEmpty() - @IsEnum(FieldType) - @ApiProperty({ enum: FieldType, example: FieldType.select }) - type: FieldType; - - @IsOptional() - @IsInt() - @Min(0) - @Type(() => Number) - @ApiPropertyOptional({ example: 1 }) - order?: number; -} diff --git a/src/modules/product/dto/update-attribute-value.dto.ts b/src/modules/product/dto/update-attribute-value.dto.ts deleted file mode 100644 index cec18f4..0000000 --- a/src/modules/product/dto/update-attribute-value.dto.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { PartialType } from '@nestjs/swagger'; -import { CreateAttributeValueDto } from './create-attribute-value.dto'; - -export class UpdateAttributeValueDto extends - PartialType(CreateAttributeValueDto) { } diff --git a/src/modules/product/dto/update-attribute.dto.ts b/src/modules/product/dto/update-attribute.dto.ts deleted file mode 100644 index 8b892b5..0000000 --- a/src/modules/product/dto/update-attribute.dto.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { PartialType } from '@nestjs/swagger'; -import { CreateAttributeDto } from './create-attribute.dto'; - -export class UpdateAttributeDto extends PartialType(CreateAttributeDto) { -} diff --git a/src/modules/product/entities/attribute-value.entity.ts b/src/modules/product/entities/attribute-value.entity.ts deleted file mode 100644 index 182c4bd..0000000 --- a/src/modules/product/entities/attribute-value.entity.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Entity, Property, ManyToOne, PrimaryKey, Unique } from '@mikro-orm/core'; -import { BaseEntity } from '../../../common/entities/base.entity'; -import { Attribute } from './attribute.entity'; -// import { Attribute } from './attribute.entity'; - -@Entity({ tableName: 'attribute_values' }) -// @Unique({ properties: ['value', 'attribute'] }) -export class AttributeValue extends BaseEntity { - @PrimaryKey({ autoincrement: true, type: 'bigint' }) - id: bigint; - - @Property() - value: string; - - // @ManyToOne(() => Attribute) - // attribute: Attribute; - - @ManyToOne(()=>Attribute) - attribute: Attribute; - - @Property({ type: 'int', nullable: true }) - order?: number; - -} diff --git a/src/modules/product/entities/attribute.entity.ts b/src/modules/product/entities/attribute.entity.ts deleted file mode 100644 index 91c2b61..0000000 --- a/src/modules/product/entities/attribute.entity.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Collection, Entity, Enum, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core'; -import { BaseEntity } from '../../../common/entities/base.entity'; -import { Product } from './product.entity'; -import { AttributeValue } from './attribute-value.entity'; -import { FieldType } from 'src/modules/form-builder/interface/print'; - -@Entity({ tableName: 'attributes' }) -export class Attribute extends BaseEntity { - @ManyToOne(() => Product) - product!: Product - - @ManyToOne(() => Attribute, { nullable: true }) - parent?: Attribute | null - - @OneToMany(() => Attribute, (c) => c.parent) - children = new Collection(this) - - @OneToMany(() => AttributeValue, (attrValue) => attrValue.attribute) - values = new Collection(this) - - @PrimaryKey({ type: 'bigint', autoincrement: true }) - id: bigint; - - @Property() - name: string; - - @Enum(() => FieldType) - type: FieldType; - - @Property({ type: 'boolean', default: false }) - isRequired: boolean = false; - - @Property({ type: 'int', nullable: true }) - order?: number; - -} diff --git a/src/modules/product/interface/product.interface.ts b/src/modules/product/interface/product.interface.ts index 1dc7d1d..8b13789 100644 --- a/src/modules/product/interface/product.interface.ts +++ b/src/modules/product/interface/product.interface.ts @@ -1,7 +1 @@ -// export enum AttributeType{ -// SELECT='select', -// TEXT='text', -// NUMBER='number', -// CHECKBOX='checkbox', -// BOOLEAN='boolean', -// } + diff --git a/src/modules/product/providers/attribute-value.service.ts b/src/modules/product/providers/attribute-value.service.ts deleted file mode 100644 index dea64dd..0000000 --- a/src/modules/product/providers/attribute-value.service.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { BadGatewayException, Injectable, NotFoundException } from '@nestjs/common'; -import { EntityManager } from '@mikro-orm/postgresql'; -import { RequiredEntityData } from '@mikro-orm/core'; -import { AttributeValueRepository } from '../repositories/attribute-value.repository'; -import { AttributeRepository } from '../repositories/attribute.repository'; -import { AttributeValue } from '../entities/attribute-value.entity'; -import { CreateAttributeValueDto } from '../dto/create-attribute-value.dto'; -import { UpdateAttributeValueDto } from '../dto/update-attribute-value.dto'; - -@Injectable() -export class AttributeValueService { - - constructor( - private readonly attributeValueRepository: AttributeValueRepository, - private readonly attributeRepository: AttributeRepository, - private readonly em: EntityManager, - ) { } - - async create(attributeId: number, dto: CreateAttributeValueDto) { - - const attribute = await this.attributeRepository.findOne({ id: attributeId }) - if (!attribute) { - throw new BadGatewayException('attribute not found') - } - - const data: RequiredEntityData = { - attribute, - value: dto.value, - order: dto.order, - }; - - const attributeValue = this.attributeValueRepository.create(data) - - await this.em.persistAndFlush(attributeValue) - - return attributeValue - - } - - async update(attributeValueId: number, dto: UpdateAttributeValueDto) { - - const attributeValue = await this.attributeValueRepository.findOne({ id: attributeValueId }) - - if (!attributeValue) { - throw new NotFoundException('Attribute value not found'); - } - - this.attributeValueRepository.assign(attributeValue, dto) - - this.em.persistAndFlush(attributeValue) - - return attributeValue - - } - - findAll(attributeId: number) { - return this.attributeValueRepository.find({ attribute: { id: attributeId } }); - } - - async findById(attributeValueId: number): Promise { - const attributeValue = await this.attributeValueRepository.findOne({ id: attributeValueId }); - - if (!attributeValue) throw new NotFoundException('Attribute value not found'); - return attributeValue; - } - - async remove(id: number): Promise { - const attributeValue = await this.findById(id); - if (!attributeValue) { - throw new NotFoundException('attribute value not found'); - } - - attributeValue.deletedAt = new Date(); - return await this.em.persistAndFlush(attributeValue); - - } - -} diff --git a/src/modules/product/providers/attribute.service.ts b/src/modules/product/providers/attribute.service.ts deleted file mode 100644 index dc0d37f..0000000 --- a/src/modules/product/providers/attribute.service.ts +++ /dev/null @@ -1,101 +0,0 @@ -import { BadGatewayException, Injectable, NotFoundException } from '@nestjs/common'; -import { EntityManager } from '@mikro-orm/postgresql'; -import { RequiredEntityData } from '@mikro-orm/core'; -import { AttributeRepository } from '../repositories/attribute.repository'; -import { CreateAttributeDto } from '../dto/create-attribute.dto'; -import { Attribute } from '../entities/attribute.entity'; -import { ProductRepository } from '../repositories/product.repository'; -import { UpdateAttributeDto } from '../dto/update-attribute.dto'; - -@Injectable() -export class AttributeService { - - constructor( - private readonly attributeRepository: AttributeRepository, - private readonly productRepository: ProductRepository, - private readonly em: EntityManager, - ) { } - - async create(productId: number, dto: CreateAttributeDto) { - let parent: null | Attribute = null - - const product = await this.productRepository.findOne({ id: productId }) - if (!product) { - throw new BadGatewayException('Product not found') - } - - if (dto.parentId) { - parent = await this.attributeRepository.findOne({ id: dto.parentId }) - if (!parent) { - throw new BadGatewayException('Parent attribute not found') - } - } - - const data: RequiredEntityData = { - product, - name: dto.name, - isRequired: dto.isRequired, - order: dto.order, - type: dto.type, - parent - }; - - const attribute = this.attributeRepository.create(data) - - await this.em.persistAndFlush(attribute) - - return attribute - - } - - async update(attributeId: number, dto: UpdateAttributeDto) { - - const attribute = await this.attributeRepository.findOne({ id: attributeId }) - - if (!attribute) { - throw new NotFoundException('Attribute not found'); - } - - if (dto.parentId) { - const parent = await this.attributeRepository.findOne({ id: dto.parentId }) - if (!parent) { - throw new NotFoundException('parent attribute not found'); - } - - this.attributeRepository.assign(attribute, { parent }) - } - - this.attributeRepository.assign(attribute, dto) - - this.em.persistAndFlush(attribute) - - return attribute - - } - - findAll(productId: number) { - return this.attributeRepository.find({ product: { id: productId } }); - } - - async findById(attributeId: number): Promise { - const attribute = await this.attributeRepository.findOne({ id: attributeId }, - { populate: ['children', 'product','values'] }); - - if (!attribute) throw new NotFoundException('Attribute not found'); - return attribute; - } - - async remove(id: number): Promise { - const attribute = await this.findById(id); - if (!attribute) { - throw new NotFoundException('attribute not found'); - } - if (attribute.children && attribute.children.length > 0) { - throw new NotFoundException('attribute has children'); - } - attribute.deletedAt = new Date(); - return await this.em.persistAndFlush(attribute); - - } - -} diff --git a/src/modules/product/repositories/attribute-value.repository.ts b/src/modules/product/repositories/attribute-value.repository.ts deleted file mode 100644 index 63e4c22..0000000 --- a/src/modules/product/repositories/attribute-value.repository.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { EntityManager, EntityRepository } from '@mikro-orm/postgresql'; -import { AttributeValue } from '../entities/attribute-value.entity'; - - -@Injectable() -export class AttributeValueRepository extends EntityRepository { - constructor(readonly em: EntityManager) { - super(em, AttributeValue); - } -} diff --git a/src/modules/product/repositories/attribute.repository.ts b/src/modules/product/repositories/attribute.repository.ts deleted file mode 100644 index 7f304ab..0000000 --- a/src/modules/product/repositories/attribute.repository.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { EntityManager, EntityRepository } from '@mikro-orm/postgresql'; -import { Attribute } from '../entities/attribute.entity'; - - -@Injectable() -export class AttributeRepository extends EntityRepository { - constructor(readonly em: EntityManager) { - super(em, Attribute); - } -}