Skip to content

Commit

Permalink
Merge pull request #11 from fumeapp/helpers-bindUser
Browse files Browse the repository at this point in the history
✨ kill bindUser and add notFound helpers
  • Loading branch information
acidjazz authored Aug 24, 2024
2 parents 5fab08c + 86a382d commit 6e4c51c
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 163 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name: Run Tests
on:
pull_request

env:
DEVRUN: true
jobs:
test:
if: github.event.pull_request.draft == false
Expand All @@ -12,16 +10,10 @@ jobs:
mysql:
image: mysql:8.0
env:
# The MySQL docker container requires these environment variables to be set
# so we can create and migrate the test database.
# See: https://hub.docker.com/_/mysql
MYSQL_DATABASE: bio-test
MYSQL_ROOT_PASSWORD: root
ports:
# Opens port 3306 on service container and host
# https://docs.github.com/en/actions/using-containerized-services/about-service-containers
- 3306:3306
# Before continuing, verify the mysql container is reachable from the ubuntu host
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -73,7 +65,7 @@ jobs:
env:
NUXT_UI_PRO_LICENSE: ${{ secrets.NUXT_UI_PRO_LICENSE }}
NODE_ENV: test
run: pnpm run test:dev:coverage
run: pnpm run test:coverage

- name: Report Coverage
if: always()
Expand Down
11 changes: 2 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@
"generate": "nuxt generate",
"preview": "nuxt preview",
"test": "vitest run",
"test:dev": "DEVRUN=true vitest run",
"test:dev:ui": "DEVRUN=true vitest --ui",
"test::ui": "vitest --ui",
"test:reset": "pnpm run db:test:reset; vitest run",
"test:dev:reset": "pnpm run db:test:reset; DEVRUN=true vitest run",
"db:test:reset": "dotenv -e .env.test -- npx prisma migrate reset --force",
"test:coverage": "vitest run --coverage.enabled true",
"test:dev:coverage": "DEVRUN=true vitest run --coverage.enabled true",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"lint:action": "pnpm run lint -f @jamesacarr/github-actions"
},
"dependencies": {
"@nuxt/ui-pro": "^1.4.1",
"date-fns": "^3.6.0",
"h3": "npm:h3-nightly@latest"
"date-fns": "^3.6.0"
},
"devDependencies": {
"@antfu/eslint-config": "^2.26.0",
Expand All @@ -47,8 +43,5 @@
"ua-parser-js": "^1.0.38",
"vitest": "^2.0.5",
"zod": "^3.23.8"
},
"resolutions": {
"h3": "npm:h3-nightly@latest"
}
}
44 changes: 19 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "mysql"
provider = "mysql"
23 changes: 7 additions & 16 deletions server/controllers/cartridges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@ import { z } from 'zod'
import type { Cartridge } from '~/types/models'
import { cartridgeContents, cartridgeMgs, cartridgeMls } from '~/utils/shared'

const index = defineEventHandler(async (event) => {
const { user } = await requireUserSession(event)
if (!user.isAdmin) return metapi().notFound(event)
const index = authedHandler(async ({ event }) => {
const schema = z.object({ id: z.number() })
const parsed = schema.safeParse({ id: Number.parseInt(event.context.params?.user as string) })
if (!parsed.success) return metapi().error(event, parsed.error.issues, 400)
return metapi().render(
await prisma.cartridge.findMany({
where: {
userId: parsed.data.id,
},
orderBy: {
updatedAt: 'desc',
},
include: {
pen: true,
shots: true,
},
where: { userId: parsed.data.id },
orderBy: { updatedAt: 'desc' },
include: { pen: true, shots: true },
}),
)
})
}, true)

const create = authedHandler(async ({ event }) => {
const schema = z.object({
Expand Down Expand Up @@ -51,7 +42,7 @@ const create = authedHandler(async ({ event }) => {

const get = authedModelHandler<Cartridge>(async ({ model: cartridge }) => {
return metapi().render(cartridge)
}, { admin: true, bindUser: false })
}, true)

const remove = authedModelHandler<Cartridge>(async ({ model: cartridge }) => {
await prisma.cartridge.delete({
Expand All @@ -60,7 +51,7 @@ const remove = authedModelHandler<Cartridge>(async ({ model: cartridge }) => {
},
})
return metapi().success('cartridge deleted')
}, { admin: true, bindUser: false })
}, true)

export default {
index,
Expand Down
10 changes: 4 additions & 6 deletions server/controllers/pens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,29 @@ const create = authedHandler(async ({ event }) => {
return metapi().success('pen created', pen)
}, true)

const update = authedHandler(async ({ event }) => {
const update = authedModelHandler<Pen>(async ({ event, model: pen }) => {
const schema = z.object({
id: z.number(),
user: z.number(),
cartridgeId: z.number().optional(),
})

const parsed = schema.safeParse({
id: Number.parseInt(event.context.params?.id as string),
user: Number.parseInt(event.context.params?.user as string),
cartridgeId: Number.parseInt((await readBody(event))?.cartridgeId) || undefined,
})
if (!parsed.success) return metapi().error(event, parsed.error.issues, 400)

const pen = await prisma.pen.update({
const updatedPen = await prisma.pen.update({
where: {
id: parsed.data.id,
id: pen.id,
userId: parsed.data.user,
},
data: {
cartridgeId: parsed.data.cartridgeId ? BigInt(parsed.data.cartridgeId) : null,
},
})

return metapi().success('pen updated', pen)
return metapi().success('pen updated', updatedPen)
}, true)

const get = authedModelHandler<Pen>(async ({ model: pen }) => {
Expand Down
Loading

0 comments on commit 6e4c51c

Please sign in to comment.