Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translating src/rewards/admin from JS to TS #88

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added src/groups/.Rhistory
Empty file.
191 changes: 117 additions & 74 deletions src/rewards/admin.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,124 @@
'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);
}

await Promise.all(data.map(data => save(data)));
await saveConditions(data);
return data;
"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());
});
};

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`),
]);
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};

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', []),
Object.defineProperty(exports, "__esModule", { value: true });
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`);
});
};

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);
}
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);
});

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 saveConditions(data) {
return __awaiter(this, void 0, void 0, function* () {
const rewardsPerCondition = {};
// 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);
});
// 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]);
});
}
return main;
}

const rewardsList = await db.getSetMembers('rewards:list');
const rewardData = await Promise.all(rewardsList.map(id => load(id)));
return rewardData.filter(Boolean);
yield Promise.all(Object.keys(rewardsPerCondition).map(c => dbAddSet(c)));
});
}
function save(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)) {
// 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);
});
}
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.get = get;
109 changes: 109 additions & 0 deletions src/rewards/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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
}

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`);
}

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);
}

async function saveConditions(data: Data[]) {
const rewardsPerCondition: Record<string, 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.delete('conditions:active');
const conditions = [];

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);
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)));
}

export async function save(data: Data[]) {
async function save(data: Data) {
if (!Object.keys(data.rewards).length) {
return;
}
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);
}

await Promise.all(data.map(data => save(data)));
await saveConditions(data);
return data;
}

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', []),
});
}
5 changes: 3 additions & 2 deletions src/types/admin.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });
2 changes: 2 additions & 0 deletions src/types/adminOther2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
File renamed without changes.