Skip to content

Commit 0a53562

Browse files
committed
move away from distinct and use branded types
1 parent edfbd73 commit 0a53562

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

apps/web/actions/organization/create-space.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import { db } from "@cap/database";
44
import { getCurrentUser } from "@cap/database/auth/session";
5-
import { nanoId, nanoIdLength } from "@cap/database/helpers";
5+
import { nanoId } from "@cap/database/helpers";
66
import { spaceMembers, spaces } from "@cap/database/schema";
7-
import { Space, User } from "@cap/web-domain";
7+
import {
8+
Space,
9+
SpaceMemberId,
10+
type SpaceMemberRole,
11+
User,
12+
} from "@cap/web-domain";
813
import { and, eq } from "drizzle-orm";
914
import { revalidatePath } from "next/cache";
10-
import { v4 as uuidv4 } from "uuid";
1115
import { uploadSpaceIcon } from "./upload-space-icon";
1216

1317
interface CreateSpaceResponse {
@@ -108,10 +112,9 @@ export async function createSpace(
108112
if (memberUserIds.length > 0) {
109113
const spaceMembersToInsert = memberUserIds.map((userId) => {
110114
// Creator is always Admin, others are member
111-
const role =
112-
userId === user.id ? ("Admin" as const) : ("member" as const);
115+
const role: SpaceMemberRole = userId === user.id ? "Admin" : "member";
113116
return {
114-
id: uuidv4().substring(0, nanoIdLength),
117+
id: SpaceMemberId.make(nanoId()),
115118
spaceId,
116119
userId: User.UserId.make(userId),
117120
role,

apps/web/app/(org)/dashboard/dashboard-data.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
organizationMembers,
77
organizations,
88
sharedVideos,
9-
spaceMembers,
109
spaces,
1110
users,
1211
videos,
@@ -135,7 +134,7 @@ export async function getDashboardData(user: typeof userSelectProps) {
135134
return yield* db
136135
.use((db) =>
137136
db
138-
.selectDistinct({
137+
.select({
139138
id: spaces.id,
140139
primary: spaces.primary,
141140
privacy: spaces.privacy,
@@ -152,18 +151,20 @@ export async function getDashboardData(user: typeof userSelectProps) {
152151
)`,
153152
})
154153
.from(spaces)
155-
.leftJoin(spaceMembers, eq(spaces.id, spaceMembers.spaceId))
156-
.leftJoin(users, eq(spaceMembers.userId, users.id))
157154
.where(
158155
and(
159156
eq(spaces.organizationId, activeOrganizationId),
160157
or(
161158
// User is the space creator
162159
eq(spaces.createdById, user.id),
163-
// User is a member of the space
164-
eq(spaceMembers.userId, user.id),
165160
// Space is public within the organization
166161
eq(spaces.privacy, "Public"),
162+
// User is a member of the space
163+
sql`EXISTS (
164+
SELECT 1 FROM space_members
165+
WHERE space_members.spaceId = spaces.id
166+
AND space_members.userId = ${user.id}
167+
)`,
167168
),
168169
),
169170
),

packages/web-domain/src/Space.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ export type SpaceId = typeof SpaceIdOrOrganisationId.Type;
66

77
export const SpaceIdOrOrganisationId = Schema.Union(SpaceId, OrganisationId);
88
export type SpaceIdOrOrganisationId = typeof SpaceIdOrOrganisationId.Type;
9+
10+
export const SpaceMemberId = Schema.String.pipe(Schema.brand("SpaceMemberId"));
11+
export type SpaceMemberId = typeof SpaceMemberId.Type;
12+
13+
export const SpaceMemberRole = Schema.Union(
14+
Schema.Literal("Admin"),
15+
Schema.Literal("member"),
16+
);
17+
export type SpaceMemberRole = typeof SpaceMemberRole.Type;

packages/web-domain/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export { Rpcs } from "./Rpcs.ts";
1414
export * as S3Bucket from "./S3Bucket.ts";
1515
export { S3Error } from "./S3Bucket.ts";
1616
export * as Space from "./Space.ts";
17+
export { SpaceMemberId, SpaceMemberRole } from "./Space.ts";
1718
export * as User from "./User.ts";
1819
export { UserId } from "./User.ts";
1920
export * as Video from "./Video.ts";

0 commit comments

Comments
 (0)