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

View File

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

View File

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

View File

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

View File

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

View File

@ -8,23 +8,19 @@
import mongoose from 'mongoose';
import { LabelCreateDto } from '@/chat/dto/label.dto';
import { Label, LabelModel } from '@/chat/schemas/label.schema';
import { BaseSchema } from '@/utils/generics/base-schema';
import { getFixturesWithDefaultValues } from '../defaultValues';
import { FixturesTypeBuilder } from '../types';
export const fieldsWithDefaultValues = {
type TLabelFixtures = FixturesTypeBuilder<Label, LabelCreateDto>;
export const fieldsWithDefaultValues: TLabelFixtures['defaultValues'] = {
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[] = [
export const labels: TLabelFixtures['values'][] = [
{
description: 'test description 1',
label_id: {
@ -49,7 +45,9 @@ export const labels: TLabel[] = [
},
];
export const labelFixtures = getFixturesWithDefaultValues<TLabel>({
export const labelFixtures = getFixturesWithDefaultValues<
TLabelFixtures['values']
>({
fixtures: labels,
defaultValues: fieldsWithDefaultValues,
});

View File

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

View File

@ -8,31 +8,26 @@
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';
import { FixturesTypeBuilder, TFixturesDefaultValues } from '../types';
import { installLabelFixtures } from './label';
import { installUserFixtures } from './user';
export const fieldsWithDefaultValues = {
type TSubscriberFixtures = FixturesTypeBuilder<Subscriber, SubscriberCreateDto>;
export const fieldsWithDefaultValues: TSubscriberFixtures['defaultValues'] = {
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[] = [
const subscribers: TSubscriberFixtures['values'][] = [
{
foreign_id: 'foreign-id-messenger',
first_name: 'Jhon',
@ -104,7 +99,9 @@ export const subscriberDefaultValues: TFixturesDefaultValues<Subscriber> = {
avatar: null,
};
export const subscriberFixtures = getFixturesWithDefaultValues<Subscriber>({
export const subscriberFixtures = getFixturesWithDefaultValues<
TSubscriberFixtures['values']
>({
fixtures: subscribers,
defaultValues: subscriberDefaultValues,
});

View File

@ -8,7 +8,7 @@
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;
}[keyof T],
@ -23,3 +23,25 @@ export type TFixtures<T> = Omit<T, keyof BaseSchema> & {
export type TFixturesDefaultValues<T, S = TFixtures<T>> = {
[key in TOptionalPropertyOf<S>]?: S[key];
} & { 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)>;
};