Skip to content

Commit

Permalink
feat: quality of life update
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan committed Jul 18, 2023
1 parent 012138e commit a3c50a0
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 31 deletions.
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default defineNuxtConfig({
},

htmlValidator: {
logLevel: 'error',
failOnError: true,
options: {
rules: {
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions server/plugins/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ export default defineNitroPlugin(() => {
})
: memoryDriver()

console.log(driver)

Check failure on line 17 in server/plugins/cache.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement

storage.mount('redis', driver)
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { z } from 'zod'
import { TRPCError } from '@trpc/server'
import type { Survey } from '@prisma/client'
import { protectedProcedure, router } from '../../trpc'
import type { QuestionsSchema, SurveyPermissionSchema, SurveyResponseSchema } from '../../../../shared/survey'

import { protectedProcedure, router } from '~/server/trpc/trpc'
import type { QuestionsSchema, SurveyPermissionSchema, SurveyResponseSchema } from '~/shared/survey'

// TODO probably need a better name for all the functions here
export const analyticsRouter = router({
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { router } from '../../trpc'
import { emailSessionRouter } from './email'
import { emailSessionRouter } from './email.router'
import { router } from '~/server/trpc/trpc'

export const authRouter = router({
email: emailSessionRouter,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Prisma, type PrismaClient } from '@prisma/client'
import { VerificationError } from './auth.error'
import { compareHash } from './email.service'

export async function verifyToken(
prisma: PrismaClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { z } from 'zod'
import { render } from '@qingu/vue-email'
import { createSSRApp } from 'vue'

import { verifyToken } from '../../services/auth'
import { VerificationError } from '../../services/auth.error'
import { publicProcedure, router } from '../../trpc'
import { VerificationCodeEmail } from '../../../emails/VerificationCodeEmail'
import { VerificationError } from './auth.error'
import { verifyToken } from './auth.service'
import { createTokenHash, createVfnToken } from './email.service'
import { publicProcedure, router } from '~/server/trpc/trpc'
import { VerificationCodeEmail } from '~/server/emails/verification-code'
import { defaultUserSelect } from '~/server/trpc/routers/user/user.select'

const admins = [
'qinguan20040914@gmail.com',
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions server/trpc/routers/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { z } from 'zod'
import { publicProcedure, router } from '../trpc'
import { authRouter } from './auth'
import { meRouter } from './me'
import { surveyRouter } from './survey'
import { analyticsRouter } from './analytics'
import { responseRouter } from './response'
import { luckyDrawRouter } from './luckydraw'
import { authRouter } from './auth/auth.router'
import { meRouter } from './me/me'
import { surveyRouter } from './survey/survey'
import { analyticsRouter } from './analytics/analytics'
import { responseRouter } from './response/response'
import { luckyDrawRouter } from './lucky-draw/lucky-draw'

export const appRouter = router({
hello: publicProcedure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getRandomValues } from 'node:crypto'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import { protectedProcedure, router } from '../../trpc'

import { protectedProcedure, router } from '~/server/trpc/trpc'

function getRandomIntInclusive(min: number, max: number) {
const randomBuffer = new Uint32Array(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { z } from 'zod'
import type { User } from '@prisma/client'
import { protectedProcedure, router } from '../../trpc'

import { defaultUserSelect } from '~/server/trpc/routers/user/user.select'
import { protectedProcedure, router } from '~/server/trpc/trpc'

export const meRouter = router({
get: protectedProcedure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { z } from 'zod'
import { TRPCError } from '@trpc/server'
import type { Survey } from '@prisma/client'
import { protectedProcedure, router } from '../../trpc'
import type { Prisma, Survey } from '@prisma/client'

import { protectedProcedure, router } from '~/server/trpc/trpc'

import type { SurveyPermissionSchema } from '~/shared/survey'
import { surveyResponseSchema } from '~/shared/survey'

Expand All @@ -11,22 +13,41 @@ const surveyProtectedProcedure = protectedProcedure.input(
}),
)

type UserIncludeSurvey = Prisma.UserGetPayload<{
include: {
surveyResponses: {
include: {
survey: true
}
}
}
}>

export const responseRouter = router({
submitted: protectedProcedure
.meta({ participants: true })
.query(async ({ ctx }) => {
const data = await ctx.prisma.user.findUniqueOrThrow({
where: {
id: ctx.session.user.id,
},
include: {
surveyResponses: {
include: {
survey: true,
let data
if (await ctx.cache.users.hasItem(`${ctx.session.user.id}-submitted`)) {
data = await ctx.cache.users.getItem<UserIncludeSurvey>(`${ctx.session.user.id}-submitted`)
if (!data)
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR' })
}
else {
data = await ctx.prisma.user.findUniqueOrThrow({
where: {
id: ctx.session.user.id,
},
include: {
surveyResponses: {
include: {
survey: true,
},
},
},
},
})
})
await ctx.cache.users.setItem(`${ctx.session.user.id}-submitted`, data)
}

return data.surveyResponses
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import type { Survey } from '@prisma/client'
import { Prisma } from '@prisma/client'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import type { QuestionsSchema, SurveyPermissionSchema } from '../../../../shared/survey'
import { questionsSchema, surveyPermissionSchema } from '../../../../shared/survey'
import { protectedProcedure, router } from '../../trpc'

import { protectedProcedure, router } from '~/server/trpc/trpc'

import type { QuestionsSchema, SurveyPermissionSchema } from '~/shared/survey'
import { questionsSchema, surveyPermissionSchema } from '~/shared/survey'

const ALL_SURVEYS_KEY = '__all_surveys'

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions server/trpc/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/v10/procedures
*/

import type { User } from '@prisma/client'
import { TRPCError, initTRPC } from '@trpc/server'
import superjson from 'superjson'
Expand Down

0 comments on commit a3c50a0

Please sign in to comment.