Skip to content

Commit

Permalink
Merge pull request #27 from siinghd/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
siinghd authored Dec 31, 2023
2 parents 674a0d3 + 50819bd commit 3191e28
Show file tree
Hide file tree
Showing 19 changed files with 243 additions and 108 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ jobs:
echo "DOMAIN_NAME=${{ env.DEV_DOMAIN_NAME }}" >> $GITHUB_ENV
echo "DOTENV_KEY=${{ env.DEV_DOTENV_KEY }}" >> $GITHUB_ENV
echo "${{ secrets.DEV_BACKEND_ENV}}" > .env
echo "${{ secrets.DEV_BACKEND_PM2_DEV}}" > pm2.dev.config.cjs
- name: Set environment variables for Prod
if: github.ref != 'refs/heads/dev'
run: |
Expand Down Expand Up @@ -112,13 +113,13 @@ jobs:
SITE_PATH="/home/${{ env.SERVER_USER }}/sites/${{ env.PROJECT_NAME }}/${{ env.ENVIRONMENT }}/${{ env.WORKING_DIRECTORY }}"
cd $SITE_PATH
bun install
bun build index.ts --outdir ./out --target bun
~/.bun/bin/bun install
~/.bun/bin/bun build index.ts --outdir ./out --target bun
pm2 delete ${{ env.PROJECT_NAME }}_${{ env.ENVIRONMENT }}_${{ env.PROJECT_TYPE }}
if [ "${{ env.ENVIRONMENT }}" = "dev" ]; then
pm2 start pm2.dev.config.js
else
pm2 start pm2.prod.config.js
fi
~/.bun/bin/bun run start-pm2-dev
else
~/.bun/bin/bun run start-pm2-prod
fi
ansible-playbook /home/${{ env.SERVER_USER }}/ansible/set_nginx_conf.ansible.yml -e "ansible_become_pass=${{ env.SERVER_PASSWORD }} env_type=${{ env.ENVIRONMENT }} project_name=${{ env.PROJECT_NAME }} project_type=${{ env.PROJECT_TYPE }} app_port=${{ env.APP_PORT }} working_directory=${{ env.WORKING_DIRECTORY }} server_name=${{ env.DOMAIN_NAME }} enable_rate_limiting=${{env.ENABLE_RATE_LIMITING}} rate_burst=${{env.RATE_BURST}} enable_cache=${{env.ENABLE_CACHE}} enable_cache_control=${{env.ENABLE_CACHE_CONTROL}}"
2 changes: 1 addition & 1 deletion .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ jobs:
cd $SITE_PATH
pnpm install
pnpm run build
pm2 delete ${{ env.PROJECT_NAME }}_${{ env.ENVIRONMENT }}_${{ env.PROJECT_TYPE }}
if [ "${{ env.ENVIRONMENT }}" = "dev" ]; then
pnpm run start-pm2-dev
else
Expand Down
6 changes: 5 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "qat-backend",
"module": "index.ts",
"type": "module",
"scripts": {
"start-pm2-dev": "pm2 restart pm2.dev.config.cjs --update-env",
"start-pm2-prod": "pm2 restart pm2.prod.config.cjs --update-env"
},
"devDependencies": {
"@types/jsonwebtoken": "^9.0.5",
"@types/socket.io": "^3.0.2",
Expand All @@ -15,4 +19,4 @@
"mongoose": "^8.0.3",
"socket.io": "^4.7.2"
}
}
}
19 changes: 19 additions & 0 deletions backend/pm2.dev.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
apps: [
{
name: 'qat_dev_backend', // Name of the TypeScript application
script: './out/index.js', // Script to be executed
// args: './out/index.js', // 'start' for running in production mode
watch: false, // Enable watching of file changes (set to false in production)
env: {
NODE_ENV: 'production', // Set NODE_ENV for production
PORT: 4002, // Application's port
},
interpreter: '~/.bun/bin/bun',
// exec_mode: 'cluster', // Enable cluster mode for load balancing
// instances: 'max', // Use 'max' to utilize all available cores
autorestart: true, // Automatically restart if the app crashes
max_memory_restart: '4G', // Restart if memory limit is reached
},
],
};
18 changes: 0 additions & 18 deletions backend/pm2.dev.config.js

This file was deleted.

19 changes: 19 additions & 0 deletions backend/pm2.prod.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
apps: [
{
name: 'qat_prod_backend', // Name of the TypeScript application
script: 'index.ts', // Script to be executed
// args: './out/index.js', // 'start' for running in production mode
watch: false, // Enable watching of file changes (set to false in production)
env: {
NODE_ENV: 'production', // Set NODE_ENV for production
PORT: 4003, // Application's port
},
interpreter: '~/.bun/bin/bun',
// exec_mode: 'cluster', // Enable cluster mode for load balancing
// instances: 'max', // Use 'max' to utilize all available cores
autorestart: true, // Automatically restart if the app crashes
max_memory_restart: '4G', // Restart if memory limit is reached
},
],
};
18 changes: 0 additions & 18 deletions backend/pm2.prod.config.js

This file was deleted.

25 changes: 23 additions & 2 deletions frontend/actions/liveChatSession/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use server';

import { auth } from '@/auth';
import { auth, signOut } from '@/auth';
import {
InputTypeCreate,
InputTypeDelete,
Expand All @@ -24,13 +24,20 @@ const createLiveSessionHandler = async (
): Promise<ReturnTypeCreate> => {
const session = await auth();

if (!session || (!session.user.id && session.user.role !== Roles.admin)) {
if (!session || !session.user.id || session.user.role !== Roles.admin) {
return { error: 'Unauthorized or insufficient permissions' };
}

const { title } = data;

try {
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
const liveSessionData = {
title,
date: new Date(),
Expand Down Expand Up @@ -61,6 +68,13 @@ const updateLiveSessionHandler = async (
const { sessionId, title, isActive } = data;

try {
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
const updatedLiveSession = await prisma.liveChatSession.update({
where: { id: sessionId },
data: { title, isActive },
Expand All @@ -86,6 +100,13 @@ const deleteLiveSessionHandler = async (
const { sessionId } = data;

try {
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
await prisma.liveChatSession.delete({
where: { id: sessionId },
});
Expand Down
24 changes: 23 additions & 1 deletion frontend/actions/message/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from './schema';
import { createSafeAction } from '@/lib/create-safe-action';
import { Session } from 'next-auth/types';
import { auth } from '@/auth';
import { auth, signOut } from '@/auth';
import { Roles } from '@/types';

export const fetchMessagesFromDatabase = async (
Expand Down Expand Up @@ -56,6 +56,14 @@ const createMessageHandler = async (
const { content, sessionId } = data;

try {
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
return { error: 'User not found.' };
}
const message = await prisma.message.create({
data: { content, authorId: session.user.id!, sessionId },
});
Expand All @@ -77,6 +85,13 @@ const updateMessageHandler = async (
const { messageId, content } = data;

try {
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
const existingMessage = await prisma.message.findUnique({
where: { id: messageId },
});
Expand Down Expand Up @@ -114,6 +129,13 @@ const deleteMessageHandler = async (
const { messageId } = data;

try {
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
const message = await prisma.message.findUnique({
where: { id: messageId },
});
Expand Down
9 changes: 8 additions & 1 deletion frontend/actions/messageVote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { createSafeAction } from '@/lib/create-safe-action';
import { MessageVoteSchema } from './schema';
import prisma from '@/PrismaClientSingleton';
import { auth } from '@/auth';
import { auth, signOut } from '@/auth';
import { MessageVoteUpdateType, ReturnTypeMessageVoteUpdate } from './types';

const handleMessageVote = async (
Expand All @@ -26,6 +26,13 @@ const handleMessageVote = async (
}

try {
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
await prisma.$transaction(async prisma => {
const existingVote = await prisma.messageVote.findFirst({
where: {
Expand Down
21 changes: 20 additions & 1 deletion frontend/actions/question/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use server';

import { auth } from '@/auth';
import { auth, signOut } from '@/auth';
import {
DeleteTypeQuestion,
InputTypeCreate,
Expand Down Expand Up @@ -37,6 +37,13 @@ const createQuestionHandler = async (
let slug = generateHandle(title);

try {
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
// Check if slug already exists
const existingQuestion = await prisma.question.findFirst({
where: { slug },
Expand Down Expand Up @@ -80,7 +87,13 @@ const updateQuestionHandler = async (
}

const { title, content, tags, questionId } = data;
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
// Check if the user is the author of the question
const existingQuestion = await prisma.question.findUnique({
where: { id: questionId },
Expand Down Expand Up @@ -142,7 +155,13 @@ const deleteQuestionHandler = async (
if (!session || !session.user.id) {
return { error: 'Unauthorized' };
}
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
const { questionId } = data;

const question = await prisma.question.findUnique({
Expand Down
9 changes: 8 additions & 1 deletion frontend/actions/sessionParticipant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ReturnTypeSessionParticipantAdd,
SessionParticipantAddType,
} from './types';
import { auth } from '@/auth';
import { auth, signOut } from '@/auth';

const handleAddParticipant = async (
data: SessionParticipantAddType
Expand All @@ -23,6 +23,13 @@ const handleAddParticipant = async (
const { userId, sessionId } = data;

try {
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
const existingParticipant = await prisma.sessionParticipant.findFirst({
where: {
userId: userId,
Expand Down
9 changes: 8 additions & 1 deletion frontend/actions/vote/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use server';

import { auth } from '@/auth';
import { auth, signOut } from '@/auth';
import { ReturnTypeVoteUpdate, VoteUpdateType } from './types';
import { revalidatePath } from 'next/cache';
import prisma from '@/PrismaClientSingleton';
Expand Down Expand Up @@ -36,6 +36,13 @@ const handleVote = async (
const typeId = questionId || answerId;

try {
const userExists = await prisma.user.findUnique({
where: { id: session.user.id },
});

if (!userExists) {
await signOut();
}
await prisma.$transaction(async prisma => {
const existingVote = await prisma.vote.findFirst({
where: {
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import NavBar from '@/components/navbar';
const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
title: 'Q&A for your community',
description: 'Q&A for your community, people can post questions and answers',
};

export default function RootLayout({
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/livechat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NewLiveChatSession } from '@/components/new-chat-session';
import Pagination from '@/components/pagination';
import { Button } from '@/components/ui/button';
import { paginationData } from '@/lib/functions';
import { QueryParams } from '@/types';
import { QueryParams, Roles } from '@/types';
import React from 'react';
async function getLiveChatSessions(searchParams: QueryParams) {
const paginationQ = paginationData(searchParams); // pageNumber: number; pageSize: number; skip: number;
Expand Down Expand Up @@ -41,7 +41,7 @@ const LiveChat = async ({
<div className="text-3xl dark:text-white text-black transition-colors duration-500">
<h1 className="text-black dark:text-white">Sessions</h1>
</div>
<NewLiveChatSession />
{session?.user.role === Roles.admin && <NewLiveChatSession />}
</div>
<main className=" p-4 md:p-10">
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 md:gap-6">
Expand Down
Loading

0 comments on commit 3191e28

Please sign in to comment.