client side state for activeMembership and fetch notes based on same

This commit is contained in:
Michael Dausmann
2023-02-13 23:51:52 +11:00
parent 90a1e4ef65
commit bb9f4dfd1d
8 changed files with 63 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
import { PrismaClient } from '@prisma/client';
import type { inferAsyncReturnType } from '@trpc/server'
import { Membership, PrismaClient, User as DBUser } from '@prisma/client';
import { inferAsyncReturnType, TRPCError } from '@trpc/server'
import { H3Event } from 'h3';
import { serverSupabaseClient } from '#supabase/server';
import SupabaseClient from '@supabase/supabase-js/dist/module/SupabaseClient';
@@ -8,8 +8,8 @@ import UserAccountService from '~~/lib/services/user.account.service';
let prisma: PrismaClient | undefined
let supabase: SupabaseClient | undefined
let user: User | null = null;
let dbUser: any | undefined
let user: User | null;
let dbUser: (DBUser & { memberships: Membership[]; }) | null
export async function createContext(event: H3Event){
if (!supabase) {
@@ -31,6 +31,13 @@ export async function createContext(event: H3Event){
}
}
if(!supabase || !user || !prisma || !dbUser) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Unable to fetch user data, please try again later.',
});
}
// TODO - This seems excessive, trim context when I have figured out what I actually need
return {
supabase,

View File

@@ -1,12 +1,13 @@
import NotesService from '~~/lib/services/notes.service';
import { protectedProcedure, router } from '../trpc'
import { protectedProcedure, router } from '../trpc';
import { z } from 'zod';
export const notesRouter = router({
getForCurrentUser: protectedProcedure
.query(async ({ ctx }) => {
.input(z.object({ account_id: z.number() }))
.query(async ({ ctx, input }) => {
const notesService = new NotesService(ctx.prisma);
console.log(`fetching notes for account_id: ${ctx.dbUser.memberships[0].account_id}`);
const notes = await notesService.getNotesForAccountId(ctx.dbUser.memberships[0].account_id);
const notes = await notesService.getNotesForAccountId(input.account_id);
return {
notes,
}

View File

@@ -3,10 +3,17 @@ import { protectedProcedure, router } from '../trpc'
import { ACCOUNT_ACCESS } from '@prisma/client';
export const userAccountRouter = router({
getDBUser: protectedProcedure
.query(({ ctx }) => {
return {
dbUser: ctx.dbUser,
}
}),
changeAccountPlan: protectedProcedure
.query(async ({ ctx }) => {
const uaService = new UserAccountService(ctx.prisma);
const account = await uaService.changeAccountPlan(ctx.dbUser.memberships[0].account_id, 2); // todo - plan should be an in put param
// TODO - account id and plan should be an input param and then the ternary removed
const account = (ctx.dbUser?.memberships[0].account_id)?await uaService.changeAccountPlan(ctx.dbUser?.memberships[0].account_id, 2):null;
return {
account,
}
@@ -14,7 +21,7 @@ export const userAccountRouter = router({
joinUserToAccount: protectedProcedure
.query(async ({ ctx }) => {
const uaService = new UserAccountService(ctx.prisma);
const membership = await uaService.joinUserToAccount(ctx.dbUser.id, 5); // todo - account should be an input param
const membership = (ctx.dbUser?.id)?await uaService.joinUserToAccount(ctx.dbUser?.id, 5):null; // todo - account should be an input param and remove this shit
return {
membership,
}