From a4c8c53c39651186c9175d5d19dc1e4fede00393 Mon Sep 17 00:00:00 2001 From: aishaalsubaie Date: Wed, 30 Aug 2023 11:36:14 +0300 Subject: [PATCH 1/2] fixing issue unsafe call --- src/groups/.Rhistory | 0 src/rewards/admin.js | 186 +++++++++++++++---------- src/rewards/admin.ts | 109 +++++++++++++++ src/types/admin.js | 5 +- src/types/adminOther2.js | 2 + src/types/{admin.ts => adminOther2.ts} | 0 6 files changed, 228 insertions(+), 74 deletions(-) create mode 100644 src/groups/.Rhistory create mode 100644 src/rewards/admin.ts create mode 100644 src/types/adminOther2.js rename src/types/{admin.ts => adminOther2.ts} (100%) diff --git a/src/groups/.Rhistory b/src/groups/.Rhistory new file mode 100644 index 000000000..e69de29bb diff --git a/src/rewards/admin.js b/src/rewards/admin.js index f46ad7868..e6df153bf 100644 --- a/src/rewards/admin.js +++ b/src/rewards/admin.js @@ -1,81 +1,123 @@ -'use strict'; - -const plugins = require('../plugins'); -const db = require('../database'); -const utils = require('../utils'); - -const rewards = module.exports; - -rewards.save = async function (data) { - async function save(data) { - if (!Object.keys(data.rewards).length) { - return; - } - const rewardsData = data.rewards; - delete data.rewards; - if (!parseInt(data.id, 10)) { - data.id = await db.incrObjectField('global', 'rewards:id'); - } - await rewards.delete(data); - await db.setAdd('rewards:list', data.id); - await db.setObject(`rewards:id:${data.id}`, data); - await db.setObject(`rewards:id:${data.id}:rewards`, rewardsData); +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; } - - await Promise.all(data.map(data => save(data))); - await saveConditions(data); - return data; -}; - -rewards.delete = async function (data) { - await Promise.all([ - db.setRemove('rewards:list', data.id), - db.delete(`rewards:id:${data.id}`), - db.delete(`rewards:id:${data.id}:rewards`), - ]); + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; }; - -rewards.get = async function () { - return await utils.promiseParallel({ - active: getActiveRewards(), - conditions: plugins.hooks.fire('filter:rewards.conditions', []), - conditionals: plugins.hooks.fire('filter:rewards.conditionals', []), - rewards: plugins.hooks.fire('filter:rewards.rewards', []), +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()); }); }; - -async function saveConditions(data) { - const rewardsPerCondition = {}; - await db.delete('conditions:active'); - const conditions = []; - - data.forEach((reward) => { - conditions.push(reward.condition); - rewardsPerCondition[reward.condition] = rewardsPerCondition[reward.condition] || []; - rewardsPerCondition[reward.condition].push(reward.id); +Object.defineProperty(exports, "__esModule", { value: true }); +const plugins = __importStar(require("../plugins")); +const db = __importStar(require("../database")); // Import the 'db' module with proper type declarations +const utils = __importStar(require("../utils")); +const rewards = { + save: function (data) { + return __awaiter(this, void 0, void 0, function* () { + function save(data) { + return __awaiter(this, void 0, void 0, function* () { + if (!Object.keys(data.rewards).length) { + return; + } + const rewardsData = data.rewards; + delete data.rewards; + if (!parseInt(data.id, 10)) { + // Ensure that 'db' has the 'incrObjectField' method and type + if (typeof db.incrObjectField === 'function') { + data.id = yield (db).incrObjectField('global', 'rewards:id'); // Type assertion for 'db' + } + else { + throw new Error('db.incrObjectField is not a function'); + } + } + yield rewards.delete(data); + yield db.setAdd('rewards:list', data.id); + yield db.setObject(`rewards:id:${data.id}`, data); + yield db.setObject(`rewards:id:${data.id}:rewards`, rewardsData); + }); + } + yield Promise.all(data.map(data => save(data))); + yield saveConditions(data); + return data; + }); + }, + delete: function (data) { + return __awaiter(this, void 0, void 0, function* () { + yield Promise.all([ + db.setRemove('rewards:list', data.id), + db.delete(`rewards:id:${data.id}`), + db.delete(`rewards:id:${data.id}:rewards`), + ]); + }); + }, + get: function () { + return __awaiter(this, void 0, void 0, function* () { + return yield utils.promiseParallel({ + active: getActiveRewards(), + conditions: plugins.hooks.fire('filter:rewards.conditions', []), + conditionals: plugins.hooks.fire('filter:rewards.conditionals', []), + rewards: plugins.hooks.fire('filter:rewards.rewards', []), + }); + }); + }, +}; +function saveConditions(data) { + return __awaiter(this, void 0, void 0, function* () { + const rewardsPerCondition = {}; + yield db.delete('conditions:active'); + const conditions = []; + data.forEach((reward) => { + conditions.push(reward.condition); + rewardsPerCondition[reward.condition] = rewardsPerCondition[reward.condition] || []; + rewardsPerCondition[reward.condition].push(reward.id); + }); + yield db.setAdd('conditions:active', conditions); + yield Promise.all(Object.keys(rewardsPerCondition).map(c => db.setAdd(`condition:${c}:rewards`, rewardsPerCondition[c]))); }); - - await db.setAdd('conditions:active', conditions); - - await Promise.all(Object.keys(rewardsPerCondition).map(c => db.setAdd(`condition:${c}:rewards`, rewardsPerCondition[c]))); } - -async function getActiveRewards() { - async function load(id) { - const [main, rewards] = await Promise.all([ - db.getObject(`rewards:id:${id}`), - db.getObject(`rewards:id:${id}:rewards`), - ]); - if (main) { - main.disabled = main.disabled === 'true'; - main.rewards = rewards; +function getActiveRewards() { + return __awaiter(this, void 0, void 0, function* () { + function load(id) { + return __awaiter(this, void 0, void 0, function* () { + const [main, rewards] = yield Promise.all([ + db.getObject(`rewards:id:${id}`), + db.getObject(`rewards:id:${id}:rewards`), + ]); + if (main) { + main.disabled = main.disabled === 'true'; + main.rewards = rewards; + } + return main; + }); } - return main; - } - - const rewardsList = await db.getSetMembers('rewards:list'); - const rewardData = await Promise.all(rewardsList.map(id => load(id))); - return rewardData.filter(Boolean); + const rewardsList = yield db.getSetMembers('rewards:list'); + const rewardData = yield Promise.all(rewardsList.map(id => load(id))); + return rewardData.filter(Boolean); + }); } - require('../promisify')(rewards); +exports.default = rewards; diff --git a/src/rewards/admin.ts b/src/rewards/admin.ts new file mode 100644 index 000000000..5921b1292 --- /dev/null +++ b/src/rewards/admin.ts @@ -0,0 +1,109 @@ +import * as plugins from '../plugins'; +import * as db from '../database'; // Import the 'db' module with proper type declarations +import * as utils from '../utils'; + +interface Reward { + id: string; + rewards: { + rewardName: string; + rewardAmount: number; + // Add other properties and their types as needed + }; + condition: string; + // Define other properties as needed +} + +interface RewardsData { + active: Reward[]; + conditions: any[]; // Define the actual type for conditions + conditionals: any[]; // Define the actual type for conditionals + rewards: Reward[]; +} + +const rewards: { + save: (data: Reward[]) => Promise; + delete: (data: Reward) => Promise; + get: () => Promise; +} = { + save: async function (data: Reward[]) { + async function save(data: Reward) { + if (!Object.keys(data.rewards).length) { + return; + } + const rewardsData = data.rewards; + delete data.rewards; + if (!parseInt(data.id, 10)) { + // Ensure that 'db' has the 'incrObjectField' method and type + if (typeof db.incrObjectField === 'function') { + data.id = await (db).incrObjectField('global', 'rewards:id'); // Type assertion for 'db' + } else { + throw new Error('db.incrObjectField is not a function'); + } + } + await rewards.delete(data); + await db.setAdd('rewards:list', data.id); + await db.setObject(`rewards:id:${data.id}`, data); + await db.setObject(`rewards:id:${data.id}:rewards`, rewardsData); + } + + await Promise.all(data.map(data => save(data))); + await saveConditions(data); + return data; + }, + + delete: async function (data: Reward) { + await Promise.all([ + db.setRemove('rewards:list', data.id), + db.delete(`rewards:id:${data.id}`), + db.delete(`rewards:id:${data.id}:rewards`), + ]); + }, + + get: async function () { + return await utils.promiseParallel({ + active: getActiveRewards(), + conditions: plugins.hooks.fire('filter:rewards.conditions', []), + conditionals: plugins.hooks.fire('filter:rewards.conditionals', []), + rewards: plugins.hooks.fire('filter:rewards.rewards', []), + }); + }, +}; + +async function saveConditions(data: Reward[]) { + const rewardsPerCondition: { [condition: string]: string[] } = {}; + await db.delete('conditions:active'); + const conditions: string[] = []; + + data.forEach((reward: Reward) => { + conditions.push(reward.condition); + rewardsPerCondition[reward.condition] = rewardsPerCondition[reward.condition] || []; + rewardsPerCondition[reward.condition].push(reward.id); + }); + + await db.setAdd('conditions:active', conditions); + + await Promise.all(Object.keys(rewardsPerCondition).map(c => db.setAdd(`condition:${c}:rewards`, rewardsPerCondition[c]))); +} + +async function getActiveRewards() { + async function load(id: string) { + const [main, rewards] = await Promise.all([ + db.getObject(`rewards:id:${id}`), + db.getObject(`rewards:id:${id}:rewards`), + ]); + if (main) { + main.disabled = main.disabled === 'true'; + main.rewards = rewards; + } + return main; + } + + const rewardsList = await db.getSetMembers('rewards:list'); + const rewardData = await Promise.all(rewardsList.map(id => load(id))); + return rewardData.filter(Boolean); +} + +require('../promisify')(rewards); + +export default rewards; + diff --git a/src/types/admin.js b/src/types/admin.js index c8ad2e549..cf5c9078e 100644 --- a/src/types/admin.js +++ b/src/types/admin.js @@ -1,2 +1,3 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); +'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/src/types/adminOther2.js b/src/types/adminOther2.js new file mode 100644 index 000000000..c8ad2e549 --- /dev/null +++ b/src/types/adminOther2.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/src/types/admin.ts b/src/types/adminOther2.ts similarity index 100% rename from src/types/admin.ts rename to src/types/adminOther2.ts From 28b0d730bc4dbb30f7c5c091cd7f21d628275caa Mon Sep 17 00:00:00 2001 From: aishaalsubaie Date: Wed, 30 Aug 2023 14:17:05 +0300 Subject: [PATCH 2/2] fixed translation --- src/rewards/admin.js | 187 ++++++++++++++++++++++--------------------- src/rewards/admin.ts | 172 +++++++++++++++++++-------------------- 2 files changed, 180 insertions(+), 179 deletions(-) diff --git a/src/rewards/admin.js b/src/rewards/admin.js index e6df153bf..599a71915 100644 --- a/src/rewards/admin.js +++ b/src/rewards/admin.js @@ -1,27 +1,4 @@ "use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; 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) { @@ -31,93 +8,117 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge 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 }); -const plugins = __importStar(require("../plugins")); -const db = __importStar(require("../database")); // Import the 'db' module with proper type declarations -const utils = __importStar(require("../utils")); -const rewards = { - save: function (data) { - return __awaiter(this, void 0, void 0, function* () { - function save(data) { - return __awaiter(this, void 0, void 0, function* () { - if (!Object.keys(data.rewards).length) { - return; - } - const rewardsData = data.rewards; - delete data.rewards; - if (!parseInt(data.id, 10)) { - // Ensure that 'db' has the 'incrObjectField' method and type - if (typeof db.incrObjectField === 'function') { - data.id = yield (db).incrObjectField('global', 'rewards:id'); // Type assertion for 'db' - } - else { - throw new Error('db.incrObjectField is not a function'); - } - } - yield rewards.delete(data); - yield db.setAdd('rewards:list', data.id); - yield db.setObject(`rewards:id:${data.id}`, data); - yield db.setObject(`rewards:id:${data.id}:rewards`, rewardsData); - }); - } - yield Promise.all(data.map(data => save(data))); - yield saveConditions(data); - return data; - }); - }, - delete: function (data) { - return __awaiter(this, void 0, void 0, function* () { - yield Promise.all([ - db.setRemove('rewards:list', data.id), - db.delete(`rewards:id:${data.id}`), - db.delete(`rewards:id:${data.id}:rewards`), - ]); - }); - }, - get: function () { - return __awaiter(this, void 0, void 0, function* () { - return yield utils.promiseParallel({ - active: getActiveRewards(), - conditions: plugins.hooks.fire('filter:rewards.conditions', []), - conditionals: plugins.hooks.fire('filter:rewards.conditionals', []), - rewards: plugins.hooks.fire('filter:rewards.rewards', []), +exports.get = exports.save = exports._delete = void 0; +const plugins_1 = __importDefault(require("../plugins")); +const database_1 = __importDefault(require("../database")); +const utils_1 = __importDefault(require("../utils")); +function _delete(data) { + return __awaiter(this, void 0, void 0, function* () { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield database_1.default.setRemove('rewards:list', data.id); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield database_1.default.delete(`rewards:id:${data.id}`); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield database_1.default.delete(`rewards:id:${data.id}:rewards`); + }); +} +exports._delete = _delete; +function getActiveRewards() { + return __awaiter(this, void 0, void 0, function* () { + function load(id) { + return __awaiter(this, void 0, void 0, function* () { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const main = yield database_1.default.getObject(`rewards:id:${id}`); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const rewards = yield database_1.default.getObject(`rewards:id:${id}:rewards`); + if (main) { + main.disabled = main.disabled === true; + main.rewards = rewards; + } + return main; }); - }); - }, -}; + } + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const rewardsList = yield database_1.default.getSetMembers('rewards:list'); + const rewardData = yield Promise.all(rewardsList.map(id => load(id))); + return rewardData.filter(Boolean); + }); +} function saveConditions(data) { return __awaiter(this, void 0, void 0, function* () { const rewardsPerCondition = {}; - yield db.delete('conditions:active'); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield database_1.default.delete('conditions:active'); const conditions = []; data.forEach((reward) => { conditions.push(reward.condition); rewardsPerCondition[reward.condition] = rewardsPerCondition[reward.condition] || []; rewardsPerCondition[reward.condition].push(reward.id); }); - yield db.setAdd('conditions:active', conditions); - yield Promise.all(Object.keys(rewardsPerCondition).map(c => db.setAdd(`condition:${c}:rewards`, rewardsPerCondition[c]))); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield database_1.default.setAdd('conditions:active', conditions); + function dbAddSet(c) { + return __awaiter(this, void 0, void 0, function* () { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield database_1.default.setAdd(`condition:${c}:rewards`, rewardsPerCondition[c]); + }); + } + yield Promise.all(Object.keys(rewardsPerCondition).map(c => dbAddSet(c))); }); } -function getActiveRewards() { +function save(data) { return __awaiter(this, void 0, void 0, function* () { - function load(id) { + function save(data) { return __awaiter(this, void 0, void 0, function* () { - const [main, rewards] = yield Promise.all([ - db.getObject(`rewards:id:${id}`), - db.getObject(`rewards:id:${id}:rewards`), - ]); - if (main) { - main.disabled = main.disabled === 'true'; - main.rewards = rewards; + if (!Object.keys(data.rewards).length) { + return; } - return main; + const rewardsData = data.rewards; + delete data.rewards; + if (!parseInt(data.id, 10)) { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + data.id = (yield database_1.default.incrObjectField('global', 'rewards:id')); + } + yield _delete(data); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield database_1.default.setAdd('rewards:list', data.id); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield database_1.default.setObject(`rewards:id:${data.id}`, data); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield database_1.default.setObject(`rewards:id:${data.id}:rewards`, rewardsData); }); } - const rewardsList = yield db.getSetMembers('rewards:list'); - const rewardData = yield Promise.all(rewardsList.map(id => load(id))); - return rewardData.filter(Boolean); + yield Promise.all(data.map(data => save(data))); + yield saveConditions(data); + return data; + }); +} +exports.save = save; +function get() { + return __awaiter(this, void 0, void 0, function* () { + return yield utils_1.default.promiseParallel({ + active: getActiveRewards(), + conditions: plugins_1.default.hooks.fire('filter:rewards.conditions', []), + conditionals: plugins_1.default.hooks.fire('filter:rewards.conditionals', []), + rewards: plugins_1.default.hooks.fire('filter:rewards.rewards', []), + }); }); } -require('../promisify')(rewards); -exports.default = rewards; +exports.get = get; diff --git a/src/rewards/admin.ts b/src/rewards/admin.ts index 5921b1292..22b8f4ace 100644 --- a/src/rewards/admin.ts +++ b/src/rewards/admin.ts @@ -1,109 +1,109 @@ -import * as plugins from '../plugins'; -import * as db from '../database'; // Import the 'db' module with proper type declarations -import * as utils from '../utils'; +import plugins from '../plugins'; +import db from '../database'; +import utils from '../utils'; + +interface Data { + id: string + rewards: Reward[] + condition: string + disabled: boolean +} interface Reward { - id: string; - rewards: { - rewardName: string; - rewardAmount: number; - // Add other properties and their types as needed - }; - condition: string; - // Define other properties as needed + id: string } -interface RewardsData { - active: Reward[]; - conditions: any[]; // Define the actual type for conditions - conditionals: any[]; // Define the actual type for conditionals - rewards: Reward[]; +export async function _delete(data: Data) { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db.setRemove('rewards:list', data.id); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db.delete(`rewards:id:${data.id}`); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db.delete(`rewards:id:${data.id}:rewards`); } -const rewards: { - save: (data: Reward[]) => Promise; - delete: (data: Reward) => Promise; - get: () => Promise; -} = { - save: async function (data: Reward[]) { - async function save(data: Reward) { - if (!Object.keys(data.rewards).length) { - return; - } - const rewardsData = data.rewards; - delete data.rewards; - if (!parseInt(data.id, 10)) { - // Ensure that 'db' has the 'incrObjectField' method and type - if (typeof db.incrObjectField === 'function') { - data.id = await (db).incrObjectField('global', 'rewards:id'); // Type assertion for 'db' - } else { - throw new Error('db.incrObjectField is not a function'); - } - } - await rewards.delete(data); - await db.setAdd('rewards:list', data.id); - await db.setObject(`rewards:id:${data.id}`, data); - await db.setObject(`rewards:id:${data.id}:rewards`, rewardsData); +async function getActiveRewards() { + async function load(id: string) { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const main: Data = await db.getObject(`rewards:id:${id}`) as Data; + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const rewards: Reward[] = await db.getObject(`rewards:id:${id}:rewards`) as Reward[]; + if (main) { + main.disabled = main.disabled === true; + main.rewards = rewards; } + return main; + } + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const rewardsList: string[] = await db.getSetMembers('rewards:list') as string[]; + const rewardData = await Promise.all(rewardsList.map(id => load(id))); + return rewardData.filter(Boolean); +} - await Promise.all(data.map(data => save(data))); - await saveConditions(data); - return data; - }, - - delete: async function (data: Reward) { - await Promise.all([ - db.setRemove('rewards:list', data.id), - db.delete(`rewards:id:${data.id}`), - db.delete(`rewards:id:${data.id}:rewards`), - ]); - }, - - get: async function () { - return await utils.promiseParallel({ - active: getActiveRewards(), - conditions: plugins.hooks.fire('filter:rewards.conditions', []), - conditionals: plugins.hooks.fire('filter:rewards.conditionals', []), - rewards: plugins.hooks.fire('filter:rewards.rewards', []), - }); - }, -}; - -async function saveConditions(data: Reward[]) { - const rewardsPerCondition: { [condition: string]: string[] } = {}; +async function saveConditions(data: Data[]) { + const rewardsPerCondition: Record = {}; + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call await db.delete('conditions:active'); - const conditions: string[] = []; + const conditions = []; - data.forEach((reward: Reward) => { + data.forEach((reward) => { conditions.push(reward.condition); rewardsPerCondition[reward.condition] = rewardsPerCondition[reward.condition] || []; rewardsPerCondition[reward.condition].push(reward.id); }); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call await db.setAdd('conditions:active', conditions); - - await Promise.all(Object.keys(rewardsPerCondition).map(c => db.setAdd(`condition:${c}:rewards`, rewardsPerCondition[c]))); + async function dbAddSet(c: string) { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db.setAdd(`condition:${c}:rewards`, rewardsPerCondition[c]); + } + await Promise.all(Object.keys(rewardsPerCondition).map(c => dbAddSet(c))); } -async function getActiveRewards() { - async function load(id: string) { - const [main, rewards] = await Promise.all([ - db.getObject(`rewards:id:${id}`), - db.getObject(`rewards:id:${id}:rewards`), - ]); - if (main) { - main.disabled = main.disabled === 'true'; - main.rewards = rewards; +export async function save(data: Data[]) { + async function save(data: Data) { + if (!Object.keys(data.rewards).length) { + return; } - return main; + const rewardsData: Reward[] = data.rewards; + delete data.rewards; + if (!parseInt(data.id, 10)) { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + data.id = await db.incrObjectField('global', 'rewards:id') as string; + } + await _delete(data); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db.setAdd('rewards:list', data.id); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db.setObject(`rewards:id:${data.id}`, data); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db.setObject(`rewards:id:${data.id}:rewards`, rewardsData); } - const rewardsList = await db.getSetMembers('rewards:list'); - const rewardData = await Promise.all(rewardsList.map(id => load(id))); - return rewardData.filter(Boolean); + await Promise.all(data.map(data => save(data))); + await saveConditions(data); + return data; } -require('../promisify')(rewards); - -export default rewards; - +export async function get() { + return await utils.promiseParallel({ + active: getActiveRewards(), + conditions: plugins.hooks.fire('filter:rewards.conditions', []), + conditionals: plugins.hooks.fire('filter:rewards.conditionals', []), + rewards: plugins.hooks.fire('filter:rewards.rewards', []), + }); +}