feat: implement dynamic create DTO for Content

This commit is contained in:
yassinedorbozgithub 2025-01-10 17:15:40 +01:00
parent 559c6c586f
commit 635687536b
5 changed files with 27 additions and 13 deletions

View File

@ -9,6 +9,7 @@
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { IsBoolean, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { DtoConfig } from '@/utils/types/dto.types';
import { IsObjectId } from '@/utils/validation-rules/is-object-id';
export class ContentCreateDto {
@ -34,3 +35,7 @@ export class ContentCreateDto {
}
export class ContentUpdateDto extends PartialType(ContentCreateDto) {}
export type ContentDTOMapActions = DtoConfig<{
create: ContentCreateDto;
}>;

View File

@ -21,6 +21,7 @@ import {
import { BaseRepository } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types';
import { ContentDTOMapActions } from '../dto/content.dto';
import {
Content,
CONTENT_POPULATE,
@ -32,7 +33,8 @@ import {
export class ContentRepository extends BaseRepository<
Content,
ContentPopulate,
ContentFull
ContentFull,
ContentDTOMapActions
> {
constructor(
readonly eventEmitter: EventEmitter2,

View File

@ -41,7 +41,7 @@ export class ContentStub extends BaseSchema {
* Either of not this content is active.
*/
@Prop({ type: Boolean, default: true })
status?: boolean;
status: boolean;
@Prop({ type: mongoose.Schema.Types.Mixed })
dynamicFields: Record<string, any>;

View File

@ -19,6 +19,7 @@ import { LoggerService } from '@/logger/logger.service';
import { BaseService } from '@/utils/generics/base-service';
import { TFilterQuery } from '@/utils/types/filter.types';
import { ContentDTOMapActions } from '../dto/content.dto';
import { ContentRepository } from '../repositories/content.repository';
import {
Content,
@ -30,7 +31,8 @@ import {
export class ContentService extends BaseService<
Content,
ContentPopulate,
ContentFull
ContentFull,
ContentDTOMapActions
> {
constructor(
readonly repository: ContentRepository,

View File

@ -8,16 +8,26 @@
import mongoose from 'mongoose';
import { ContentCreateDto } from '@/cms/dto/content.dto';
import { Content, ContentModel } from '@/cms/schemas/content.schema';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues';
import { TFixturesDefaultValues } from '../types';
import { installAttachmentFixtures } from './attachment';
import { installContentTypeFixtures } from './contenttype';
const contents: ContentCreateDto[] = [
export const fieldsWithDefaultValues = {
status: true,
} satisfies Partial<Content>;
type TFieldWithDefaultValues =
| keyof typeof fieldsWithDefaultValues
| keyof BaseSchema;
type TTransformedField<T> = Omit<T, TFieldWithDefaultValues> &
Partial<Pick<Content, TFieldWithDefaultValues>>;
type TContent = TTransformedField<Content>;
const contents: TContent[] = [
{
title: 'Jean',
dynamicFields: {
@ -131,14 +141,9 @@ const contents: ContentCreateDto[] = [
},
];
export const categoryDefaultValues: TFixturesDefaultValues<Content> = {
status: true,
createdAt: undefined,
};
export const contentFixtures = getFixturesWithDefaultValues<Content>({
export const contentFixtures = getFixturesWithDefaultValues<TContent>({
fixtures: contents,
defaultValues: categoryDefaultValues,
defaultValues: fieldsWithDefaultValues,
});
export const installContentFixtures = async () => {