From 7578c485f174a821f4721bd2ed9875e64c87818a Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Fri, 10 Jan 2025 16:37:47 +0100 Subject: [PATCH] feat: implement dynamic create DTO for Subscriber --- api/src/channel/channel.service.ts | 1 - api/src/chat/dto/subscriber.dto.ts | 13 +++++++---- .../repositories/subscriber.repository.ts | 5 ++-- api/src/chat/services/subscriber.service.ts | 5 ++-- api/src/utils/test/fixtures/subscriber.ts | 23 ++++++++++++++----- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/api/src/channel/channel.service.ts b/api/src/channel/channel.service.ts index 66745c97..49af6016 100644 --- a/api/src/channel/channel.service.ts +++ b/api/src/channel/channel.service.ts @@ -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, diff --git a/api/src/chat/dto/subscriber.dto.ts b/api/src/chat/dto/subscriber.dto.ts index 82148f96..7b8a5bd2 100644 --- a/api/src/chat/dto/subscriber.dto.ts +++ b/api/src/chat/dto/subscriber.dto.ts @@ -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; +}>; diff --git a/api/src/chat/repositories/subscriber.repository.ts b/api/src/chat/repositories/subscriber.repository.ts index a3ee6cd1..34bd5d71 100644 --- a/api/src/chat/repositories/subscriber.repository.ts +++ b/api/src/chat/repositories/subscriber.repository.ts @@ -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, diff --git a/api/src/chat/services/subscriber.service.ts b/api/src/chat/services/subscriber.service.ts index d5b688a9..b822fa61 100644 --- a/api/src/chat/services/subscriber.service.ts +++ b/api/src/chat/services/subscriber.service.ts @@ -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; diff --git a/api/src/utils/test/fixtures/subscriber.ts b/api/src/utils/test/fixtures/subscriber.ts index 605d7560..1b4c4757 100644 --- a/api/src/utils/test/fixtures/subscriber.ts +++ b/api/src/utils/test/fixtures/subscriber.ts @@ -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; + +type TFieldWithDefaultValues = + | keyof typeof fieldsWithDefaultValues + | keyof BaseSchema; +type TTransformedField = Omit & + Partial>; +type TSubscriber = TTransformedField; + +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'), },