Skip to content
Open

redis #173

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
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/node": "18.11.17",
"@types/react": "18.0.26",
"@types/react-dom": "18.0.9",
"@upstash/redis": "^1.20.1",
"class-variance-authority": "^0.4.0",
"clsx": "^1.2.1",
"eslint-config-next": "13.0.7",
Expand Down
7 changes: 7 additions & 0 deletions pages/api/v1/request/[requestId]/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getEmails } from "lib/dynamic"
import { sendNewCommentEmail } from "lib/sendgrid"
import { NextApiRequest, NextApiResponse } from "next"
import db from "../../../../../prisma/client"
import { clearRequestsCache } from "../../../../../src/hooks/useRequests"
import { getRequestById } from "../../../../../src/models/request/queries/getRequestById"
import { Terminal } from "../../../../../src/models/terminal/types"

Expand Down Expand Up @@ -40,6 +41,12 @@ export default async function handler(
const request = await getRequestById(query.requestId as string)
const terminal = request?.terminal as Terminal

// clear redis cache since we are performing an update
await clearRequestsCache(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should skip caching comments until we're aware of the security implications

request.terminal.chainId,
request.terminal.safeAddress,
)

try {
const addresses = request.signers
const emails = await getEmails(addresses)
Expand Down
6 changes: 6 additions & 0 deletions pages/api/v1/request/[requestId]/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getEmails } from "lib/dynamic"
import { sendNewProposalExecutionEmail } from "lib/sendgrid"
import { NextApiRequest, NextApiResponse } from "next"
import db from "../../../../../prisma/client"
import { clearRequestsCache } from "../../../../../src/hooks/useRequests"
import { getRequestById } from "../../../../../src/models/request/queries/getRequestById"
import { Terminal } from "../../../../../src/models/terminal/types"

Expand Down Expand Up @@ -36,6 +37,11 @@ export default async function handler(
try {
const request = await getRequestById(query.requestId as string)
const terminal = request?.terminal as Terminal
// clear redis cache since we are performing an update
await clearRequestsCache(
request.terminal.chainId,
request.terminal.safeAddress,
)

const addresses = request.signers
const emails = await getEmails(addresses)
Expand Down
7 changes: 7 additions & 0 deletions pages/api/v1/request/[requestId]/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { actionsTree } from "lib/signatures/tree"
import { verifyTree } from "lib/signatures/verify"
import { NextApiRequest, NextApiResponse } from "next"
import db from "../../../../../prisma/client"
import { clearRequestsCache } from "../../../../../src/hooks/useRequests"
import { getRequestById } from "../../../../../src/models/request/queries/getRequestById"
import { TokenTransferVariant } from "../../../../../src/models/request/types"

Expand Down Expand Up @@ -98,6 +99,12 @@ export default async function handler(
// bundle creates as one atomic transaction
await db.$transaction([signatureCreate, activityCreate])

// clear redis cache since we are performing an update
await clearRequestsCache(
request.terminal.chainId,
request.terminal.safeAddress,
)

try {
// this means we are approving the request and we are the final signer
// request object is a snapshot before this vote is taken into account
Expand Down
39 changes: 39 additions & 0 deletions pages/api/v1/request/batch-execute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ActionStatus } from "@prisma/client"
import { getEmails } from "lib/dynamic"
import { sendNewProposalExecutionEmail } from "lib/sendgrid"
import { NextApiRequest, NextApiResponse } from "next"
import db from "../../../../prisma/client"
import { clearRequestsCache } from "../../../../src/hooks/useRequests"
import { Action } from "../../../../src/models/action/types"
import { getRequestById } from "../../../../src/models/request/queries/getRequestById"

export default async function handler(
req: NextApiRequest,
Expand Down Expand Up @@ -30,5 +35,39 @@ export default async function handler(
},
})

const actions = (await db.action.findMany({
where: {
id: {
in: actionIds,
},
},
})) as Action[]

if (actions.length > 0) {
// clear redis cache since we are performing an update
const chainId = actions[0].chainId
const safeAddress = actions[0].safeAddress
await clearRequestsCache(chainId, safeAddress)
}

const processedRequests = new Set<string>()
for (const action of actions) {
if (processedRequests.has(action.requestId)) {
continue
}
processedRequests.add(action.requestId)
const request = await getRequestById(action.requestId)
const signerEmails = await getEmails(request.signers)

await sendNewProposalExecutionEmail({
recipients: signerEmails,
proposalTitle: request.data.note,
requestId: request.id,
terminalName: request.terminal.data.name,
chainId: request.terminal.chainId,
safeAddress: request.terminal.safeAddress,
})
}

res.status(200).json({})
}
49 changes: 48 additions & 1 deletion pages/api/v1/request/batch-vote.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { ActivityVariant } from "@prisma/client"
import { ActivityVariant, RequestVariantType } from "@prisma/client"
import { getEmails } from "lib/dynamic"
import {
sendNewProposalReadyForClaimingEmail,
sendNewProposalReadyForExecutionEmail,
} from "lib/sendgrid"
import { hashAction } from "lib/signatures/action"
import { actionsTree } from "lib/signatures/tree"
import { verifyTree } from "lib/signatures/verify"
import { NextApiRequest, NextApiResponse } from "next"
import db from "../../../../prisma/client"
import { clearRequestsCache } from "../../../../src/hooks/useRequests"
import { Action } from "../../../../src/models/action/types"
import { getRequestById } from "../../../../src/models/request/queries/getRequestById"
import { TokenTransferVariant } from "../../../../src/models/request/types"

export default async function handler(
req: NextApiRequest,
Expand Down Expand Up @@ -82,5 +90,44 @@ export default async function handler(
// bundle creates as one atomic transaction
await db.$transaction([createSignature, ...createActivities])

if (actions.length > 0) {
// clear redis cache since we are performing an update
const chainId = actions[0].chainId
const safeAddress = actions[0].safeAddress
await clearRequestsCache(chainId, safeAddress)
}

const processedRequests = new Set<string>()
for (const action of actions) {
if (processedRequests.has(action.requestId)) {
continue
}
processedRequests.add(action.requestId)
const request = await getRequestById(action.requestId)
const signerEmails = await getEmails(request.signers)

if (request.variant === RequestVariantType.TOKEN_TRANSFER) {
const meta = request.data.meta as TokenTransferVariant
const recipientEmail = await getEmails([meta.recipient])

await sendNewProposalReadyForClaimingEmail({
recipients: recipientEmail,
requestId: request.id,
chainId: request.terminal.chainId,
safeAddress: request.terminal.safeAddress,
terminalName: request.terminal.data.name,
})
}

await sendNewProposalReadyForExecutionEmail({
recipients: signerEmails,
proposalTitle: request.data.note,
requestId: request.id,
chainId: request.terminal.chainId,
safeAddress: request.terminal.safeAddress,
terminalName: request.terminal.data.name,
})
}

res.status(200).json({})
}
7 changes: 2 additions & 5 deletions src/components/core/AccountNavBar/AccountDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
DynamicUserProfile,
useDynamicContext,
} from "@dynamic-labs/sdk-react"
import { DynamicUserProfile, useDynamicContext } from "@dynamic-labs/sdk-react"
import { BellIcon } from "@heroicons/react/24/solid"
import { Avatar } from "@ui/Avatar"
import BottomDrawer from "@ui/BottomDrawer"
Expand Down Expand Up @@ -139,7 +136,7 @@ export const AccountNavBar = () => {
</Breakpoint>
</div>
<div
className="h-8 w-8 rounded bg-gray-80 p-1"
className="cur h-8 w-8 cursor-pointer rounded bg-gray-90 p-1 transition-colors hover:bg-gray-80"
onClick={() => setNotificationOpen(true)}
>
<BellIcon />
Expand Down
6 changes: 3 additions & 3 deletions src/components/core/TerminalActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const TerminalActionBar = () => {
setQrCodeOpen(true)
}}
>
<div className="flex h-12 w-12 items-center justify-center rounded-full border border-white hover:bg-gray-90">
<div className="flex h-12 w-12 items-center justify-center rounded-full border border-white transition-colors hover:bg-gray-90">
<PlusIcon className="h-6 w-6" />
</div>
<span className="text-sm">Add Tokens</span>
Expand Down Expand Up @@ -137,7 +137,7 @@ const TerminalActionBar = () => {
addQueryParam(router, "requestTokenSliderOpen", "true")
}}
>
<div className="flex h-12 w-12 items-center justify-center rounded-full border border-white hover:bg-gray-90">
<div className="flex h-12 w-12 items-center justify-center rounded-full border border-white transition-colors hover:bg-gray-90">
<ArrowUpRight />
</div>
<span className="text-sm">Send</span>
Expand Down Expand Up @@ -170,7 +170,7 @@ const TerminalActionBar = () => {
addQueryParam(router, "automationSliderOpen", "true")
}}
>
<div className="flex h-12 w-12 items-center justify-center rounded-full border border-white hover:bg-gray-90">
<div className="flex h-12 w-12 items-center justify-center rounded-full border border-white transition-colors hover:bg-gray-90">
<CogIcon className="h-6 w-6" />
</div>
<span className="text-sm">Automate</span>
Expand Down
11 changes: 0 additions & 11 deletions src/components/email/EmailNotificationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,9 @@ const EmailNotificationForm = ({
}
const onError = async (data: any) => {}

const testEmails = async () => {
fetch("/api/v1/demo")
.then((res) => res.json())
.then((res) => console.log(res))
// const emails = await getEmails([
// "0x65A3870F48B5237f27f674Ec42eA1E017E111D63",
// ])
// console.log(emails)
}

return (
<div className="px-4">
<h2 className="mb-[30px] font-bold">Notifications</h2>
<button onClick={() => testEmails()}>Test emails</button>
<p>
Enter your email to get notifications about your Terminals. We’ll never
spam you with useless emails.
Expand Down
Loading