Skip to content

Commit

Permalink
Fix bugs & Fix Semistandard and Fix ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
n1klaus committed May 5, 2023
1 parent 33d6d88 commit 93bb822
Show file tree
Hide file tree
Showing 50 changed files with 559 additions and 522 deletions.
8 changes: 8 additions & 0 deletions .hintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"development"
],
"hints": {
"no-inline-styles": "off"
}
}
6 changes: 5 additions & 1 deletion STATE.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
- implement searchChannels, profile, searchVideos, video page
- implement searchChannels, profile, searchVideos, video page

- fix ui
- fix useeffect
- add tests
6 changes: 6 additions & 0 deletions backend/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
./.eslintrc.cjs
*.config.cjs
*.config.mjs
*.config.js
*.config.ts
node_modules/
25 changes: 19 additions & 6 deletions backend/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
extends: [
'airbnb-base',
'airbnb-typescript/base',
'plugin:jest/all',
'plugin:jest/recommended',
'plugin:@typescript-eslint/recommended'
],
globals: {
Expand All @@ -19,25 +19,38 @@ module.exports = {
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
project: './tsconfig.json'
project: './tsconfig.json',
tsconfigRootDir: __dirname
},
plugins: ['jest', '@typescript-eslint'],
settings: {
'import/extensions': ['.js', '.jsx', '.ts', '.tsx'],
'import/extensions': ['.js', '.ts'],
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx']
'@typescript-eslint/parser': ['.ts']
},
'import/resolver': {
typescript: {
directory: './tsconfig.json'
},
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx']
extensions: ['.js', '.ts']
}
}
},
rules: {
quotes: [2, "single", { "avoidEscape": true }],
quotes: [2, 'single', { avoidEscape: true }],
semi: [2, 'always'],
'no-underscore-dangle': 'off',
'@typescript-eslint/no-explicit-any': ['off'],
'import/extensions': [
2,
'ignorePackages',
{
'': 'never',
js: 'never',
ts: 'never'
}
],
'no-console': 'off',
'no-shadow': 'off',
'no-restricted-syntax': ['error', 'LabeledStatement', 'WithStatement']
Expand Down
11 changes: 9 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
"license": "MIT",
"scripts": {
"start": "ts-node-dev --respawn --transpile-only src/server.ts",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
"check-lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
"check-lint": "eslint . --ext .js,.jsx,.ts,.tsx ",
"fmt": "prettier --write .",
"check-fmt": "prettier --check .",
"semifix": "semistandard --fix .",
"check-semi": "semistandard .",
"start-server": "nodemon --exec babel-node --presets @babel/preset-env src/server.ts",
"start-worker": "nodemon --exec babel-node --presets @babel/preset-env src/worker.ts",
"test-types": "tsc --incremental --noEmit",
Expand Down Expand Up @@ -42,18 +46,21 @@
"eslint-config-airbnb-base-typescript": "^1.1.0",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-airbnb-typescript-base": "^4.0.2",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.2.1",
"jest": "^29.5.0",
"mocha": "^10.2.0",
"morgan": "^1.10.0",
"nodemon": "^2.0.22",
"prettier": "^2.8.8",
"semistandard": "^16.0.1",
"sinon": "^15.0.3",
"ts-node-dev": "^2.0.0",
"typescript": "^4.9.5"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.327.0",
"@aws-sdk/lib-storage": "^3.312.0",
"aws-sdk": "^2.1359.0",
"axios": "^1.2.2",
Expand Down
36 changes: 22 additions & 14 deletions backend/src/controllers/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@ import Comment from '../models/comment.model';

export default class AppController {
static getStatus(req: Request, resp: Response, next: NextFunction) {
resp.status(200).json({
// redis: redisClient.isAlive(),
db: dbClient.isAlive(),
});
try {
return resp.status(200).json({
redis: redisClient.isAlive(),
db: dbClient.isAlive(),
});
} catch (err) {
return next(err);
}
}

static async getStats(req: Request, resp: Response, next: NextFunction) {
const userCount = await User.countDocuments();
const videoCount = await Video.countDocuments();
const channelCount = await Channel.countDocuments();
const commentCount = await Comment.countDocuments();
resp.status(200).json({
users: userCount,
videos: videoCount,
comments: commentCount,
channels: channelCount,
});
try {
const userCount = await User.countDocuments();
const videoCount = await Video.countDocuments();
const channelCount = await Channel.countDocuments();
const commentCount = await Comment.countDocuments();
return resp.status(200).json({
users: userCount,
videos: videoCount,
comments: commentCount,
channels: channelCount,
});
} catch (err) {
return next(err);
}
}
}
42 changes: 27 additions & 15 deletions backend/src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
getGoogleOauthToken,
getGoogleUser,
} from '../services/session.service';
import ChannelController from './channel.controller';
import User from '../models/user.model';
import createError from '../error';
import { ChannelModel as Channel } from '../models/channel.model';
Expand All @@ -26,7 +25,7 @@ const DEFAULT_AVATAR = process.env.DEFAULT_AVATAR as unknown as string;

class AuthController {
static async registerHandler(
req: Request<{}, {}, CreateUserInput>,
req: Request<object, object, CreateUserInput>,
resp: Response,
next: NextFunction,
) {
Expand Down Expand Up @@ -62,7 +61,7 @@ class AuthController {
resp.cookie('auth_token', token, {
expires: new Date(Date.now() + TOKEN_EXPIRES_IN * 60 * 1000),
});
resp.status(201).json({
return resp.status(201).json({
status: 'success',
data: {
user: exclude(user, ['password']),
Expand All @@ -75,12 +74,12 @@ class AuthController {
message: 'Email already exist',
});
}
next(err);
return next(err);
}
}

static async loginHandler(
req: Request<{}, {}, LoginUserInput>,
req: Request<object, object, LoginUserInput>,
resp: Response,
next: NextFunction,
) {
Expand Down Expand Up @@ -109,11 +108,11 @@ class AuthController {
expires: new Date(Date.now() + TOKEN_EXPIRES_IN * 60 * 1000),
});

resp.status(200).json({
return resp.status(200).json({
status: 'success',
});
} catch (err: any) {
next(err);
return next(err);
}
}

Expand Down Expand Up @@ -165,11 +164,11 @@ class AuthController {
expires: new Date(Date.now() + TOKEN_EXPIRES_IN * 60 * 1000),
});

res.status(200).json({
return res.status(200).json({
status: 'success',
});
} catch (err: any) {
next(err);
return next(err);
}
}

Expand All @@ -188,16 +187,16 @@ class AuthController {
});
}

const { id_token, access_token } = await getGoogleOauthToken({ code });
const { idToken, accessToken } = await getGoogleOauthToken({ code });

const {
name, verified_email, email, picture,
verifiedEmail, email, picture,
} = await getGoogleUser({
id_token,
access_token,
idToken,
accessToken,
});

if (!verified_email) {
if (!verifiedEmail) {
return res.status(403).json({
status: 'fail',
message: 'Google account not verified',
Expand All @@ -223,6 +222,19 @@ class AuthController {

if (!user) return res.redirect(`${FRONTEND_ENDPOINT}/oauth/error`);

// create default channel if one is not found
if (user.channels.length === 0) {
const channel = new Channel({
name: randomUUID(),
userId: user._id,
imgUrl: user.avatar,
});
if (!channel) return res.redirect(`${FRONTEND_ENDPOINT}/oauth/error`);
await channel.save();
user.channels.push(channel);
await user.save();
}

const TOKEN_EXPIRES_IN = process.env
.TOKEN_EXPIRES_IN as unknown as number;
const TOKEN_SECRET = process.env.JWT_SECRET as unknown as string;
Expand All @@ -234,7 +246,7 @@ class AuthController {
expires: new Date(Date.now() + TOKEN_EXPIRES_IN * 60 * 1000),
});

res.redirect(`${FRONTEND_ENDPOINT}${pathUrl}`);
return res.redirect(`${FRONTEND_ENDPOINT}${pathUrl}`);
} catch (err: any) {
console.log('Failed to authorize Google User', err);
return res.redirect(`${FRONTEND_ENDPOINT}/oauth/error`);
Expand Down
32 changes: 16 additions & 16 deletions backend/src/controllers/channel.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Video from '../models/video.model';
import { ChannelModel as Channel } from '../models/channel.model';
import createError from '../error';

const DEFAULT_AVATAR = process.env.DEFAULT_AVATAR as unknown as string;
// const DEFAULT_AVATAR = process.env.DEFAULT_AVATAR as unknown as string;
const DEFAULT_THUMBNAIL = process.env.DEFAULT_THUMBNAIL as unknown as string;

class ChannelController {
Expand All @@ -28,17 +28,17 @@ class ChannelController {
await channel.save();
user.channels.push(channel);
await user.save();
resp.status(200).json(channel);
return resp.status(200).json(channel);
} catch (err) {
next(err);
return next(err);
}
}

static async updateChannel(req: Request, resp: Response, next: NextFunction) {
const userId = String(resp.locals.user._id);
const user = await User.findById(userId);
if (!user) {
return resp.status(401).json({ error: 'User not found' });
return next(createError(401, 'User not found!'));
}
const ownedChannel = user.channels.filter(
(channel) => channel?._id?.toString() === req.params.id,
Expand All @@ -52,9 +52,9 @@ class ChannelController {
},
{ new: true },
);
resp.status(200).json(updatedChannel);
return resp.status(200).json(updatedChannel);
} catch (err) {
next(err);
return next(err);
}
} else {
return next(createError(403, 'You can only update your channel!'));
Expand All @@ -70,9 +70,9 @@ class ChannelController {
if (channel.userId === userId) {
try {
await Channel.findByIdAndDelete(req.params.id);
resp.status(200).json('Channel has been deleted.');
return resp.status(200).json('Channel has been deleted.');
} catch (err) {
next(err);
return next(err);
}
} else {
return next(createError(403, 'You can only delete your channel!'));
Expand Down Expand Up @@ -106,9 +106,9 @@ class ChannelController {
const savedVideo = await newVideo.save();
channel.videos.push(savedVideo.id);
channel.save();
resp.status(201).json(savedVideo);
return resp.status(201).json(savedVideo);
} catch (err) {
next(err);
return next(err);
}
}

Expand All @@ -118,9 +118,9 @@ class ChannelController {
if (!channels) {
return next(createError(404, 'No Channels found!'));
}
resp.status(200).json(channels);
return resp.status(200).json(channels);
} catch (err) {
next(err);
return next(err);
}
}

Expand All @@ -130,9 +130,9 @@ class ChannelController {
if (!channel) {
return next(createError(404, 'Channel not found!'));
}
resp.status(200).json(channel);
return resp.status(200).json(channel);
} catch (err) {
next(err);
return next(err);
}
}

Expand All @@ -143,9 +143,9 @@ class ChannelController {
name: { $regex: query, $options: 'i' },
}).limit(20);
if (!channels) return next(createError(404, 'Channels not found!'));
resp.status(200).json(channels);
return resp.status(200).json(channels);
} catch (err) {
next(err);
return next(err);
}
}
}
Expand Down
Loading

0 comments on commit 93bb822

Please sign in to comment.