🐛 Fix itteration on the different types of services

This commit is contained in:
ajnart
2022-06-21 19:16:29 +02:00
parent c29d6f58dd
commit 45de715390
2 changed files with 80 additions and 80 deletions

View File

@@ -3,18 +3,17 @@ import { QBittorrent } from '@ctrl/qbittorrent';
import { NormalizedTorrent } from '@ctrl/shared-torrent'; import { NormalizedTorrent } from '@ctrl/shared-torrent';
import { Transmission } from '@ctrl/transmission'; import { Transmission } from '@ctrl/transmission';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiRequest, NextApiResponse } from 'next';
import { useConfig } from '../../../tools/state';
import { Config } from '../../../tools/types'; import { Config } from '../../../tools/types';
async function Post(req: NextApiRequest, res: NextApiResponse) { async function Post(req: NextApiRequest, res: NextApiResponse) {
// Get the type of service from the request url // Get the type of service from the request url
const torrents: NormalizedTorrent[] = []; const { config }: { config: Config } = useConfig();
const { config }: { config: Config } = req.body; const qBittorrentServices = config.services.filter((service) => service.type === 'qBittorrent');
const qBittorrentServices = config.services
.filter((service) => service.type === 'qBittorrent');
const delugeServices = config.services.filter((service) => service.type === 'Deluge'); const delugeServices = config.services.filter((service) => service.type === 'Deluge');
const transmissionServices = config.services const transmissionServices = config.services.filter((service) => service.type === 'Transmission');
.filter((service) => service.type === 'Transmission');
const torrents: NormalizedTorrent[] = [];
if (!qBittorrentServices && !delugeServices && !transmissionServices) { if (!qBittorrentServices && !delugeServices && !transmissionServices) {
return res.status(500).json({ return res.status(500).json({
@@ -23,7 +22,7 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
}); });
} }
if (qBittorrentServices) { if (qBittorrentServices) {
for (const service of qBittorrentServices) { qBittorrentServices.map(async (service) =>
torrents.push( torrents.push(
...( ...(
await new QBittorrent({ await new QBittorrent({
@@ -32,11 +31,11 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
password: service.password, password: service.password,
}).getAllData() }).getAllData()
).torrents ).torrents
)
); );
} }
}
if (delugeServices) { if (delugeServices) {
for (const service of delugeServices) { delugeServices.map(async (service) =>
torrents.push( torrents.push(
...( ...(
await new Deluge({ await new Deluge({
@@ -45,10 +44,11 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
}).getAllData() }).getAllData()
).torrents ).torrents
) )
} );
} }
if (transmissionServices) { if (transmissionServices) {
for (const service of transmissionServices) { // Map transmissionServices
transmissionServices.map(async (service) => {
torrents.push( torrents.push(
...( ...(
await new Transmission({ await new Transmission({
@@ -58,7 +58,7 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
}).getAllData() }).getAllData()
).torrents ).torrents
); );
} });
} }
res.status(200).json(torrents); res.status(200).json(torrents);
} }