Skip to content

Commit

Permalink
chore: Field Decorator Refactor (#302)
Browse files Browse the repository at this point in the history
* refactor: impove field decorators

* chore: update scripts

* refactor: improve lang

---------

Co-authored-by: leaftail1880 <110915645+leaftaul1880@users.noreply.github.com>
  • Loading branch information
leaftail1880 and leaftail1880 authored Nov 12, 2024
1 parent 3238a62 commit 61fe220
Show file tree
Hide file tree
Showing 55 changed files with 803 additions and 589 deletions.
8 changes: 7 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ module.exports = {
'error',
{
enforceBuildableLibDependency: true,
allow: [],

// We allow it because we need to use import from
// postybirb-ui to make jump-to definition in the
// FieldType.label
allow: ['@postybirb/form-builder'],

depConstraints: [
{
sourceTag: '*',
Expand Down Expand Up @@ -73,6 +78,7 @@ module.exports = {
'import/prefer-default-export': 'off',
'react/jsx-props-no-spreading': 'off',
'react/react-in-jsx-scope': 'off',
'no-restricted-syntax': 'off',

'@typescript-eslint/ban-types': 'warn',
'import/no-extraneous-dependencies': [
Expand Down
2 changes: 2 additions & 0 deletions apps/client-server/src/app/file/file.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { MessageSubmissionService } from '../submission/services/message-submiss
import { SubmissionService } from '../submission/services/submission.service';
import { TagConvertersService } from '../tag-converters/tag-converters.service';
import { UserSpecifiedWebsiteOptionsService } from '../user-specified-website-options/user-specified-website-options.service';
import { ValidationService } from '../validation/validation.service';
import { WebsiteOptionsService } from '../website-options/website-options.service';
import { WebsiteImplProvider } from '../websites/implementations';
import { WebsiteRegistryService } from '../websites/website-registry.service';
Expand Down Expand Up @@ -76,6 +77,7 @@ describe('FileService', () => {
CreateFileService,
UpdateFileService,
FileService,
ValidationService,
SubmissionService,
FileSubmissionService,
MessageSubmissionService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('FormGeneratorService', () => {
col: 1,
defaultValue: '',
formField: 'input',
label: 'Content Warning / Spoilers',
label: 'contentWarning',
row: 2,
type: 'text',
},
Expand All @@ -87,16 +87,15 @@ describe('FormGeneratorService', () => {
overrideDefault: false,
},
formField: 'description',
i18nLabel: 'form.descriptions',
label: 'Description',
label: 'description',
row: 3,
type: 'description',
},
rating: {
col: 0,
defaultValue: SubmissionRating.ADULT,
defaultValue: 'ADULT',
formField: 'rating',
label: 'Rating',
label: 'rating',
layout: 'vertical',
options: [
{
Expand Down Expand Up @@ -127,16 +126,15 @@ describe('FormGeneratorService', () => {
tags: [],
},
formField: 'tag',
i18nLabel: 'form.tags',
label: 'Tags',
label: 'tags',
row: 1,
type: 'tag',
},
title: {
col: 1,
defaultValue: '',
formField: 'input',
label: 'Title',
label: 'title',
required: true,
row: 0,
type: 'text',
Expand All @@ -151,7 +149,7 @@ describe('FormGeneratorService', () => {
col: 1,
defaultValue: '',
formField: 'input',
label: 'Content Warning / Spoilers',
label: 'contentWarning',
row: 2,
type: 'text',
},
Expand All @@ -162,16 +160,14 @@ describe('FormGeneratorService', () => {
overrideDefault: false,
},
formField: 'description',
i18nLabel: 'form.descriptions',
label: 'Description',
label: 'description',
row: 3,
type: 'description',
},
rating: {
col: 0,
defaultValue: 'GENERAL',
formField: 'rating',
label: 'Rating',
label: 'rating',
layout: 'vertical',
options: [
{
Expand Down Expand Up @@ -202,16 +198,15 @@ describe('FormGeneratorService', () => {
tags: [],
},
formField: 'tag',
i18nLabel: 'form.tags',
label: 'Tags',
label: 'tags',
row: 1,
type: 'tag',
},
title: {
col: 1,
defaultValue: '',
formField: 'input',
label: 'Title',
label: 'title',
required: true,
row: 0,
type: 'text',
Expand All @@ -224,7 +219,7 @@ describe('FormGeneratorService', () => {
col: 1,
defaultValue: '',
formField: 'input',
label: 'Content Warning / Spoilers',
label: 'contentWarning',
row: 2,
type: 'text',
},
Expand All @@ -235,16 +230,14 @@ describe('FormGeneratorService', () => {
overrideDefault: false,
},
formField: 'description',
i18nLabel: 'form.descriptions',
label: 'Description',
label: 'description',
row: 3,
type: 'description',
},
rating: {
col: 0,
defaultValue: 'GENERAL',
formField: 'rating',
label: 'Rating',
label: 'rating',
layout: 'vertical',
options: [
{
Expand Down Expand Up @@ -275,16 +268,15 @@ describe('FormGeneratorService', () => {
tags: [],
},
formField: 'tag',
i18nLabel: 'form.tags',
label: 'Tags',
label: 'tags',
row: 1,
type: 'tag',
},
title: {
col: 1,
defaultValue: '',
formField: 'input',
label: 'Title',
label: 'title',
required: true,
row: 0,
type: 'text',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class FormGeneratorService {
*/
async generateForm(
request: FormGenerationRequestDto,
): Promise<FormBuilderMetadata<never>> {
): Promise<FormBuilderMetadata> {
const account = await this.accountService.findById(request.accountId, {
failOnMissing: true,
});
Expand Down Expand Up @@ -96,10 +96,10 @@ export class FormGeneratorService {
}

private async populateUserDefaults(
form: FormBuilderMetadata<never>,
form: FormBuilderMetadata,
accountId: AccountId,
type: SubmissionType,
): Promise<FormBuilderMetadata<never>> {
): Promise<FormBuilderMetadata> {
const userSpecifiedDefaults =
await this.userSpecifiedWebsiteOptionsService.findByAccountAndSubmissionType(
accountId,
Expand All @@ -110,7 +110,6 @@ export class FormGeneratorService {
Object.entries(userSpecifiedDefaults.options).forEach(([key, value]) => {
const field = form[key];
if (field) {
// eslint-disable-next-line no-param-reassign
field.defaultValue = value ?? field.defaultValue;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ describe('TitleParserService', () => {
it('should parse title', async () => {
const submission = new Submission({});
const defaultOptions: IWebsiteOptions = {
id: 'default',
data: {
title: 'default',
},
} as IWebsiteOptions;
const websiteOptions: IWebsiteOptions = {
id: 'website',
data: {
title: 'website',
},
Expand Down Expand Up @@ -71,15 +73,17 @@ describe('TitleParserService', () => {
const submission = new Submission({});
const instance = new WebsiteInstanceMock();
const defaultOptions: IWebsiteOptions = {
id: 'default',
data: {
title: 'default',
},
} as IWebsiteOptions;
const websiteOptions: IWebsiteOptions = {
id: 'website',
data: {},
} as IWebsiteOptions;
const defaultForm = { title: [{ maxLength: 10 }] };
const websiteForm = { title: [{ maxLength: 5 }] };
const defaultForm = { title: { maxLength: 10 } };
const websiteForm = { title: { maxLength: 5 } };
(formGeneratorService.getDefaultForm as jest.Mock).mockResolvedValue(
defaultForm,
);
Expand All @@ -94,18 +98,21 @@ describe('TitleParserService', () => {
websiteOptions,
);

// Title should be truncated
expect(title).toBe('defau');
});

it('should parse title and use default form if website form is not available', async () => {
const submission = new Submission({});
const instance = new WebsiteInstanceMock();
const defaultOptions: IWebsiteOptions = {
id: 'default',
data: {
title: 'default',
},
} as IWebsiteOptions;
const websiteOptions: IWebsiteOptions = {
id: 'website',
data: {
title: 'website',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ISubmission, IWebsiteOptions } from '@postybirb/types';
import { FormGeneratorService } from '../../form-generator/form-generator.service';
import { UnknownWebsite } from '../../websites/website';

type TitleType = { title: TextFieldType[] };
type TitleType = { title: TextFieldType };

@Injectable()
export class TitleParserService {
Expand All @@ -29,10 +29,7 @@ export class TitleParserService {
})) as unknown as TitleType);

const title = websiteOptions.data.title ?? defaultOptions.data.title ?? '';
const field: TextFieldType =
websiteForm?.title[0] ??
defaultForm?.title[0] ??
({ maxLength: Infinity } as TextFieldType);
const field = websiteForm?.title ?? defaultForm?.title;
const maxLength = field?.maxLength ?? Infinity;

return title.trim().slice(0, maxLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import { PostParsersService } from './post-parsers.service';
WebsiteImplProvider,
DescriptionParserService,
],
exports: [PostParsersService],
exports: [
PostParsersService,
TagParserService,
TitleParserService,
DescriptionParserService,
],
})
export class PostParsersModule {}
8 changes: 7 additions & 1 deletion apps/client-server/src/app/post/post-manager.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CreateSubmissionDto } from '../submission/dtos/create-submission.dto';
import { SubmissionService } from '../submission/services/submission.service';
import { SubmissionModule } from '../submission/submission.module';
import { UserSpecifiedWebsiteOptionsModule } from '../user-specified-website-options/user-specified-website-options.module';
import { ValidationService } from '../validation/validation.service';
import { CreateWebsiteOptionsDto } from '../website-options/dtos/create-website-options.dto';
import { WebsiteOptionsModule } from '../website-options/website-options.module';
import { WebsiteOptionsService } from '../website-options/website-options.service';
Expand Down Expand Up @@ -47,7 +48,12 @@ describe('PostManagerService', () => {
PostParsersModule,
PostModule,
],
providers: [PostManagerService, PostService, PostFileResizerService],
providers: [
PostManagerService,
PostService,
PostFileResizerService,
ValidationService,
],
}).compile();

service = module.get<PostManagerService>(PostManagerService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { FormGeneratorModule } from '../../form-generator/form-generator.module'
import { PostParsersModule } from '../../post-parsers/post-parsers.module';
import { UserSpecifiedWebsiteOptionsModule } from '../../user-specified-website-options/user-specified-website-options.module';
import { UserSpecifiedWebsiteOptionsService } from '../../user-specified-website-options/user-specified-website-options.service';
import { ValidationService } from '../../validation/validation.service';
import { WebsiteOptionsService } from '../../website-options/website-options.service';
import { WebsiteImplProvider } from '../../websites/implementations';
import { WebsiteRegistryService } from '../../websites/website-registry.service';
Expand Down Expand Up @@ -68,6 +69,7 @@ describe('SubmissionService', () => {
AccountService,
WebsiteRegistryService,
UserSpecifiedWebsiteOptionsService,
ValidationService,
WebsiteOptionsService,
WebsiteImplProvider,
],
Expand Down
16 changes: 15 additions & 1 deletion apps/client-server/src/app/validation/validation.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { Test, TestingModule } from '@nestjs/testing';
import { DatabaseModule } from '../database/database.module';
import { DatabaseUpdateSubscriber } from '../database/subscribers/database.subscriber';
import { PostParsersModule } from '../post-parsers/post-parsers.module';
import { PostParsersService } from '../post-parsers/post-parsers.service';
import { WebsiteImplProvider } from '../websites/implementations';
import { WebsiteRegistryService } from '../websites/website-registry.service';
import { WebsitesModule } from '../websites/websites.module';
import { ValidationService } from './validation.service';

describe('ValidationService', () => {
let service: ValidationService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ValidationService],
imports: [WebsitesModule, PostParsersModule, DatabaseModule],
providers: [
WebsiteImplProvider,
ValidationService,
DatabaseUpdateSubscriber,
WebsiteRegistryService,
PostParsersService,
],
}).compile();

service = module.get<ValidationService>(ValidationService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { MessageSubmissionService } from '../submission/services/message-submiss
import { SubmissionService } from '../submission/services/submission.service';
import { UserSpecifiedWebsiteOptionsModule } from '../user-specified-website-options/user-specified-website-options.module';
import { UserSpecifiedWebsiteOptionsService } from '../user-specified-website-options/user-specified-website-options.service';
import { ValidationService } from '../validation/validation.service';
import { WebsiteImplProvider } from '../websites/implementations';
import { WebsiteRegistryService } from '../websites/website-registry.service';
import { WebsitesModule } from '../websites/websites.module';
Expand Down Expand Up @@ -76,6 +77,7 @@ describe('WebsiteOptionsService', () => {
MessageSubmissionService,
AccountService,
WebsiteRegistryService,
ValidationService,
WebsiteOptionsService,
WebsiteImplProvider,
UserSpecifiedWebsiteOptionsService,
Expand Down
Loading

0 comments on commit 61fe220

Please sign in to comment.