feat: add repo funcs and share link toggle
This commit is contained in:
71
packages/db/src/repository/inviteLink.repo.ts
Normal file
71
packages/db/src/repository/inviteLink.repo.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { and, eq, gt } from "drizzle-orm";
|
||||
|
||||
import type { dbClient } from "@kan/db/client";
|
||||
import { workspaceInviteLinks } from "@kan/db/schema";
|
||||
import { generateUID } from "@kan/shared/utils";
|
||||
|
||||
export const createInviteLink = async (
|
||||
db: dbClient,
|
||||
args: {
|
||||
workspaceId: number;
|
||||
code: string;
|
||||
expiresAt: Date | null;
|
||||
createdBy: string;
|
||||
},
|
||||
) => {
|
||||
const [result] = await db
|
||||
.insert(workspaceInviteLinks)
|
||||
.values({
|
||||
publicId: generateUID(),
|
||||
workspaceId: args.workspaceId,
|
||||
code: args.code,
|
||||
expiresAt: args.expiresAt ?? null,
|
||||
status: "active",
|
||||
createdBy: args.createdBy,
|
||||
})
|
||||
.returning({
|
||||
publicId: workspaceInviteLinks.publicId,
|
||||
code: workspaceInviteLinks.code,
|
||||
status: workspaceInviteLinks.status,
|
||||
expiresAt: workspaceInviteLinks.expiresAt,
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
export const deactivateAllActiveForWorkspace = async (
|
||||
db: dbClient,
|
||||
args: { workspaceId: number; updatedBy: string },
|
||||
) => {
|
||||
await db
|
||||
.update(workspaceInviteLinks)
|
||||
.set({
|
||||
status: "inactive",
|
||||
updatedBy: args.updatedBy,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(workspaceInviteLinks.workspaceId, args.workspaceId),
|
||||
eq(workspaceInviteLinks.status, "active"),
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
export const getActiveForWorkspace = async (
|
||||
db: dbClient,
|
||||
workspaceId: number,
|
||||
) => {
|
||||
return db.query.workspaceInviteLinks.findFirst({
|
||||
where: and(
|
||||
eq(workspaceInviteLinks.workspaceId, workspaceId),
|
||||
eq(workspaceInviteLinks.status, "active"),
|
||||
),
|
||||
orderBy: (links, { desc }) => [desc(links.createdAt)],
|
||||
});
|
||||
};
|
||||
|
||||
export const getByCode = async (db: dbClient, code: string) => {
|
||||
return db.query.workspaceInviteLinks.findFirst({
|
||||
where: eq(workspaceInviteLinks.code, code),
|
||||
});
|
||||
};
|
||||
@@ -87,11 +87,25 @@ export const getByPublicId = (db: dbClient, workspacePublicId: string) => {
|
||||
publicId: true,
|
||||
name: true,
|
||||
plan: true,
|
||||
slug: true,
|
||||
},
|
||||
where: eq(workspaces.publicId, workspacePublicId),
|
||||
});
|
||||
};
|
||||
|
||||
export const getById = (db: dbClient, workspaceId: number) => {
|
||||
return db.query.workspaces.findFirst({
|
||||
columns: {
|
||||
id: true,
|
||||
publicId: true,
|
||||
name: true,
|
||||
plan: true,
|
||||
slug: true,
|
||||
},
|
||||
where: eq(workspaces.id, workspaceId),
|
||||
});
|
||||
};
|
||||
|
||||
export const getByPublicIdWithMembers = (
|
||||
db: dbClient,
|
||||
workspacePublicId: string,
|
||||
|
||||
@@ -11,3 +11,4 @@ export * from "./users";
|
||||
export * from "./integrations";
|
||||
export * from "./workspaces";
|
||||
export * from "./subscriptions";
|
||||
export * from "./workspaceInviteLinks";
|
||||
|
||||
@@ -4,9 +4,11 @@ import {
|
||||
pgEnum,
|
||||
pgTable,
|
||||
timestamp,
|
||||
uuid,
|
||||
varchar,
|
||||
} from "drizzle-orm/pg-core";
|
||||
|
||||
import { users } from "./users";
|
||||
import { workspaces } from "./workspaces";
|
||||
|
||||
export const inviteLinkStatuses = ["active", "inactive"] as const;
|
||||
@@ -18,6 +20,7 @@ export const inviteLinkStatusEnum = pgEnum(
|
||||
|
||||
export const workspaceInviteLinks = pgTable("workspace_invite_links", {
|
||||
id: bigserial("id", { mode: "number" }).primaryKey(),
|
||||
publicId: varchar("publicId", { length: 12 }).notNull().unique(),
|
||||
workspaceId: bigint("workspaceId", { mode: "number" })
|
||||
.notNull()
|
||||
.references(() => workspaces.id, { onDelete: "cascade" }),
|
||||
@@ -25,4 +28,11 @@ export const workspaceInviteLinks = pgTable("workspace_invite_links", {
|
||||
status: inviteLinkStatusEnum("status").notNull().default("active"),
|
||||
expiresAt: timestamp("expiresAt"),
|
||||
createdAt: timestamp("createdAt").defaultNow().notNull(),
|
||||
createdBy: uuid("createdBy").references(() => users.id, {
|
||||
onDelete: "set null",
|
||||
}),
|
||||
updatedAt: timestamp("updatedAt"),
|
||||
updatedBy: uuid("updatedBy").references(() => users.id, {
|
||||
onDelete: "set null",
|
||||
}),
|
||||
}).enableRLS();
|
||||
|
||||
Reference in New Issue
Block a user