-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5e9c45
commit f680eb6
Showing
17 changed files
with
413 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import DiscordAPI from './DiscordAPI' | ||
import vps from './vps' | ||
export { DiscordAPI, vps }; | ||
import Project from './projects' | ||
export { DiscordAPI, vps, Project }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mongoose, { Schema, Document } from 'mongoose'; | ||
|
||
interface IProject extends Document { | ||
uniqueId: string; | ||
name: string; | ||
description?: string; | ||
users: mongoose.Types.ObjectId[]; | ||
vps: mongoose.Types.ObjectId[]; | ||
} | ||
|
||
const projectSchema: Schema = new Schema({ | ||
uniqueId: { type: String, required: true }, | ||
name: { type: String, required: true }, | ||
description: { type: String }, | ||
users: [{ type: String, ref: 'User' }], | ||
vps: [{ type: String, ref: 'VPS' }], | ||
}); | ||
|
||
const Project = mongoose.model<IProject>('Project', projectSchema); | ||
|
||
export default Project; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Router, Request, Response } from 'express'; | ||
import { Project } from '../../../database/schemas'; | ||
import { DiscordAPI } from '../../../database/schemas'; // Import User model | ||
|
||
const router = Router(); | ||
|
||
// Function to generate a unique 6-digit ID | ||
async function generateUniqueId(): Promise<string> { | ||
let uniqueId = ''; // Initialize with a default value | ||
let isUnique = false; | ||
|
||
while (!isUnique) { | ||
uniqueId = Math.floor(100000 + Math.random() * 900000).toString(); | ||
|
||
// Check if the generated ID already exists | ||
const existingProject = await Project.findOne({ uniqueId }); | ||
if (!existingProject) { | ||
isUnique = true; | ||
} | ||
} | ||
|
||
return uniqueId; | ||
} | ||
|
||
// Create a project | ||
router.post('/', async (req: Request, res: Response) => { | ||
try { | ||
const { name, description, email } = req.body; | ||
|
||
// Find user by email | ||
const user = await DiscordAPI.findOne({ email }); | ||
if (!user) { | ||
return res.status(400).json({ error: 'User not found' }); | ||
} | ||
|
||
const uniqueId = await generateUniqueId(); // Ensure unique ID | ||
const project = new Project({ uniqueId, name, description, users: [user.unid] }); // Add user ObjectId to users array | ||
|
||
await project.save(); | ||
res.status(201).json(project); | ||
} catch (err) { | ||
console.error('Error creating project:', err); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Router, Request, Response } from 'express'; | ||
import { Project } from '../../../database/schemas'; | ||
|
||
const router = Router(); | ||
|
||
// Delete a project | ||
router.post('/', async (req: Request, res: Response) => { | ||
try { | ||
const { projectId } = req.body; | ||
const deletedProject = await Project.findOneAndDelete({ projectId }); | ||
if (!deletedProject) { | ||
return res.status(404).json({ error: 'Project not found' }); | ||
} | ||
res.status(200).json({ message: 'Project deleted successfully' }); | ||
} catch (err) { | ||
console.error('Error deleting project:', err); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Request, Router, Response } from "express"; | ||
import { Project } from '../../database/schemas'; | ||
|
||
const router = Router(); | ||
|
||
import createProjectRouter from './create' | ||
import deleteProjectRouter from './delete' | ||
import manageProjectRouter from './manage' | ||
|
||
router.use('/create', createProjectRouter) | ||
router.use('/delete', deleteProjectRouter) | ||
router.use('/manage', manageProjectRouter) | ||
|
||
router.get('/list', async (req: Request, res: Response) => { | ||
try { | ||
const projectList = await Project.find(); | ||
res.status(200).json(projectList) | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).json({ error: "Internal Server Error" }); | ||
} | ||
}) | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Request, Router, Response } from "express"; | ||
const router = Router(); | ||
import user from './user' | ||
import vps from './vps' | ||
|
||
router.use('/user', user); | ||
router.use('/vps', vps); | ||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Router, Request, Response } from 'express'; | ||
import { Project, DiscordAPI } from '../../../database/schemas'; // Adjust path as needed | ||
|
||
const router = Router(); | ||
|
||
// Assign a user to a project | ||
router.post('/assign', async (req: Request, res: Response) => { | ||
try { | ||
const { projectId, email } = req.body; | ||
|
||
// Find the project | ||
const project = await Project.findOne({ id: projectId }); | ||
if (!project) { | ||
return res.status(404).json({ error: 'Project not found' }); | ||
} | ||
|
||
// Find the user | ||
const user = await DiscordAPI.findOne({ email: email }); | ||
if (!user) { | ||
return res.status(404).json({ error: 'User not found' }); | ||
} | ||
|
||
// Assign user to project by email | ||
if (!project.users.includes(email)) { | ||
project.users.push(email); | ||
await project.save(); | ||
} | ||
|
||
res.status(200).json({ message: 'User assigned to project successfully' }); | ||
} catch (err) { | ||
console.error('Error assigning user to project:', err); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
// Remove a user from a project | ||
router.post('/remove', async (req: Request, res: Response) => { | ||
try { | ||
const { projectId, userEmail } = req.body; | ||
|
||
// Find the project | ||
const project = await Project.findOne({ id: projectId }); | ||
if (!project) { | ||
return res.status(404).json({ error: 'Project not found' }); | ||
} | ||
|
||
// Remove user from project by email | ||
project.users = project.users.filter(email => email !== userEmail); | ||
await project.save(); | ||
|
||
res.status(200).json({ message: 'User removed from project successfully' }); | ||
} catch (err) { | ||
console.error('Error removing user from project:', err); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Router, Request, Response } from 'express'; | ||
import { Project, vps } from '../../../database/schemas'; // Adjust path as needed | ||
|
||
const router = Router(); | ||
|
||
// Assign a VPS to a project | ||
router.post('/assign', async (req: Request, res: Response) => { | ||
try { | ||
const { projectId, vpsId } = req.body; | ||
|
||
// Validate input | ||
if (!projectId || !vpsId) { | ||
return res.status(400).json({ error: 'Project ID and VPS ID are required' }); | ||
} | ||
|
||
// Find the project | ||
const project = await Project.findOne({ id: projectId }); | ||
if (!project) { | ||
return res.status(404).json({ error: 'Project not found' }); | ||
} | ||
|
||
// Find the VPS | ||
const vpsInstance = await vps.findOne({ id: vpsId }); | ||
if (!vpsInstance) { | ||
return res.status(404).json({ error: 'VPS not found' }); | ||
} | ||
|
||
// Assign VPS to project by ID | ||
if (!project.vps.includes(vpsId)) { | ||
project.vps.push(vpsId); | ||
await project.save(); | ||
} | ||
|
||
res.status(200).json({ message: 'VPS assigned to project successfully' }); | ||
} catch (err) { | ||
console.error('Error assigning VPS to project:', err); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
// Remove a VPS from a project | ||
router.post('/remove', async (req: Request, res: Response) => { | ||
try { | ||
const { projectId, vpsId } = req.body; | ||
|
||
// Validate input | ||
if (!projectId || !vpsId) { | ||
return res.status(400).json({ error: 'Project ID and VPS ID are required' }); | ||
} | ||
|
||
// Find the project | ||
const project = await Project.findOne({ id: projectId }); | ||
if (!project) { | ||
return res.status(404).json({ error: 'Project not found' }); | ||
} | ||
|
||
// Remove VPS from project by ID | ||
project.vps = project.vps.filter(vps => vps !== vpsId); | ||
await project.save(); | ||
|
||
res.status(200).json({ message: 'VPS removed from project successfully' }); | ||
} catch (err) { | ||
console.error('Error removing VPS from project:', err); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,51 @@ | ||
import { Request, Response, Router } from "express"; | ||
import { DiscordAPI } from "../../../database/schemas"; | ||
import crypto from "crypto"; | ||
|
||
const router = Router(); | ||
|
||
function generateUNID(): string { | ||
return crypto.randomBytes(3).toString('hex').toUpperCase(); | ||
} | ||
|
||
router.post('/', async (req: Request, res: Response) => { | ||
try { | ||
const method = req.body.method; | ||
const email = req.body.email; | ||
if (!method || !email) { | ||
return res.status(400).json({ error: 'Invalid Request Body!'}) | ||
} | ||
|
||
const existingUser = await DiscordAPI.findOne({ email }); | ||
if (existingUser) { | ||
return res.status(409).json({ error: 'User already exists' }); | ||
} | ||
const { method, email } = req.body; | ||
if (!method || !email) { | ||
return res.status(400).json({ error: 'Invalid Request Body!' }); | ||
} | ||
|
||
const existingUser = await DiscordAPI.findOne({ email }); | ||
if (existingUser) { | ||
return res.status(409).json({ error: 'User already exists' }); | ||
} | ||
|
||
const newUser = new DiscordAPI({ | ||
email, | ||
method | ||
}); | ||
|
||
await newUser.save(); | ||
|
||
res.status(200).json({ message: 'User created successfully' }); | ||
let unid = generateUNID(); | ||
while (await DiscordAPI.findOne({ unid })) { | ||
unid = generateUNID(); | ||
} | ||
|
||
const newUser = new DiscordAPI({ | ||
email, | ||
method, | ||
joinDate: new Date(), | ||
unid | ||
}); | ||
|
||
await newUser.save(); | ||
|
||
res.status(201).json({ | ||
message: 'User created successfully', | ||
user: { | ||
email: newUser.email, | ||
unid: newUser.unid, | ||
joinDate: newUser.joinDate | ||
} | ||
}); | ||
} catch (err) { | ||
console.error('Error creating user:', err); | ||
return res.status(500).json({ error: 'Internal Server Error' }); | ||
console.error('Error creating user:', err); | ||
return res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
export default router; | ||
}); | ||
|
||
export default router; |
Oops, something went wrong.