Skip to content

Commit

Permalink
Merge pull request #16 from fumeapp/cleanup
Browse files Browse the repository at this point in the history
⚗️ tons of cleanup
  • Loading branch information
acidjazz authored Aug 25, 2024
2 parents a273ed5 + e53f1c9 commit 1062f9e
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 50 deletions.
4 changes: 2 additions & 2 deletions app/types/auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
declare module '#auth-utils' {
interface User extends import('~/types/models').User
}
interface User extends import('~/types/models').User {}
}
4 changes: 3 additions & 1 deletion app/utils/shared.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { HeaderLink } from '@nuxt/ui-pro/types'
import type { CookieOptions } from '#app'

(BigInt.prototype as any).toJSON = function () { return this.toString() }
(BigInt.prototype as any).toJSON = function () {
return this.toString()
}

export const weekDays = [...Array(7)].map((_, i) =>
new Intl.DateTimeFormat('en-US', { weekday: 'short' }).format(new Date(1970, 0, 4 + i)),
Expand Down
1 change: 1 addition & 0 deletions lib/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const prismaClientSingleton = () => {

declare const globalThis: {
prismaGlobal: ReturnType<typeof prismaClientSingleton>
// eslint-disable-next-line no-restricted-globals
} & typeof global

const prisma = globalThis.prismaGlobal ?? prismaClientSingleton()
Expand Down
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"
2 changes: 1 addition & 1 deletion server/controllers/cartridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod'
import type { Cartridge } from '~/types/models'
import { cartridgeContents, cartridgeMgs, cartridgeMls } from '~/utils/shared'

const index = authedHandler(async ({ user, event }) => {
const index = authedHandler(async ({ user }) => {
return metapi().render(
await prisma.cartridge.findMany({
where: {
Expand Down
21 changes: 3 additions & 18 deletions server/controllers/pen.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import { z } from 'zod'
import { penColors } from '~/utils/shared'

const inc = {
cartridge: {
include: {
shots: {
orderBy: {
date: 'asc',
},
},
},
},
}
const inc = { cartridge: { include: { shots: { orderBy: { date: 'asc' } } } } }

const index = defineEventHandler(async (event) => {
const { user } = await requireUserSession(event)
Expand Down Expand Up @@ -61,8 +51,7 @@ const update = authedHandler(async ({ user, event }) => {
shotDay: body?.shotDay || undefined,
})
if (!parsed.success) return metapi().error(event, parsed.error.issues, 400)
console.log(parsed.data)
const update = {
return metapi().success('pen updated', await prisma.pen.update({
where: {
id: parsed.data.id,
userId: user.id,
Expand All @@ -73,11 +62,7 @@ const update = authedHandler(async ({ user, event }) => {
shotDay: parsed.data.shotDay || null,
},
include: inc,
}
console.log(update)
const pen = await prisma.pen.update(update)

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

const get = defineEventHandler(async (event) => {
Expand Down
1 change: 0 additions & 1 deletion server/controllers/shot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { z } from 'zod'
import { format } from 'date-fns'

const index = defineEventHandler(async (event) => {
const { user } = await requireUserSession(event)
Expand Down
13 changes: 8 additions & 5 deletions test/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,22 @@ async function actingAs(email: string) {
const post = <T>(url: string, params: object) => $fetch<MetapiResponse<T>>(url, { method: 'POST', body: params, headers: { cookie: user.cookie as string } })
const put = <T>(url: string, params: object) => $fetch<MetapiResponse<T>>(url, { method: 'PUT', body: params, headers: { cookie: user.cookie as string } })
const remove = <T>(url: string, params?: object) => $fetch<MetapiResponse<T>>(url, { method: 'DELETE', body: params, headers: { cookie: user.cookie as string } })
const notFound = async (method: string, url: string, params?: object): Promise<number> => {
const expectStatus = async (expectedStatus: number, method: string, url: string, params?: object): Promise<number> => {
try {
await $fetch(url, { method, body: params, headers: { cookie: user.cookie as string } })
throw new Error(`Expected 404 status for ${method}: ${url}, but request succeeded`)
throw new Error(`Expected ${expectedStatus} status for ${method}: ${url}, but request succeeded`)
}
catch (error) {
if (error.response && error.response.status === 404)
return 404
if (error.response && error.response.status === expectedStatus)
return expectedStatus
throw error
}
}

return { get, post, put, remove, notFound, user }
const notFound = (method: string, url: string, params?: object) => expectStatus(404, method, url, params)
const unAuth = (method: string, url: string, params?: object) => expectStatus(401, method, url, params)

return { get, post, put, remove, notFound, unAuth, user }
}

export {
Expand Down
5 changes: 2 additions & 3 deletions test/cartridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ describe('/api/cartridge', async () => {

it('delete /api/cartridge/:id - delete a cartridge', async () => {
if (!cartridges[0]) throw new Error('Cartridge not found')
const { remove, get } = await actingAs('[email protected]')
const { remove, notFound } = await actingAs('[email protected]')
await remove<Cartridge>(`/api/cartridge/${cartridges[0]?.id}`)
try { await get<Cartridge[]>(`/api/cartridge/${cartridges[0]?.id}`) }
catch (error: any) { expect(error.response.status).toBe(404) }
expect(await notFound('GET', `/api/cartridge/${cartridges[0]?.id}`)).toBe(404)
})
})
6 changes: 2 additions & 4 deletions test/cartridges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ describe ('/api/user/:user/cartridge admin-only apiResource', async () => {

it ('delete /api/user/:user/cartridge/:id - delete a cartridge', async () => {
if (!cartridges[0]) throw new Error('Cartridge not found')
const { remove, get } = await actingAs('[email protected]')

const { remove, notFound } = await actingAs('[email protected]')
await remove<Cartridge>(`/api/user/1/cartridge/${cartridges[0]?.id}`)
try { await get<Cartridge[]>(`/api/user/1/cartridge/${cartridges[0]?.id}`) }
catch (error: any) { expect(error.response.status).toBe(404) }
expect(await notFound('GET', `/api/user/1/cartridge/${cartridges[0]?.id}`)).toBe(404)
})
})
5 changes: 2 additions & 3 deletions test/pen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ describe('/api/pen', async () => {

it ('delete /api/pen/:id - delete a pen', async () => {
if (!pens[0]) throw new Error('Pen not found')
const { remove, get } = await actingAs('[email protected]')
const { remove, notFound } = await actingAs('[email protected]')
await remove<Pen>(`/api/pen/${pens[0]?.id}`)
try { await get<Pen[]>(`/api/pen/${pens[0]?.id}`) }
catch (error: any) { expect(error.response.status).toBe(404) }
expect(await notFound('GET', `/api/pen/${pens[0]?.id}`)).toBe(404)
})
})
6 changes: 2 additions & 4 deletions test/pens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ describe ('/api/user/:user/pen admin-only apiResource', async () => {

it ('remove /api/user/:user/pen/:id - delete a pen', async () => {
if (!pens[0]) throw new Error('Pen not found')
const { remove, get } = await actingAs('[email protected]')

const { remove, notFound } = await actingAs('[email protected]')
await remove<Pen>(`/api/user/1/pen/${pens[0]?.id}`)
try { await get<Pen[]>(`/api/user/1/pen/${pens[0]?.id}`) }
catch (error: any) { expect(error.response.status).toBe(404) }
expect(await notFound('GET', `/api/user/1/pen/${pens[0]?.id}`)).toBe(404)
})
})
5 changes: 2 additions & 3 deletions test/shot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ describe('/api/shot', async () => {

it ('delete /api/shot/:id - delete a shot', async () => {
if (!shots[0]) throw new Error('Shot not found')
const { remove, get } = await actingAs('[email protected]')
const { remove, notFound } = await actingAs('[email protected]')
await remove<Shot>(`/api/shot/${shots[0]?.id}`)
try { await get<Shot[]>(`/api/shot/${shots[0]?.id}`) }
catch (error: any) { expect(error.response.status).toBe(404) }
expect(await notFound('GET', `/api/shot/${shots[0]?.id}`)).toBe(404)
})
})
10 changes: 6 additions & 4 deletions test/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import type { User } from '~/types/models'
describe('/api/me and /api/user', async () => {
await setup(setupConfig())
it('/api/me should 401', async () => {
try { await $fetch('/api/me') }
try {
await $fetch('/api/me')
}
catch (error: any) { expect(error.response.status).toBe(401) }
})

Expand All @@ -17,9 +19,9 @@ describe('/api/me and /api/user', async () => {
expect(response.data.email).toEqual(user.session.email)
})

it ('get /api/user isAdmin: false - 404', async () => {
try { await (await actingAs('[email protected]')).get('/api/all/user') }
catch (error: any) { expect(error.response.status).toBe(404) }
it ('get /api/all/user isAdmin: false - 404', async () => {
const { notFound } = await actingAs('[email protected]')
expect(await notFound('GET', '/api/all/user')).toBe(404)
})

it ('get /api/user GET', async () => {
Expand Down

0 comments on commit 1062f9e

Please sign in to comment.