feat: add Fixtures Type Builder

This commit is contained in:
yassinedorbozgithub 2025-01-11 20:09:35 +01:00
parent ed928ad597
commit 156d10210b
9 changed files with 110 additions and 99 deletions

View File

@ -8,16 +8,19 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { BlockCreateDto } from '@/chat/dto/block.dto';
import { Block, BlockModel } from '@/chat/schemas/block.schema'; import { Block, BlockModel } from '@/chat/schemas/block.schema';
import { CategoryModel } from '@/chat/schemas/category.schema'; import { CategoryModel } from '@/chat/schemas/category.schema';
import { FileType } from '@/chat/schemas/types/attachment'; import { FileType } from '@/chat/schemas/types/attachment';
import { ButtonType } from '@/chat/schemas/types/button'; import { ButtonType } from '@/chat/schemas/types/button';
import { QuickReplyType } from '@/chat/schemas/types/quick-reply'; import { QuickReplyType } from '@/chat/schemas/types/quick-reply';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues'; import { getFixturesWithDefaultValues } from '../defaultValues';
import { FixturesTypeBuilder } from '../types';
export const fieldsWithDefaultValues = { type TBlockFixtures = FixturesTypeBuilder<Block, BlockCreateDto>;
export const blockDefaultValues: TBlockFixtures['defaultValues'] = {
options: {}, options: {},
nextBlocks: [], nextBlocks: [],
capture_vars: [], capture_vars: [],
@ -27,16 +30,9 @@ export const fieldsWithDefaultValues = {
starts_conversation: false, starts_conversation: false,
attachedBlock: null, attachedBlock: null,
attachedToBlock: null, attachedToBlock: null,
} satisfies Partial<Block>; };
type TFieldWithDefaultValues = export const blocks: TBlockFixtures['values'][] = [
| keyof typeof fieldsWithDefaultValues
| keyof BaseSchema;
type TTransformedField<T> = Omit<T, TFieldWithDefaultValues> &
Partial<Pick<Block, TFieldWithDefaultValues>>;
type TBlock = TTransformedField<Block>;
export const blocks: TBlock[] = [
{ {
name: 'hasNextBlocks', name: 'hasNextBlocks',
patterns: ['Hi'], patterns: ['Hi'],
@ -181,9 +177,11 @@ export const blocks: TBlock[] = [
}, },
]; ];
export const blockFixtures = getFixturesWithDefaultValues<TBlock>({ export const blockFixtures = getFixturesWithDefaultValues<
TBlockFixtures['values']
>({
fixtures: blocks, fixtures: blocks,
defaultValues: fieldsWithDefaultValues, defaultValues: blockDefaultValues,
}); });
export const installBlockFixtures = async () => { export const installBlockFixtures = async () => {

View File

@ -8,25 +8,24 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { CategoryCreateDto } from '@/chat/dto/category.dto';
import { Category, CategoryModel } 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 { getFixturesWithDefaultValues } from '../defaultValues';
import { FixturesTypeBuilder } from '../types';
export const fieldsWithDefaultValues = { export type TCategoryFixtures = FixturesTypeBuilder<
Category,
CategoryCreateDto
>;
export const fieldsWithDefaultValues: TCategoryFixtures['defaultValues'] = {
builtin: false, builtin: false,
zoom: 100, zoom: 100,
offset: [0, 0], offset: [0, 0],
} satisfies Partial<Category>; };
type TFieldWithDefaultValues = export const categories: TCategoryFixtures['values'][] = [
| keyof typeof fieldsWithDefaultValues
| keyof BaseSchema;
type TTransformedField<T> = Omit<T, TFieldWithDefaultValues> &
Partial<Pick<Category, TFieldWithDefaultValues>>;
type TCategory = TTransformedField<Category>;
export const categories: TCategory[] = [
{ {
label: 'test category 1', label: 'test category 1',
}, },
@ -35,7 +34,9 @@ export const categories: TCategory[] = [
}, },
]; ];
export const categoryFixtures = getFixturesWithDefaultValues<TCategory>({ export const categoryFixtures = getFixturesWithDefaultValues<
TCategoryFixtures['values']
>({
fixtures: categories, fixtures: categories,
defaultValues: fieldsWithDefaultValues, defaultValues: fieldsWithDefaultValues,
}); });

View File

@ -8,26 +8,22 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { ContentCreateDto } from '@/cms/dto/content.dto';
import { Content, ContentModel } from '@/cms/schemas/content.schema'; import { Content, ContentModel } from '@/cms/schemas/content.schema';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues'; import { getFixturesWithDefaultValues } from '../defaultValues';
import { FixturesTypeBuilder } from '../types';
import { installAttachmentFixtures } from './attachment'; import { installAttachmentFixtures } from './attachment';
import { installContentTypeFixtures } from './contenttype'; import { installContentTypeFixtures } from './contenttype';
export const fieldsWithDefaultValues = { type TContentFixtures = FixturesTypeBuilder<Content, ContentCreateDto>;
export const fieldsWithDefaultValues: TContentFixtures['defaultValues'] = {
status: true, status: true,
} satisfies Partial<Content>; };
type TFieldWithDefaultValues = const contents: TContentFixtures['values'][] = [
| keyof typeof fieldsWithDefaultValues
| keyof BaseSchema;
type TTransformedField<T> = Omit<T, TFieldWithDefaultValues> &
Partial<Pick<Content, TFieldWithDefaultValues>>;
type TContent = TTransformedField<Content>;
const contents: TContent[] = [
{ {
title: 'Jean', title: 'Jean',
dynamicFields: { dynamicFields: {
@ -141,7 +137,9 @@ const contents: TContent[] = [
}, },
]; ];
export const contentFixtures = getFixturesWithDefaultValues<TContent>({ export const contentFixtures = getFixturesWithDefaultValues<
TContentFixtures['values']
>({
fixtures: contents, fixtures: contents,
defaultValues: fieldsWithDefaultValues, defaultValues: fieldsWithDefaultValues,
}); });

View File

@ -8,15 +8,21 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { ContentTypeCreateDto } from '@/cms/dto/contentType.dto';
import { import {
ContentType, ContentType,
ContentTypeModel, ContentTypeModel,
} from '@/cms/schemas/content-type.schema'; } from '@/cms/schemas/content-type.schema';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues'; import { getFixturesWithDefaultValues } from '../defaultValues';
import { FixturesTypeBuilder } from '../types';
export const fieldsWithDefaultValues = { type TContentTypeFixtures = FixturesTypeBuilder<
ContentType,
ContentTypeCreateDto
>;
export const fieldsWithDefaultValues: TContentTypeFixtures['defaultValues'] = {
fields: [ fields: [
{ {
name: 'title', name: 'title',
@ -29,16 +35,9 @@ export const fieldsWithDefaultValues = {
type: 'checkbox', type: 'checkbox',
}, },
], ],
} satisfies Partial<ContentType>; };
type TFieldWithDefaultValues = const contentTypes: TContentTypeFixtures['values'][] = [
| keyof typeof fieldsWithDefaultValues
| keyof BaseSchema;
type TTransformedField<T> = Omit<T, TFieldWithDefaultValues> &
Partial<Pick<ContentType, TFieldWithDefaultValues>>;
type TContentType = TTransformedField<ContentType>;
const contentTypes: TContentType[] = [
{ {
name: 'Product', name: 'Product',
fields: [ fields: [
@ -121,7 +120,9 @@ const contentTypes: TContentType[] = [
}, },
]; ];
export const contentTypeFixtures = getFixturesWithDefaultValues<TContentType>({ export const contentTypeFixtures = getFixturesWithDefaultValues<
TContentTypeFixtures['values']
>({
fixtures: contentTypes, fixtures: contentTypes,
defaultValues: fieldsWithDefaultValues, defaultValues: fieldsWithDefaultValues,
}); });

View File

@ -8,23 +8,19 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { ContextVarCreateDto } from '@/chat/dto/context-var.dto';
import { ContextVar, ContextVarModel } from '@/chat/schemas/context-var.schema'; import { ContextVar, ContextVarModel } from '@/chat/schemas/context-var.schema';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues'; import { getFixturesWithDefaultValues } from '../defaultValues';
import { FixturesTypeBuilder } from '../types';
export const fieldsWithDefaultValues = { type TContentVarFixtures = FixturesTypeBuilder<ContextVar, ContextVarCreateDto>;
export const fieldsWithDefaultValues: TContentVarFixtures['defaultValues'] = {
permanent: false, permanent: false,
} satisfies Partial<ContextVar>; };
type TFieldWithDefaultValues = const contextVars: TContentVarFixtures['values'][] = [
| keyof typeof fieldsWithDefaultValues
| keyof BaseSchema;
type TTransformedField<T> = Omit<T, TFieldWithDefaultValues> &
Partial<Pick<ContextVar, TFieldWithDefaultValues>>;
type TContextVar = TTransformedField<ContextVar>;
const contextVars: TContextVar[] = [
{ {
label: 'test context var 1', label: 'test context var 1',
name: 'test1', name: 'test1',
@ -35,7 +31,9 @@ const contextVars: TContextVar[] = [
}, },
]; ];
export const contextVarFixtures = getFixturesWithDefaultValues<TContextVar>({ export const contextVarFixtures = getFixturesWithDefaultValues<
TContentVarFixtures['values']
>({
fixtures: contextVars, fixtures: contextVars,
defaultValues: fieldsWithDefaultValues, defaultValues: fieldsWithDefaultValues,
}); });

View File

@ -8,23 +8,19 @@
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 { FixturesTypeBuilder } from '../types';
export const fieldsWithDefaultValues = { type TLabelFixtures = FixturesTypeBuilder<Label, LabelCreateDto>;
export const fieldsWithDefaultValues: TLabelFixtures['defaultValues'] = {
builtin: false, builtin: false,
} satisfies Partial<Label>; };
type TFieldWithDefaultValues = export const labels: TLabelFixtures['values'][] = [
| 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: {
@ -49,7 +45,9 @@ export const labels: TLabel[] = [
}, },
]; ];
export const labelFixtures = getFixturesWithDefaultValues<TLabel>({ export const labelFixtures = getFixturesWithDefaultValues<
TLabelFixtures['values']
>({
fixtures: labels, fixtures: labels,
defaultValues: fieldsWithDefaultValues, defaultValues: fieldsWithDefaultValues,
}); });

View File

@ -8,27 +8,23 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { NlpSampleCreateDto } from '@/nlp/dto/nlp-sample.dto';
import { NlpSample, NlpSampleModel } from '@/nlp/schemas/nlp-sample.schema'; import { NlpSample, NlpSampleModel } from '@/nlp/schemas/nlp-sample.schema';
import { NlpSampleState } from '@/nlp/schemas/types'; import { NlpSampleState } from '@/nlp/schemas/types';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues'; import { getFixturesWithDefaultValues } from '../defaultValues';
import { FixturesTypeBuilder } from '../types';
import { installLanguageFixtures } from './language'; import { installLanguageFixtures } from './language';
export const fieldsWithDefaultValues = { type TNlpSampleFixtures = FixturesTypeBuilder<NlpSample, NlpSampleCreateDto>;
export const fieldsWithDefaultValues: TNlpSampleFixtures['defaultValues'] = {
type: NlpSampleState.train, type: NlpSampleState.train,
trained: false, trained: false,
} satisfies Partial<NlpSample>; };
type TFieldWithDefaultValues = const nlpSamples: TNlpSampleFixtures['values'][] = [
| keyof typeof fieldsWithDefaultValues
| keyof BaseSchema;
type TTransformedField<T> = Omit<T, TFieldWithDefaultValues> &
Partial<Pick<NlpSample, TFieldWithDefaultValues>>;
type TNlpSample = TTransformedField<NlpSample>;
const nlpSamples: TNlpSample[] = [
{ {
text: 'yess', text: 'yess',
language: '0', language: '0',
@ -49,7 +45,9 @@ const nlpSamples: TNlpSample[] = [
}, },
]; ];
export const nlpSampleFixtures = getFixturesWithDefaultValues<TNlpSample>({ export const nlpSampleFixtures = getFixturesWithDefaultValues<
TNlpSampleFixtures['values']
>({
fixtures: nlpSamples, fixtures: nlpSamples,
defaultValues: fieldsWithDefaultValues, defaultValues: fieldsWithDefaultValues,
}); });

View File

@ -8,31 +8,26 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { SubscriberCreateDto } from '@/chat/dto/subscriber.dto';
import { Subscriber, SubscriberModel } from '@/chat/schemas/subscriber.schema'; import { Subscriber, SubscriberModel } from '@/chat/schemas/subscriber.schema';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues'; import { getFixturesWithDefaultValues } from '../defaultValues';
import { TFixturesDefaultValues } from '../types'; import { FixturesTypeBuilder, TFixturesDefaultValues } from '../types';
import { installLabelFixtures } from './label'; import { installLabelFixtures } from './label';
import { installUserFixtures } from './user'; import { installUserFixtures } from './user';
export const fieldsWithDefaultValues = { type TSubscriberFixtures = FixturesTypeBuilder<Subscriber, SubscriberCreateDto>;
export const fieldsWithDefaultValues: TSubscriberFixtures['defaultValues'] = {
context: {}, context: {},
avatar: null, avatar: null,
assignedAt: null, assignedAt: null,
assignedTo: null, assignedTo: null,
timezone: 0, timezone: 0,
} satisfies Partial<Subscriber>; };
type TFieldWithDefaultValues = const subscribers: TSubscriberFixtures['values'][] = [
| 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', foreign_id: 'foreign-id-messenger',
first_name: 'Jhon', first_name: 'Jhon',
@ -104,7 +99,9 @@ export const subscriberDefaultValues: TFixturesDefaultValues<Subscriber> = {
avatar: null, avatar: null,
}; };
export const subscriberFixtures = getFixturesWithDefaultValues<Subscriber>({ export const subscriberFixtures = getFixturesWithDefaultValues<
TSubscriberFixtures['values']
>({
fixtures: subscribers, fixtures: subscribers,
defaultValues: subscriberDefaultValues, defaultValues: subscriberDefaultValues,
}); });

View File

@ -8,7 +8,7 @@
import { BaseSchema } from '../generics/base-schema'; import { BaseSchema } from '../generics/base-schema';
type TOptionalPropertyOf<T> = Exclude< export type TOptionalPropertyOf<T> = Exclude<
{ {
[K in keyof T]: T extends Record<K, T[K]> ? never : K; [K in keyof T]: T extends Record<K, T[K]> ? never : K;
}[keyof T], }[keyof T],
@ -23,3 +23,25 @@ export type TFixtures<T> = Omit<T, keyof BaseSchema> & {
export type TFixturesDefaultValues<T, S = TFixtures<T>> = { export type TFixturesDefaultValues<T, S = TFixtures<T>> = {
[key in TOptionalPropertyOf<S>]?: S[key]; [key in TOptionalPropertyOf<S>]?: S[key];
} & { createdAt?: BaseSchema['createdAt'] }; } & { createdAt?: BaseSchema['createdAt'] };
export type TOptionalPropertyFrom<O extends object, O1 extends object> = Pick<
O1,
Exclude<keyof O1, keyof O>
> &
Pick<O, Exclude<keyof O, keyof O1>>;
export type OptionalProperties<T, K extends keyof T> = Omit<
T,
K | keyof BaseSchema
> &
Partial<Pick<T, K>>;
export type FixturesTypeBuilder<
S extends object,
D extends object,
DO = TFixturesDefaultValues<D>,
U = Partial<TFixtures<TOptionalPropertyFrom<D, S>>>,
> = {
defaultValues: DO & U;
values: OptionalProperties<S, keyof S & keyof (DO & U)>;
};