-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--- Complete redesign of architecture --- (#245)
- Loading branch information
1 parent
63e89a9
commit 4249500
Showing
160 changed files
with
1,746 additions
and
7,858 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 +1,2 @@ | ||
auto-install-peers = true | ||
public-hoist-pattern[] = *prisma* |
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
File renamed without changes.
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,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['custom'], | ||
}; |
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,74 @@ | ||
import { Request, Response } from 'express'; | ||
|
||
import prisma from '../utils/database'; | ||
|
||
export const getEvents = async (req: Request, res: Response) => { | ||
try { | ||
const userId = req?.auth?.payload?.sub; | ||
const orgId = req?.params?.orgId; | ||
|
||
const organization = await prisma.organization.findUnique({ | ||
where: { | ||
id: orgId, | ||
}, | ||
}); | ||
|
||
if (!organization) { | ||
return res.status(404).json({ error: 'Organization not found' }); | ||
} | ||
|
||
if (organization?.organizationUsers?.some((ou: any) => ou.userId === userId)) { | ||
return res.status(403).json({ error: 'Forbidden' }); | ||
} | ||
|
||
const events = await prisma.event.findMany({ | ||
where: { | ||
organizationId: orgId, | ||
}, | ||
}); | ||
|
||
return res.status(200).json({ events }); | ||
} catch (err: any) { | ||
console.error(err); | ||
return res.status(500).json({ error: 'Something went wrong' }); | ||
} | ||
}; | ||
|
||
export const createNewEvent = async (req: Request, res: Response) => { | ||
try { | ||
const userId = req?.auth?.payload?.sub; | ||
const { id, name } = req.body; | ||
const { orgId } = req?.params; | ||
|
||
const organization = await prisma.organization.findUnique({ | ||
where: { | ||
id: orgId, | ||
}, | ||
}); | ||
|
||
if (!organization) { | ||
return res.status(404).json({ error: 'Organization not found' }); | ||
} | ||
|
||
if (organization?.organizationUsers?.some((ou: any) => ou.userId === userId)) { | ||
return res.status(403).json({ error: 'Forbidden' }); | ||
} | ||
|
||
const newEvent = await prisma.event.create({ | ||
data: { | ||
name, | ||
organizationId: orgId, | ||
}, | ||
}); | ||
|
||
if (!newEvent) { | ||
return res.status(500).json({ error: 'Something went wrong' }); | ||
} | ||
|
||
return res.status(200).json(newEvent); | ||
} catch (err: any) { | ||
console.error(err); | ||
|
||
return res.status(500).json({ error: 'Something went wrong' }); | ||
} | ||
}; |
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,31 @@ | ||
import { Request, Response } from 'express'; | ||
|
||
import prisma from '../utils/database'; | ||
|
||
export const addNewUserToDatabaseOnRegister = async (req: Request, res: Response) => { | ||
try { | ||
const { userId, email } = req.body; | ||
|
||
console.log(req.body); | ||
|
||
if (!userId || !email) { | ||
return res.status(400).send('Invalid Request'); | ||
} | ||
|
||
const newUser = await prisma.user.create({ | ||
data: { | ||
email: email, | ||
id: userId, | ||
}, | ||
}); | ||
|
||
if (!newUser) { | ||
return res.status(500).send('Error creating user'); | ||
} | ||
|
||
return res.status(200).send(newUser); | ||
} catch (err) { | ||
console.error(err); | ||
return res.status(500); | ||
} | ||
}; |
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,56 @@ | ||
import { Request, Response } from 'express'; | ||
|
||
import prisma from '../utils/database'; | ||
|
||
export const createNewOrganization = async (req: Request, res: Response) => { | ||
try { | ||
const userId = req?.auth?.payload?.sub; | ||
const { id, name } = req.body; | ||
|
||
const newOrganization = await prisma.organization.create({ | ||
data: { | ||
id, | ||
name, | ||
OrganizationUser: { | ||
create: { | ||
userId, | ||
role: 'ADMIN', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!newOrganization) { | ||
return res.status(500).json({ error: 'Something went wrong' }); | ||
} | ||
|
||
return res.status(200).json(newOrganization); | ||
} catch (err: any) { | ||
console.error(err); | ||
if (err.code === 'P2002') { | ||
return res.status(400).json({ error: 'Organization with the same id already exists' }); | ||
} | ||
return res.status(500).json({ error: 'Something went wrong' }); | ||
} | ||
}; | ||
|
||
export const getUsersOrganizations = async (req: Request, res: Response) => { | ||
try { | ||
const userId = req?.auth?.payload?.sub; | ||
|
||
const organizations = await prisma.organization.findMany({ | ||
where: { | ||
OrganizationUser: { | ||
some: { | ||
userId, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return res.status(200).json({ organizations }); | ||
} catch (err: any) { | ||
console.error(err); | ||
return res.status(500).json({ error: 'Something went wrong' }); | ||
} | ||
}; |
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,46 @@ | ||
import * as dotenv from 'dotenv'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { auth, claimCheck, InsufficientScopeError } from 'express-oauth2-jwt-bearer'; | ||
|
||
dotenv.config(); | ||
|
||
export const validateAccessToken = auth({ | ||
issuerBaseURL: `https://${process.env.AUTH0_DOMAIN}`, | ||
audience: process.env.AUTH0_AUDIENCE, | ||
}); | ||
|
||
export const checkRequiredPermissions = (requiredPermissions: string[]) => { | ||
return (req: Request, res: Response, next: NextFunction) => { | ||
const permissionCheck = claimCheck((payload) => { | ||
const permissions = payload.permissions as string[]; | ||
|
||
const hasPermissions = requiredPermissions.every((requiredPermission) => | ||
permissions.includes(requiredPermission), | ||
); | ||
|
||
if (!hasPermissions) { | ||
throw new InsufficientScopeError(); | ||
} | ||
|
||
return hasPermissions; | ||
}); | ||
|
||
permissionCheck(req, res, next); | ||
}; | ||
}; | ||
|
||
export const decodeUserInfo = (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const userId = req?.auth?.payload?.sub; | ||
|
||
if (!userId) { | ||
console.error('User ID not found in the payload'); | ||
return res.status(500).send('Internal Server Error'); | ||
} | ||
|
||
next(); | ||
} catch (err) { | ||
console.error(err); | ||
return res.status(500).send('Internal Server Error'); | ||
} | ||
}; |
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 express, { Router } from 'express'; | ||
import { createNewOrganization, getUsersOrganizations } from './controllers/organizations'; | ||
import { createNewEvent, getEvents } from './controllers/events'; | ||
const router: Router = express.Router(); | ||
|
||
router.get('/', (req: any, res: any) => { | ||
try { | ||
return res.send('Hello World!'); | ||
} catch (err) { | ||
console.error(err); | ||
return res.status(500); | ||
} | ||
}); | ||
|
||
router.get('/organizations', getUsersOrganizations); | ||
router.post('/organizations', createNewOrganization); | ||
|
||
router.get('/organizations/:orgId/events', getEvents); | ||
router.post('/organizations/:orgId/events', createNewEvent); | ||
|
||
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,7 @@ | ||
const { PrismaClient } = require('database'); | ||
|
||
const prisma = new PrismaClient({ | ||
datasourceUrl: process.env.DATABASE_URL, | ||
}); | ||
|
||
export default prisma; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.