VIBN Frontend for Coolify deployment
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import admin from '@/lib/firebase/admin';
|
||||
|
||||
/**
|
||||
* Post a new message/comment on a work item
|
||||
*/
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ projectId: string; itemId: string }> }
|
||||
) {
|
||||
try {
|
||||
const { projectId, itemId } = await params;
|
||||
const { message, author, authorId, type } = await request.json();
|
||||
|
||||
if (!message || !author) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Message and author are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const db = admin.firestore();
|
||||
|
||||
// Create new message
|
||||
const messageRef = db
|
||||
.collection('projects')
|
||||
.doc(projectId)
|
||||
.collection('workItems')
|
||||
.doc(itemId)
|
||||
.collection('messages')
|
||||
.doc();
|
||||
|
||||
await messageRef.set({
|
||||
message,
|
||||
author,
|
||||
authorId: authorId || 'anonymous',
|
||||
type: type || 'comment', // comment, feedback, question, etc.
|
||||
createdAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
reactions: [],
|
||||
});
|
||||
|
||||
// Update message count on work item metadata
|
||||
await db
|
||||
.collection('projects')
|
||||
.doc(projectId)
|
||||
.collection('workItemStates')
|
||||
.doc(itemId)
|
||||
.set(
|
||||
{
|
||||
messageCount: admin.firestore.FieldValue.increment(1),
|
||||
lastMessageAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
},
|
||||
{ merge: true }
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
messageId: messageRef.id,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error posting message:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to post message',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messages/comments for a work item
|
||||
*/
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ projectId: string; itemId: string }> }
|
||||
) {
|
||||
try {
|
||||
const { projectId, itemId } = await params;
|
||||
const db = admin.firestore();
|
||||
|
||||
const messagesSnapshot = await db
|
||||
.collection('projects')
|
||||
.doc(projectId)
|
||||
.collection('workItems')
|
||||
.doc(itemId)
|
||||
.collection('messages')
|
||||
.orderBy('createdAt', 'desc')
|
||||
.get();
|
||||
|
||||
const messages = messagesSnapshot.docs.map(doc => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
createdAt: doc.data().createdAt?.toDate().toISOString(),
|
||||
}));
|
||||
|
||||
return NextResponse.json({
|
||||
messages,
|
||||
count: messages.length,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching messages:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to fetch messages',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a message
|
||||
*/
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ projectId: string; itemId: string }> }
|
||||
) {
|
||||
try {
|
||||
const { projectId, itemId } = await params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const messageId = searchParams.get('messageId');
|
||||
|
||||
if (!messageId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Message ID is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const db = admin.firestore();
|
||||
|
||||
// Delete message
|
||||
await db
|
||||
.collection('projects')
|
||||
.doc(projectId)
|
||||
.collection('workItems')
|
||||
.doc(itemId)
|
||||
.collection('messages')
|
||||
.doc(messageId)
|
||||
.delete();
|
||||
|
||||
// Update message count
|
||||
await db
|
||||
.collection('projects')
|
||||
.doc(projectId)
|
||||
.collection('workItemStates')
|
||||
.doc(itemId)
|
||||
.set(
|
||||
{
|
||||
messageCount: admin.firestore.FieldValue.increment(-1),
|
||||
},
|
||||
{ merge: true }
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error deleting message:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to delete message',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import admin from '@/lib/firebase/admin';
|
||||
|
||||
/**
|
||||
* Update work item state (draft/final)
|
||||
*/
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ projectId: string; itemId: string }> }
|
||||
) {
|
||||
try {
|
||||
const { projectId, itemId } = await params;
|
||||
const { state } = await request.json();
|
||||
|
||||
if (!state || !['draft', 'final'].includes(state)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid state. Must be "draft" or "final"' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const db = admin.firestore();
|
||||
|
||||
// Update state in work item
|
||||
// For now, store in a separate collection since work items are generated from MVP checklist
|
||||
await db
|
||||
.collection('projects')
|
||||
.doc(projectId)
|
||||
.collection('workItemStates')
|
||||
.doc(itemId)
|
||||
.set(
|
||||
{
|
||||
state,
|
||||
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
},
|
||||
{ merge: true }
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
state,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error updating work item state:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to update state',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get work item state
|
||||
*/
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ projectId: string; itemId: string }> }
|
||||
) {
|
||||
try {
|
||||
const { projectId, itemId } = await params;
|
||||
const db = admin.firestore();
|
||||
|
||||
const stateDoc = await db
|
||||
.collection('projects')
|
||||
.doc(projectId)
|
||||
.collection('workItemStates')
|
||||
.doc(itemId)
|
||||
.get();
|
||||
|
||||
if (!stateDoc.exists) {
|
||||
return NextResponse.json({
|
||||
state: 'draft', // Default state
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
state: stateDoc.data()?.state || 'draft',
|
||||
updatedAt: stateDoc.data()?.updatedAt,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching work item state:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to fetch state',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import admin from '@/lib/firebase/admin';
|
||||
|
||||
/**
|
||||
* Create a new version of a work item
|
||||
*/
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ projectId: string; itemId: string }> }
|
||||
) {
|
||||
try {
|
||||
const { projectId, itemId } = await params;
|
||||
const { description, changes, createdBy } = await request.json();
|
||||
|
||||
const db = admin.firestore();
|
||||
|
||||
// Get current version count
|
||||
const versionsSnapshot = await db
|
||||
.collection('projects')
|
||||
.doc(projectId)
|
||||
.collection('workItems')
|
||||
.doc(itemId)
|
||||
.collection('versions')
|
||||
.orderBy('versionNumber', 'desc')
|
||||
.limit(1)
|
||||
.get();
|
||||
|
||||
const currentVersion = versionsSnapshot.empty ? 0 : versionsSnapshot.docs[0].data().versionNumber;
|
||||
const newVersionNumber = currentVersion + 1;
|
||||
|
||||
// Create new version
|
||||
const versionRef = db
|
||||
.collection('projects')
|
||||
.doc(projectId)
|
||||
.collection('workItems')
|
||||
.doc(itemId)
|
||||
.collection('versions')
|
||||
.doc();
|
||||
|
||||
await versionRef.set({
|
||||
versionNumber: newVersionNumber,
|
||||
description: description || `Version ${newVersionNumber}`,
|
||||
changes: changes || {},
|
||||
createdBy: createdBy || 'system',
|
||||
createdAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
versionId: versionRef.id,
|
||||
versionNumber: newVersionNumber,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error creating version:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to create version',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get version history for a work item
|
||||
*/
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ projectId: string; itemId: string }> }
|
||||
) {
|
||||
try {
|
||||
const { projectId, itemId } = await params;
|
||||
const db = admin.firestore();
|
||||
|
||||
const versionsSnapshot = await db
|
||||
.collection('projects')
|
||||
.doc(projectId)
|
||||
.collection('workItems')
|
||||
.doc(itemId)
|
||||
.collection('versions')
|
||||
.orderBy('versionNumber', 'desc')
|
||||
.get();
|
||||
|
||||
const versions = versionsSnapshot.docs.map(doc => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
createdAt: doc.data().createdAt?.toDate().toISOString(),
|
||||
}));
|
||||
|
||||
return NextResponse.json({
|
||||
versions,
|
||||
count: versions.length,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching versions:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to fetch versions',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user