import { Response } from "express"; import { AuthenticatedRequest } from "../../../../shared/utils/token.ts"; import { restoreSnapshotService } from "../../../../shared/services/yjs/snapshot.ts"; import { clearSnapshotController, restoreYDocFromAutoSave } from "../../../../shared/services/yjs/auto-save.ts"; export const autoSaveRestore = async ( req: AuthenticatedRequest, res: Response ): Promise => { try { const { organization, versionId, projectId, } = req.body; if (!organization || !versionId ) { res.status(400).json({ message: "All fields are required", }); return; } const result = await restoreYDocFromAutoSave({ organization, versionId,projectId }); switch (result.status) { case "User not found": res.status(404).json({ message: "User not found", }); break; case "Snapshot not found": res.status(404).json({ message: "Snapshot not found", }); break; case "Success": res.status(200).json({ AutoSave: result, }); break; default: res.status(500).json({ message: "Internal server error", }); break; } } catch (error) { res.status(500).json({ message: "Unknown error", }); } }; export const clearAutoSaveRestore = async ( req: AuthenticatedRequest, res: Response ): Promise => { try { const { organization, versionId, projectId, } = req.body; if (!organization || !versionId ) { res.status(400).json({ message: "All fields are required", }); return; } const result = await clearSnapshotController({ organization, versionId,projectId }); switch (result.status) { case "User not found": res.status(404).json({ message: "User not found", }); break; case "Snapshot not found": res.status(404).json({ message: "Snapshot not found", }); break; case "Success": res.status(200).json({ message: "Snapshot autosave deleted", }); break; default: res.status(500).json({ message: "Internal server error", }); break; } } catch (error) { res.status(500).json({ message: "Unknown error", }); } };