Is it possible to limit user's compile to max 1 time in 1 hour?

Let's say I have a seperate user management system, an record in MySQL like:

userEmail lastCompile
wang@163.com 2025/3/29 14:15
lee@qq.com 2024/5/6 10:00

And I have an api, tell him the user email, it will return the lastcompile time.

What I want is :
When user click Build -> Android App (.apk), the AI builder will visit this api first, then compare the returned lastcompile time with now time. If more than 1 hour past, it will continue to compile, and update the mysql record. Otherwise, popup an error message box.

How to achieve this?
Thanks
Kevin

Install dependency in your node js server

npm install express axios mysql2

Use an api inthe server

const express = require("express");
const axios = require("axios");
const mysql = require("mysql2/promise");

const app = express();
const PORT = 3000; // Change if needed

// MySQL Database Connection
const db = mysql.createPool({
    host: "your-mysql-host",
    user: "your-mysql-user",
    password: "your-mysql-password",
    database: "your-database",
});

// API Endpoint to check if the user can compile
app.get("/check-build", async (req, res) => {
    const userEmail = req.query.email;
    if (!userEmail) {
        return res.status(400).json({ success: false, message: "Email is required" });
    }

    try {
        // Fetch last compile time from your API
        const response = await axios.get(`https://your-api.com/getLastCompile?email=${userEmail}`);
        const lastCompileTime = new Date(response.data.lastCompile);
        const now = new Date();

        // Calculate time difference in minutes
        const timeDifference = (now - lastCompileTime) / (1000 * 60);

        if (timeDifference < 60) {
            return res.status(400).json({
                success: false,
                message: `You need to wait ${60 - timeDifference.toFixed(2)} more minutes before recompiling.`,
            });
        }

        // Update MySQL with the new compile time
        await db.execute("UPDATE users SET lastCompile = ? WHERE userEmail = ?", [now, userEmail]);

        return res.json({ success: true, message: "Compilation allowed." });
    } catch (error) {
        console.error("Error:", error);
        return res.status(500).json({ success: false, message: "Server error." });
    }
});

// Start Server
app.listen(PORT, () => {
    console.log(`Server running on http://localhost:${PORT}`);
});

Use this js in the frontend

async function checkBuildAndCompile(userEmail) {
    const response = await fetch(`/check-build?email=${userEmail}`);
    const data = await response.json();

    if (!data.success) {
        alert(data.message); // Show error if waiting time isn't met
    } else {
        console.log("Compilation started...");
        // Trigger compilation process here
    }
}

document.getElementById("buildButton").addEventListener("click", () => {
    checkBuildAndCompile("wang@163.com");
});


  1. Save the file as server.js.

  2. Run the server:

node server.js

  1. Open http://localhost:3000/check-build?email=test@email.com in a browser to test

Sorry if i am wrong but if the api sets up correctly it will do good as expected i think.

1 Like

Thanks, but you maybe missed this.

Anyway, I may have found a solution, and will post here later when i finally make it working.

Well, however you be using HTML as frontend with a button click, so why dont we run this api call after the click and them allow to build if it met the requirements?

<button id="buildButton">Build -> Android App (.apk)</button>

<script>
async function checkBuildAndCompile(userEmail) {
    const response = await fetch(`/check-build?email=${userEmail}`);
    const data = await response.json();

    if (!data.success) {
        alert(data.message); // Show error if waiting time isn't met
    } else {
        console.log("Compilation started...");
        startCompilation(); // Call the function to start building the APK
    }
}

function startCompilation() {
    // Logic to start the AI Builder's compilation process
    console.log("Building APK...");
    // Example: Send a request to the AI Builder backend to trigger the build
}

document.getElementById("buildButton").addEventListener("click", () => {
    checkBuildAndCompile("wang@163.com"); // Replace with the actual user email
});
</script>

i mean clicking this button:

The proper way to do this in the App Inventor code base would be to create a new subclass of ChainableCommand in appengine/src/.../client/explorer/commands that performs the check. You would then insert this into the call chain of appengine/src/.../client/actions/BarcodeAction.java, which is responsible for triggering the sequence of actions for the build.

1 Like