@goscribe/server 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,10 +2,14 @@ import { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
2
2
  import { router } from '../trpc.js';
3
3
  import { auth } from './auth.js';
4
4
  import { workspace } from './workspace.js';
5
+ import { flashcards } from './flashcards';
6
+ import { worksheets } from './worksheets';
5
7
 
6
8
  export const appRouter = router({
7
9
  auth,
8
- workspace
10
+ workspace,
11
+ flashcards,
12
+ worksheets,
9
13
  });
10
14
 
11
15
  // Export type for client inference
@@ -1,6 +1,7 @@
1
1
  import { z } from 'zod';
2
2
  import { router, publicProcedure, authedProcedure } from '../trpc.js';
3
3
  import bcrypt from 'bcryptjs';
4
+ import { serialize } from 'cookie';
4
5
 
5
6
  export const auth = router({
6
7
  signup: publicProcedure
@@ -48,7 +49,14 @@ export const auth = router({
48
49
  throw new Error("Invalid credentials");
49
50
  }
50
51
 
51
- return { id: user.id, email: user.email, name: user.name };
52
+ const session = await ctx.db.session.create({
53
+ data: {
54
+ userId: user.id,
55
+ expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30),
56
+ },
57
+ });
58
+
59
+ return { id: session.id, session: session.id, user: { id: user.id, email: user.email, name: user.name, image: user.image } };
52
60
  }),
53
61
  getSession: publicProcedure.query(async ({ ctx }) => {
54
62
  const session = await ctx.db.session.findUnique({
@@ -73,6 +81,14 @@ export const auth = router({
73
81
  throw new Error("User not found");
74
82
  }
75
83
 
84
+ ctx.res.setHeader("Set-Cookie", serialize("auth_token", session.id, {
85
+ httpOnly: true,
86
+ secure: process.env.NODE_ENV === "production",
87
+ sameSite: "none", // cross-origin XHR needs None
88
+ path: "/",
89
+ maxAge: 60 * 60 * 24 * 7,
90
+ }));
91
+
76
92
  return { id: session.id, userId: session.userId, user: { id: user.id, email: user.email, name: user.name, image: user.image } };
77
93
  }),
78
94
  });
@@ -0,0 +1,129 @@
1
+ import { z } from 'zod';
2
+ import { TRPCError } from '@trpc/server';
3
+ import { router, authedProcedure } from '../trpc.js';
4
+ // Prisma enum values mapped manually to avoid type import issues in ESM
5
+ const ArtifactType = {
6
+ STUDY_GUIDE: 'STUDY_GUIDE',
7
+ FLASHCARD_SET: 'FLASHCARD_SET',
8
+ WORKSHEET: 'WORKSHEET',
9
+ MEETING_SUMMARY: 'MEETING_SUMMARY',
10
+ PODCAST_EPISODE: 'PODCAST_EPISODE',
11
+ } as const;
12
+
13
+ export const flashcards = router({
14
+ listSets: authedProcedure
15
+ .input(z.object({ workspaceId: z.string().uuid() }))
16
+ .query(async ({ ctx, input }) => {
17
+ const workspace = await ctx.db.workspace.findFirst({
18
+ where: { id: input.workspaceId, ownerId: ctx.session.user.id },
19
+ });
20
+ if (!workspace) throw new TRPCError({ code: 'NOT_FOUND' });
21
+ return ctx.db.artifact.findMany({
22
+ where: { workspaceId: input.workspaceId, type: ArtifactType.FLASHCARD_SET },
23
+ orderBy: { updatedAt: 'desc' },
24
+ });
25
+ }),
26
+
27
+ createSet: authedProcedure
28
+ .input(z.object({ workspaceId: z.string().uuid(), title: z.string().min(1).max(120) }))
29
+ .mutation(async ({ ctx, input }) => {
30
+ const workspace = await ctx.db.workspace.findFirst({
31
+ where: { id: input.workspaceId, ownerId: ctx.session.user.id },
32
+ });
33
+ if (!workspace) throw new TRPCError({ code: 'NOT_FOUND' });
34
+ return ctx.db.artifact.create({
35
+ data: {
36
+ workspaceId: input.workspaceId,
37
+ type: ArtifactType.FLASHCARD_SET,
38
+ title: input.title,
39
+ createdById: ctx.session.user.id,
40
+ },
41
+ });
42
+ }),
43
+
44
+ getSet: authedProcedure
45
+ .input(z.object({ setId: z.string().uuid() }))
46
+ .query(async ({ ctx, input }) => {
47
+ const set = await ctx.db.artifact.findFirst({
48
+ where: {
49
+ id: input.setId,
50
+ type: ArtifactType.FLASHCARD_SET,
51
+ workspace: { ownerId: ctx.session.user.id },
52
+ },
53
+ include: { flashcards: true },
54
+ });
55
+ if (!set) throw new TRPCError({ code: 'NOT_FOUND' });
56
+ return set;
57
+ }),
58
+
59
+ createCard: authedProcedure
60
+ .input(z.object({
61
+ setId: z.string().uuid(),
62
+ front: z.string().min(1),
63
+ back: z.string().min(1),
64
+ tags: z.array(z.string()).optional(),
65
+ order: z.number().int().optional(),
66
+ }))
67
+ .mutation(async ({ ctx, input }) => {
68
+ const set = await ctx.db.artifact.findFirst({
69
+ where: { id: input.setId, type: ArtifactType.FLASHCARD_SET, workspace: { ownerId: ctx.session.user.id } },
70
+ });
71
+ if (!set) throw new TRPCError({ code: 'NOT_FOUND' });
72
+ return ctx.db.flashcard.create({
73
+ data: {
74
+ artifactId: input.setId,
75
+ front: input.front,
76
+ back: input.back,
77
+ tags: input.tags ?? [],
78
+ order: input.order ?? 0,
79
+ },
80
+ });
81
+ }),
82
+
83
+ updateCard: authedProcedure
84
+ .input(z.object({
85
+ cardId: z.string().uuid(),
86
+ front: z.string().optional(),
87
+ back: z.string().optional(),
88
+ tags: z.array(z.string()).optional(),
89
+ order: z.number().int().optional(),
90
+ }))
91
+ .mutation(async ({ ctx, input }) => {
92
+ const card = await ctx.db.flashcard.findFirst({
93
+ where: { id: input.cardId, artifact: { type: ArtifactType.FLASHCARD_SET, workspace: { ownerId: ctx.session.user.id } } },
94
+ });
95
+ if (!card) throw new TRPCError({ code: 'NOT_FOUND' });
96
+ return ctx.db.flashcard.update({
97
+ where: { id: input.cardId },
98
+ data: {
99
+ front: input.front ?? card.front,
100
+ back: input.back ?? card.back,
101
+ tags: input.tags ?? card.tags,
102
+ order: input.order ?? card.order,
103
+ },
104
+ });
105
+ }),
106
+
107
+ deleteCard: authedProcedure
108
+ .input(z.object({ cardId: z.string().uuid() }))
109
+ .mutation(async ({ ctx, input }) => {
110
+ const card = await ctx.db.flashcard.findFirst({
111
+ where: { id: input.cardId, artifact: { workspace: { ownerId: ctx.session.user.id } } },
112
+ });
113
+ if (!card) throw new TRPCError({ code: 'NOT_FOUND' });
114
+ await ctx.db.flashcard.delete({ where: { id: input.cardId } });
115
+ return true;
116
+ }),
117
+
118
+ deleteSet: authedProcedure
119
+ .input(z.object({ setId: z.string().uuid() }))
120
+ .mutation(async ({ ctx, input }) => {
121
+ const deleted = await ctx.db.artifact.deleteMany({
122
+ where: { id: input.setId, type: ArtifactType.FLASHCARD_SET, workspace: { ownerId: ctx.session.user.id } },
123
+ });
124
+ if (deleted.count === 0) throw new TRPCError({ code: 'NOT_FOUND' });
125
+ return true;
126
+ }),
127
+ });
128
+
129
+
@@ -0,0 +1,143 @@
1
+ import { z } from 'zod';
2
+ import { TRPCError } from '@trpc/server';
3
+ import { router, authedProcedure } from '../trpc.js';
4
+
5
+ // Avoid importing Prisma enums directly; mirror values as string literals
6
+ const ArtifactType = {
7
+ WORKSHEET: 'WORKSHEET',
8
+ } as const;
9
+
10
+ const Difficulty = {
11
+ EASY: 'EASY',
12
+ MEDIUM: 'MEDIUM',
13
+ HARD: 'HARD',
14
+ } as const;
15
+
16
+ export const worksheets = router({
17
+ // List all worksheet artifacts for a workspace
18
+ listSets: authedProcedure
19
+ .input(z.object({ workspaceId: z.string().uuid() }))
20
+ .query(async ({ ctx, input }) => {
21
+ const workspace = await ctx.db.workspace.findFirst({
22
+ where: { id: input.workspaceId, ownerId: ctx.session.user.id },
23
+ });
24
+ if (!workspace) throw new TRPCError({ code: 'NOT_FOUND' });
25
+ return ctx.db.artifact.findMany({
26
+ where: { workspaceId: input.workspaceId, type: ArtifactType.WORKSHEET },
27
+ orderBy: { updatedAt: 'desc' },
28
+ });
29
+ }),
30
+
31
+ // Create a worksheet set
32
+ createSet: authedProcedure
33
+ .input(z.object({ workspaceId: z.string().uuid(), title: z.string().min(1).max(120) }))
34
+ .mutation(async ({ ctx, input }) => {
35
+ const workspace = await ctx.db.workspace.findFirst({
36
+ where: { id: input.workspaceId, ownerId: ctx.session.user.id },
37
+ });
38
+ if (!workspace) throw new TRPCError({ code: 'NOT_FOUND' });
39
+ return ctx.db.artifact.create({
40
+ data: {
41
+ workspaceId: input.workspaceId,
42
+ type: ArtifactType.WORKSHEET,
43
+ title: input.title,
44
+ createdById: ctx.session.user.id,
45
+ },
46
+ });
47
+ }),
48
+
49
+ // Get a worksheet with its questions
50
+ getSet: authedProcedure
51
+ .input(z.object({ setId: z.string().uuid() }))
52
+ .query(async ({ ctx, input }) => {
53
+ const set = await ctx.db.artifact.findFirst({
54
+ where: {
55
+ id: input.setId,
56
+ type: ArtifactType.WORKSHEET,
57
+ workspace: { ownerId: ctx.session.user.id },
58
+ },
59
+ include: { questions: true },
60
+ });
61
+ if (!set) throw new TRPCError({ code: 'NOT_FOUND' });
62
+ return set;
63
+ }),
64
+
65
+ // Add a question to a worksheet
66
+ createQuestion: authedProcedure
67
+ .input(z.object({
68
+ setId: z.string().uuid(),
69
+ prompt: z.string().min(1),
70
+ answer: z.string().optional(),
71
+ difficulty: z.enum(['EASY', 'MEDIUM', 'HARD']).optional(),
72
+ order: z.number().int().optional(),
73
+ meta: z.record(z.string(), z.unknown()).optional(),
74
+ }))
75
+ .mutation(async ({ ctx, input }) => {
76
+ const set = await ctx.db.artifact.findFirst({
77
+ where: { id: input.setId, type: ArtifactType.WORKSHEET, workspace: { ownerId: ctx.session.user.id } },
78
+ });
79
+ if (!set) throw new TRPCError({ code: 'NOT_FOUND' });
80
+ return ctx.db.worksheetQuestion.create({
81
+ data: {
82
+ artifactId: input.setId,
83
+ prompt: input.prompt,
84
+ answer: input.answer,
85
+ difficulty: (input.difficulty ?? Difficulty.MEDIUM) as any,
86
+ order: input.order ?? 0,
87
+ meta: input.meta as any,
88
+ },
89
+ });
90
+ }),
91
+
92
+ // Update a question
93
+ updateQuestion: authedProcedure
94
+ .input(z.object({
95
+ questionId: z.string().uuid(),
96
+ prompt: z.string().optional(),
97
+ answer: z.string().optional(),
98
+ difficulty: z.enum(['EASY', 'MEDIUM', 'HARD']).optional(),
99
+ order: z.number().int().optional(),
100
+ meta: z.record(z.string(), z.unknown()).optional(),
101
+ }))
102
+ .mutation(async ({ ctx, input }) => {
103
+ const q = await ctx.db.worksheetQuestion.findFirst({
104
+ where: { id: input.questionId, artifact: { type: ArtifactType.WORKSHEET, workspace: { ownerId: ctx.session.user.id } } },
105
+ });
106
+ if (!q) throw new TRPCError({ code: 'NOT_FOUND' });
107
+ return ctx.db.worksheetQuestion.update({
108
+ where: { id: input.questionId },
109
+ data: {
110
+ prompt: input.prompt ?? q.prompt,
111
+ answer: input.answer ?? q.answer,
112
+ difficulty: (input.difficulty ?? q.difficulty) as any,
113
+ order: input.order ?? q.order,
114
+ meta: (input.meta ?? q.meta) as any,
115
+ },
116
+ });
117
+ }),
118
+
119
+ // Delete a question
120
+ deleteQuestion: authedProcedure
121
+ .input(z.object({ questionId: z.string().uuid() }))
122
+ .mutation(async ({ ctx, input }) => {
123
+ const q = await ctx.db.worksheetQuestion.findFirst({
124
+ where: { id: input.questionId, artifact: { workspace: { ownerId: ctx.session.user.id } } },
125
+ });
126
+ if (!q) throw new TRPCError({ code: 'NOT_FOUND' });
127
+ await ctx.db.worksheetQuestion.delete({ where: { id: input.questionId } });
128
+ return true;
129
+ }),
130
+
131
+ // Delete a worksheet set and its questions
132
+ deleteSet: authedProcedure
133
+ .input(z.object({ setId: z.string().uuid() }))
134
+ .mutation(async ({ ctx, input }) => {
135
+ const deleted = await ctx.db.artifact.deleteMany({
136
+ where: { id: input.setId, type: ArtifactType.WORKSHEET, workspace: { ownerId: ctx.session.user.id } },
137
+ });
138
+ if (deleted.count === 0) throw new TRPCError({ code: 'NOT_FOUND' });
139
+ return true;
140
+ }),
141
+ });
142
+
143
+
@@ -1,16 +1,17 @@
1
1
  import { z } from 'zod';
2
+ import { TRPCError } from '@trpc/server';
2
3
  import { router, publicProcedure, authedProcedure } from '../trpc.js';
3
4
  import { bucket } from '../lib/storage.js';
4
- import { FileAsset } from '@prisma/client';
5
5
 
6
6
  export const workspace = router({
7
- // Mutation with Zod input
7
+ // List current user's workspaces
8
8
  list: authedProcedure
9
- .query(async ({ ctx, input }) => {
9
+ .query(async ({ ctx }) => {
10
10
  const workspaces = await ctx.db.workspace.findMany({
11
11
  where: {
12
- ownerId: ctx.session?.user.id,
12
+ ownerId: ctx.session.user.id,
13
13
  },
14
+ orderBy: { updatedAt: 'desc' },
14
15
  });
15
16
  return workspaces;
16
17
  }),
@@ -20,25 +21,26 @@ export const workspace = router({
20
21
  name: z.string().min(1).max(100),
21
22
  description: z.string().max(500).optional(),
22
23
  }))
23
- .mutation(({ ctx, input}) => {
24
- return ctx.db.workspace.create({
24
+ .mutation(async ({ ctx, input}) => {
25
+ const ws = await ctx.db.workspace.create({
25
26
  data: {
26
27
  title: input.name,
27
28
  description: input.description,
28
- ownerId: ctx.session?.user.id,
29
+ ownerId: ctx.session.user.id,
29
30
  },
30
31
  });
32
+ return ws;
31
33
  }),
32
34
  get: authedProcedure
33
35
  .input(z.object({
34
36
  id: z.string().uuid(),
35
37
  }))
36
- .query(({ ctx, input }) => {
37
- return ctx.db.workspace.findUnique({
38
- where: {
39
- id: input.id,
40
- },
38
+ .query(async ({ ctx, input }) => {
39
+ const ws = await ctx.db.workspace.findFirst({
40
+ where: { id: input.id, ownerId: ctx.session.user.id },
41
41
  });
42
+ if (!ws) throw new TRPCError({ code: 'NOT_FOUND' });
43
+ return ws;
42
44
  }),
43
45
  update: authedProcedure
44
46
  .input(z.object({
@@ -46,27 +48,29 @@ export const workspace = router({
46
48
  name: z.string().min(1).max(100).optional(),
47
49
  description: z.string().max(500).optional(),
48
50
  }))
49
- .mutation(({ ctx, input }) => {
50
- return ctx.db.workspace.update({
51
- where: {
52
- id: input.id,
53
- },
51
+ .mutation(async ({ ctx, input }) => {
52
+ const existed = await ctx.db.workspace.findFirst({
53
+ where: { id: input.id, ownerId: ctx.session.user.id },
54
+ });
55
+ if (!existed) throw new TRPCError({ code: 'NOT_FOUND' });
56
+ const updated = await ctx.db.workspace.update({
57
+ where: { id: input.id },
54
58
  data: {
55
- title: input.name,
59
+ title: input.name ?? existed.title,
56
60
  description: input.description,
57
61
  },
58
- });
62
+ });
63
+ return updated;
59
64
  }),
60
65
  delete: authedProcedure
61
66
  .input(z.object({
62
67
  id: z.string().uuid(),
63
68
  }))
64
- .mutation(({ ctx, input }) => {
65
- ctx.db.workspace.delete({
66
- where: {
67
- id: input.id,
68
- },
69
+ .mutation(async ({ ctx, input }) => {
70
+ const deleted = await ctx.db.workspace.deleteMany({
71
+ where: { id: input.id, ownerId: ctx.session.user.id },
69
72
  });
73
+ if (deleted.count === 0) throw new TRPCError({ code: 'NOT_FOUND' });
70
74
  return true;
71
75
  }),
72
76
  uploadFiles: authedProcedure
@@ -81,6 +85,9 @@ export const workspace = router({
81
85
  ),
82
86
  }))
83
87
  .mutation(async ({ ctx, input }) => {
88
+ // ensure workspace belongs to user
89
+ const ws = await ctx.db.workspace.findFirst({ where: { id: input.id, ownerId: ctx.session.user.id } });
90
+ if (!ws) throw new TRPCError({ code: 'NOT_FOUND' });
84
91
  const results = [];
85
92
 
86
93
  for (const file of input.files) {
@@ -127,31 +134,32 @@ export const workspace = router({
127
134
  fileId: z.array(z.string().uuid()),
128
135
  id: z.string().uuid(),
129
136
  }))
130
- .mutation(({ ctx, input }) => {
131
- const files = ctx.db.fileAsset.findMany({
137
+ .mutation(async ({ ctx, input }) => {
138
+ // ensure files are in the user's workspace
139
+ const files = await ctx.db.fileAsset.findMany({
132
140
  where: {
133
141
  id: { in: input.fileId },
134
142
  workspaceId: input.id,
143
+ userId: ctx.session.user.id,
135
144
  },
136
145
  });
146
+ // Delete from GCS (best-effort)
147
+ for (const file of files) {
148
+ if (file.bucket && file.objectKey) {
149
+ const gcsFile: import('@google-cloud/storage').File = bucket.file(file.objectKey);
150
+ gcsFile.delete({ ignoreNotFound: true }).catch((err: unknown) => {
151
+ console.error(`Error deleting file ${file.objectKey} from bucket ${file.bucket}:`, err);
152
+ });
153
+ }
154
+ }
137
155
 
138
- // Delete from GCS
139
- files.then((fileRecords: FileAsset[]) => {
140
- fileRecords.forEach((file: FileAsset) => {
141
- if (file.bucket && file.objectKey) {
142
- const gcsFile: import('@google-cloud/storage').File = bucket.file(file.objectKey);
143
- gcsFile.delete({ ignoreNotFound: true }).catch((err: unknown) => {
144
- console.error(`Error deleting file ${file.objectKey} from bucket ${file.bucket}:`, err);
145
- });
146
- }
147
- });
148
- });
149
-
150
- return ctx.db.fileAsset.deleteMany({
156
+ await ctx.db.fileAsset.deleteMany({
151
157
  where: {
152
158
  id: { in: input.fileId },
153
159
  workspaceId: input.id,
160
+ userId: ctx.session.user.id,
154
161
  },
155
162
  });
163
+ return true;
156
164
  }),
157
165
  });