/ opencodehub / src / db / schema / slack-integration.ts
swadhinbiswas Fix: Add date validation in analysis.ts to prevent RangeError · 1/20/2026
a7662cd
135 lines · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 * Slack Integration Schema
 * Workspace connections, channel mappings, and notification preferences
 */

import { relations } from "drizzle-orm";
import { boolean, integer, pgTable, text, timestamp } from "drizzle-orm/pg-core";
// import { organizations } from "./organizations";
import { repositories } from "./repositories";
import { users } from "./users";

// Slack workspace connections
export const slackWorkspaces = pgTable("slack_workspaces", {
    id: text("id").primaryKey(),
    // organizationId: text("organization_id").references(() => organizations.id, { onDelete: "cascade" }), // Removed as per instruction

    // Slack info
    teamId: text("team_id").notNull(), // Slack team/workspace ID
    teamName: text("team_name"),
    teamDomain: text("team_domain"),

    // Auth
    accessToken: text("access_token").notNull(),
    botUserId: text("bot_user_id"),
    botAccessToken: text("bot_access_token"),

    // Scopes
    scopes: text("scopes"), // JSON array of granted scopes

    // Installation
    installedById: text("installed_by_id")
        .notNull()
        .references(() => users.id),
    installedAt: timestamp("installed_at").notNull().defaultNow(),

    // Status
    isActive: boolean("is_active").default(true),
    lastUsedAt: timestamp("last_used_at"),
});

// Channel mappings - which repos notify which channels
export const slackChannelMappings = pgTable("slack_channel_mappings", {
    id: text("id").primaryKey(),
    workspaceId: text("workspace_id")
        .notNull()
        .references(() => slackWorkspaces.id, { onDelete: "cascade" }),
    repositoryId: text("repository_id")
        .notNull()
        .references(() => repositories.id, { onDelete: "cascade" }),

    // Channel info
    channelId: text("channel_id").notNull(),
    channelName: text("channel_name"),

    // Notification settings (JSON array)
    // ["pr_created", "pr_merged", "review_requested", "ci_failed", "ci_passed"]
    notifyOn: text("notify_on").default('["pr_created","pr_merged","review_requested","ci_failed"]'),

    // Filters
    notifyBranches: text("notify_branches"), // JSON: branch patterns to notify on
    notifyAuthors: text("notify_authors"), // JSON: specific user IDs or "all"

    isActive: boolean("is_active").default(true),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow(),
});

// User Slack mappings - for DMs
export const slackUserMappings = pgTable("slack_user_mappings", {
    id: text("id").primaryKey(),
    userId: text("user_id")
        .notNull()
        .references(() => users.id, { onDelete: "cascade" }),
    workspaceId: text("workspace_id")
        .notNull()
        .references(() => slackWorkspaces.id, { onDelete: "cascade" }),

    // Slack user info
    slackUserId: text("slack_user_id").notNull(),
    slackUsername: text("slack_username"),

    // DM preferences (JSON)
    // { "review_requested": true, "pr_approved": true, "ci_failed": true, "mentions": true }
    dmPreferences: text("dm_preferences").default('{"review_requested":true,"pr_approved":true,"ci_failed":true}'),

    // Do not disturb
    dndEnabled: boolean("dnd_enabled").default(false),
    dndStart: text("dnd_start"), // HH:MM
    dndEnd: text("dnd_end"), // HH:MM
    dndTimezone: text("dnd_timezone"),

    isActive: boolean("is_active").default(true),
    createdAt: timestamp("created_at").notNull().defaultNow(),
});

// Relations
export const slackWorkspacesRelations = relations(slackWorkspaces, ({ one, many }) => ({
    installedBy: one(users, {
        fields: [slackWorkspaces.installedById],
        references: [users.id],
    }),
    channelMappings: many(slackChannelMappings),
    userMappings: many(slackUserMappings),
}));

export const slackChannelMappingsRelations = relations(slackChannelMappings, ({ one }) => ({
    workspace: one(slackWorkspaces, {
        fields: [slackChannelMappings.workspaceId],
        references: [slackWorkspaces.id],
    }),
    repository: one(repositories, {
        fields: [slackChannelMappings.repositoryId],
        references: [repositories.id],
    }),
}));

export const slackUserMappingsRelations = relations(slackUserMappings, ({ one }) => ({
    user: one(users, {
        fields: [slackUserMappings.userId],
        references: [users.id],
    }),
    workspace: one(slackWorkspaces, {
        fields: [slackUserMappings.workspaceId],
        references: [slackWorkspaces.id],
    }),
}));

// Types
export type SlackWorkspace = typeof slackWorkspaces.$inferSelect;
export type NewSlackWorkspace = typeof slackWorkspaces.$inferInsert;
export type SlackChannelMapping = typeof slackChannelMappings.$inferSelect;
export type NewSlackChannelMapping = typeof slackChannelMappings.$inferInsert;
export type SlackUserMapping = typeof slackUserMappings.$inferSelect;
export type NewSlackUserMapping = typeof slackUserMappings.$inferInsert;