import UserMiddleware from "../Middleware/UserAuthorization";
import NotificationService from "../Services/NotificationService";
import Express from "../Utils/Express";
import Response from "../Utils/Response";
import BadDataException from "../../Types/Exception/BadDataException";
import JSONFunctions from "../../Types/JSONFunctions";
import Permission from "../../Types/Permission";
import PositiveNumber from "../../Types/PositiveNumber";
const router = Express.getRouter();
router.post("/notification/recharge", UserMiddleware.getUserMiddleware, async (req, res, next) => {
    try {
        let amount = JSONFunctions.deserializeValue(req.body.amount);
        if (amount instanceof PositiveNumber) {
            amount = amount.toNumber();
        }
        if (typeof amount === "string") {
            amount = parseInt(amount);
        }
        const projectId = JSONFunctions.deserializeValue(req.body.projectId);
        if (!amount || typeof amount !== "number") {
            return Response.sendErrorResponse(req, res, new BadDataException("Invalid amount"));
        }
        if (amount > 1000) {
            return Response.sendErrorResponse(req, res, new BadDataException("Amount cannot be greater than 1000"));
        }
        if (amount < 20) {
            return Response.sendErrorResponse(req, res, new BadDataException("Amount cannot be less than 20"));
        }
        if (!projectId || !projectId.toString()) {
            return Response.sendErrorResponse(req, res, new BadDataException("Invalid projectId"));
        }
        // get permissions. if user has permission to recharge, then recharge
        if (!req.userTenantAccessPermission ||
            !req.userTenantAccessPermission[projectId.toString()]) {
            return Response.sendErrorResponse(req, res, new BadDataException("Permission for this user not found"));
        }
        const permissions = req.userTenantAccessPermission[projectId.toString()].permissions.map((permission) => {
            return permission.permission;
        });
        if (permissions.includes(Permission.ProjectOwner) ||
            permissions.includes(Permission.ManageProjectBilling)) {
            await NotificationService.rechargeBalance(projectId, amount);
        }
        else {
            return Response.sendErrorResponse(req, res, new BadDataException("User does not have permission to recharge. You need any one of these permissions - ProjectOwner, CanManageProjectBilling"));
        }
    }
    catch (err) {
        return next(err);
    }
    return Response.sendEmptySuccessResponse(req, res);
});
export default router;
//# sourceMappingURL=NotificationAPI.js.map