swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { getDatabase } from "../db";
import { type NodePgDatabase } from "drizzle-orm/node-postgres";
import * as schema from "../db/schema";
import { eq, and, exists } from "drizzle-orm";
import { alias } from "drizzle-orm/pg-core";
import {
pullRequests,
pullRequestReviewers,
pullRequestAssignees,
pullRequestReviews,
} from "../db/schema/pull-requests";
import { repositories } from "../db/schema/repositories";
import { users } from "../db/schema/users";
// Force cast to avoid union type issues in this file
const db = getDatabase() as NodePgDatabase<typeof schema>;
export type InboxItem = {
id: string;
number: number;
title: string;
repository: {
owner: string;
name: string;
};
author: {
username: string;
avatarUrl: string | null;
};
updatedAt: Date;
type: "review_required" | "changes_requested" | "assigned";
};
export async function getInboxItems(userId: string): Promise<InboxItem[]> {
const items: InboxItem[] = [];
// Alias for repo owner to distinguish from PR author
const repoOwners = alias(users, "repo_owners");
// 1. Review Required
const reviewsRequired = await db
.select({
pr: pullRequests,
repo: repositories,
author: users,
owner: repoOwners
})
.from(pullRequests)
.innerJoin(repositories, eq(pullRequests.repositoryId, repositories.id))
.innerJoin(users, eq(pullRequests.authorId, users.id))
.innerJoin(repoOwners, eq(repositories.ownerId, repoOwners.id))
.innerJoin(
pullRequestReviewers,
eq(pullRequests.id, pullRequestReviewers.pullRequestId)
)
.where(
and(
eq(pullRequestReviewers.userId, userId),
eq(pullRequests.state, "open")
)
);
reviewsRequired.forEach(({ pr, repo, author, owner }) => {
items.push({
id: pr.id,
number: pr.number,
title: pr.title,
repository: { owner: owner.username, name: repo.name },
author: { username: author.username, avatarUrl: author.avatarUrl },
updatedAt: pr.updatedAt,
type: "review_required",
});
});
// 2. Changes Requested
const changesRequested = await db
.select({
pr: pullRequests,
repo: repositories,
author: users,
owner: repoOwners
})
.from(pullRequests)
.innerJoin(repositories, eq(pullRequests.repositoryId, repositories.id))
.innerJoin(users, eq(pullRequests.authorId, users.id))
.innerJoin(repoOwners, eq(repositories.ownerId, repoOwners.id))
.where(
and(
eq(pullRequests.authorId, userId),
eq(pullRequests.state, "open"),
exists(
db
.select()
.from(pullRequestReviews)
.where(
and(
eq(pullRequestReviews.pullRequestId, pullRequests.id),
eq(pullRequestReviews.state, "changes_requested")
)
)
)
)
);
changesRequested.forEach(({ pr, repo, author, owner }) => {
items.push({
id: pr.id,
number: pr.number,
title: pr.title,
repository: { owner: owner.username, name: repo.name },
author: { username: author.username, avatarUrl: author.avatarUrl },
updatedAt: pr.updatedAt,
type: "changes_requested",
});
});
// 3. Assigned
const assigned = await db
.select({
pr: pullRequests,
repo: repositories,
author: users,
owner: repoOwners
})
.from(pullRequests)
.innerJoin(repositories, eq(pullRequests.repositoryId, repositories.id))
.innerJoin(users, eq(pullRequests.authorId, users.id))
.innerJoin(repoOwners, eq(repositories.ownerId, repoOwners.id))
.innerJoin(
pullRequestAssignees,
eq(pullRequests.id, pullRequestAssignees.pullRequestId)
)
.where(
and(
eq(pullRequestAssignees.userId, userId),
eq(pullRequests.state, "open")
)
);
assigned.forEach(({ pr, repo, author, owner }) => {
if (!items.find((i) => i.id === pr.id)) {
items.push({
id: pr.id,
number: pr.number,
title: pr.title,
repository: { owner: owner.username, name: repo.name },
author: { username: author.username, avatarUrl: author.avatarUrl },
updatedAt: pr.updatedAt,
type: "assigned",
});
}
});
return items.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
}