Describe snapshot source revisions
This commit is contained in:
@@ -6,7 +6,10 @@ import {
|
||||
createNamedProjectSnapshotSchema,
|
||||
restoreProjectSnapshotSchema,
|
||||
} from "../../shared/validation/project-snapshot.schemas.js";
|
||||
import { projectCommandService } from "../composition/project-command-stores.js";
|
||||
import {
|
||||
projectCommandService,
|
||||
projectHistoryStore,
|
||||
} from "../composition/project-command-stores.js";
|
||||
import { projectSnapshotStore } from "../composition/project-snapshot-store.js";
|
||||
|
||||
export function listProjectSnapshots(req: Request, res: Response) {
|
||||
@@ -18,7 +21,7 @@ export function listProjectSnapshots(req: Request, res: Response) {
|
||||
if (!snapshots) {
|
||||
return res.status(404).json({ error: "Project not found" });
|
||||
}
|
||||
return res.json(snapshots);
|
||||
return res.json(withSourceRevisionMetadata(projectId, snapshots));
|
||||
}
|
||||
|
||||
export function createNamedProjectSnapshot(
|
||||
@@ -43,7 +46,9 @@ export function createNamedProjectSnapshot(
|
||||
if (!snapshot) {
|
||||
return res.status(404).json({ error: "Project not found" });
|
||||
}
|
||||
return res.status(201).json(snapshot);
|
||||
return res
|
||||
.status(201)
|
||||
.json(withSourceRevisionMetadata(projectId, [snapshot])[0]);
|
||||
} catch (error) {
|
||||
if (error instanceof ProjectRevisionConflictError) {
|
||||
return res.status(409).json({
|
||||
@@ -97,7 +102,9 @@ export function restoreProjectSnapshot(
|
||||
command: prepared.command,
|
||||
});
|
||||
return res.json({
|
||||
snapshot: prepared.snapshot,
|
||||
snapshot: withSourceRevisionMetadata(projectId, [
|
||||
prepared.snapshot,
|
||||
])[0],
|
||||
revision: result.revision,
|
||||
history: result.history,
|
||||
});
|
||||
@@ -120,6 +127,29 @@ export function restoreProjectSnapshot(
|
||||
}
|
||||
}
|
||||
|
||||
function withSourceRevisionMetadata<
|
||||
TSnapshot extends { sourceRevision: number },
|
||||
>(
|
||||
projectId: string,
|
||||
snapshots: TSnapshot[]
|
||||
) {
|
||||
const revisions = projectHistoryStore.listRevisionsByNumbers(
|
||||
projectId,
|
||||
snapshots.map((snapshot) => snapshot.sourceRevision)
|
||||
);
|
||||
const revisionByNumber = new Map(
|
||||
revisions.map((revision) => [
|
||||
revision.revisionNumber,
|
||||
revision,
|
||||
])
|
||||
);
|
||||
return snapshots.map((snapshot) => ({
|
||||
...snapshot,
|
||||
sourceRevisionMetadata:
|
||||
revisionByNumber.get(snapshot.sourceRevision) ?? null,
|
||||
}));
|
||||
}
|
||||
|
||||
function getProjectId(req: Request, res: Response) {
|
||||
const { projectId } = req.params;
|
||||
if (typeof projectId !== "string" || !projectId.trim()) {
|
||||
|
||||
Reference in New Issue
Block a user