diff --git a/index.js b/index.js index 6b42c4f..f1e40e2 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ var prankPath = "prankdata.txt"; var activityPath = "activitydata.txt"; var treasurePath = "treasuredata.txt"; var goldenUsersPath = "goldenusers.txt"; +var servicePath = "servicestate.txt"; initFs(); @@ -18,6 +19,7 @@ let PrankData = JSON.parse(fs.readFileSync(prankPath)); let ActivityData = JSON.parse(fs.readFileSync(activityPath)); let TreasureData = JSON.parse(fs.readFileSync(treasurePath)); let GoldenUsers = JSON.parse(fs.readFileSync(goldenUsersPath)); +let ServiceState = JSON.parse(fs.readFileSync(servicePath)); let AdminUsersUid = ["asyncnomi", "johan", "enthalpine", "fleur", "arina", "billy", "remi", "pierre", "matmaz", "mariusdrgc", "agathe", "jsansa"]; let UsersToken = {}; let TokenDurationSecond = 3600; @@ -135,6 +137,32 @@ fastify.post('/register', async (request, reply) => { } }) +fastify.post('/switchState', async (request, reply) => { + let content = request.body; + let auth = checkAuthetification(content); + if (auth.success) { + if (AdminUsersUid.includes(content.uid)) { + if (ServiceState.state == "closed") { + ServiceState.state = "open"; + } else { + ServiceState.state = "closed"; + } + saveData(servicePath, ServiceState); + return { + success: true, + state: ServiceState.state + } + } else { + return { + success: false, + why: "Not allowed" + } + } + } else { + return auth + } +}) + fastify.post('/addPrank', async (request, reply) => { let content = request.body; let auth = checkAuthetification(content); @@ -384,6 +412,12 @@ fastify.post('/get', async (request, reply) => { } } break; + case "state": + return { + success: true, + state: ServiceState.state + } + break; default: return { success: false, @@ -759,6 +793,9 @@ function initFs() { if (!fs.existsSync(goldenUsersPath)) { fs.writeFileSync(goldenUsersPath, "{}"); } + if (!fs.existsSync(servicePath)) { + fs.writeFileSync(servicePath, JSON.stringify({state: 'closed'})); + } if (!fs.existsSync("static/images")){ fs.mkdirSync("static/images"); }