Skip to content

Commit

Permalink
update database
Browse files Browse the repository at this point in the history
  • Loading branch information
steeeee0223 committed Jul 17, 2024
1 parent b66b87a commit 884a690
Show file tree
Hide file tree
Showing 135 changed files with 2,739 additions and 1,732 deletions.
2 changes: 1 addition & 1 deletion apps/storybook/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acme/storybook",
"version": "0.2.0",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion apps/storybook/src/stories/custom/crud-item.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const Settings: Story = {
export const TreeItem: Story = {
args: {
id: "test-id",
username: "John Doe",
lastEditedBy: "John Doe",
label: "Folder",
icon: { type: "lucide", name: "folder" },
onClick: () => alert(`Clicked item`),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ export default meta;
type Story = StoryObj<typeof meta>;

const mockSettings: SettingsStore = {
user: {
id: "1",
name: "John Doe",
email: "[email protected]",
imageUrl: "https://github.com/shadcn.png",
},
workspace: {
id: "fake-workspace-id-12345",
name: "John's Private",
Expand All @@ -32,6 +26,8 @@ const mockSettings: SettingsStore = {
avatarUrl: "https://github.com/shadcn.png",
preferredName: "John Doe",
email: "[email protected]",
id: "fake-account-id-123",
name: "John Wick",
},
};

Expand Down
4 changes: 2 additions & 2 deletions apps/storybook/src/stories/notion/settings-panel/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const Panel = () => {
const { data, isOpen, setClose } = useModal<SettingsStore>();
const props: SettingsPanelProps = {
settings: data,
onUpdate: async ({ user, account }) => {
console.log(`mutating`, { user, account });
onUpdate: async ({ account }) => {
console.log(`mutating`, { account });
},
};

Expand Down
53 changes: 32 additions & 21 deletions apps/storybook/src/stories/notion/workspace-provider/utils.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
import type { UserState, Workspace } from "@acme/ui/notion";

const createWorkspaceData = (
name: string,
id: number | string,
user: UserState,
): Workspace => ({
id: `dummy-workspace-${id}`,
name,
icon: "🎑",
owner: user.name,
ownerId: user.id,
members: [],
});

export const user: UserState = {
id: "dummy-user",
name: "Steve",
email: "[email protected]",
isDarkMode: false,
profilePicture: {
url: "",
},
};
export const workspaces: Workspace[] = [
createWorkspaceData("Steve's Workspace", `personal`, user),
createWorkspaceData("Workspace 1", 1, user),
createWorkspaceData("Workspace 2", 2, user),
createWorkspaceData("Workspace 3", 3, user),
{
id: "dummy-workspace-personal",
name: "John's Workspace",
icon: { type: "lucide", name: "activity", color: "#CB912F" },
members: 1,
plan: "Education Plus Plan",
role: "owner",
},
{
id: "dummy-workspace-1",
name: "Workspace 1",
icon: { type: "lucide", name: "briefcase", color: "#337EA9" },
members: 3,
plan: "Free Plan",
role: "owner",
},
{
id: "dummy-workspace-2",
name: "Workspace 2",
icon: { type: "emoji", emoji: "🎨" },
members: 2,
plan: "Business Plan",
role: "member",
},
{
id: "dummy-workspace-3",
name: "Workspace 3",
icon: { type: "emoji", emoji: "🚧" },
members: 8,
plan: "Enterprise Plan",
role: "guest",
},
];
4 changes: 4 additions & 0 deletions apps/worxpace/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const config = {
protocol: "https",
hostname: "files.edgestore.dev",
},
{
protocol: "https",
hostname: "www.notion.so",
},
],
},
webpack: (config, { isServer }) => {
Expand Down
2 changes: 1 addition & 1 deletion apps/worxpace/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acme/worxpace",
"version": "0.3.1",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
Expand Down
56 changes: 33 additions & 23 deletions apps/worxpace/src/actions/archive-document.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
"use server";

import { revalidatePath } from "next/cache";
import type { MutationFetcher } from "swr/mutation";

import type { Document, ENTITY_TYPE } from "@acme/prisma";
import type { ENTITY_TYPE } from "@acme/prisma";
import { type Modified } from "@acme/ui/lib";
import { DeleteDocument, type DeleteDocumentInput } from "@acme/validators";

import {
account,
auditLogs,
createMutationFetcher,
documents,
fetchClient,
UnauthorizedError,
type Action,
type DetailedDocument,
type DocumentsKey,
} from "~/lib";

const handler: Action<DeleteDocumentInput, Modified<Document>> = async (
_key,
{ arg },
) => {
try {
const { userId, orgId } = fetchClient();
const result = await documents.archive({ ...arg, userId, orgId });
/** Activity Log */
const type = result.item.type.toUpperCase() as ENTITY_TYPE;
await auditLogs.create(
{ title: result.item.title, entityId: arg.id, type },
"DELETE",
);
revalidatePath(`/documents/${arg.id}`);
return result;
} catch (error) {
if (error instanceof UnauthorizedError) throw error;
throw new Error("Failed to archive document.");
}
};
const handler = createMutationFetcher(
DeleteDocument,
async (workspaceId, { arg }) => {
try {
const { clerkId } = fetchClient();
const inWorkspace = await account.isInWorkspace({ clerkId, workspaceId });
if (!inWorkspace) throw new UnauthorizedError();
const result = await documents.archive(arg);
/** Activity Log */
const type = result.item.type.toUpperCase() as ENTITY_TYPE;
await auditLogs.create({
entity: { title: result.item.title, entityId: arg.id, type },
action: "DELETE",
accountId: arg.accountId,
});
revalidatePath(`/documents/${arg.id}`);
return result;
} catch (error) {
if (error instanceof UnauthorizedError) throw error;
throw new Error("Failed to archive document.");
}
},
);

export const archiveDocument = createMutationFetcher(DeleteDocument, handler);
export const archiveDocument: MutationFetcher<
Modified<DetailedDocument>,
DocumentsKey,
DeleteDocumentInput
> = ({ workspaceId }, data) => handler(workspaceId, data);
24 changes: 15 additions & 9 deletions apps/worxpace/src/actions/copy-card.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use server";

import { revalidatePath } from "next/cache";
import type { MutationFetcher } from "swr/mutation";

import type { Card } from "@acme/prisma";
import { CopyCard, type CopyCardInput } from "@acme/validators";
Expand All @@ -11,31 +12,36 @@ import {
fetchClient,
kanban,
UnauthorizedError,
type Action,
type KanbanKey,
} from "~/lib";

const handler: Action<CopyCardInput, Card> = async (_key, { arg }) => {
const { src, dest, boardId } = arg;
const handler = createMutationFetcher(CopyCard, async (_key, { arg }) => {
const { src, dest, boardId, accountId } = arg;
try {
fetchClient();
const srcCard = await kanban.getCard(src.id);
if (!srcCard) throw new Error("Not found");

const result = await kanban.createCard({
accountId,
...dest,
description: srcCard.description,
});
/** Activity Log */
await auditLogs.create(
{ entityId: boardId, title: dest.title, type: "ITEM" },
"CREATE",
);
await auditLogs.create({
entity: { entityId: boardId, title: dest.title, type: "ITEM" },
action: "CREATE",
accountId,
});
revalidatePath(`/kanban/${boardId}`);
return result;
} catch (error) {
if (error instanceof UnauthorizedError) throw error;
throw new Error("Failed to copy card.");
}
};
});

export const copyCard = createMutationFetcher(CopyCard, handler);
export const copyCard: MutationFetcher<Card, KanbanKey, CopyCardInput> = (
{ boardId },
data,
) => handler(boardId, data);
24 changes: 15 additions & 9 deletions apps/worxpace/src/actions/copy-list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use server";

import { revalidatePath } from "next/cache";
import type { MutationFetcher } from "swr/mutation";

import type { List } from "@acme/prisma";
import { CopyList, type CopyListInput } from "@acme/validators";
Expand All @@ -11,11 +12,11 @@ import {
fetchClient,
kanban,
UnauthorizedError,
type Action,
type KanbanKey,
} from "~/lib";

const handler: Action<CopyListInput, List> = async (_key, { arg }) => {
const { boardId, srcId, destId } = arg;
const handler = createMutationFetcher(CopyList, async (_key, { arg }) => {
const { boardId, srcId, destId, accountId } = arg;
try {
fetchClient();
const srcList = await kanban.getListById({ boardId, id: srcId });
Expand All @@ -32,6 +33,7 @@ const handler: Action<CopyListInput, List> = async (_key, { arg }) => {
: undefined;
const result = await kanban.createList(
{
accountId,
id: destId,
title: `${srcList.title} Copy`,
order: numLists + 1,
Expand All @@ -40,16 +42,20 @@ const handler: Action<CopyListInput, List> = async (_key, { arg }) => {
cards,
);
/** Activity Log */
await auditLogs.create(
{ title: result.title, entityId: boardId, type: "LIST" },
"CREATE",
);
await auditLogs.create({
entity: { title: result.title, entityId: boardId, type: "LIST" },
action: "CREATE",
accountId,
});
revalidatePath(`/kanban/${boardId}`);
return result;
} catch (error) {
if (error instanceof UnauthorizedError) throw error;
throw new Error("Failed to copy list.");
}
};
});

export const copyList = createMutationFetcher(CopyList, handler);
export const copyList: MutationFetcher<List, KanbanKey, CopyListInput> = (
{ boardId },
data,
) => handler(boardId, data);
28 changes: 28 additions & 0 deletions apps/worxpace/src/actions/create-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use server";

import type { MutationFetcher } from "swr/mutation";

import type { Account } from "@acme/prisma";
import { CreateAccount, type CreateAccountInput } from "@acme/validators";

import { account, createMutationFetcher, UnauthorizedError } from "~/lib";

const handler = createMutationFetcher(
CreateAccount,
async (clerkId, { arg }) => {
try {
const data = await account.get(clerkId);
if (data) return data;
return await account.create(arg);
} catch (error) {
if (error instanceof UnauthorizedError) throw error;
throw new Error("Failed to create account.");
}
},
);

export const createAccount: MutationFetcher<
Account,
{ type: "settings"; clerkId: string },
CreateAccountInput
> = ({ clerkId }, data) => handler(clerkId, data);
25 changes: 15 additions & 10 deletions apps/worxpace/src/actions/create-card.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use server";

import { revalidatePath } from "next/cache";
import type { MutationFetcher } from "swr/mutation";

import type { Card } from "@acme/prisma";
import { CreateCard, type CreateCardInput } from "@acme/validators";
Expand All @@ -11,25 +12,29 @@ import {
fetchClient,
kanban,
UnauthorizedError,
type Action,
type KanbanKey,
} from "~/lib";

const handler: Action<CreateCardInput, Card> = async (_key, { arg }) => {
const { boardId, ...info } = arg;
const handler = createMutationFetcher(CreateCard, async (_key, { arg }) => {
const { boardId, ...data } = arg;
try {
fetchClient();
const result = await kanban.createCard(info);
const result = await kanban.createCard(data);
/** Activity Log */
await auditLogs.create(
{ title: arg.title, entityId: boardId, type: "ITEM" },
"CREATE",
);
await auditLogs.create({
entity: { title: arg.title, entityId: boardId, type: "ITEM" },
action: "CREATE",
accountId: data.accountId,
});
revalidatePath(`/kanban/${boardId}`);
return result;
} catch (error) {
if (error instanceof UnauthorizedError) throw error;
throw new Error("Failed to create card.");
}
};
});

export const createCard = createMutationFetcher(CreateCard, handler);
export const createCard: MutationFetcher<Card, KanbanKey, CreateCardInput> = (
{ boardId },
data,
) => handler(boardId, data);
Loading

0 comments on commit 884a690

Please sign in to comment.