Ka-Note/ka-note/client/src/lib/db/seed.ts

83 lines
7.8 KiB
TypeScript

import { db } from './schema';
import { newId, now } from './helpers';
import type { AgendaContext, Topic, HistoryEntry } from '@ka-note/shared';
const today = new Date().toISOString().split('T')[0];
const yesterday = new Date(Date.now() - 86400000).toISOString().split('T')[0];
const lastWeek = new Date(Date.now() - 604800000).toISOString().split('T')[0];
export async function seedIfEmpty(): Promise<void> {
const count = await db.contexts.count();
if (count > 0) return;
const ts = now();
const contexts: AgendaContext[] = [
{ id: 'daily-log', name: 'Daily Log / Inbox', type: 'meeting', sortOrder: 0, meta: null, archivedAt: null, isFavorite: true, updatedAt: ts, deletedAt: null, version: 1 },
{ id: 'jf-sysadmins', name: 'JF Team Sysadmins', type: 'meeting', sortOrder: 1, meta: null, archivedAt: null, isFavorite: true, updatedAt: ts, deletedAt: null, version: 1 },
{ id: 'jf-devs', name: 'JF Developer', type: 'meeting', sortOrder: 2, meta: null, archivedAt: null, isFavorite: true, updatedAt: ts, deletedAt: null, version: 1 },
{ id: 'p-tisax', name: 'Project TISAX', type: 'project', sortOrder: 0, meta: { status: '#stInArbeit', owner: 'STEFE', links: 'FileServer/Projects/TISAX' }, archivedAt: null, isFavorite: true, updatedAt: ts, deletedAt: null, version: 1 },
{ id: 'p-cloud-migration', name: 'Project CLOUD-MIGRATION', type: 'project', sortOrder: 1, meta: { status: '#stBlocked', owner: 'CHFI', links: 'Jira-1234' }, archivedAt: null, isFavorite: false, updatedAt: ts, deletedAt: null, version: 1 },
{ id: 'u-stefe', name: 'Person STEFE', type: 'person', sortOrder: 0, meta: { fullName: 'Stefan E.', email: 'stefe@example.com', phone: '+49 123 456', duSince: '2020', personSubType: 'employee' }, archivedAt: null, isFavorite: true, updatedAt: ts, deletedAt: null, version: 1 },
{ id: 'u-chfi', name: 'Person CHFI', type: 'person', sortOrder: 1, meta: { fullName: 'Christoph F.', email: 'chfi@example.com', phone: '98765', duSince: '2024', personSubType: 'employee' }, archivedAt: null, isFavorite: false, updatedAt: ts, deletedAt: null, version: 1 },
{ id: 'u-vendor-x', name: 'Person VENDOR-X', type: 'person', sortOrder: 2, meta: { fullName: 'Hr. Müller (Vendor X)', email: 'sales@vendor-x.com', phone: '', duSince: '', personSubType: 'contact' }, archivedAt: null, isFavorite: false, updatedAt: ts, deletedAt: null, version: 1 },
{ id: 'u-chrkl', name: 'Person ChrKl', type: 'person', sortOrder: 3, meta: { fullName: 'Christian Kl.', email: '', phone: '', duSince: '', personSubType: 'colleague' }, archivedAt: null, isFavorite: false, updatedAt: ts, deletedAt: null, version: 1 },
{ id: 'f-vendor-x', name: 'Firma VENDOR-X', type: 'company', sortOrder: 0, meta: { website: 'https://vendor-x.com', address: 'Musterstr. 1, 12345 Berlin' }, archivedAt: null, isFavorite: true, updatedAt: ts, deletedAt: null, version: 1 }
];
// Topic IDs
const t201 = newId(), t202 = newId(), t203 = newId(), t204 = newId();
const t1 = newId(), t2 = newId(), t101 = newId();
const topics: Topic[] = [
{ id: t201, contextId: 'daily-log', title: 'Anruf: Lizenzprobleme Vendor X', status: 'active', snoozeUntil: null, sortOrder: 0, isNew: false, updatedAt: ts, deletedAt: null, version: 1 },
{ id: t202, contextId: 'daily-log', title: 'Idee: Cloud-Backup Optimierung', status: 'active', snoozeUntil: null, sortOrder: 1, isNew: false, updatedAt: ts, deletedAt: null, version: 1 },
{ id: t203, contextId: 'daily-log', title: 'Quick-Fix: Drucker Buchhaltung', status: 'active', snoozeUntil: null, sortOrder: 2, isNew: false, updatedAt: ts, deletedAt: null, version: 1 },
{ id: t204, contextId: 'daily-log', title: 'Personalgespräch Vorbereitung', status: 'active', snoozeUntil: null, sortOrder: 3, isNew: false, updatedAt: ts, deletedAt: null, version: 1 },
{ id: t1, contextId: 'jf-sysadmins', title: 'TISAX: Sperren Produktionsrechner', status: 'active', snoozeUntil: null, sortOrder: 0, isNew: false, updatedAt: ts, deletedAt: null, version: 1 },
{ id: t2, contextId: 'jf-sysadmins', title: 'Greenbone-Prozess', status: 'active', snoozeUntil: null, sortOrder: 1, isNew: false, updatedAt: ts, deletedAt: null, version: 1 },
{ id: t101, contextId: 'jf-devs', title: 'API Refactoring', status: 'active', snoozeUntil: null, sortOrder: 0, isNew: false, updatedAt: ts, deletedAt: null, version: 1 }
];
// Journal topic for daily-log
const journalTopicId = 'daily-log-journal';
const journalTopic: Topic = {
id: journalTopicId, contextId: 'daily-log', title: '__journal__', status: 'active',
snoozeUntil: null, sortOrder: -1, isNew: false, updatedAt: ts, deletedAt: null, version: 1
};
const history: HistoryEntry[] = [
{ id: newId(), topicId: t201, date: today, text: 'Hr. Müller angerufen. Lizenzserver ist down.\n- Ersatzticket erstellt: #INC-999\n- Eskalation an -> VENDOR-X\n- Info an Team -> STEFE\n- @VENDOR-X war sehr hilfsbereit bei der Umgehung.\n- Betrifft @F:VENDOR-X', sortOrder: 0, linkedContextId: null, doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
{ id: newId(), topicId: t202, date: yesterday, text: 'Kosten laufen aus dem Ruder.\n- Idee: S3 Glacier Deep Archive nutzen?\n- Prüfen -> CHFI @P:CLOUD-MIGRATION\n- @CHFI hat super Ideen geliefert.', sortOrder: 0, linkedContextId: null, doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
{ id: newId(), topicId: t203, date: today, text: 'Papierstau behoben. Toner bestellt.', sortOrder: 0, linkedContextId: null, doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
{ id: newId(), topicId: t204, date: lastWeek, text: '- Ziele für Q1 prüfen\n- Schulungsbedarf klären\n- -> CHFI', sortOrder: 0, linkedContextId: null, doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
{ id: newId(), topicId: t1, date: today, text: '- Entscheidung: Keine Ausnahmen mehr.\n- Umsetzung startet nächste Woche @P:TISAX', sortOrder: 1, linkedContextId: null, doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
{ id: newId(), topicId: t1, date: lastWeek, text: '- Maßnahmen definiert:\n - Benutzergruppe für kein Internet -> STEFE @P:TISAX\n - VLAN „shared" nutzen -> PHILO\n - IP-Range prüfen', sortOrder: 0, linkedContextId: null, doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
{ id: newId(), topicId: t2, date: yesterday, text: 'Priorisierung nötig -> ERAY @P:Security', sortOrder: 0, linkedContextId: null, doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
{ id: newId(), topicId: t101, date: today, text: 'Neues Schema prüfen @P:TISAX\n- @ChrKl hat das verbockt.', sortOrder: 0, linkedContextId: null, doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
// Journal entries
{ id: newId(), topicId: journalTopicId, date: today, text: 'Standup: Alle Tickets im Sprint on track.', sortOrder: 0, linkedContextId: null, doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
{ id: newId(), topicId: journalTopicId, date: today, text: 'Lizenzthema mit Vendor X besprochen, Eskalation läuft.', sortOrder: 1, linkedContextId: 'jf-sysadmins', doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
{ id: newId(), topicId: journalTopicId, date: yesterday, text: 'Cloud-Migration Review vorbereitet.', sortOrder: 0, linkedContextId: null, doneAt: null, updatedAt: ts, deletedAt: null, version: 1 },
];
await db.transaction('rw', [db.contexts, db.topics, db.historyEntries], async () => {
await db.contexts.bulkPut(contexts);
await db.topics.bulkPut([...topics, journalTopic]);
await db.historyEntries.bulkPut(history);
});
console.log('Seed data loaded');
}
export async function resetAndReseed(): Promise<void> {
await db.transaction('rw', [db.contexts, db.topics, db.historyEntries, db.ratings, db.syncMeta], async () => {
await db.contexts.clear();
await db.topics.clear();
await db.historyEntries.clear();
await db.ratings.clear();
await db.syncMeta.clear();
});
await seedIfEmpty();
}