feat: rewrite lib/server/projects.ts to use Postgres instead of Firestore
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { FieldValue } from 'firebase-admin/firestore';
|
||||
import { getAdminDb } from '@/lib/firebase/admin';
|
||||
import { query } from '@/lib/db-postgres';
|
||||
import type {
|
||||
ProjectPhase,
|
||||
ProjectPhaseData,
|
||||
@@ -21,14 +20,17 @@ export function toStage(stage?: string | null): ProjectStage {
|
||||
}
|
||||
|
||||
export async function loadPhaseContainers(projectId: string) {
|
||||
const adminDb = getAdminDb();
|
||||
const projectRef = adminDb.collection('projects').doc(projectId);
|
||||
const snapshot = await projectRef.get();
|
||||
const doc = snapshot.data() || {};
|
||||
const rows = await query<{ id: string; data: any }>(`
|
||||
SELECT id, data FROM fs_projects WHERE id = $1 LIMIT 1
|
||||
`, [projectId]);
|
||||
|
||||
if (rows.length === 0) throw new Error(`Project ${projectId} not found`);
|
||||
|
||||
const doc = rows[0].data || {};
|
||||
const phaseData = (doc.phaseData ?? {}) as ProjectPhaseData;
|
||||
const phaseScores = (doc.phaseScores ?? {}) as ProjectPhaseScores;
|
||||
const phaseHistory = Array.isArray(doc.phaseHistory) ? [...doc.phaseHistory] : [];
|
||||
return { projectRef, phaseData, phaseScores, phaseHistory };
|
||||
return { projectId, doc, phaseData, phaseScores, phaseHistory };
|
||||
}
|
||||
|
||||
interface PersistencePayload {
|
||||
@@ -46,19 +48,19 @@ export async function persistPhaseArtifacts(
|
||||
phaseHistory: Array<Record<string, unknown>>,
|
||||
) => PersistencePayload,
|
||||
) {
|
||||
const { projectRef, phaseData, phaseScores, phaseHistory } = await loadPhaseContainers(projectId);
|
||||
const { doc, phaseData, phaseScores, phaseHistory } = await loadPhaseContainers(projectId);
|
||||
const payload = builder(phaseData, phaseScores, phaseHistory);
|
||||
|
||||
await projectRef.set(
|
||||
{
|
||||
const updated = {
|
||||
...doc,
|
||||
phaseData: payload.phaseData,
|
||||
phaseScores: payload.phaseScores,
|
||||
phaseHistory: payload.phaseHistory,
|
||||
...(payload.nextPhase ? { currentPhase: payload.nextPhase, phaseStatus: 'completed' as const } : {}),
|
||||
updatedAt: FieldValue.serverTimestamp(),
|
||||
},
|
||||
{ merge: true },
|
||||
);
|
||||
updatedAt: new Date().toISOString(),
|
||||
...(payload.nextPhase ? { currentPhase: payload.nextPhase, phaseStatus: 'completed' } : {}),
|
||||
};
|
||||
|
||||
await query(`
|
||||
UPDATE fs_projects SET data = $1::jsonb WHERE id = $2
|
||||
`, [JSON.stringify(updated), projectId]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user