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

Translated src/rewards/admin.js to typescript #79

Closed
wants to merge 1 commit into from
Closed
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
189 changes: 115 additions & 74 deletions src/rewards/admin.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,122 @@
'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;
};

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

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', []),
"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());
});
};

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 });
exports.getActiveRewards = exports.saveConditions = void 0;
// Note: ChatGPT is used for debugging and generating code snippets for this file
const plugins = require("../plugins");
const db = require("../database");
const utils = require("../utils");
const rewards = module.exports;
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 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
yield db.setAdd('conditions:active', conditions);
// 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 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`),
exports.saveConditions = saveConditions;
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)) {
// 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 db.incrObjectField('global', 'rewards:id'));
}
yield rewards.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 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
yield 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
yield db.setObject(`rewards:id:${data.id}:rewards`, rewardsData);
});
}
yield Promise.all(data.map(data => save(data)));
yield saveConditions(data);
return data;
});
};
rewards.delete = function (data) {
return __awaiter(this, void 0, void 0, function* () {
yield Promise.all([
// 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
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
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
db.delete(`rewards:id:${data.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([
// 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
db.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
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);
// 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 db.getSetMembers('rewards:list');
const rewardData = yield Promise.all(rewardsList.map(id => load(id)));
return rewardData.filter(Boolean);
});
}

require('../promisify')(rewards);
exports.getActiveRewards = getActiveRewards;
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', []),
});
});
};
// require('../promisify')(rewards);
133 changes: 133 additions & 0 deletions src/rewards/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Note: ChatGPT is used for debugging and generating code snippets for this file
import plugins = require('../plugins');
import db = require('../database');
import utils = require('../utils');

type Condition = string;

type Reward = {
id: string;
rewards: Record<string, string>;
condition: Condition;
disabled: string | boolean;
};

type ActiveRewardsData = {
active: Reward[];
conditions: Condition[];
conditionals: Record<string, string>[];
rewards: Record<string, string>[];
};

type RewardsModule = {
save: (data: Reward[]) => Promise<Reward[]>;
delete: (data: Reward) => Promise<void>;
get: () => Promise<ActiveRewardsData>;
};

const rewards: RewardsModule = module.exports as RewardsModule;

export async function saveConditions(data: Reward[]): Promise<void> {
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: Condition[] = [];

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

// 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 Promise.all(Object.keys(rewardsPerCondition).map(c => db.setAdd(`condition:${c}:rewards`, rewardsPerCondition[c]) as void));
}

rewards.save = async function (data: Reward[]): Promise<Reward[]> {
async function save(data: Reward): Promise<void> {
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 = await db.incrObjectField('global', 'rewards:id') as string;
}

await rewards.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;
};

rewards.delete = async function (data: Reward): Promise<void> {
await Promise.all([
// 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
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
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
db.delete(`rewards:id:${data.id}:rewards`),
]);
};


export async function getActiveRewards(): Promise<Reward[]> {
async function load(id: string): Promise<Reward | null> {
const [main, rewards]: [Reward | null, Record<string, string> | null] = await Promise.all([
// 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
db.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
db.getObject(`rewards:id:${id}:rewards`),
]) as [Reward | null, Record<string, string> | null];

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: (Reward | null)[] = await Promise.all(rewardsList.map(id => load(id)));
return rewardData.filter(Boolean);
}


rewards.get = async function (): Promise<ActiveRewardsData> {
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', []),
});
};


// require('../promisify')(rewards);
Loading