import { RequestHandler, RequestType } from "../../RequestHandler"; import { FastifyRequest, FastifyReply } from 'fastify'; import { UserIsLoggedIn } from "../../requirements/user/UserIsLoggedIn"; import { UserCanUseApp } from "./UserCanUseApp"; import { FilesSync } from "../../../files/FilesSync"; import { Files } from "../../../files/Files"; export class AppsSaveHandler extends RequestHandler { static url = "/apps/save"; constructor() { super( RequestType.POST, AppsSaveHandler.url, [ new UserIsLoggedIn(), new UserCanUseApp() ] ); } async _handle( request:FastifyRequest, reply:FastifyReply ) { let requestBody = request.body; let appData = requestBody as { appData: any, appID:string }; let user = await this.getUser(); let filePath = Files.joinPaths( [ this._ums._settings.appsPath, appData.appID, user.id + ".json" ] ); await Files.ensureParentDirectoryExists( filePath ); await Files.saveJSON( filePath, appData.appData, true ); let app = this._ums.apps.find( a => a.id === appData.appID ); this._ums.onFileChanged.dispatch( filePath ); app.onFileChange.dispatch( filePath ); return this.sendInfo( "Data saved" ); } }