aboutsummaryrefslogtreecommitdiffstats
path: root/apps/workers/trpc.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/workers/trpc.ts')
-rw-r--r--apps/workers/trpc.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/apps/workers/trpc.ts b/apps/workers/trpc.ts
new file mode 100644
index 00000000..cd2e4c99
--- /dev/null
+++ b/apps/workers/trpc.ts
@@ -0,0 +1,33 @@
+import { eq } from "drizzle-orm";
+
+import { db } from "@hoarder/db";
+import { users } from "@hoarder/db/schema";
+import { createCallerFactory } from "@hoarder/trpc";
+import { appRouter } from "@hoarder/trpc/routers/_app";
+
+/**
+ * This is only safe to use in the context of a worker.
+ */
+export async function buildImpersonatingTRPCClient(userId: string) {
+ const createCaller = createCallerFactory(appRouter);
+
+ const user = await db.query.users.findFirst({
+ where: eq(users.id, userId),
+ });
+ if (!user) {
+ throw new Error("User not found");
+ }
+
+ return createCaller({
+ user: {
+ id: user.id,
+ name: user.name,
+ email: user.email,
+ role: user.role,
+ },
+ db,
+ req: {
+ ip: null,
+ },
+ });
+}