@thisisagile/easy
Version:
Straightforward library for building domain-driven microservice architectures
39 lines (34 loc) • 1.07 kB
text/typescript
import { Scope } from './Scope';
import { App } from './App';
import { Enum } from '../types/Enum';
import { text } from '../types/Template';
import { kebab, Text } from '../types/Text';
import { List, toList } from '../types/List';
import { IdName } from '../types/Identity';
import { isIn, isString } from '../types/Is';
export class UseCase extends Enum {
constructor(
readonly app: App,
name: string,
id: Text = text(name).kebab,
readonly scopes: List<Scope> = toList<Scope>()
) {
super(name, id.toString());
}
with(...s: Scope[]): this {
this.scopes.add(
...toList(s)
.flatMap(s => s.expand())
.distinct()
);
return this;
}
for(item: string | IdName): UseCase {
return new UseCase(this.app, `${this.name} ${isString(item) ? item : item?.name}`, kebab(`${this.id} ${isString(item) ? item : item.id}`)).with(
...this.scopes.map(s => s?.for(item))
);
}
static byScopes<U extends UseCase>(...s: Scope[]): List<U> {
return this.filter(u => u.scopes.some(us => isIn(us, s)));
}
}