migration of job_id tags in notes and updated mine filter
This commit is contained in:
+101
-1
@@ -1095,6 +1095,105 @@ const runLegacyTagMigration = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
const runLegacyJobIdTagMigration = async () => {
|
||||
const currentUserId = pb.authStore.model?.id;
|
||||
if (!currentUserId) return;
|
||||
|
||||
try {
|
||||
const userRec = await pb.collection('users').getOne(currentUserId, { requestKey: null });
|
||||
const prefs = userRec.Taskgrid_pref || {};
|
||||
|
||||
if (prefs.migratedLegacyJobIdTagsV1) return;
|
||||
|
||||
// Fetch all Notes (all users)
|
||||
const notes = await pb.collection(NOTES_COLLECTION).getFullList({
|
||||
requestKey: null
|
||||
});
|
||||
|
||||
const extractLegacyJobNumbers = (tags: string[] | undefined) => {
|
||||
if (!tags || tags.length === 0) return [];
|
||||
const nums: string[] = [];
|
||||
tags.forEach(t => {
|
||||
if (!t.startsWith("job_id:")) return;
|
||||
const val = t.split(":")[1]?.trim();
|
||||
if (val && /^\d{4}$/.test(val)) nums.push(val);
|
||||
});
|
||||
return nums;
|
||||
};
|
||||
|
||||
const legacyNumbers = new Set<string>();
|
||||
notes.forEach(n => extractLegacyJobNumbers(n.tags).forEach(nm => legacyNumbers.add(nm)));
|
||||
|
||||
if (legacyNumbers.size > 0) {
|
||||
const allNumbers = Array.from(legacyNumbers);
|
||||
const jobMap = new Map<string, string>(); // jobNumber -> jobId
|
||||
const chunkSize = 50;
|
||||
|
||||
for (let i = 0; i < allNumbers.length; i += chunkSize) {
|
||||
const chunk = allNumbers.slice(i, i + chunkSize);
|
||||
const filter = chunk.map(n => `Job_Number = "${n}"`).join(" || ");
|
||||
const jobs = await pb.collection('Job_Info_Prod').getFullList({
|
||||
filter,
|
||||
fields: "id,Job_Number",
|
||||
requestKey: null
|
||||
});
|
||||
jobs.forEach((j: any) => {
|
||||
if (j.Job_Number !== undefined && j.Job_Number !== null && j.Job_Number !== "") {
|
||||
jobMap.set(String(j.Job_Number), String(j.id));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let migratedNotes = 0;
|
||||
for (const n of notes) {
|
||||
if (!n.tags || n.tags.length === 0) continue;
|
||||
let changed = false;
|
||||
const newTags = n.tags.map((tag: string) => {
|
||||
if (!tag.startsWith("job_id:")) return tag;
|
||||
const val = tag.split(":")[1]?.trim();
|
||||
if (!val || !/^\d{4}$/.test(val)) return tag;
|
||||
const mappedId = jobMap.get(val);
|
||||
if (!mappedId) return tag;
|
||||
changed = true;
|
||||
return `job_id:${mappedId}`;
|
||||
});
|
||||
if (changed) {
|
||||
await pb.collection(NOTES_COLLECTION).update(n.id, { tags: newTags }, { requestKey: null });
|
||||
migratedNotes++;
|
||||
}
|
||||
}
|
||||
|
||||
if (migratedNotes > 0) {
|
||||
setStore("notes", (items) => items.map(n => {
|
||||
if (!n.tags || n.tags.length === 0) return n;
|
||||
let localChanged = false;
|
||||
const nt = n.tags.map(tag => {
|
||||
if (!tag.startsWith("job_id:")) return tag;
|
||||
const val = tag.split(":")[1]?.trim();
|
||||
if (!val || !/^\d{4}$/.test(val)) return tag;
|
||||
const mappedId = jobMap.get(val);
|
||||
if (!mappedId) return tag;
|
||||
localChanged = true;
|
||||
return `job_id:${mappedId}`;
|
||||
});
|
||||
return localChanged ? { ...n, tags: nt } : n;
|
||||
}));
|
||||
}
|
||||
|
||||
console.log(`[MIGRATION] Job tag migration complete. Updated ${migratedNotes} notes.`);
|
||||
}
|
||||
|
||||
prefs.migratedLegacyJobIdTagsV1 = true;
|
||||
await pb.collection('users').update(currentUserId, { Taskgrid_pref: prefs }, { requestKey: null });
|
||||
|
||||
if (pb.authStore.model) {
|
||||
pb.authStore.model.Taskgrid_pref = prefs;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[MIGRATION] Job tag migration failed:", err);
|
||||
}
|
||||
};
|
||||
|
||||
export const initStore = async () => {
|
||||
if (!pb.authStore.isValid || store.isInitializing) return;
|
||||
setStore("isInitializing", true);
|
||||
@@ -1438,8 +1537,9 @@ export const initStore = async () => {
|
||||
// Fire off background sync
|
||||
await backgroundSync();
|
||||
|
||||
// Run one-time migration for legacy tags
|
||||
// Run one-time migrations
|
||||
await runLegacyTagMigration();
|
||||
await runLegacyJobIdTagMigration();
|
||||
|
||||
// Separate Templates load (less critical)
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user