Prechádzať zdrojové kódy

feat: finished youtube getApiRequest/GET_API_REQUEST implementation

Kristian Vos 2 rokov pred
rodič
commit
3f076808a6
2 zmenil súbory, kde vykonal 48 pridanie a 24 odobranie
  1. 6 1
      backend/logic/actions/youtube.js
  2. 42 23
      backend/logic/youtube.js

+ 6 - 1
backend/logic/actions/youtube.js

@@ -1,3 +1,5 @@
+import mongoose from "mongoose";
+
 import { isAdminRequired } from "./hooks";
 
 // eslint-disable-next-line
@@ -49,7 +51,10 @@ export default {
 	 * @returns {{status: string, data: object}}
 	 */
 	getApiRequest: isAdminRequired(function getApiRequest(session, apiRequestId, cb) {
-		YouTubeModule.runJob("GET_API_REQUEST", { apiRequestId }, this)
+		if (!mongoose.Types.ObjectId.isValid(apiRequestId))
+			return cb({ status: "error", message: "Api request id is not a valid ObjectId." });
+
+		return YouTubeModule.runJob("GET_API_REQUEST", { apiRequestId }, this)
 			.then(response => {
 				this.log(
 					"SUCCESS",

+ 42 - 23
backend/logic/youtube.js

@@ -907,34 +907,53 @@ class _YouTubeModule extends CoreClass {
 	GET_API_REQUEST(payload) {
 		return new Promise((resolve, reject) => {
 			const { apiRequestId } = payload;
-			// TODO validate apiRequestId
-			// TODO better error handling/waterfall
+			
+			async.waterfall(
+				[
+					next => {
+						YouTubeModule.youtubeApiRequestModel
+							.findOne({ _id: apiRequestId })
+							.exec(next);
+					},
 
-			YouTubeModule.youtubeApiRequestModel
-				.findOne({ _id: apiRequestId })
-				.exec((err, apiRequest) => {
-					if (err) reject(new Error("Couldn't load YouTube API requests."));
-					else {
-						CacheModule.runJob("HGET", {
-							table: "youtubeApiRequestParams",
-							key: apiRequestId.toString()
-						}).then(apiRequestParams => {
-							CacheModule.runJob("HGET", {
+					(apiRequest, next) => {
+						CacheModule.runJob(
+							"HGET",
+							{
+								table: "youtubeApiRequestParams",
+								key: apiRequestId.toString()
+							},
+							this
+						).then(apiRequestParams => {
+							next(null, {
+								...apiRequest._doc,
+								params: apiRequestParams
+							});
+						}
+						).catch(err => next(err));
+					},
+
+					(apiRequest, next) => {
+						CacheModule.runJob(
+							"HGET",
+							{
 								table: "youtubeApiRequestResults",
 								key: apiRequestId.toString()
-							}).then(apiRequestResults => {
-								resolve({
-									apiRequest: {
-										...apiRequest._doc,
-										params: apiRequestParams,
-										results: apiRequestResults
-									}
-								});
+							},
+							this
+						).then(apiRequestResults => {
+							next(null, {
+								...apiRequest,
+								results: apiRequestResults
 							});
-						});
+						}).catch(err => next(err));
 					}
-				});
-
+				],
+				(err, apiRequest) => {
+					if (err) reject(new Error(err));
+					else resolve({ apiRequest });
+				}
+			);
 		});
 	}
 }