* feat: add workspace invite links schema * feat: update workspace invite schema * feat: add repo funcs and share link toggle * feat: redirect to next param on authentication * feat: add invite page * feat: switch to workspace on invite success * refactor: tweak light mode styles * feat(cloud): update subscription for cloud * feat: only allow admin users to create links * chore: add translations
72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
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),
|
|
});
|
|
};
|