From 9e49e8256e98c3de1fb4769d71e77a9d05011cac Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Fri, 10 Jan 2025 14:52:38 +0100 Subject: [PATCH] feat: implement dynamic create DTO for Category --- api/src/chat/dto/category.dto.ts | 10 +++++-- .../chat/repositories/category.repository.ts | 8 ++++- api/src/chat/schemas/category.schema.ts | 6 ++-- api/src/chat/seeds/category.seed.ts | 8 ++++- api/src/chat/services/category.service.ts | 8 ++++- api/src/utils/test/fixtures/category.ts | 30 +++++++++++-------- 6 files changed, 50 insertions(+), 20 deletions(-) diff --git a/api/src/chat/dto/category.dto.ts b/api/src/chat/dto/category.dto.ts index ca896e22..0a857d6d 100644 --- a/api/src/chat/dto/category.dto.ts +++ b/api/src/chat/dto/category.dto.ts @@ -8,14 +8,16 @@ import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger'; import { + IsArray, IsBoolean, IsNotEmpty, + IsNumber, IsOptional, IsString, - IsNumber, - IsArray, } from 'class-validator'; +import { DtoConfig } from '@/utils/types/dto.types'; + export class CategoryCreateDto { @ApiProperty({ description: 'Category label', type: String }) @IsNotEmpty() @@ -39,3 +41,7 @@ export class CategoryCreateDto { } export class CategoryUpdateDto extends PartialType(CategoryCreateDto) {} + +export type CategoryDTOMapActions = DtoConfig<{ + create: CategoryCreateDto; +}>; diff --git a/api/src/chat/repositories/category.repository.ts b/api/src/chat/repositories/category.repository.ts index 45170306..f27f05ac 100644 --- a/api/src/chat/repositories/category.repository.ts +++ b/api/src/chat/repositories/category.repository.ts @@ -14,11 +14,17 @@ import { Document, Model, Query } from 'mongoose'; import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository'; import { TFilterQuery } from '@/utils/types/filter.types'; +import { CategoryDTOMapActions } from '../dto/category.dto'; import { Category } from '../schemas/category.schema'; import { BlockService } from '../services/block.service'; @Injectable() -export class CategoryRepository extends BaseRepository { +export class CategoryRepository extends BaseRepository< + Category, + never, + never, + CategoryDTOMapActions +> { private readonly blockService: BlockService; constructor( diff --git a/api/src/chat/schemas/category.schema.ts b/api/src/chat/schemas/category.schema.ts index 7ed8cdf3..bc7b8cfd 100644 --- a/api/src/chat/schemas/category.schema.ts +++ b/api/src/chat/schemas/category.schema.ts @@ -25,19 +25,19 @@ export class Category extends BaseSchema { type: Boolean, default: false, }) - builtin?: boolean; + builtin: boolean; @Prop({ type: Number, default: 100, }) - zoom?: number; + zoom: number; @Prop({ type: [Number, Number], default: [0, 0], }) - offset?: [number, number]; + offset: [number, number]; } export const CategoryModel: ModelDefinition = LifecycleHookManager.attach({ diff --git a/api/src/chat/seeds/category.seed.ts b/api/src/chat/seeds/category.seed.ts index d1917309..034f9623 100644 --- a/api/src/chat/seeds/category.seed.ts +++ b/api/src/chat/seeds/category.seed.ts @@ -10,11 +10,17 @@ import { Injectable } from '@nestjs/common'; import { BaseSeeder } from '@/utils/generics/base-seeder'; +import { CategoryDTOMapActions } from '../dto/category.dto'; import { CategoryRepository } from '../repositories/category.repository'; import { Category } from '../schemas/category.schema'; @Injectable() -export class CategorySeeder extends BaseSeeder { +export class CategorySeeder extends BaseSeeder< + Category, + never, + never, + CategoryDTOMapActions +> { constructor(private readonly categoryRepository: CategoryRepository) { super(categoryRepository); } diff --git a/api/src/chat/services/category.service.ts b/api/src/chat/services/category.service.ts index 079a3a7e..8bb4472e 100644 --- a/api/src/chat/services/category.service.ts +++ b/api/src/chat/services/category.service.ts @@ -10,11 +10,17 @@ import { Injectable } from '@nestjs/common'; import { BaseService } from '@/utils/generics/base-service'; +import { CategoryDTOMapActions } from '../dto/category.dto'; import { CategoryRepository } from '../repositories/category.repository'; import { Category } from '../schemas/category.schema'; @Injectable() -export class CategoryService extends BaseService { +export class CategoryService extends BaseService< + Category, + never, + never, + CategoryDTOMapActions +> { constructor(readonly repository: CategoryRepository) { super(repository); } diff --git a/api/src/utils/test/fixtures/category.ts b/api/src/utils/test/fixtures/category.ts index 2bb0f6c5..479b1b99 100644 --- a/api/src/utils/test/fixtures/category.ts +++ b/api/src/utils/test/fixtures/category.ts @@ -8,13 +8,25 @@ import mongoose from 'mongoose'; -import { CategoryCreateDto } from '@/chat/dto/category.dto'; -import { CategoryModel, Category } from '@/chat/schemas/category.schema'; +import { Category, CategoryModel } from '@/chat/schemas/category.schema'; +import { BaseSchema } from '@/utils/generics/base-schema'; import { getFixturesWithDefaultValues } from '../defaultValues'; -import { TFixturesDefaultValues } from '../types'; -export const categories: CategoryCreateDto[] = [ +export const fieldsWithDefaultValues = { + builtin: false, + zoom: 100, + offset: [0, 0], +} satisfies Partial; + +type TFieldWithDefaultValues = + | keyof typeof fieldsWithDefaultValues + | keyof BaseSchema; +type TTransformedField = Omit & + Partial>; +type TCategory = TTransformedField; + +export const categories: TCategory[] = [ { label: 'test category 1', }, @@ -23,15 +35,9 @@ export const categories: CategoryCreateDto[] = [ }, ]; -export const categoryDefaultValues: TFixturesDefaultValues = { - builtin: false, - zoom: 100, - offset: [0, 0], -}; - -export const categoryFixtures = getFixturesWithDefaultValues({ +export const categoryFixtures = getFixturesWithDefaultValues({ fixtures: categories, - defaultValues: categoryDefaultValues, + defaultValues: fieldsWithDefaultValues, }); export const installCategoryFixtures = async () => {