starry / backend /omr-service /src /services /issueMeasure.service.ts
k-l-lambda's picture
Claude Sonnet 4.6
update: sync from starry 2026-04-23
6367981
Raw History Blame Contribute Delete
4.5 kB
import { query } from '../db/client.js';
interface IssueMeasureRow {
id: string;
score_id: string;
measure_index: number;
hash: string | null;
measure: any;
status: number;
by_user: boolean;
annotator: string | null;
created_at: Date;
updated_at: Date;
}
interface IssueMeasureResult {
id: string;
scoreId: string;
measureIndex: number;
hash: string | null;
measure: any;
status: number;
byUser: boolean;
annotator: string | null;
lastUpdate: Date;
}
function rowToResult(row: IssueMeasureRow): IssueMeasureResult {
return {
id: row.id,
scoreId: row.score_id,
measureIndex: row.measure_index,
hash: row.hash,
measure: row.measure,
status: row.status,
byUser: row.by_user,
annotator: row.annotator,
lastUpdate: row.updated_at,
};
}
export async function list(
scoreId: string,
{ offset = 0, limit = 200, status }: { offset?: number; limit?: number; status?: number[] } = {}
): Promise<{ count: number; rows: IssueMeasureResult[] }> {
const params: any[] = [scoreId];
let whereClause = 'WHERE score_id = $1';
if (status && status.length > 0) {
const placeholders = status.map((_, i) => `$${i + 2}`).join(', ');
whereClause += ` AND status IN (${placeholders})`;
params.push(...status);
}
const countResult = await query(`SELECT COUNT(*)::int as count FROM issue_measures ${whereClause}`, params);
const paramOffset = params.length;
const { rows } = await query<IssueMeasureRow>(
`SELECT * FROM issue_measures ${whereClause}
ORDER BY measure_index ASC
LIMIT $${paramOffset + 1} OFFSET $${paramOffset + 2}`,
[...params, limit, offset]
);
return {
count: countResult.rows[0].count,
rows: rows.map(rowToResult),
};
}
export async function upsert(
scoreId: string,
hash: string,
measure: any,
status: number,
annotator?: string | null,
measureIndex?: number
): Promise<IssueMeasureResult> {
// Try to find existing record for this score+hash (any status)
const { rows: existing } = await query<IssueMeasureRow>('SELECT * FROM issue_measures WHERE score_id = $1 AND hash = $2 ORDER BY updated_at DESC LIMIT 1', [
scoreId,
hash,
]);
let row: IssueMeasureRow;
// undefined = not provided (preserve existing), null = explicitly cleared
const annotatorProvided = annotator !== undefined;
if (existing.length > 0) {
const params: any[] = [JSON.stringify(measure), status, annotator ?? null];
const setClauses = ['measure = $1', 'status = $2', `annotator = ${annotatorProvided ? '$3' : 'COALESCE($3, annotator)'}`, 'updated_at = NOW()'];
// Update measure_index if provided (parameterized to prevent injection)
if (measureIndex !== undefined) {
params.push(measureIndex);
setClauses.push(`measure_index = $${params.length}`);
}
params.push(existing[0].id);
const { rows } = await query<IssueMeasureRow>(
`UPDATE issue_measures
SET ${setClauses.join(', ')}
WHERE id = $${params.length}
RETURNING *`,
params
);
row = rows[0];
} else {
const { rows } = await query<IssueMeasureRow>(
`INSERT INTO issue_measures (score_id, measure_index, hash, measure, status, by_user, annotator)
VALUES ($1, $2, $3, $4, $5, false, $6)
ON CONFLICT (score_id, hash) WHERE status > 0 AND hash IS NOT NULL
DO UPDATE SET measure = EXCLUDED.measure, status = EXCLUDED.status,
annotator = ${annotatorProvided ? 'EXCLUDED.annotator' : 'COALESCE(EXCLUDED.annotator, issue_measures.annotator)'},
measure_index = COALESCE(EXCLUDED.measure_index, issue_measures.measure_index),
updated_at = NOW()
RETURNING *`,
[scoreId, measureIndex ?? -1, hash, JSON.stringify(measure), status, annotator ?? null]
);
row = rows[0];
}
return rowToResult(row);
}
export async function batchGetByHash(hashes: string[]): Promise<IssueMeasureResult[]> {
if (hashes.length === 0) return [];
// Dedupe and cap batch size
const uniqueHashes = [...new Set(hashes)].slice(0, 1000);
const { rows } = await query<IssueMeasureRow>('SELECT * FROM issue_measures WHERE hash = ANY($1)', [uniqueHashes]);
return rows.map(rowToResult);
}
export async function getByScoreId(scoreId: string): Promise<IssueMeasureResult[]> {
const { rows } = await query<IssueMeasureRow>('SELECT * FROM issue_measures WHERE score_id = $1 ORDER BY measure_index ASC', [scoreId]);
return rows.map(rowToResult);
}
export async function deleteByScore(scoreId: string): Promise<number> {
const result = await query('DELETE FROM issue_measures WHERE score_id = $1', [scoreId]);
return result.rowCount ?? 0;
}