"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
const tenancy_1 = require("@devlego/backend-core/tenancy");
const db_1 = require("@devlego/backend-core/db");
const rows_1 = require("../../../utilities/usageQuota/rows");
const pro_1 = require("@devlego/pro");
const run = () => __awaiter(void 0, void 0, void 0, function* () {
    // get all rows in all apps
    // @ts-ignore
    const allApps = yield (0, db_1.getAllApps)({ all: true });
    // @ts-ignore
    const appIds = allApps ? allApps.map((app) => app.appId) : [];
    const rows = yield (0, rows_1.getUniqueRows)(appIds);
    const rowCount = rows ? rows.length : 0;
    // sync row count
    const tenantId = (0, tenancy_1.getTenantId)();
    console.log(`[Tenant: ${tenantId}] Syncing row count: ${rowCount}`);
    yield pro_1.quotas.setUsage(rowCount, pro_1.StaticQuotaName.ROWS, pro_1.QuotaUsageType.STATIC);
});
exports.run = run;
