feat: implement dynamic create DTO for Label

This commit is contained in:
yassinedorbozgithub 2025-01-10 15:59:02 +01:00
parent df8e48a830
commit dc7d451378
5 changed files with 34 additions and 14 deletions

View File

@ -9,12 +9,14 @@
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger'; import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { import {
IsNotEmpty, IsNotEmpty,
IsObject,
IsOptional, IsOptional,
IsString, IsString,
Matches, Matches,
IsObject,
} from 'class-validator'; } from 'class-validator';
import { DtoConfig } from '@/utils/types/dto.types';
export class LabelCreateDto { export class LabelCreateDto {
@ApiProperty({ description: 'Label title', type: String }) @ApiProperty({ description: 'Label title', type: String })
@IsNotEmpty() @IsNotEmpty()
@ -39,3 +41,7 @@ export class LabelCreateDto {
} }
export class LabelUpdateDto extends PartialType(LabelCreateDto) {} export class LabelUpdateDto extends PartialType(LabelCreateDto) {}
export type LabelDtoMapActions = DtoConfig<{
create: LabelCreateDto;
}>;

View File

@ -14,6 +14,7 @@ import { Document, Model, Query } from 'mongoose';
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 { LabelDtoMapActions } from '../dto/label.dto';
import { import {
Label, Label,
LABEL_POPULATE, LABEL_POPULATE,
@ -26,7 +27,8 @@ import {
export class LabelRepository extends BaseRepository< export class LabelRepository extends BaseRepository<
Label, Label,
LabelPopulate, LabelPopulate,
LabelFull LabelFull,
LabelDtoMapActions
> { > {
constructor( constructor(
readonly eventEmitter: EventEmitter2, readonly eventEmitter: EventEmitter2,

View File

@ -43,13 +43,13 @@ export class LabelStub extends BaseSchema {
@Prop({ @Prop({
type: String, type: String,
}) })
description?: string; description: string;
@Prop({ @Prop({
type: Boolean, type: Boolean,
default: false, default: false,
}) })
builtin?: boolean; builtin: boolean;
} }
@Schema({ timestamps: true }) @Schema({ timestamps: true })

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 { LabelDtoMapActions } from '../dto/label.dto';
import { LabelRepository } from '../repositories/label.repository'; import { LabelRepository } from '../repositories/label.repository';
import { Label, LabelFull, LabelPopulate } from '../schemas/label.schema'; import { Label, LabelFull, LabelPopulate } from '../schemas/label.schema';
@Injectable() @Injectable()
export class LabelService extends BaseService<Label, LabelPopulate, LabelFull> { export class LabelService extends BaseService<
Label,
LabelPopulate,
LabelFull,
LabelDtoMapActions
> {
constructor(readonly repository: LabelRepository) { constructor(readonly repository: LabelRepository) {
super(repository); super(repository);
} }

View File

@ -8,13 +8,23 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { LabelCreateDto } from '@/chat/dto/label.dto';
import { Label, LabelModel } from '@/chat/schemas/label.schema'; import { Label, LabelModel } from '@/chat/schemas/label.schema';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues'; import { getFixturesWithDefaultValues } from '../defaultValues';
import { TFixturesDefaultValues } from '../types';
export const labels: LabelCreateDto[] = [ export const fieldsWithDefaultValues = {
builtin: false,
} satisfies Partial<Label>;
type TFieldWithDefaultValues =
| keyof typeof fieldsWithDefaultValues
| keyof BaseSchema;
type TTransformedField<T> = Omit<T, TFieldWithDefaultValues> &
Partial<Pick<Label, TFieldWithDefaultValues>>;
type TLabel = TTransformedField<Label>;
export const labels: TLabel[] = [
{ {
description: 'test description 1', description: 'test description 1',
label_id: { label_id: {
@ -39,13 +49,9 @@ export const labels: LabelCreateDto[] = [
}, },
]; ];
export const labelDefaultValues: TFixturesDefaultValues<Label> = { export const labelFixtures = getFixturesWithDefaultValues<TLabel>({
builtin: false,
};
export const labelFixtures = getFixturesWithDefaultValues<Label>({
fixtures: labels, fixtures: labels,
defaultValues: labelDefaultValues, defaultValues: fieldsWithDefaultValues,
}); });
export const installLabelFixtures = async () => { export const installLabelFixtures = async () => {