feat: implement dynamic create DTO for Subscriber

This commit is contained in:
yassinedorbozgithub 2025-01-10 16:37:47 +01:00
parent dc7d451378
commit 7578c485f1
5 changed files with 32 additions and 15 deletions

View File

@ -161,7 +161,6 @@ export class ChannelService {
foreign_id: req.session.passport.user.id,
},
{
id: req.session.passport.user.id,
foreign_id: req.session.passport.user.id,
first_name: req.session.passport.user.first_name,
last_name: req.session.passport.user.last_name,

View File

@ -17,6 +17,7 @@ import {
} from 'class-validator';
import { ChannelName } from '@/channel/types';
import { DtoConfig } from '@/utils/types/dto.types';
import { IsObjectId } from '@/utils/validation-rules/is-object-id';
import { SubscriberChannelData } from '../schemas/types/channel';
@ -36,7 +37,7 @@ export class SubscriberCreateDto {
@ApiPropertyOptional({ description: 'Subscriber locale', type: String })
@IsOptional()
@IsString()
locale: string;
locale?: string;
@ApiPropertyOptional({ description: 'Subscriber timezone', type: Number })
@IsOptional()
@ -51,17 +52,17 @@ export class SubscriberCreateDto {
@ApiPropertyOptional({ description: 'Subscriber gender', type: String })
@IsOptional()
@IsString()
gender: string;
gender?: string;
@ApiPropertyOptional({ description: 'Subscriber country', type: String })
@IsOptional()
@IsString()
country: string;
country?: string;
@ApiPropertyOptional({ description: 'Subscriber foreign id', type: String })
@IsOptional()
@IsString()
foreign_id: string;
foreign_id?: string;
@ApiProperty({ description: 'Subscriber labels', type: Array })
@IsNotEmpty()
@ -114,3 +115,7 @@ export class SubscriberCreateDto {
}
export class SubscriberUpdateDto extends PartialType(SubscriberCreateDto) {}
export type UserDtoMapActions = DtoConfig<{
create: SubscriberCreateDto;
}>;

View File

@ -20,7 +20,7 @@ import {
import { BaseRepository } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types';
import { SubscriberUpdateDto } from '../dto/subscriber.dto';
import { SubscriberUpdateDto, UserDtoMapActions } from '../dto/subscriber.dto';
import {
Subscriber,
SUBSCRIBER_POPULATE,
@ -33,7 +33,8 @@ import {
export class SubscriberRepository extends BaseRepository<
Subscriber,
SubscriberPopulate,
SubscriberFull
SubscriberFull,
UserDtoMapActions
> {
constructor(
readonly eventEmitter: EventEmitter2,

View File

@ -28,7 +28,7 @@ import { SocketRequest } from '@/websocket/utils/socket-request';
import { SocketResponse } from '@/websocket/utils/socket-response';
import { WebsocketGateway } from '@/websocket/websocket.gateway';
import { SubscriberUpdateDto } from '../dto/subscriber.dto';
import { SubscriberUpdateDto, UserDtoMapActions } from '../dto/subscriber.dto';
import { SubscriberRepository } from '../repositories/subscriber.repository';
import {
Subscriber,
@ -40,7 +40,8 @@ import {
export class SubscriberService extends BaseService<
Subscriber,
SubscriberPopulate,
SubscriberFull
SubscriberFull,
UserDtoMapActions
> {
private readonly gateway: WebsocketGateway;

View File

@ -8,8 +8,8 @@
import mongoose from 'mongoose';
import { SubscriberCreateDto } from '@/chat/dto/subscriber.dto';
import { Subscriber, SubscriberModel } from '@/chat/schemas/subscriber.schema';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues';
import { TFixturesDefaultValues } from '../types';
@ -17,7 +17,22 @@ import { TFixturesDefaultValues } from '../types';
import { installLabelFixtures } from './label';
import { installUserFixtures } from './user';
const subscribers: SubscriberCreateDto[] = [
export const fieldsWithDefaultValues = {
context: {},
avatar: null,
assignedAt: null,
assignedTo: null,
timezone: 0,
} satisfies Partial<Subscriber>;
type TFieldWithDefaultValues =
| keyof typeof fieldsWithDefaultValues
| keyof BaseSchema;
type TTransformedField<T> = Omit<T, TFieldWithDefaultValues> &
Partial<Pick<Subscriber, TFieldWithDefaultValues>>;
type TSubscriber = TTransformedField<Subscriber>;
const subscribers: TSubscriber[] = [
{
foreign_id: 'foreign-id-messenger',
first_name: 'Jhon',
@ -30,7 +45,6 @@ const subscribers: SubscriberCreateDto[] = [
name: 'messenger-channel',
},
labels: [],
assignedAt: null,
lastvisit: new Date('2020-01-01T20:40:03.249Z'),
retainedFrom: new Date('2020-01-01T20:40:03.249Z'),
},
@ -46,7 +60,6 @@ const subscribers: SubscriberCreateDto[] = [
name: 'web-channel',
},
labels: [],
assignedAt: null,
lastvisit: new Date('2021-01-01T20:40:03.249Z'),
retainedFrom: new Date('2021-01-02T20:40:03.249Z'),
},
@ -62,7 +75,6 @@ const subscribers: SubscriberCreateDto[] = [
name: 'web-channel',
},
labels: [],
assignedAt: null,
lastvisit: new Date('2022-01-01T20:40:03.249Z'),
retainedFrom: new Date('2022-01-02T20:40:03.249Z'),
},
@ -78,7 +90,6 @@ const subscribers: SubscriberCreateDto[] = [
name: 'web-channel',
},
labels: [],
assignedAt: null,
lastvisit: new Date('2024-01-01T20:40:03.249Z'),
retainedFrom: new Date('2024-01-02T20:40:03.249Z'),
},