Skip to content

Commit

Permalink
refactor(requests): cleaned up requests so they are faster and more r…
Browse files Browse the repository at this point in the history
…eadable
  • Loading branch information
ADRFranklin committed Apr 3, 2024
1 parent 4ae9a73 commit 3451038
Show file tree
Hide file tree
Showing 6 changed files with 457 additions and 203 deletions.
50 changes: 35 additions & 15 deletions pkg/api/src/api/routes/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Radarr from '@/services/downloaders/radarr';
import Sonarr from '@/services/downloaders/sonarr';
import Mailer from '@/services/mail/mailer';
import { getArchive } from '@/services/requests/archive';
import { getRequests } from '@/services/requests/display';
import { getAllUserRequests, getRequests } from '@/services/requests/display';
import ProcessRequest from '@/services/requests/process';

const listRequests = async (ctx: Context) => {
Expand All @@ -20,14 +20,26 @@ const listRequests = async (ctx: Context) => {
};

const getUserRequests = async (ctx: Context) => {
const userId = ctx.state.user.id;
if (!userId) {
ctx.status = StatusCodes.NOT_FOUND;
ctx.body = {};
try {
const userId = ctx.state.user.id;
if (!userId) {
ctx.error({
statusCode: StatusCodes.BAD_REQUEST,
code: 'USER_NOT_FOUND',
message: 'User not found',
});
return;
}
const requests = await getAllUserRequests(userId);
ctx.ok(requests);
} catch (err) {
logger.error(`Error getting user requests`, err);
ctx.error({
statusCode: StatusCodes.INTERNAL_SERVER_ERROR,
code: 'INTERNAL_SERVER_ERROR',
message: 'Internal Server Error',
});
}

ctx.status = StatusCodes.OK;
ctx.body = await getRequests(userId, false);
};

const getRequestMinified = async (ctx: Context) => {
Expand Down Expand Up @@ -67,8 +79,11 @@ const getRequestMinified = async (ctx: Context) => {
);
} catch (err) {
logger.error(`ROUTE: Error getting requests`, err);
ctx.status = StatusCodes.INTERNAL_SERVER_ERROR;
ctx.body = {};
ctx.error({
statusCode: StatusCodes.INTERNAL_SERVER_ERROR,
code: 'INTERNAL_SERVER_ERROR',
message: 'Internal Server Error',
});
return;
}

Expand All @@ -85,8 +100,11 @@ const addRequest = async (ctx: Context) => {
ctx.body = await new ProcessRequest(request, ctx.state.user).new();
} catch (err) {
logger.error(`ROUTE: Error adding request`, err);
ctx.status = StatusCodes.INTERNAL_SERVER_ERROR;
ctx.body = {};
ctx.error({
statusCode: StatusCodes.INTERNAL_SERVER_ERROR,
code: 'INTERNAL_SERVER_ERROR',
message: 'Internal Server Error',
});
}
};

Expand Down Expand Up @@ -211,9 +229,11 @@ const updateRequest = async (ctx: Context) => {
ctx.body = {};
} catch (err) {
logger.error(`ROUTE: Error updating requests`, err);

ctx.status = StatusCodes.INTERNAL_SERVER_ERROR;
ctx.body = {};
ctx.error({
statusCode: StatusCodes.INTERNAL_SERVER_ERROR,
code: 'INTERNAL_SERVER_ERROR',
message: 'Internal Server Error',
});
}
};

Expand Down
24 changes: 22 additions & 2 deletions pkg/api/src/models/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import mongoose from 'mongoose';
import { z } from 'zod';

const RequestSchema = new mongoose.Schema({
const RequestZodSchema = z.object({
requestId: z.string(),
type: z.string(),
title: z.string(),
thumb: z.string(),
imdb_id: z.string(),
tmdb_id: z.string(),
tvdb_id: z.string(),
users: z.array(z.string()),
sonarrId: z.array(z.string()),
radarrId: z.array(z.string()),
approved: z.boolean(),
manualStatus: z.number(),
pendingDefault: z.unknown(),
seasons: z.unknown(),
timeStamp: z.date(),
});
export type IRequest = z.infer<typeof RequestZodSchema>;

const RequestSchema = new mongoose.Schema<IRequest>({
requestId: String,
type: String,
title: String,
Expand All @@ -18,4 +38,4 @@ const RequestSchema = new mongoose.Schema({
timeStamp: Date,
});

export default mongoose.model('Request', RequestSchema);
export default mongoose.model<IRequest>('Request', RequestSchema);
Loading

0 comments on commit 3451038

Please sign in to comment.