"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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = handleBackup; const grab_user_resource_1 = __importDefault(require("@/src/functions/db/grab-user-resource")); const add_backup_1 = __importDefault(require("@/src/functions/api/su/add-backup")); const grab_config_1 = __importDefault(require("@/package-shared/utils/backend/config/grab-config")); const delete_backup_1 = __importDefault(require("@/src/functions/api/su/add-backup/delete-backup")); function handleBackup(_a) { return __awaiter(this, arguments, void 0, function* ({ appBackup, userId, }) { var _b; const { appConfig } = (0, grab_config_1.default)(); const maxBackups = ((_b = appConfig.main.max_backups) === null || _b === void 0 ? void 0 : _b.value) || 20; const { count: existingAppBackupsCount } = yield (0, grab_user_resource_1.default)({ tableName: "backups", isSuperUser: true, query: { query: { user_id: { nullValue: appBackup ? true : undefined, value: appBackup ? undefined : String(userId), }, }, }, countOnly: true, }); if (existingAppBackupsCount && existingAppBackupsCount >= maxBackups) { const { single: oldestAppBackup } = yield (0, grab_user_resource_1.default)({ tableName: "backups", isSuperUser: true, query: { query: { user_id: { nullValue: appBackup ? true : undefined, value: appBackup ? undefined : String(userId), }, }, order: { field: "id", strategy: "ASC", }, limit: 1, }, }); if (oldestAppBackup === null || oldestAppBackup === void 0 ? void 0 : oldestAppBackup.id) { yield (0, delete_backup_1.default)({ backup: oldestAppBackup }); } } yield (0, add_backup_1.default)({ targetUserId: userId }); }); }