Skip to content

Commit

Permalink
chore: remove userKey from schemas and apply authenticatedProcedures …
Browse files Browse the repository at this point in the history
…to dao routes
  • Loading branch information
vinicius-sacramento committed Sep 17, 2024
1 parent cfc13ad commit 4e3a6d2
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { ExclamationTriangleIcon } from "@heroicons/react/20/solid";
import { z } from "zod";

import type { AppRouter } from "@commune-ts/api";
import { useCommune } from "@commune-ts/providers/use-commune";
import { toast } from "@commune-ts/providers/use-toast";

import { api } from "~/trpc/react";
Expand Down Expand Up @@ -35,8 +34,6 @@ export function ReportComment({ commentId }: ReportCommentProps) {

const [errors, setErrors] = useState<Partial<ReportFormData>>({});

const { selectedAccount } = useCommune();

const reportCommentMutation =
api.proposalComment.createCommentReport.useMutation({
onSuccess: () => {
Expand Down Expand Up @@ -85,7 +82,6 @@ export function ReportComment({ commentId }: ReportCommentProps) {
if (validateForm()) {
reportCommentMutation.mutate({
commentId,
userKey: selectedAccount?.address,
reason: formData.reason,
content: formData.content,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export function CreateCadreCandidates() {
createCadreCandidateMutation.mutate({
discordId,
content,
userKey: selectedAccount.address,
});
} catch (err) {
if (err instanceof z.ZodError) {
Expand Down
32 changes: 12 additions & 20 deletions apps/commune-governance/src/app/components/dao/dao-vote-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,13 @@ export function DaoVoteCard(props: {
// If user has already voted, first remove the existing vote
deleteVoteMutation.mutate(
{
userKey: selectedAccount.address,
daoId,
},
{
onSuccess: () => {
// After successful deletion, create a new "remove" vote
createVoteMutation.mutate({
daoId,
userKey: selectedAccount.address,
daoVoteType: vote,
});
},
Expand All @@ -89,7 +87,6 @@ export function DaoVoteCard(props: {
// If user hasn't voted yet, directly create a "remove" vote
createVoteMutation.mutate({
daoId,
userKey: selectedAccount.address,
daoVoteType: "REMOVE",
});
}
Expand Down Expand Up @@ -120,7 +117,6 @@ export function DaoVoteCard(props: {

createVoteMutation.mutate({
daoId,
userKey: selectedAccount.address,
daoVoteType: vote,
});
}
Expand All @@ -132,7 +128,6 @@ export function DaoVoteCard(props: {
}

deleteVoteMutation.mutate({
userKey: selectedAccount.address,
daoId,
});
}
Expand Down Expand Up @@ -178,23 +173,21 @@ export function DaoVoteCard(props: {
<>
<div className="flex w-full gap-4">
<button
className={`w-full border border-green-600 py-1 ${
vote === "ACCEPT"
? "border-green-500 bg-green-500/10 text-green-500"
: "text-green-600"
} ${createVoteMutation.isPending && "cursor-not-allowed"}`}
className={`w-full border border-green-600 py-1 ${vote === "ACCEPT"
? "border-green-500 bg-green-500/10 text-green-500"
: "text-green-600"
} ${createVoteMutation.isPending && "cursor-not-allowed"}`}
onClick={() => handleVotePreference("ACCEPT")}
type="button"
disabled={createVoteMutation.isPending}
>
Approve
</button>
<button
className={`w-full border border-red-600 py-1 ${
vote === "REFUSE"
? "border-red-500 bg-red-500/10 text-red-500"
: "text-red-500"
} ${createVoteMutation.isPending && "cursor-not-allowed"}`}
className={`w-full border border-red-600 py-1 ${vote === "REFUSE"
? "border-red-500 bg-red-500/10 text-red-500"
: "text-red-500"
} ${createVoteMutation.isPending && "cursor-not-allowed"}`}
onClick={() => handleVotePreference("REFUSE")}
type="button"
disabled={createVoteMutation.isPending}
Expand All @@ -203,11 +196,10 @@ export function DaoVoteCard(props: {
</button>
</div>
<button
className={`mt-4 w-full border p-1.5 ${
!vote || createVoteMutation.isPending
? "cursor-not-allowed border-gray-400 text-gray-400"
: "border-blue-400 bg-blue-500/10 text-blue-400"
} `}
className={`mt-4 w-full border p-1.5 ${!vote || createVoteMutation.isPending
? "cursor-not-allowed border-gray-400 text-gray-400"
: "border-blue-400 bg-blue-500/10 text-blue-400"
} `}
disabled={!vote || createVoteMutation.isPending}
onClick={handleVote}
type="button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,11 @@ export function DelegatedModulesList() {

try {
// Delete existing user module data
await deleteUserModuleData.mutateAsync({
userKey: selectedAccount.address,
});
await deleteUserModuleData.mutateAsync();

// Submit new user module data
for (const delegatedModule of delegatedModules) {
await createUserModuleData.mutateAsync({
userKey: selectedAccount.address,
moduleId: delegatedModule.id,
weight: delegatedModule.percentage,
});
Expand Down Expand Up @@ -190,9 +187,8 @@ export function DelegatedModulesList() {
<span>{isOpen ? "COLLAPSE VIEW" : "EXPAND VIEW"}</span>
<span>
<ChevronUpIcon
className={`h-5 w-5 transform transition-transform ${
isOpen ? "rotate-180" : ""
}`}
className={`h-5 w-5 transform transition-transform ${isOpen ? "rotate-180" : ""
}`}
/>
</span>
</button>
Expand Down Expand Up @@ -245,12 +241,11 @@ export function DelegatedModulesList() {
<div className="flex flex-col gap-3">
<button
onClick={handleSubmit}
className={`mt-4 w-full animate-fade rounded-full border border-white/20 bg-[#898989]/5 p-2 text-white backdrop-blur-md transition duration-200 animate-delay-300 ${
isSubmitting ||
className={`mt-4 w-full animate-fade rounded-full border border-white/20 bg-[#898989]/5 p-2 text-white backdrop-blur-md transition duration-200 animate-delay-300 ${isSubmitting ||
totalPercentage !== 100 ||
(!selectedAccount.address &&
`hover:border-green-500 hover:bg-green-500/10`)
} disabled:opacity-50`}
} disabled:opacity-50`}
disabled={
isSubmitting ||
totalPercentage !== 100 ||
Expand Down
14 changes: 5 additions & 9 deletions apps/commune-validator/src/app/components/report-module.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import { XMarkIcon } from "@heroicons/react/16/solid";
import { ExclamationTriangleIcon } from "@heroicons/react/20/solid";
import { z } from "zod";

import { useCommune } from "@commune-ts/providers/use-commune";
import { toast } from "@commune-ts/providers/use-toast";

import { api } from "~/trpc/react";

enum ReportReason {
spam = "spam",
harassment = "harassment",
hateSpeech = "hateSpeech",
violence = "violence",
sexualContent = "sexualContent",
spam = "SPAM",
harassment = "HARASSMENT",
hateSpeech = "HATE_SPEECH",
violence = "VIOLENCE",
sexualContent = "SEXUAL_CONTENT",
}

const reportSchema = z.object({
Expand All @@ -37,8 +36,6 @@ export function ReportModule({ moduleId }: ReportModuleProps) {
});
const [errors, setErrors] = useState<Partial<ReportFormData>>({});

const { selectedAccount } = useCommune();

const reportModuleMutation = api.module.createModuleReport.useMutation({
onSuccess: () => {
setModalOpen(false);
Expand Down Expand Up @@ -84,7 +81,6 @@ export function ReportModule({ moduleId }: ReportModuleProps) {
if (validateForm()) {
reportModuleMutation.mutate({
moduleId,
userKey: selectedAccount?.address,
reason: formData.reason,
content: formData.content,
});
Expand Down
30 changes: 19 additions & 11 deletions packages/api/src/router/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
DAO_VOTE_INSERT_SCHEMA,
} from "@commune-ts/db/validation";

import { publicProcedure } from "../trpc";
import { authenticatedProcedure, publicProcedure } from "../trpc";

export const daoRouter = {
// GET
Expand All @@ -37,48 +37,56 @@ export const daoRouter = {
return ctx.db.query.cadreCandidatesSchema.findMany();
}),
// POST
createVote: publicProcedure
createVote: authenticatedProcedure
.input(DAO_VOTE_INSERT_SCHEMA)
.mutation(async ({ ctx, input }) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userKey = ctx.user!.userKey
await ctx.db
.update(daoVoteSchema)
.set({
deletedAt: new Date(),
})
.where(
and(
eq(daoVoteSchema.userKey, input.userKey),
eq(daoVoteSchema.userKey, userKey),
eq(daoVoteSchema.daoId, input.daoId),
isNull(daoVoteSchema.deletedAt),
),
)
.execute();

await ctx.db.insert(daoVoteSchema).values(input).execute();
await ctx.db.insert(daoVoteSchema).values({...input, userKey: userKey}).execute();
}),
deleteVote: publicProcedure
.input(z.object({ userKey: z.string(), daoId: z.number() }))
deleteVote: authenticatedProcedure
.input(z.object({ daoId: z.number() }))
.mutation(async ({ ctx, input }) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userKey = ctx.user!.userKey
await ctx.db
.update(daoVoteSchema)
.set({
deletedAt: new Date(),
})
.where(
and(
eq(daoVoteSchema.userKey, input.userKey),
eq(daoVoteSchema.userKey, userKey),
eq(daoVoteSchema.daoId, input.daoId),
),
);
}),
addCadreCandidates: publicProcedure
addCadreCandidates: authenticatedProcedure
.input(CADRE_CANDIDATES_INSERT_SCHEMA)
.mutation(async ({ ctx, input }) => {
await ctx.db.insert(cadreCandidatesSchema).values(input).execute();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userKey = ctx.user!.userKey
await ctx.db.insert(cadreCandidatesSchema).values({...input, userKey: userKey }).execute();
}),
createCadreVote: publicProcedure
createCadreVote: authenticatedProcedure
.input(CADRE_VOTE_INSERT_SCHEMA)
.mutation(async ({ ctx, input }) => {
await ctx.db.insert(cadreVoteSchema).values(input).execute();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userKey = ctx.user!.userKey
await ctx.db.insert(cadreVoteSchema).values({...input, userKey: userKey }).execute();
}),
} satisfies TRPCRouterRecord;
6 changes: 3 additions & 3 deletions packages/api/src/router/proposal-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ export const proposalCommentRouter = {
}),
// POST
createComment: authenticatedProcedure
.input(PROPOSAL_COMMENT_INSERT_SCHEMA.omit({ userKey: true }))
.input(PROPOSAL_COMMENT_INSERT_SCHEMA)
.mutation(async ({ ctx, input }) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userKey = ctx.user!.userKey;
await ctx.db.insert(proposalCommentSchema).values({ ...input, userKey });
}),
createCommentReport: authenticatedProcedure
.input(COMMENT_REPORT_INSERT_SCHEMA.omit({ userKey: true }))
.input(COMMENT_REPORT_INSERT_SCHEMA)
.mutation(async ({ ctx, input }) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userKey = ctx.user!.userKey;
await ctx.db.insert(commentReportSchema).values({ ...input, userKey });
}),
castVote: authenticatedProcedure
.input(COMMENT_INTERACTION_INSERT_SCHEMA.omit({ userKey: true }))
.input(COMMENT_INTERACTION_INSERT_SCHEMA)
.mutation(async ({ ctx, input }) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userKey = ctx.user!.userKey;
Expand Down
7 changes: 7 additions & 0 deletions packages/db/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const PROPOSAL_COMMENT_INSERT_SCHEMA = createInsertSchema(
id: true,
createdAt: true,
deletedAt: true,
userKey: true,
});

export const COMMENT_INTERACTION_INSERT_SCHEMA = createInsertSchema(
Expand All @@ -57,6 +58,7 @@ export const COMMENT_INTERACTION_INSERT_SCHEMA = createInsertSchema(
).omit({
id: true,
createdAt: true,
userKey: true,
});

export const COMMENT_REPORT_INSERT_SCHEMA = createInsertSchema(
Expand All @@ -72,6 +74,7 @@ export const COMMENT_REPORT_INSERT_SCHEMA = createInsertSchema(
).omit({
id: true,
createdAt: true,
userKey: true,
});

export const MODULE_REPORT_INSERT_SCHEMA = createInsertSchema(moduleReport, {
Expand All @@ -93,13 +96,15 @@ export const USER_MODULE_DATA_INSERT_SCHEMA = createInsertSchema(
},
).omit({
id: true,
userKey: true
});

export const DAO_VOTE_INSERT_SCHEMA = createInsertSchema(daoVoteSchema).omit({
id: true,
createdAt: true,
updatedAt: true,
deletedAt: true,
userKey: true,
});

export const CADRE_INSERT_SCHEMA = createInsertSchema(cadreSchema).omit({
Expand All @@ -115,6 +120,7 @@ export const CADRE_CANDIDATES_INSERT_SCHEMA = createInsertSchema(
id: true,
createdAt: true,
deletedAt: true,
userKey: true,
});

export const CADRE_VOTE_INSERT_SCHEMA = createInsertSchema(
Expand All @@ -124,4 +130,5 @@ export const CADRE_VOTE_INSERT_SCHEMA = createInsertSchema(
createdAt: true,
updatedAt: true,
deletedAt: true,
userKey: true,
});

0 comments on commit 4e3a6d2

Please sign in to comment.