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

280 update validation for sdk #281

Merged
merged 7 commits into from
Nov 15, 2024
Merged
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
1 change: 1 addition & 0 deletions docker-compose-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ services:
AWS_SECRET_ACCESS_KEY: ${filebase_secret_access_key}
FILEBASE_BUCKET: ${filebase_bucket}
YOTI_CLIENT_ID: ${yoti_client_id}
RATE_LIMIT_MINUTE: ${rate_limit_minute}
ports:
- 3000:3000
- 5000:5000
Expand Down
1 change: 1 addition & 0 deletions docker-compose-web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ services:
AWS_SECRET_ACCESS_KEY: ${filebase_secret_access_key}
FILEBASE_BUCKET: ${filebase_bucket}
YOTI_CLIENT_ID: ${yoti_client_id}
RATE_LIMIT_MINUTE: ${rate_limit_minute}
ports:
- 3000:3000
- 5000:5000
Expand Down
1 change: 1 addition & 0 deletions docker-compose.local-new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ services:
AWS_SECRET_ACCESS_KEY: ${filebase_secret_access_key}
FILEBASE_BUCKET: ${filebase_bucket}
YOTI_CLIENT_ID: ${yoti_client_id}
RATE_LIMIT_MINUTE: ${rate_limit_minute}
ports:
- 3000:3000
- 5000:5000
Expand Down
1 change: 1 addition & 0 deletions docker-compose.local-ssl-with_certbot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ services:
AWS_SECRET_ACCESS_KEY: ${filebase_secret_access_key}
FILEBASE_BUCKET: ${filebase_bucket}
YOTI_CLIENT_ID: ${yoti_client_id}
RATE_LIMIT_MINUTE: ${rate_limit_minute}
ports:
- 3000:3000
- 5000:5000
Expand Down
1 change: 1 addition & 0 deletions docker-compose.local-ssl-without_certbot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ services:
AWS_SECRET_ACCESS_KEY: ${filebase_secret_access_key}
FILEBASE_BUCKET: ${filebase_bucket}
YOTI_CLIENT_ID: ${yoti_client_id}
RATE_LIMIT_MINUTE: ${rate_limit_minute}
ports:
- 3000:3000
- 5000:5000
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ services:
AWS_SECRET_ACCESS_KEY: ${filebase_secret_access_key}
FILEBASE_BUCKET: ${filebase_bucket}
YOTI_CLIENT_ID: ${yoti_client_id}
RATE_LIMIT_MINUTE: ${rate_limit_minute}
ports:
- 3000:3000
- 5000:5000
Expand Down
18 changes: 13 additions & 5 deletions rair-node/bin/api/files/files.Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ const { File } = require('../../models');
const router = express.Router();

router.patch(
'/update/:mediaId',
'/update/:id',
requireUserSession,
validation(['removeMedia'], 'params'),
validation(['fileId'], 'params'),
validation(['updateMedia'], 'body'),
isOwner(File),
updateMedia,
);

router.delete(
'/remove/:mediaId',
'/remove/:id',
requireUserSession,
validation(['removeMedia'], 'params'),
validation(['fileId'], 'params'),
isOwner(File),
deleteMedia,
);
Expand Down Expand Up @@ -70,15 +70,23 @@ router.get(
validation(['dbId'], 'params'),
getFilesForToken,
);
router.get('/:id/unlocks', getFileAndOffer);
router.get(
'/:id/unlocks',
validation(['fileId'], 'params'),
getFileAndOffer,
);
router.post(
'/:id/unlocks',
validation(['fileId'], 'params'),
validation(['offerArray'], 'body'),
requireUserSession,
isFileOwner,
connectFileAndOffer,
);
router.delete(
'/:id/unlocks',
validation(['fileId'], 'params'),
validation(['singleOffer'], 'body'),
requireUserSession,
isFileOwner,
removeFileAndOffer,
Expand Down
24 changes: 12 additions & 12 deletions rair-node/bin/api/files/files.Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,32 +176,32 @@ module.exports = {
},
deleteMedia: async (req, res, next) => {
try {
const { mediaId } = req.params;
const { id } = req.params;

const fileData = await File.findOne({ _id: mediaId });
const fileData = await File.findOne({ _id: id });

let deleteResponse;
if (!fileData.storage) {
log.error(`Can't tell where media ID ${mediaId} is stored, will not unpin/delete from storage, just from DB`);
log.error(`Can't tell where media ID ${id} is stored, will not unpin/delete from storage, just from DB`);
deleteResponse = { success: true };
} else {
switch (fileData.storage) {
case 'gcp':
deleteResponse = await gcp.removeFile(config.gcp.videoBucketName, mediaId);
deleteResponse = await gcp.removeFile(config.gcp.videoBucketName, id);
break;
case 'ipfs':
deleteResponse = await removePin(mediaId);
deleteResponse = await removePin(id);
break;
default:
log.error(`Unknown storage type for media ID ${mediaId} : ${fileData.storage}`);
log.error(`Unknown storage type for media ID ${id} : ${fileData.storage}`);
break;
}
}

if (deleteResponse.success) {
await File.deleteOne({ _id: mediaId });
await Unlock.deleteMany({ file: mediaId });
log.info(`File with ID: ${mediaId}, was removed from DB.`);
await File.deleteOne({ _id: id });
await Unlock.deleteMany({ file: id });
log.info(`File with ID: ${id}, was removed from DB.`);
res.json({
success: true,
});
Expand All @@ -218,7 +218,7 @@ module.exports = {
},
updateMedia: async (req, res, next) => {
try {
const { mediaId } = req.params;
const { id } = req.params;

// eslint-disable-next-line no-unused-vars
const { _id, ...cleanBody } = req.body;
Expand All @@ -229,15 +229,15 @@ module.exports = {
req.body = bodyForNonAdmins;
}

const updateRes = await File.updateOne({ _id: mediaId }, cleanBody);
const updateRes = await File.updateOne({ _id: id }, cleanBody);

if (!updateRes.acknowledged) {
return res.json({ success: false, message: 'An error has ocurred' });
}
if (updateRes.matchedCount === 1 && updateRes.modifiedCount === 0) {
return res.json({ success: false, message: 'Nothing to update' });
}
log.info(`File with ID: ${mediaId}, was updated on DB.`);
log.info(`File with ID: ${id}, was updated on DB.`);
return res.json({ success: true });
} catch (err) {
return next(err);
Expand Down
22 changes: 18 additions & 4 deletions rair-node/bin/api/users/users.Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,26 @@ exports.listUsers = async (req, res, next) => {
}
queriedFields[field] = 1;
});
const list = await User.find({}, queriedFields)
.skip(pageNum * itemsPerPage)
.limit(itemsPerPage);
const [result] = await User.aggregate([
{
$project: queriedFields,
},
{
$facet: {
list: [
{ $skip: pageNum * itemsPerPage },
{ $limit: itemsPerPage },
],
count: [
{ $count: 'total' },
],
},
},
]);
return res.json({
success: true,
data: list,
data: result.list,
totalCount: result?.count?.[0]?.total || 0,
});
} catch (err) {
return next(err);
Expand Down
4 changes: 1 addition & 3 deletions rair-node/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const cookieParser = require('cookie-parser');
const { nanoid } = require('nanoid');
const fs = require('fs');
const cors = require('cors');
const lusca = require('lusca');
const { createServer } = require('http');
const { Server } = require('socket.io');
const morgan = require('morgan');
Expand Down Expand Up @@ -55,12 +54,11 @@ async function main() {
},
});


const hls = await StartHLS();

const limiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
limit: 500,
limit: process.env.RATE_LIMIT_MINUTE || 500,
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
});
Expand Down
20 changes: 20 additions & 0 deletions rair-node/bin/schemas/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Joi = require('joi');
const { mongoId } = require('./reusableCustomTypes');

module.exports = {
updateMedia: () => ({
title: Joi.string().min(1).max(30),
description: Joi.string().min(1).max(300),
contract: Joi.string(),
product: Joi.string(),
offer: Joi.array().items(mongoId),
category: Joi.string().min(1),
demo: Joi.boolean(),
}),
offerArray: () => ({
offers: Joi.array().items(mongoId).required(),
}),
singleOffer: () => ({
offer: mongoId.required(),
}),
};
10 changes: 5 additions & 5 deletions rair-node/bin/schemas/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const admin = require('./admin');
const databaseSchemas = require('./databaseSchemas');
const addMedia = require('./addMedia');
const authentication = require('./authentication');
const createContract = require('./createContract');
Expand All @@ -7,8 +8,7 @@ const getChallenge = require('./getChallenge');
const getChallengeV2 = require('./getChallengeV2');
const filterAndSort = require('./filterAndSort');
const getToken = require('./getToken');
const removeMedia = require('./removeMedia');
const updateMedia = require('./updateMedia');
const { updateMedia, offerArray, singleOffer } = require('./files');
const stream = require('./stream');
const uploadVideo = require('./uploadVideo');
const uploadVideoFile = require('./uploadVideoFile');
Expand Down Expand Up @@ -70,7 +70,6 @@ module.exports = {
getChallenge,
filterAndSort,
getToken,
removeMedia,
stream,
uploadVideo,
uploadVideoFile,
Expand All @@ -85,6 +84,8 @@ module.exports = {

// Media files
updateMedia,
offerArray,
singleOffer,
analyticsParams,
analyticsQuery,

Expand Down Expand Up @@ -115,9 +116,8 @@ module.exports = {
// favorites
createFavoriteToken,

// V2 Validation
// Database schemas (using the Entity helper)
...require('./databaseSchemas'),
...databaseSchemas,
// Media schemas
validateMediaData,
addFileFromMediaService,
Expand Down
7 changes: 0 additions & 7 deletions rair-node/bin/schemas/removeMedia.js

This file was deleted.

11 changes: 0 additions & 11 deletions rair-node/bin/schemas/updateMedia.js

This file was deleted.

Loading