Files
profilarr/src/lib/server/pcd/queries/customFormats/testUpdate.ts
Sam Chau 5d82cc910b feat: add testing functionality for custom formats
- Implemented server-side logic for loading and managing tests in custom formats.
- Created new page for editing existing tests with form handling.
- Developed a reusable TestForm component for creating and editing test cases.
- Added functionality to create new tests with validation and error handling.
- Integrated layer permission checks for writing to base layer.
- Enhanced user experience with modals for save and delete actions.
2025-12-31 03:05:09 +10:30

68 lines
2.0 KiB
TypeScript

/**
* Update a custom format test operation
*/
import { writeOperation, type OperationLayer } from '../../writer.ts';
import type { CustomFormatTest } from './types.ts';
export interface UpdateTestInput {
title: string;
type: 'movie' | 'series';
should_match: boolean;
description: string | null;
}
export interface UpdateTestOptions {
databaseId: number;
layer: OperationLayer;
formatName: string;
/** The current test data (for value guards) */
current: CustomFormatTest;
/** The new values */
input: UpdateTestInput;
}
/**
* Escape a string for SQL
*/
function esc(value: string): string {
return value.replace(/'/g, "''");
}
/**
* Update a custom format test by writing an operation to the specified layer
* Uses value guards to detect conflicts with upstream changes
*/
export async function updateTest(options: UpdateTestOptions) {
const { databaseId, layer, formatName, current, input } = options;
const descriptionValue = input.description ? `'${esc(input.description)}'` : 'NULL';
// Update with value guards on the current values
// We match on id AND verify the current values haven't changed
const updateTest = {
sql: `UPDATE custom_format_tests SET title = '${esc(input.title)}', type = '${esc(input.type)}', should_match = ${input.should_match ? 1 : 0}, description = ${descriptionValue} WHERE custom_format_id = cf('${esc(formatName)}') AND title = '${esc(current.title)}' AND type = '${esc(current.type)}'`,
parameters: [],
query: {} as never
};
// Track if title changed for metadata
const isTitleChange = input.title !== current.title;
// Write the operation
const result = await writeOperation({
databaseId,
layer,
description: `update-test-${formatName}`,
queries: [updateTest],
metadata: {
operation: 'update',
entity: 'custom_format_test',
name: `${formatName}: ${input.title.substring(0, 30)}`,
...(isTitleChange && { previousName: `${formatName}: ${current.title.substring(0, 30)}` })
}
});
return result;
}