mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: implement dynamic create DTO for Conversation
This commit is contained in:
parent
dfa38c9c61
commit
382bdd4ef1
@ -16,6 +16,7 @@ import {
|
|||||||
IsString,
|
IsString,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
|
|
||||||
|
import { DtoConfig } from '@/utils/types/dto.types';
|
||||||
import { IsObjectId } from '@/utils/validation-rules/is-object-id';
|
import { IsObjectId } from '@/utils/validation-rules/is-object-id';
|
||||||
|
|
||||||
import { Context } from './../schemas/types/context';
|
import { Context } from './../schemas/types/context';
|
||||||
@ -45,7 +46,7 @@ export class ConversationCreateDto {
|
|||||||
@IsObjectId({
|
@IsObjectId({
|
||||||
message: 'Current must be a valid objectId',
|
message: 'Current must be a valid objectId',
|
||||||
})
|
})
|
||||||
current: string;
|
current?: string;
|
||||||
|
|
||||||
@ApiProperty({ description: 'next conversation', type: Array })
|
@ApiProperty({ description: 'next conversation', type: Array })
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@ -54,5 +55,9 @@ export class ConversationCreateDto {
|
|||||||
each: true,
|
each: true,
|
||||||
message: 'next must be a valid objectId',
|
message: 'next must be a valid objectId',
|
||||||
})
|
})
|
||||||
next: string[];
|
next?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type IConversationDto = DtoConfig<{
|
||||||
|
create: ConversationCreateDto;
|
||||||
|
}>;
|
||||||
|
@ -13,6 +13,7 @@ import { Model } from 'mongoose';
|
|||||||
|
|
||||||
import { BaseRepository } from '@/utils/generics/base-repository';
|
import { BaseRepository } from '@/utils/generics/base-repository';
|
||||||
|
|
||||||
|
import { IConversationDto } from '../dto/conversation.dto';
|
||||||
import {
|
import {
|
||||||
Conversation,
|
Conversation,
|
||||||
CONVERSATION_POPULATE,
|
CONVERSATION_POPULATE,
|
||||||
@ -24,7 +25,8 @@ import {
|
|||||||
export class ConversationRepository extends BaseRepository<
|
export class ConversationRepository extends BaseRepository<
|
||||||
Conversation,
|
Conversation,
|
||||||
ConversationPopulate,
|
ConversationPopulate,
|
||||||
ConversationFull
|
ConversationFull,
|
||||||
|
IConversationDto
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly eventEmitter: EventEmitter2,
|
readonly eventEmitter: EventEmitter2,
|
||||||
|
@ -51,19 +51,19 @@ class ConversationStub extends BaseSchema {
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: true,
|
default: true,
|
||||||
})
|
})
|
||||||
active?: boolean;
|
active: boolean;
|
||||||
|
|
||||||
@Prop({
|
@Prop({
|
||||||
type: Object,
|
type: Object,
|
||||||
default: getDefaultConversationContext(),
|
default: getDefaultConversationContext(),
|
||||||
})
|
})
|
||||||
context?: Context;
|
context: Context;
|
||||||
|
|
||||||
@Prop({
|
@Prop({
|
||||||
type: MongooseSchema.Types.ObjectId,
|
type: MongooseSchema.Types.ObjectId,
|
||||||
ref: 'Block',
|
ref: 'Block',
|
||||||
})
|
})
|
||||||
current?: unknown;
|
current: unknown;
|
||||||
|
|
||||||
@Prop([
|
@Prop([
|
||||||
{
|
{
|
||||||
@ -72,7 +72,7 @@ class ConversationStub extends BaseSchema {
|
|||||||
default: [],
|
default: [],
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
next?: unknown;
|
next: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Schema({ timestamps: true })
|
@Schema({ timestamps: true })
|
||||||
@ -81,10 +81,10 @@ export class Conversation extends ConversationStub {
|
|||||||
sender: string;
|
sender: string;
|
||||||
|
|
||||||
@Transform(({ obj }) => obj.current.toString())
|
@Transform(({ obj }) => obj.current.toString())
|
||||||
current?: string;
|
current: string;
|
||||||
|
|
||||||
@Transform(({ obj }) => obj.next.map((elem) => elem.toString()))
|
@Transform(({ obj }) => obj.next.map((elem) => elem.toString()))
|
||||||
next?: string[];
|
next: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Schema({ timestamps: true })
|
@Schema({ timestamps: true })
|
||||||
|
@ -12,6 +12,7 @@ import EventWrapper from '@/channel/lib/EventWrapper';
|
|||||||
import { LoggerService } from '@/logger/logger.service';
|
import { LoggerService } from '@/logger/logger.service';
|
||||||
import { BaseService } from '@/utils/generics/base-service';
|
import { BaseService } from '@/utils/generics/base-service';
|
||||||
|
|
||||||
|
import { IConversationDto } from '../dto/conversation.dto';
|
||||||
import { VIEW_MORE_PAYLOAD } from '../helpers/constants';
|
import { VIEW_MORE_PAYLOAD } from '../helpers/constants';
|
||||||
import { ConversationRepository } from '../repositories/conversation.repository';
|
import { ConversationRepository } from '../repositories/conversation.repository';
|
||||||
import { Block, BlockFull } from '../schemas/block.schema';
|
import { Block, BlockFull } from '../schemas/block.schema';
|
||||||
@ -30,7 +31,8 @@ import { SubscriberService } from './subscriber.service';
|
|||||||
export class ConversationService extends BaseService<
|
export class ConversationService extends BaseService<
|
||||||
Conversation,
|
Conversation,
|
||||||
ConversationPopulate,
|
ConversationPopulate,
|
||||||
ConversationFull
|
ConversationFull,
|
||||||
|
IConversationDto
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly repository: ConversationRepository,
|
readonly repository: ConversationRepository,
|
||||||
|
@ -30,6 +30,7 @@ import {
|
|||||||
import { TFilterQuery } from '@/utils/types/filter.types';
|
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||||
|
|
||||||
import { PageQueryDto, QuerySortDto } from '../pagination/pagination-query.dto';
|
import { PageQueryDto, QuerySortDto } from '../pagination/pagination-query.dto';
|
||||||
|
import { DtoInfer, DtoOperations, DtoProps } from '../types/dto.types';
|
||||||
|
|
||||||
import { BaseSchema } from './base-schema';
|
import { BaseSchema } from './base-schema';
|
||||||
import { LifecycleHookManager } from './lifecycle-hook-manager';
|
import { LifecycleHookManager } from './lifecycle-hook-manager';
|
||||||
@ -70,6 +71,7 @@ export abstract class BaseRepository<
|
|||||||
T extends FlattenMaps<unknown>,
|
T extends FlattenMaps<unknown>,
|
||||||
P extends string = never,
|
P extends string = never,
|
||||||
TFull extends Omit<T, P> = never,
|
TFull extends Omit<T, P> = never,
|
||||||
|
DTO extends DtoProps<T> = unknown,
|
||||||
U = Omit<T, keyof BaseSchema>,
|
U = Omit<T, keyof BaseSchema>,
|
||||||
D = Document<T>,
|
D = Document<T>,
|
||||||
> {
|
> {
|
||||||
@ -454,7 +456,7 @@ export abstract class BaseRepository<
|
|||||||
return await this.model.countDocuments(criteria).exec();
|
return await this.model.countDocuments(criteria).exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(dto: U): Promise<T> {
|
async create(dto: DtoInfer<DtoOperations.Create, DTO, U>): Promise<T> {
|
||||||
const doc = await this.model.create(dto);
|
const doc = await this.model.create(dto);
|
||||||
|
|
||||||
return plainToClass(
|
return plainToClass(
|
||||||
|
@ -14,6 +14,7 @@ import { ProjectionType, QueryOptions } from 'mongoose';
|
|||||||
import { TFilterQuery } from '@/utils/types/filter.types';
|
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||||
|
|
||||||
import { PageQueryDto, QuerySortDto } from '../pagination/pagination-query.dto';
|
import { PageQueryDto, QuerySortDto } from '../pagination/pagination-query.dto';
|
||||||
|
import { DtoInfer, DtoOperations, DtoProps } from '../types/dto.types';
|
||||||
|
|
||||||
import { BaseRepository } from './base-repository';
|
import { BaseRepository } from './base-repository';
|
||||||
import { BaseSchema } from './base-schema';
|
import { BaseSchema } from './base-schema';
|
||||||
@ -22,6 +23,7 @@ export abstract class BaseService<
|
|||||||
T extends BaseSchema,
|
T extends BaseSchema,
|
||||||
P extends string = never,
|
P extends string = never,
|
||||||
TFull extends Omit<T, P> = never,
|
TFull extends Omit<T, P> = never,
|
||||||
|
DTO extends DtoProps<any> = unknown,
|
||||||
> {
|
> {
|
||||||
constructor(protected readonly repository: BaseRepository<T, P, TFull>) {}
|
constructor(protected readonly repository: BaseRepository<T, P, TFull>) {}
|
||||||
|
|
||||||
@ -140,7 +142,9 @@ export abstract class BaseService<
|
|||||||
return await this.repository.count(criteria);
|
return await this.repository.count(criteria);
|
||||||
}
|
}
|
||||||
|
|
||||||
async create<D extends Omit<T, keyof BaseSchema>>(dto: D): Promise<T> {
|
async create<
|
||||||
|
D extends DtoInfer<DtoOperations.Create, DTO, Omit<T, keyof BaseSchema>>,
|
||||||
|
>(dto: D): Promise<T> {
|
||||||
try {
|
try {
|
||||||
return await this.repository.create(dto);
|
return await this.repository.create(dto);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -153,10 +157,9 @@ export abstract class BaseService<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOneOrCreate<D extends Omit<T, keyof BaseSchema>>(
|
async findOneOrCreate<
|
||||||
criteria: string | TFilterQuery<T>,
|
D extends DtoInfer<DtoOperations.Create, DTO, Omit<T, keyof BaseSchema>>,
|
||||||
dto: D,
|
>(criteria: string | TFilterQuery<T>, dto: D): Promise<T> {
|
||||||
): Promise<T> {
|
|
||||||
const result = await this.findOne(criteria);
|
const result = await this.findOne(criteria);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return await this.create(dto);
|
return await this.create(dto);
|
||||||
|
15
api/src/utils/test/fixtures/conversation.ts
vendored
15
api/src/utils/test/fixtures/conversation.ts
vendored
@ -9,10 +9,7 @@
|
|||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
|
|
||||||
import { ConversationCreateDto } from '@/chat/dto/conversation.dto';
|
import { ConversationCreateDto } from '@/chat/dto/conversation.dto';
|
||||||
import {
|
import { ConversationModel } from '@/chat/schemas/conversation.schema';
|
||||||
Conversation,
|
|
||||||
ConversationModel,
|
|
||||||
} from '@/chat/schemas/conversation.schema';
|
|
||||||
|
|
||||||
import { getFixturesWithDefaultValues } from '../defaultValues';
|
import { getFixturesWithDefaultValues } from '../defaultValues';
|
||||||
import { TFixturesDefaultValues } from '../types';
|
import { TFixturesDefaultValues } from '../types';
|
||||||
@ -116,14 +113,16 @@ const conversations: ConversationCreateDto[] = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export const conversationDefaultValues: TFixturesDefaultValues<Conversation> = {
|
export const conversationDefaultValues: TFixturesDefaultValues<ConversationCreateDto> =
|
||||||
|
{
|
||||||
active: false,
|
active: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const conversationFixtures = getFixturesWithDefaultValues<Conversation>({
|
export const conversationFixtures =
|
||||||
|
getFixturesWithDefaultValues<ConversationCreateDto>({
|
||||||
fixtures: conversations,
|
fixtures: conversations,
|
||||||
defaultValues: conversationDefaultValues,
|
defaultValues: conversationDefaultValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const installConversationTypeFixtures = async () => {
|
export const installConversationTypeFixtures = async () => {
|
||||||
const subscribers = await installSubscriberFixtures();
|
const subscribers = await installSubscriberFixtures();
|
||||||
|
24
api/src/utils/types/dto.types.ts
Normal file
24
api/src/utils/types/dto.types.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2025 Hexastack. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||||
|
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
||||||
|
* 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).
|
||||||
|
*/
|
||||||
|
|
||||||
|
export enum DtoOperations {
|
||||||
|
Create = 'create',
|
||||||
|
Read = 'read',
|
||||||
|
Update = 'update',
|
||||||
|
Delete = 'delete',
|
||||||
|
}
|
||||||
|
|
||||||
|
export type DtoConfig<T extends Partial<Record<DtoOperations, object>>> = T;
|
||||||
|
|
||||||
|
export type DtoProps<T extends Record<string, unknown>> = {
|
||||||
|
[K in DtoOperations]?: T[K];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type DtoInfer<K extends keyof DTO, DTO, T> = DTO[K] extends object
|
||||||
|
? DTO[K]
|
||||||
|
: T;
|
Loading…
Reference in New Issue
Block a user