Finish adding config endpoints

This commit is contained in:
Dominic Ferrando
2026-07-04 02:32:59 -04:00
parent f85225e08f
commit c1d3bed404
4 changed files with 131 additions and 7 deletions
+29 -1
View File
@@ -21,13 +21,41 @@ export class CalculatorService {
return this.levelCalculator.calculate(player, activityPerformances);
}
async getConfig(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
async getSelectedConfig(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
if (!opt.skipCache) return this.levelCalculator.standards.cfg;
await this.refreshConfig();
return this.levelCalculator.standards.cfg;
}
async selectConfig(id: string) {
await db.transaction().execute(async (trx) => {
await trx.updateTable("calculator_configs")
.set({ is_selected: false })
.where("is_selected", "=", true)
.execute();
const result = await trx.updateTable("calculator_configs")
.set({ is_selected: true })
.where("id", "=", id)
.execute();
if (result[0]?.numUpdatedRows === 0n) throw new Error(`No config with id "${id}"`);
});
}
async setConfig(id: string, standardsConfig: StandardsConfig) {
const result = await db.updateTable("calculator_configs")
.set({ standards_config: standardsConfig })
.where("id", "=", id)
.execute();
if (result[0]?.numUpdatedRows === 0n) throw new Error(`No config with id "${id}"`);
}
async createConfig(standardsConfig: StandardsConfig) {
await db.insertInto("calculator_configs")
.values({ standards_config: standardsConfig })
.execute();
}
async refreshConfig(opt: { onlyIfStale?: boolean } = {}) {
if (opt.onlyIfStale)
if (Date.now() - this.lastRefreshedAt < CalculatorService.CONFIG_REFRESH_INTERVAL_MS) return;