# tssr

> Project created to facilitate the execution of business rules.

Latest version **1.1.1** (published 2022-04-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install tssr
pnpm add tssr
yarn add tssr
bun add tssr
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2022-04-04 |
| First published | 2021-07-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Adenilson Prado Raul |
| Maintainers | adenilsonpr |

## Links

- npm: https://www.npmjs.com/package/tssr
- Repository: https://github.com/AdenilsonPR/tssr
- npm.io page: https://npm.io/package/tssr

## Recent versions

- 1.1.1 (latest) — 2022-04-04
- 1.1.0 — 2021-12-15
- 1.0.4 — 2021-12-15
- 1.0.3 — 2021-12-12
- 1.0.2 — 2021-12-12
- 1.0.1 — 2021-07-10

## README

# Typescript strategy runner

Project created to facilitate the execution of business rules.

## Getting start

yarn add tssr
or
npm install tssr

## Creating domain classes

```typescript
import { IEntity } from "tssr";

class Person implements IEntity {
  private name!: string;
  private birthday!: Date;

  public getName() {
    return this.name;
  }

  public setName(name: string) {
    this.name = name;
  }

  public getBirthday() {
    return this.birthday;
  }

  public setBirthday(birthday: Date) {
    this.birthday = birthday;
  }
}
```

## Creating classes that contain a business rule

```typescript
import { IStrategy } from "tssr";

class ValidatesName implements IStrategy<Person> {
  execute(entity: Person, context: Context): void {
    const name = entity.getName();

    if (!name) {
      context.addErrorMessages("The name field is required!");
    }
  }
}
```

```typescript
import { IStrategy } from "tssr";

class ValidatesBirthday implements IStrategy<Person> {
  execute(entity: Person, context: Context): void {
    const age = entity.getBirthday().getFullYear();

    if (age <= 1900) {
      context.addErrorMessages("Invalid birthday date!");
    }
  }
}
```

## Creating the execution of business rules

```typescript
import { Context } from "tssr";

let person = new Person();
person.setName("Test name");
person.setBirthday(new Date("01/01/2000"));

const context = new BuilderRunner()
  .add(new ValidatesName())
  .add(new ValidatesBirthday())
  .execute(person);
```

Result:

```
Result {
  entity: Person { name: 'Test name', birthday: 2000-01-01T02:00:00.000Z },
  messages: [],
  error: false
}
```

If name is empty:

```typescript
person.setName("");
```

Result:

```
Result {
  entity: Person { name: '', birthday: 2000-01-01T02:00:00.000Z },
  messages: [ 'The name field is required!' ],
  error: true
}
```

If the name is empty and the year of the anniversary date is less than or equal to 1900:

```typescript
person.setName("");
person.setBirthday(new Date("01/01/1900"));
```

Result:

```
Result {
  entity: Person { name: '', birthday: 1900-01-01T03:06:28.000Z },
  messages: [ 'The name field is required!', 'Invalid birthday date!' ],
  error: true
}
```

If you want to stop propagation after a specific business rule, use: "context.setStopPropagation(true)"

```typescript
class ValidatesName implements IStrategy<Person> {
  execute(entity: Person, context: Context): void {
    const name = entity.getName();

    if (!name) {
      context.addErrorMessages("The name field is required!");
      context.setStopPropagation(true);
    }
  }
}
```

```typescript
person.setName("");
person.setBirthday(new Date("01/01/1900"));
```

Result:

```
Result {
  entity: Person { name: '', birthday: 1900-01-01T03:06:28.000Z },
  messages: [ 'The name field is required!' ],
  error: true
}
```

---
_Source: https://npm.io/package/tssr · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
