Skip to content

Commit 3881b5a

Browse files
committed
not clear how to do mutations and data fetching with server fns
1 parent 39e38a0 commit 3881b5a

9 files changed

Lines changed: 266 additions & 175 deletions

File tree

app/database/schema.sql.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
import { text, pgTable, timestamp, jsonb, serial, integer, primaryKey, uuid } from "drizzle-orm/pg-core";
1+
import { sql } from "drizzle-orm";
2+
import {
3+
text,
4+
pgTable,
5+
timestamp,
6+
jsonb,
7+
serial,
8+
integer,
9+
primaryKey,
10+
uuid,
11+
uniqueIndex,
12+
} from "drizzle-orm/pg-core";
213

3-
export const UserTable = pgTable("users", {
4-
id: text("id").primaryKey(),
5-
externalId: text("external_id").notNull().unique(),
6-
email: text("email").notNull(),
7-
name: text("name").notNull(),
8-
avatarUrl: text("avatar_url"),
9-
});
14+
export const UserTable = pgTable(
15+
"users",
16+
{
17+
id: text("id").primaryKey(),
18+
externalId: text("external_id").notNull().unique(),
19+
email: text("email").notNull(),
20+
name: text("name").notNull(),
21+
avatarUrl: text("avatar_url"),
22+
},
23+
(t) => [uniqueIndex("emailUniqueIndex").on(sql`lower(${t.email})`)],
24+
);
1025

1126
export const SlackInstallationTable = pgTable("slack_installations", {
1227
id: serial("id").primaryKey(),
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ProductFeed } from "@/lib/product";
22
import { readdirSync } from "node:fs";
33

4-
export const getProducts = async (): Promise<ProductFeed[]> => {
4+
export const getFeeds = async (): Promise<ProductFeed[]> => {
55
const products: ProductFeed[] = [];
66
const path = require("path");
77
for (const value of readdirSync(path.join(__dirname))) {

app/functions/run.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ConfigTable, SlackInstallationTable } from "@/database/schema.sql";
22
import { db } from "@/database/db";
33
import { eq, inArray, and, getTableColumns } from "drizzle-orm";
4-
import { getProducts } from "../../products";
4+
import { getFeeds } from "@/feeds";
55
import { ClassifiedMessage } from "@/lib/interfaces";
66
import { client } from "integrations/slack/client";
77

@@ -11,7 +11,7 @@ export async function handler() {
1111
// Fetch new messages from all services
1212
// Classify messages
1313
// Store messages in database
14-
const products = await getProducts();
14+
const products = await getFeeds();
1515
const newMessages = await products.reduce(
1616
async (acc, product) => {
1717
const messages = await product.refreshStatusMessages();
@@ -48,7 +48,7 @@ export async function handler() {
4848
);
4949
toNotify.forEach(async (installation) => {
5050
const response = await client.chat.postMessage({
51-
token: installation.bot.token,
51+
token: installation.botToken,
5252
channel: installation.incomingWebhook.channelId,
5353
text: message.content,
5454
});

app/routes/__root.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import { getWebRequest } from "vinxi/http";
55
import appCss from "@/app.css?url";
66
import { ClerkProvider } from "@clerk/tanstack-start";
77
import { getAuth } from "@clerk/tanstack-start/server";
8+
import { db } from "@/database/db";
9+
import { eq } from "drizzle-orm";
10+
import { UserTable } from "@/database/schema.sql";
811

912
const getUser = createServerFn({ method: "GET" }).handler(async () => {
1013
const { userId } = await getAuth(getWebRequest());
1114

15+
// const [user] = userId ? await db.select().from(UserTable).where(eq(UserTable.id, userId!)) : [];
16+
1217
return { userId };
1318
});
1419

app/routes/_authed.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,33 @@ import { createServerFn } from "@tanstack/start";
77
import { getWebRequest } from "vinxi/http";
88
import { getAuth } from "@clerk/tanstack-start/server";
99
import { clerkClient } from "@clerk/tanstack-start/server";
10+
import { Resource } from "sst";
11+
import { client } from "integrations/slack/client";
12+
import { eq } from "drizzle-orm";
13+
import { db } from "@/database/db";
14+
import { SlackInstallationTable } from "@/database/schema.sql";
15+
16+
const getUser = createServerFn({ method: "GET" }).handler(async () => {
17+
const { userId } = await getAuth(getWebRequest());
18+
if (!userId) throw redirect({ to: "/sign-in/$" });
19+
const clerk = await clerkClient({
20+
secretKey: Resource.ClerkSecretKey.value,
21+
});
22+
const accessTokens = (await clerk.users.getUserOauthAccessToken(userId!, "oauth_slack")).data;
23+
const token = accessTokens[0].token || "";
24+
const userInfo = await client.openid.connect.userInfo({
25+
token,
26+
});
27+
const [installation] = await db
28+
.select({ id: SlackInstallationTable.id })
29+
.from(SlackInstallationTable)
30+
.where(eq(SlackInstallationTable.teamId, userInfo["https://slack.com/team_id"]!));
31+
return { userId, teamId: userInfo["https://slack.com/team_id"]!, installationId: installation!.id };
32+
});
1033

1134
export const Route = createFileRoute("/_authed")({
1235
component: RouteComponent,
13-
loader: ({ context }) => {
14-
if (!context.userId) throw redirect({ to: "/sign-in/$" });
15-
},
36+
beforeLoad: async () => await getUser(),
1637
});
1738

1839
function RouteComponent() {

0 commit comments

Comments
 (0)