Skip to content

Commit

Permalink
refactor: pathway rules
Browse files Browse the repository at this point in the history
  • Loading branch information
collettemathieu committed Oct 14, 2024
1 parent a94f94a commit c12202b
Show file tree
Hide file tree
Showing 23 changed files with 225 additions and 16 deletions.
22 changes: 22 additions & 0 deletions libs/pathway-design/common/business/pathway/rules/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"jsc": {
"target": "es2022",
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"transform": {
"decoratorMetadata": true,
"legacyDecorator": true
},
"keepClassNames": true,
"externalHelpers": true,
"loose": true
},
"module": {
"type": "es6"
},
"sourceMaps": true,
"exclude": [".*\\.spec.tsx?$", ".*\\.step.ts$", ".*\\.test.tsx?$", ".*.js$"]
}
17 changes: 17 additions & 0 deletions libs/pathway-design/common/business/pathway/rules/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "../../../../../../node_modules/@biomejs/biome/configuration_schema.json",
"extends": ["../../../../../../biome.json"],
"linter": {
"rules": {
"nursery": {
"noRestrictedImports": {
"options": {
"paths": {}
},
"level": "error"
}
}
}
},
"overrides": [{}]
}
11 changes: 11 additions & 0 deletions libs/pathway-design/common/business/pathway/rules/cucumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
default: {
paths: ['libs/pathway-design/common/business/pathway/rules/src/lib/**/*.feature'],
requireModule: ['ts-node/register', 'tsconfig-paths/register'],
require: ['libs/pathway-design/common/business/pathway/rules/src/lib/**/*.step.ts'],
format: [
'json:dist/reports/libs/pathway-design/common/business/pathway/rules/test-feature/index.json',
'html:dist/reports/libs/pathway-design/common/business/pathway/rules/test-feature/index.html',
],
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@bewoak/pathway-design-common-business-pathway-rules",
"version": "0.0.1",
"dependencies": {
"@swc/helpers": "~0.5.11"
},
"main": "./src/index.js",
"typings": "./src/index.d.ts"
}
50 changes: 50 additions & 0 deletions libs/pathway-design/common/business/pathway/rules/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "pathway-design-common-business-pathway-rules",
"$schema": "../../../../../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/pathway-design/common-business/pathway/rules/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:swc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/pathway-design/common-business/pathway/rules",
"main": "libs/pathway-design/common-business/pathway/rules/src/index.ts",
"tsConfig": "libs/pathway-design/common-business/pathway/rules/tsconfig.lib.json",
"assets": []
}
},
"nx-release-publish": {
"options": {
"packageRoot": "dist/{projectRoot}"
}
},
"lint": {
"executor": "nx:run-commands",
"options": {
"command": "bun run biome check --write {projectRoot} --config-path={projectRoot}/biome.json"
}
},
"test": {
"executor": "nx:run-commands",
"options": {
"command": "bun test --coverage --coverage-dir=dist/reports/{projectRoot}/coverage --coverage-reporter=lcov {projectRoot}"
}
},
"test-feature": {
"executor": "nx:run-commands",
"options": {
"command": "TS_NODE_PROJECT='{projectRoot}/tsconfig.spec.json' bun run cucumber-js --config={projectRoot}/cucumber.js"
}
}
},
"tags": ["type:pathway-design:common:business:pathway:rules"],
"release": {
"version": {
"generatorOptions": {
"packageRoot": "dist/{projectRoot}",
"currentVersionResolver": "git-tag"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PDCBPR_titleRules } from './lib/title/title.rules';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Rules } from '../types';

export const PDCBPR_titleRules: Rules = {
textError: function () {
return `Renseigner un nom entre 1 et ${this.maxLength} caractères.`;
},
isValid: function (value: string | null) {
return value !== null && value.trim().length !== 0 && value.trim().length <= this.maxLength;
},
isRequired: true,
maxLength: 100,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, test } from 'bun:test';
import { PDCBPR_titleRules } from './title.rules';

describe('PDCBPR_titleRules', () => {
test('should return true for a valid title', () => {
const validTitle = 'A valid title';
expect(PDCBPR_titleRules.isValid(validTitle)).toBe(true);
});

test('should return false for a null value', () => {
expect(PDCBPR_titleRules.isValid(null)).toBe(false);
});

test('should return false for an empty title', () => {
const emptyTitle = '';
expect(PDCBPR_titleRules.isValid(emptyTitle)).toBe(false);
});

test('should return false for a title longer than the maxLength', () => {
const longTitle = 'A'.repeat(PDCBPR_titleRules.maxLength + 1);
expect(PDCBPR_titleRules.isValid(longTitle)).toBe(false);
});

test('should return true for a title exactly at maxLength', () => {
const maxLengthTitle = 'A'.repeat(PDCBPR_titleRules.maxLength);
expect(PDCBPR_titleRules.isValid(maxLengthTitle)).toBe(true);
});

test('should return false for a title with only spaces', () => {
const spacesTitle = ' ';
expect(PDCBPR_titleRules.isValid(spacesTitle)).toBe(false);
});

test('should return correct error message textError', () => {
const expectedErrorMessage = `Renseigner un nom entre 1 et ${PDCBPR_titleRules.maxLength} caractères.`;
expect(PDCBPR_titleRules.textError()).toBe(expectedErrorMessage);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Rules {
textError: () => string;
isValid: (value: string | null) => boolean;
readonly isRequired: boolean;
readonly maxLength: number;
}
22 changes: 22 additions & 0 deletions libs/pathway-design/common/business/pathway/rules/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../../../../../tsconfig.base.json",
"compilerOptions": {
"module": "es2022",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../../../../dist/out-tsc",
"types": ["node"],
"composite": true,
"declaration": true
},
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.spec.ts", "src/**/*.test.ts", "src/**/*.step.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"esModuleInterop": true,
"composite": true,
"declaration": true
},
"include": ["src/**/*.ts"]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { PDSPAInitializePathwayCommand } from './lib/initialize/command/initialize-pathway.command';
export { PDSPAInitializePathwayCommandHandler } from './lib/initialize/command/initialize-pathway.command-handler';
export { PDSPA_InitializePathwayService } from './lib/initialize/service/initialize-pathway.service';
export { PDSPAInitializePathwayService } from './lib/initialize/service/initialize-pathway.service';
export { PDSPAIUInitializePathwayUsecase } from './lib/initialize/usecase/initialize-pathway.usecase';

export { PDSPAChangeTitlePathwayCommand } from './lib/change-title/command/change-title-pathway.command';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CommandBus } from '@nestjs/cqrs';
import type { PDSPAInitializePathwayCommand } from '../command/initialize-pathway.command';

@Injectable()
export class PDSPA_InitializePathwayService {
export class PDSPAInitializePathwayService {
constructor(private readonly commandBus: CommandBus) {}

init(pDSPAInitializePathwayCommand: PDSPAInitializePathwayCommand) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CTSEBadRequestException } from '@bewoak/common-tools-server-http-exceptions';

export class DescriptionValueObject {
constructor(private description: string) {
constructor(private readonly description: string) {
if (this.isEmpty(description)) {
throw new CTSEBadRequestException('Description is required');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CTSEBadRequestException } from '@bewoak/common-tools-server-http-exceptions';

export class PathwayIdValueObject {
constructor(private id: string) {
constructor(private readonly id: string) {
if (!this.isUuid(id)) {
throw new CTSEBadRequestException('Pathway id must be a valid uuid');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CTSEBadRequestException } from '@bewoak/common-tools-server-http-exceptions';

export class ResearchFieldValueObjects {
constructor(private researchField: string) {
constructor(private readonly researchField: string) {
if (this.isEmpty(researchField)) {
throw new CTSEBadRequestException('Research field is required');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CTSEBadRequestException } from '@bewoak/common-tools-server-http-exceptions';
import { PDCBPR_titleRules } from '@bewoak/pathway-design-common-business-pathway-rules';

export class PDSPBVOTitleValueObjects {
constructor(private title: string) {
if (this.isEmpty(title)) {
constructor(private readonly title: string) {
if (PDCBPR_titleRules.isValid(title) === false) {
throw new CTSEBadRequestException('Title is required');
}
}
Expand All @@ -18,8 +19,4 @@ export class PDSPBVOTitleValueObjects {
toString() {
return this.title;
}

private isEmpty(title: string | undefined) {
return title === undefined || title.length === 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// biome-ignore lint/style/useImportType: <explanation>
import { PDSPAInitializePathwayCommand, PDSPA_InitializePathwayService } from '@bewoak/pathway-design-server-pathway-application';
import { PDSPAInitializePathwayCommand, PDSPAInitializePathwayService } from '@bewoak/pathway-design-server-pathway-application';
import { Body, Controller, HttpStatus, Post, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiBadRequestResponse, ApiCreatedResponse, ApiTags } from '@nestjs/swagger';
// biome-ignore lint/style/useImportType: <explanation>
Expand All @@ -17,7 +17,7 @@ import { InitializedPathwayResponseBodyDto } from '../dtos/response/body/respons
path: 'pathway',
})
export class InitializePathwayController {
constructor(private readonly pDSPAInitializePathwayService: PDSPA_InitializePathwayService) {}
constructor(private readonly pDSPAInitializePathwayService: PDSPAInitializePathwayService) {}

@Post('init')
@ApiCreatedResponse({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
PDSPAIUInitializePathwayUsecase,
PDSPAInitializePathwayCommandHandler,
PDSPA_InitializePathwayService,
PDSPAInitializePathwayService,
} from '@bewoak/pathway-design-server-pathway-application';
import { type DynamicModule, Module, type Type } from '@nestjs/common';
import { InitializePathwayController } from './controller/initialize-pathway.controller';

@Module({
controllers: [InitializePathwayController],
providers: [PDSPAInitializePathwayCommandHandler, PDSPA_InitializePathwayService, PDSPAIUInitializePathwayUsecase],
exports: [PDSPA_InitializePathwayService],
providers: [PDSPAInitializePathwayCommandHandler, PDSPAInitializePathwayService, PDSPAIUInitializePathwayUsecase],
exports: [PDSPAInitializePathwayService],
})
// biome-ignore lint/complexity/noStaticOnlyClass: not pertinent here because this is a module
export class PDSPIAInitializePathwayInterfaceAdaptersModule {
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"@bewoak/common-configs-server-swagger": ["libs/common/configs/server/swagger/src/index.ts"],
"@bewoak/common-events": ["libs/common/events/src/index.ts"],
"@bewoak/common-tools-server-http-exceptions": ["libs/common/tools/server/http-exceptions/src/index.ts"],
"@bewoak/pathway-design-common-business-pathway-rules": [
"libs/pathway-design/common/business/pathway/rules/src/index.ts"
],
"@bewoak/pathway-design-server-pathway-application": ["libs/pathway-design/server/pathway/application/src/index.ts"],
"@bewoak/pathway-design-server-pathway-business": ["libs/pathway-design/server/pathway/business/src/index.ts"],
"@bewoak/pathway-design-server-pathway-infrastructure": [
Expand Down

0 comments on commit c12202b

Please sign in to comment.