Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tags #321

Merged
merged 8 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions apps/dashboard/src/actions/create-tag-action.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use server";

import { LogEvents } from "@midday/events/events";
import { authActionClient } from "./safe-action";
import { createTagSchema } from "./schema";

export const createTagAction = authActionClient
.schema(createTagSchema)
.metadata({
name: "create-tag",
track: {
event: LogEvents.CreateTag.name,
channel: LogEvents.CreateTag.channel,
},
})
.action(async ({ parsedInput: { name }, ctx: { user, supabase } }) => {
const { data } = await supabase
.from("tags")
.insert({
name,
team_id: user.team_id!,
})
.select("id, name")
.single();

return data;
});
32 changes: 32 additions & 0 deletions apps/dashboard/src/actions/create-transaction-tag-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use server";

import { LogEvents } from "@midday/events/events";
import { revalidateTag } from "next/cache";
import { authActionClient } from "./safe-action";
import { createTransactionTagSchema } from "./schema";

export const createTransactionTagAction = authActionClient
.schema(createTransactionTagSchema)
.metadata({
name: "create-transaction-tag",
track: {
event: LogEvents.CreateTransactionTag.name,
channel: LogEvents.CreateTransactionTag.channel,
},
})
.action(
async ({
parsedInput: { tagId, transactionId },
ctx: { user, supabase },
}) => {
const { data } = await supabase.from("transaction_tags").insert({
tag_id: tagId,
transaction_id: transactionId,
team_id: user.team_id!,
});

revalidateTag(`transactions_${user.team_id}`);

return data;
},
);
32 changes: 32 additions & 0 deletions apps/dashboard/src/actions/delete-transaction-tag-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use server";

import { LogEvents } from "@midday/events/events";
import { revalidateTag } from "next/cache";
import { authActionClient } from "./safe-action";
import { deleteTransactionTagSchema } from "./schema";

export const deleteTransactionTagAction = authActionClient
.schema(deleteTransactionTagSchema)
.metadata({
name: "delete-transaction-tag",
track: {
event: LogEvents.DeleteTransactionTag.name,
channel: LogEvents.DeleteTransactionTag.channel,
},
})
.action(
async ({
parsedInput: { tagId, transactionId },
ctx: { user, supabase },
}) => {
const { data } = await supabase
.from("transaction_tags")
.delete()
.eq("transaction_id", transactionId)
.eq("tag_id", tagId);

revalidateTag(`transactions_${user.team_id}`);

return data;
},
);
44 changes: 28 additions & 16 deletions apps/dashboard/src/actions/project/create-project-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,35 @@ export const createProjectAction = authActionClient
channel: LogEvents.ProjectCreated.channel,
},
})
.action(async ({ parsedInput: params, ctx: { user, supabase } }) => {
const { data } = await createProject(supabase, {
...params,
team_id: user.team_id,
});
.action(
async ({ parsedInput: { tags, ...params }, ctx: { user, supabase } }) => {
const { data } = await createProject(supabase, {
...params,
team_id: user.team_id!,
});

if (!data) {
throw new Error("Failed to create project");
}
if (!data) {
throw new Error("Failed to create project");
}

cookies().set({
name: Cookies.LastProject,
value: data.id,
expires: addYears(new Date(), 1),
});
if (tags?.length) {
await supabase.from("tracker_project_tags").insert(
tags.map((tag) => ({
tag_id: tag.id,
tracker_project_id: data?.id,
team_id: user.team_id!,
})),
);
}

revalidateTag(`tracker_projects_${user.team_id}`);
cookies().set({
name: Cookies.LastProject,
value: data.id,
expires: addYears(new Date(), 1),
});

return data;
});
revalidateTag(`tracker_projects_${user.team_id}`);

return data;
},
);
29 changes: 29 additions & 0 deletions apps/dashboard/src/actions/project/create-project-tag-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use server";

import { LogEvents } from "@midday/events/events";
import { revalidateTag } from "next/cache";
import { authActionClient } from "../safe-action";
import { createProjectTagSchema } from "../schema";

export const createProjectTagAction = authActionClient
.schema(createProjectTagSchema)
.metadata({
name: "create-project-tag",
track: {
event: LogEvents.CreateProjectTag.name,
channel: LogEvents.CreateProjectTag.channel,
},
})
.action(
async ({ parsedInput: { tagId, projectId }, ctx: { user, supabase } }) => {
const { data } = await supabase.from("tracker_project_tags").insert({
tag_id: tagId,
tracker_project_id: projectId,
team_id: user.team_id!,
});

revalidateTag(`tracker_projects_${user.team_id}`);

return data;
},
);
29 changes: 29 additions & 0 deletions apps/dashboard/src/actions/project/delete-project-tag-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use server";

import { LogEvents } from "@midday/events/events";
import { revalidateTag } from "next/cache";
import { authActionClient } from "../safe-action";
import { deleteProjectTagSchema } from "../schema";

export const deleteProjectTagAction = authActionClient
.schema(deleteProjectTagSchema)
.metadata({
name: "delete-project-tag",
track: {
event: LogEvents.DeleteProjectTag.name,
channel: LogEvents.DeleteProjectTag.channel,
},
})
.action(
async ({ parsedInput: { tagId, projectId }, ctx: { user, supabase } }) => {
const { data } = await supabase
.from("tracker_project_tags")
.delete()
.eq("tracker_project_id", projectId)
.eq("tag_id", tagId);

revalidateTag(`tracker_projects_${user.team_id}`);

return data;
},
);
3 changes: 2 additions & 1 deletion apps/dashboard/src/actions/project/update-project-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const updateProjectAction = authActionClient
},
})
.action(async ({ parsedInput: params, ctx: { user, supabase } }) => {
const { id, ...data } = params;
// We store tags in the form state, it's deleted from the action
const { id, tags, ...data } = params;

await supabase.from("tracker_projects").update(data).eq("id", id);

Expand Down
39 changes: 39 additions & 0 deletions apps/dashboard/src/actions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ export const updateUserSchema = z.object({
revalidatePath: z.string().optional(),
});

export const createTagSchema = z.object({ name: z.string() });
export const createTransactionTagSchema = z.object({
tagId: z.string(),
transactionId: z.string(),
});

export const deleteTransactionTagSchema = z.object({
tagId: z.string(),
transactionId: z.string(),
});

export const deleteProjectTagSchema = z.object({
tagId: z.string(),
projectId: z.string(),
});

export const createProjectTagSchema = z.object({
tagId: z.string(),
projectId: z.string(),
});

export type UpdateUserFormValues = z.infer<typeof updateUserSchema>;

export const trackingConsentSchema = z.boolean();
Expand Down Expand Up @@ -320,6 +341,15 @@ export const createProjectSchema = z.object({
currency: z.string().optional(),
status: z.enum(["in_progress", "completed"]).optional(),
customer_id: z.string().uuid().nullable().optional(),
tags: z
.array(
z.object({
id: z.string().uuid(),
value: z.string(),
}),
)
.optional()
.nullable(),
});

export const updateProjectSchema = z.object({
Expand All @@ -332,6 +362,15 @@ export const updateProjectSchema = z.object({
currency: z.string().optional(),
status: z.enum(["in_progress", "completed"]).optional(),
customer_id: z.string().uuid().nullable().optional(),
tags: z
.array(
z.object({
id: z.string().uuid(),
value: z.string(),
}),
)
.optional()
.nullable(),
});

export const deleteProjectSchema = z.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default async function Transactions({
statuses,
recurring,
accounts,
tags,
} = searchParamsCache.parse(searchParams);

// Move this in a suspense
Expand All @@ -59,6 +60,7 @@ export default async function Transactions({
statuses,
recurring,
accounts,
tags,
};

const sort = searchParams?.sort?.split(":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const searchParamsCache = createSearchParamsCache({
start: parseAsString,
end: parseAsString,
categories: parseAsArrayOf(parseAsString),
tags: parseAsArrayOf(parseAsString),
accounts: parseAsArrayOf(parseAsString),
assignees: parseAsArrayOf(parseAsString),
recurring: parseAsArrayOf(
Expand Down
Loading