Setup getConfig with typebox validations
This commit is contained in:
@@ -31,7 +31,6 @@
|
||||
"kysely": "^0.29.2",
|
||||
"ml-levenberg-marquardt": "^5.0.1",
|
||||
"pg": "^8.22.0",
|
||||
"pino": "^10.3.1",
|
||||
"zipcodes-us": "^1.1.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATA } from "@blade-and-brawn/calculator";
|
||||
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATA, type StandardsConfig, StandardsConfigSchema } from "@blade-and-brawn/calculator";
|
||||
import type { ActivityPerformance, Player } from "@blade-and-brawn/domain";
|
||||
import { db } from "../database/db";
|
||||
import { Value } from "@sinclair/typebox/value";
|
||||
|
||||
export class CalculatorService {
|
||||
private readonly levelCalculator;
|
||||
@@ -13,4 +15,18 @@ export class CalculatorService {
|
||||
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
||||
return this.levelCalculator.calculate(player, activityPerformances)
|
||||
}
|
||||
|
||||
async getConfig(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
|
||||
if (!opt.skipCache) return this.levelCalculator.standards.cfg;
|
||||
|
||||
const result = await db.selectFrom("calculator_configs")
|
||||
.select(["parameters"])
|
||||
.where("is_selected", "=", true)
|
||||
.limit(1)
|
||||
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No calculator config selected") });
|
||||
|
||||
const standardsConfig = result.parameters;
|
||||
Value.Assert(StandardsConfigSchema, standardsConfig);
|
||||
return standardsConfig;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user