feat: implement dynamic create DTO for ContentType

This commit is contained in:
yassinedorbozgithub 2025-01-10 16:53:50 +01:00
parent 7578c485f1
commit 559c6c586f
5 changed files with 52 additions and 28 deletions

View File

@ -9,16 +9,18 @@
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger'; import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { Type } from 'class-transformer'; import { Type } from 'class-transformer';
import { import {
IsString,
IsArray, IsArray,
ValidateNested,
IsOptional,
IsEnum, IsEnum,
Matches,
IsNotEmpty, IsNotEmpty,
IsOptional,
IsString,
Matches,
Validate, Validate,
ValidateNested,
} from 'class-validator'; } from 'class-validator';
import { DtoConfig } from '@/utils/types/dto.types';
import { ValidateRequiredFields } from '../validators/validate-required-fields.validator'; import { ValidateRequiredFields } from '../validators/validate-required-fields.validator';
export class FieldType { export class FieldType {
@ -55,3 +57,7 @@ export class ContentTypeCreateDto {
} }
export class ContentTypeUpdateDto extends PartialType(ContentTypeCreateDto) {} export class ContentTypeUpdateDto extends PartialType(ContentTypeCreateDto) {}
export type ContentTypeDtoMapActions = DtoConfig<{
create: ContentTypeCreateDto;
}>;

View File

@ -15,11 +15,17 @@ import { BlockService } from '@/chat/services/block.service';
import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository'; import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types'; import { TFilterQuery } from '@/utils/types/filter.types';
import { ContentTypeDtoMapActions } from '../dto/contentType.dto';
import { ContentType } from '../schemas/content-type.schema'; import { ContentType } from '../schemas/content-type.schema';
import { Content } from '../schemas/content.schema'; import { Content } from '../schemas/content.schema';
@Injectable() @Injectable()
export class ContentTypeRepository extends BaseRepository<ContentType> { export class ContentTypeRepository extends BaseRepository<
ContentType,
never,
never,
ContentTypeDtoMapActions
> {
constructor( constructor(
readonly eventEmitter: EventEmitter2, readonly eventEmitter: EventEmitter2,
@InjectModel(ContentType.name) readonly model: Model<ContentType>, @InjectModel(ContentType.name) readonly model: Model<ContentType>,

View File

@ -6,7 +6,7 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/ */
import { Prop, Schema, SchemaFactory, ModelDefinition } from '@nestjs/mongoose'; import { ModelDefinition, Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { BaseSchema } from '@/utils/generics/base-schema'; import { BaseSchema } from '@/utils/generics/base-schema';
@ -39,7 +39,7 @@ export class ContentType extends BaseSchema {
}, },
], ],
}) })
fields?: { fields: {
name: string; name: string;
label: string; label: string;
type: string; type: string;

View File

@ -10,11 +10,17 @@ import { Injectable } from '@nestjs/common';
import { BaseService } from '@/utils/generics/base-service'; import { BaseService } from '@/utils/generics/base-service';
import { ContentTypeDtoMapActions } from '../dto/contentType.dto';
import { ContentTypeRepository } from '../repositories/content-type.repository'; import { ContentTypeRepository } from '../repositories/content-type.repository';
import { ContentType } from '../schemas/content-type.schema'; import { ContentType } from '../schemas/content-type.schema';
@Injectable() @Injectable()
export class ContentTypeService extends BaseService<ContentType> { export class ContentTypeService extends BaseService<
ContentType,
never,
never,
ContentTypeDtoMapActions
> {
constructor(readonly repository: ContentTypeRepository) { constructor(readonly repository: ContentTypeRepository) {
super(repository); super(repository);
} }

View File

@ -8,16 +8,37 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { ContentTypeCreateDto } from '@/cms/dto/contentType.dto';
import { import {
ContentType, ContentType,
ContentTypeModel, ContentTypeModel,
} from '@/cms/schemas/content-type.schema'; } from '@/cms/schemas/content-type.schema';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues'; import { getFixturesWithDefaultValues } from '../defaultValues';
import { TFixturesDefaultValues } from '../types';
const contentTypes: ContentTypeCreateDto[] = [ export const fieldsWithDefaultValues = {
fields: [
{
name: 'title',
label: 'Title',
type: 'text',
},
{
name: 'status',
label: 'Status',
type: 'checkbox',
},
],
} satisfies Partial<ContentType>;
type TFieldWithDefaultValues =
| keyof typeof fieldsWithDefaultValues
| keyof BaseSchema;
type TTransformedField<T> = Omit<T, TFieldWithDefaultValues> &
Partial<Pick<ContentType, TFieldWithDefaultValues>>;
type TContentType = TTransformedField<ContentType>;
const contentTypes: TContentType[] = [
{ {
name: 'Product', name: 'Product',
fields: [ fields: [
@ -100,24 +121,9 @@ const contentTypes: ContentTypeCreateDto[] = [
}, },
]; ];
export const contentTypeDefaultValues: TFixturesDefaultValues<ContentType> = { export const contentTypeFixtures = getFixturesWithDefaultValues<TContentType>({
fields: [
{
name: 'title',
label: 'Title',
type: 'text',
},
{
name: 'status',
label: 'Status',
type: 'checkbox',
},
],
};
export const contentTypeFixtures = getFixturesWithDefaultValues<ContentType>({
fixtures: contentTypes, fixtures: contentTypes,
defaultValues: contentTypeDefaultValues, defaultValues: fieldsWithDefaultValues,
}); });
export const installContentTypeFixtures = async () => { export const installContentTypeFixtures = async () => {