Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set archiving #102

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/app/api/tactics/archive/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { prisma } from '~/server/db'

import { getKindeServerSession } from '@kinde-oss/kinde-auth-nextjs/server'
import * as Sentry from '@sentry/nextjs'
import { errorResponse, successResponse } from '~/app/api/responses'

export async function POST(request: Request) {
const session = getKindeServerSession()
if (!session) return errorResponse('Unauthorized', 401)

const user = await session.getUser()
if (!user) return errorResponse('Unauthorized', 401)

const { setId } = (await request.json()) as {
setId: string
}

if (!setId) {
return errorResponse('Missing required fields', 400)
}

try {
await prisma.tacticsSet.update({
where: {
id: setId,
},
data: {
active: false,
},
})

return successResponse('Set Archived', { setId }, 200)
} catch (e) {
Sentry.captureException(e)
if (e instanceof Error) return errorResponse(e.message, 500)
else return errorResponse('Unknown error', 500)
} finally {
await prisma.$disconnect()
}
}
44 changes: 14 additions & 30 deletions src/app/api/tactics/delete/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ export async function POST(request: Request) {
setId: string
}

if (!setId) {
return errorResponse('Missing required fields', 400)
}
if (!setId) return errorResponse('Missing required fields', 400)

try {
const existingSet = await prisma.tacticsSet.findFirst({
Expand All @@ -27,37 +25,23 @@ export async function POST(request: Request) {
},
})

if (!existingSet) {
return errorResponse('Set not found', 404)
}

// check if the set is a purchased one, if so, just archive it and delete progress
if (existingSet.curatedSetId) {
await prisma.tacticsSet.update({
where: {
id: setId,
},
data: {
active: false,
rounds: {
deleteMany: {},
},
},
})
} else {
await prisma.tacticsSet.delete({
where: {
id: setId,
userId: user.id,
},
})
}
if (!existingSet) return errorResponse('Set not found', 404)

// check if the set is a purchased one, we can't delete purchased sets
if (existingSet.curatedSetId)
return errorResponse('Cannot delete purchased set', 400)

await prisma.tacticsSet.delete({
where: {
id: setId,
userId: user.id,
},
})

return successResponse('Set Deleted', { setId }, 200)
} catch (e) {
Sentry.captureException(e)
if (e instanceof Error) return errorResponse(e.message, 500)
else return errorResponse('Unknown error', 500)
} finally {
await prisma.$disconnect()
}
}
48 changes: 48 additions & 0 deletions src/app/api/tactics/resetProgress/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { prisma } from '~/server/db'

import { getKindeServerSession } from '@kinde-oss/kinde-auth-nextjs/server'
import * as Sentry from '@sentry/nextjs'
import { errorResponse, successResponse } from '~/app/api/responses'

export async function POST(request: Request) {
const session = getKindeServerSession(request)
if (!session) return errorResponse('Unauthorized', 401)

const user = await session.getUser()
if (!user) return errorResponse('Unauthorized', 401)

const { setId } = (await request.json()) as {
setId: string
}

if (!setId) return errorResponse('Missing required fields', 400)

try {
const existingSet = await prisma.tacticsSet.findFirst({
where: {
id: setId,
userId: user.id,
},
})

if (!existingSet) return errorResponse('Set not found', 404)

// Remove the rounds, which will reset the progress
await prisma.tacticsSet.update({
where: {
id: setId,
},
data: {
rounds: {
deleteMany: {},
},
},
})

return successResponse('Progress Reset', { setId }, 200)
} catch (e) {
Sentry.captureException(e)
if (e instanceof Error) return errorResponse(e.message, 500)
else return errorResponse('Unknown error', 500)
}
}
2 changes: 1 addition & 1 deletion src/app/components/template/header/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export default function Nav(props: {
) : (
<Link
key={link.name}
className="text-center block cursor-pointer px-4 py-2 text-gray-700 hover:bg-orange-100 lg:text-white lg:hover:bg-slate-600"
className="block cursor-pointer px-4 py-2 text-gray-700 hover:bg-orange-100 lg:text-white lg:hover:bg-slate-600"
href={link.href}
onClick={() => {
setMenuOpen(false)
Expand Down
Loading