mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 22:47:15 +01:00
switch to prisma
This commit is contained in:
@@ -1,64 +1,51 @@
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import NextAuth from 'next-auth'
|
||||
import EmailProvider from 'next-auth/providers/email'
|
||||
import GitHubProvider from 'next-auth/providers/github'
|
||||
|
||||
import { MongoDBAdapter } from '@next-auth/mongodb-adapter'
|
||||
import { MONGO_URI } from '../../../db'
|
||||
import { MongoClient } from 'mongodb'
|
||||
import { PrismaAdapter } from "@next-auth/prisma-adapter"
|
||||
import { PrismaClient } from "@prisma/client"
|
||||
|
||||
let client: MongoClient
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
const ADMIN_EMAIL = process.env.ADMIN_EMAIL
|
||||
const GITHUB_USERS_GRANTED = ['111471']
|
||||
|
||||
async function getMongoClient() {
|
||||
if (!client) {
|
||||
client = new MongoClient(MONGO_URI)
|
||||
await client.connect()
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
export default async function auth(req: NextApiRequest, res: NextApiResponse) {
|
||||
return await NextAuth(req, res, {
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
adapter: MongoDBAdapter(getMongoClient()),
|
||||
providers: [
|
||||
GitHubProvider({
|
||||
clientId: process.env.GITHUB_CLIENT_ID,
|
||||
clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
||||
}),
|
||||
EmailProvider({
|
||||
server: {
|
||||
host: 'smtp.sendgrid.net',
|
||||
port: 587,
|
||||
auth: {
|
||||
user: 'apikey',
|
||||
pass: process.env.SENDGRID_API_KEY,
|
||||
},
|
||||
export default NextAuth({
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
adapter: PrismaAdapter(prisma),
|
||||
providers: [
|
||||
GitHubProvider({
|
||||
clientId: process.env.GITHUB_CLIENT_ID,
|
||||
clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
||||
}),
|
||||
EmailProvider({
|
||||
server: {
|
||||
host: 'smtp.sendgrid.net',
|
||||
port: 587,
|
||||
auth: {
|
||||
user: 'apikey',
|
||||
pass: process.env.SENDGRID_API_KEY,
|
||||
},
|
||||
from: process.env.FROM_EMAIL,
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
async signIn({ account, email }) {
|
||||
// if user sigin requested magic link via EmailProvider
|
||||
if (account.provider === 'email') {
|
||||
if (email.verificationRequest) {
|
||||
// only allow admins by email entered
|
||||
return account.providerAccountId === ADMIN_EMAIL
|
||||
}
|
||||
|
||||
// if user accesses with magic link, also only allow admin
|
||||
return account.providerAccountId === ADMIN_EMAIL
|
||||
} else if (account.provider === 'github') {
|
||||
// only one and only one user
|
||||
return GITHUB_USERS_GRANTED.includes(account.providerAccountId)
|
||||
}
|
||||
return false
|
||||
},
|
||||
from: process.env.FROM_EMAIL,
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
async signIn({ account, email }) {
|
||||
// if user sigin requested magic link via EmailProvider
|
||||
if (account.provider === 'email') {
|
||||
if (email.verificationRequest) {
|
||||
// only allow admins by email entered
|
||||
return account.providerAccountId === ADMIN_EMAIL
|
||||
}
|
||||
|
||||
// if user accesses with magic link, also only allow admin
|
||||
return account.providerAccountId === ADMIN_EMAIL
|
||||
} else if (account.provider === 'github') {
|
||||
// only one and only one user
|
||||
return GITHUB_USERS_GRANTED.includes(account.providerAccountId)
|
||||
}
|
||||
return false
|
||||
},
|
||||
})
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { Bill } from '../../../../db/bill'
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { createBill, patchBill } from '../../../../db/index'
|
||||
import { log } from '../../../../helpers/log'
|
||||
|
||||
@@ -13,7 +13,7 @@ export default async function billHandler(
|
||||
} = req
|
||||
const bookingUUID = Array.isArray(uuids) ? uuids[0] : uuids
|
||||
|
||||
let bill: Bill
|
||||
let bill: Prisma.BillUpdateInput
|
||||
|
||||
switch (method) {
|
||||
case 'POST':
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { Booking } from '../../../../db/booking'
|
||||
import { BOOKING_STATUS } from '../../../../db/enums'
|
||||
import { Prisma, BookingStatus } from '@prisma/client';
|
||||
import { patchBooking } from '../../../../db/index'
|
||||
import {
|
||||
sendBookingConfirmed,
|
||||
@@ -10,28 +9,27 @@ import {
|
||||
import { log } from '../../../../helpers/log'
|
||||
|
||||
function changedStatus(
|
||||
previous: Booking,
|
||||
current: Partial<Booking>,
|
||||
status: BOOKING_STATUS
|
||||
previous: Prisma.BookingUpdateInput,
|
||||
current: Prisma.BookingUpdateInput,
|
||||
status: BookingStatus
|
||||
): boolean {
|
||||
return (
|
||||
[BOOKING_STATUS.REQUESTED].includes(previous.status) &&
|
||||
BookingStatus.REQUESTED === previous.status &&
|
||||
current.status === status
|
||||
)
|
||||
}
|
||||
function wasRejected(previous: Booking, current: Partial<Booking>): boolean {
|
||||
return changedStatus(previous, current, BOOKING_STATUS.REJECTED)
|
||||
function wasRejected(previous: Prisma.BookingUpdateInput, current: Prisma.BookingUpdateInput): boolean {
|
||||
return changedStatus(previous, current, BookingStatus.REJECTED)
|
||||
}
|
||||
|
||||
function wasConfirmed(previous: Booking, current: Partial<Booking>): boolean {
|
||||
return changedStatus(previous, current, BOOKING_STATUS.CONFIRMED)
|
||||
function wasConfirmed(previous: Prisma.BookingUpdateInput, current: Prisma.BookingUpdateInput): boolean {
|
||||
return changedStatus(previous, current, BookingStatus.CONFIRMED)
|
||||
}
|
||||
|
||||
function wasCanceled(previous: Booking, current: Partial<Booking>): boolean {
|
||||
function wasCanceled(previous: Prisma.BookingUpdateInput, current: Prisma.BookingUpdateInput): boolean {
|
||||
return (
|
||||
[BOOKING_STATUS.REQUESTED, BOOKING_STATUS.CONFIRMED].includes(
|
||||
previous.status
|
||||
) && current.status === BOOKING_STATUS.CANCELED
|
||||
[BookingStatus.REQUESTED, BookingStatus.CONFIRMED].find(s => s === previous.status)
|
||||
&& current.status === BookingStatus.CANCELED
|
||||
)
|
||||
}
|
||||
|
||||
@@ -48,12 +46,12 @@ export default async function userHandler(
|
||||
|
||||
switch (method) {
|
||||
case 'PATCH':
|
||||
if (!Object.values(BOOKING_STATUS).includes(req.body.status)) {
|
||||
if (!Object.values(BookingStatus).includes(req.body.status)) {
|
||||
res
|
||||
.status(400)
|
||||
.end(
|
||||
`The attribute status can only be: ${Object.values(
|
||||
BOOKING_STATUS
|
||||
BookingStatus
|
||||
).join(', ')}`
|
||||
)
|
||||
break
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Error } from 'mongoose'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { Booking } from '../../../db/booking'
|
||||
import { Booking, Prisma } from '@prisma/client';
|
||||
import { createBooking } from '../../../db/index'
|
||||
import { log } from '../../../helpers/log'
|
||||
import {
|
||||
@@ -14,17 +13,14 @@ export default async function userHandler(
|
||||
): Promise<void> {
|
||||
const { method } = req
|
||||
|
||||
let booking: Booking
|
||||
let booking: Booking;
|
||||
|
||||
switch (method) {
|
||||
case 'POST':
|
||||
try {
|
||||
booking = await createBooking(req.body)
|
||||
} catch (e) {
|
||||
if (e instanceof Error.ValidationError) {
|
||||
res.status(400).json({ message: e.message, errors: e.errors })
|
||||
return
|
||||
}
|
||||
// TODO: add validation for booking on same day
|
||||
log.error('Failed to store booking', e)
|
||||
res.status(500).end(`Internal Server Error...Guru is meditating...`)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user