starry / backend /omr-service /src /routes /regulation.ts
k-l-lambda's picture
Initial deployment: frontend + omr-service + cluster-server + nginx proxy
6f1c297
import { FastifyInstance } from 'fastify';
import { regulateWithBeadSolver, starry } from 'starry-omr';
import { DbSolutionStore } from '../lib/dbSolutionStore.js';
import { getPickers, isRegulationReady, regulateScore } from '../lib/regulation.js';
import * as issueMeasureService from '../services/issueMeasure.service.js';
import * as scoreService from '../services/score.service.js';
export default async function regulationRoutes(fastify: FastifyInstance) {
fastify.post<{ Body: { score: any } }>('/regulate-score', async (request, reply) => {
if (!isRegulationReady()) {
reply.code(503);
return { code: 503, message: 'Regulation service not ready' };
}
const { score } = request.body;
if (!score) {
reply.code(400);
return { code: 400, message: 'Missing score in request body' };
}
try {
const result = await regulateScore(score);
return { code: 0, data: result };
} catch (err) {
fastify.log.error(err, '[regulate-score] regulation failed');
reply.code(500);
return { code: 500, message: (err as Error).message };
}
});
// Regulate by scoreId: loads score from DB, runs regulation to populate solution cache, does NOT save score back.
fastify.post<{ Params: { id: string } }>('/scores/:id/regulate', async (request, reply) => {
if (!isRegulationReady()) {
reply.code(503);
return { code: 503, message: 'Regulation service not ready' };
}
const { id } = request.params;
const scoreRow = await scoreService.getScore(id);
if (!scoreRow?.data) {
reply.code(404);
return { code: 404, message: 'Score not found' };
}
try {
const score = starry.recoverJSON(scoreRow.data, starry) as any;
score.assemble();
const stat = await regulateWithBeadSolver(score, {
pickers: getPickers(),
solutionStore: DbSolutionStore,
onSaveIssueMeasure: (data: any) => {
issueMeasureService
.upsert(id, data.measureIndex, new starry.EditableMeasure(data.measure), data.status)
.catch((err: any) => console.error('[regulate] failed to save issue measure:', err));
},
});
return { code: 0, data: { stat } };
} catch (err) {
fastify.log.error(err, '[regulate] regulation failed');
reply.code(500);
return { code: 500, message: (err as Error).message };
}
});
}