Files
privacy.sexy/src/domain/Application.ts
undergroundwires ded55a66d6 Refactor executable IDs to use strings #262
This commit unifies executable ID structure across categories and
scripts, paving the way for more complex ID solutions for #262.
It also refactors related code to adapt to the changes.

Key changes:

- Change numeric IDs to string IDs for categories
- Use named types for string IDs to improve code clarity
- Add unit tests to verify ID uniqueness

Other supporting changes:

- Separate concerns in entities for data access and executables by using
  separate abstractions (`Identifiable` and `RepositoryEntity`)
- Simplify usage and construction of entities.
- Remove `BaseEntity` for simplicity.
- Move creation of categories/scripts to domain layer
- Refactor CategoryCollection for better validation logic isolation
- Rename some categories to keep the names (used as pseudo-IDs) unique
  on Windows.
2024-08-03 16:54:14 +02:00

45 lines
1.5 KiB
TypeScript

import { OperatingSystem } from './OperatingSystem';
import type { IApplication } from './IApplication';
import type { ICategoryCollection } from './Collection/ICategoryCollection';
import type { ProjectDetails } from './Project/ProjectDetails';
export class Application implements IApplication {
constructor(
public projectDetails: ProjectDetails,
public collections: readonly ICategoryCollection[],
) {
validateCollections(collections);
}
public getSupportedOsList(): OperatingSystem[] {
return this.collections.map((collection) => collection.os);
}
public getCollection(operatingSystem: OperatingSystem): ICategoryCollection {
const collection = this.collections.find((c) => c.os === operatingSystem);
if (!collection) {
throw new Error(`Operating system "${OperatingSystem[operatingSystem]}" is not defined in application`);
}
return collection;
}
}
function validateCollections(collections: readonly ICategoryCollection[]) {
if (!collections.length) {
throw new Error('missing collections');
}
if (collections.filter((c) => !c).length > 0) {
throw new Error('missing collection in the list');
}
const osList = collections.map((c) => c.os);
const duplicates = getDuplicates(osList);
if (duplicates.length > 0) {
throw new Error(`multiple collections with same os: ${
duplicates.map((os) => OperatingSystem[os].toLowerCase()).join('", "')}`);
}
}
function getDuplicates(list: readonly OperatingSystem[]): OperatingSystem[] {
return list.filter((os, index) => list.indexOf(os) !== index);
}