fix: title increment based on DB max, not stored template only
Release APK / build (push) Has been cancelled
Release APK / build (push) Has been cancelled
After each incident save, query existing titles with the same prefix to find the real max number, then use max(dbMax, tplNum) + 1. Prevents duplicate counters when incidents were created before the template was configured. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+9
-8
@@ -16,6 +16,7 @@ import {
|
|||||||
updateIncident,
|
updateIncident,
|
||||||
getIncidentById,
|
getIncidentById,
|
||||||
getDistinctServices,
|
getDistinctServices,
|
||||||
|
getMaxTitleNumber,
|
||||||
} from "@/lib/db";
|
} from "@/lib/db";
|
||||||
import { Colors } from "@/constants/theme";
|
import { Colors } from "@/constants/theme";
|
||||||
import { useVoice } from "@/hooks/useVoice";
|
import { useVoice } from "@/hooks/useVoice";
|
||||||
@@ -50,13 +51,6 @@ const LABEL_STYLE = {
|
|||||||
|
|
||||||
type VoiceField = "title" | "service" | "symptom" | "rootCause" | "fix";
|
type VoiceField = "title" | "service" | "symptom" | "rootCause" | "fix";
|
||||||
|
|
||||||
function incrementTemplate(template: string): string {
|
|
||||||
const match = template.match(/^([\s\S]*?)(\d+)$/);
|
|
||||||
if (!match) return template;
|
|
||||||
const [, prefix, numStr] = match;
|
|
||||||
const next = (parseInt(numStr, 10) + 1).toString().padStart(numStr.length, "0");
|
|
||||||
return prefix + next;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function NewIncidentScreen() {
|
export default function NewIncidentScreen() {
|
||||||
const { editId } = useLocalSearchParams<{ editId?: string }>();
|
const { editId } = useLocalSearchParams<{ editId?: string }>();
|
||||||
@@ -180,7 +174,14 @@ export default function NewIncidentScreen() {
|
|||||||
await createIncident(form);
|
await createIncident(form);
|
||||||
const tpl = await SecureStore.getItemAsync("pref_title_template");
|
const tpl = await SecureStore.getItemAsync("pref_title_template");
|
||||||
if (tpl) {
|
if (tpl) {
|
||||||
await SecureStore.setItemAsync("pref_title_template", incrementTemplate(tpl));
|
const match = tpl.match(/^([\s\S]*?)(\d+)$/);
|
||||||
|
if (match) {
|
||||||
|
const [, prefix, numStr] = match;
|
||||||
|
const dbMax = await getMaxTitleNumber(prefix);
|
||||||
|
const tplNum = parseInt(numStr, 10);
|
||||||
|
const next = (Math.max(dbMax, tplNum) + 1).toString().padStart(numStr.length, "0");
|
||||||
|
await SecureStore.setItemAsync("pref_title_template", prefix + next);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
router.back();
|
router.back();
|
||||||
|
|||||||
@@ -163,6 +163,20 @@ export async function deleteIncident(id: string): Promise<void> {
|
|||||||
await db.runAsync("DELETE FROM incidents WHERE id = ?", [id]);
|
await db.runAsync("DELETE FROM incidents WHERE id = ?", [id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getMaxTitleNumber(prefix: string): Promise<number> {
|
||||||
|
const db = await getDb();
|
||||||
|
const rows = await db.getAllAsync<{ title: string }>(
|
||||||
|
"SELECT title FROM incidents WHERE title LIKE ?",
|
||||||
|
[`${prefix}%`]
|
||||||
|
);
|
||||||
|
let max = 0;
|
||||||
|
for (const row of rows) {
|
||||||
|
const n = parseInt(row.title.slice(prefix.length), 10);
|
||||||
|
if (!isNaN(n) && n > max) max = n;
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
export async function getDistinctServices(): Promise<string[]> {
|
export async function getDistinctServices(): Promise<string[]> {
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
const rows = await db.getAllAsync<{ service: string }>(
|
const rows = await db.getAllAsync<{ service: string }>(
|
||||||
|
|||||||
Reference in New Issue
Block a user