Skip to content

Commit

Permalink
Migrate to Lens v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoginth committed Dec 8, 2024
1 parent 059862e commit 4d54b83
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 52 deletions.
14 changes: 9 additions & 5 deletions apps/api/src/helpers/email/sendEmailToAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ import logger from "@hey/helpers/logger";
import sendEmail from "./sendEmail";

const sendEmailToAccount = async ({
id,
accountAddress,
body,
subject
}: {
id: string;
accountAddress: string;
body: string;
subject: string;
}) => {
try {
const foundEmail = await prisma.email.findUnique({ where: { id } });
const foundEmail = await prisma.email.findUnique({
where: { accountAddress }
});

if (!foundEmail?.email) {
return logger.error(`sendEmailToAccount: Email not found for ${id}`);
return logger.error(
`sendEmailToAccount: Email not found for ${accountAddress}`
);
}

await sendEmail({
Expand All @@ -25,7 +29,7 @@ const sendEmailToAccount = async ({
});

return logger.info(
`sendEmailToAccount: Email sent to ${foundEmail?.email} - ${id}`
`sendEmailToAccount: Email sent to ${foundEmail?.email} - ${accountAddress}`
);
} catch (error) {
return logger.error(error as any);
Expand Down
18 changes: 10 additions & 8 deletions apps/api/src/routes/account/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,33 @@ import { noBody } from "src/helpers/responses";
export const get = [
rateLimiter({ requests: 250, within: 1 }),
async (req: Request, res: Response) => {
const { id } = req.query;
const { address } = req.query;

if (!id) {
if (!address) {
return noBody(res);
}

try {
const cacheKey = `account:${id}`;
const cacheKey = `account:${address}`;
const cachedData = await getRedis(cacheKey);

if (cachedData) {
logger.info(`(cached) Account details fetched for ${id}`);
logger.info(`(cached) Account details fetched for ${address}`);
return res
.status(200)
.json({ result: JSON.parse(cachedData), success: true });
}

const [accountPermission, accountStatus] = await prisma.$transaction([
prisma.profilePermission.findFirst({
prisma.accountPermission.findFirst({
where: {
permissionId: PermissionId.Suspended,
profileId: id as string
accountAddress: address as string
}
}),
prisma.profileStatus.findUnique({ where: { id: id as string } })
prisma.accountStatus.findUnique({
where: { accountAddress: address as string }
})
]);

const response: AccountDetails = {
Expand All @@ -44,7 +46,7 @@ export const get = [
};

await setRedis(cacheKey, response);
logger.info(`Account details fetched for ${id}`);
logger.info(`Account details fetched for ${address}`);

return res.status(200).json({ result: response, success: true });
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/routes/account/status/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const post = [
const idToken = req.headers["x-id-token"] as string;
const payload = parseJwt(idToken);

const accountStatus = await prisma.profileStatus.deleteMany({
where: { id: payload.act.sub }
const accountStatus = await prisma.accountStatus.deleteMany({
where: { accountAddress: payload.act.sub }
});

await delRedis(`account:${payload.act.sub}`);
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/routes/account/status/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export const post = [
const idToken = req.headers["x-id-token"] as string;
const payload = parseJwt(idToken);

const accountStatus = await prisma.profileStatus.upsert({
create: { message, emoji, id: payload.act.sub },
const accountStatus = await prisma.accountStatus.upsert({
create: { message, emoji, accountAddress: payload.act.sub },
update: { message, emoji },
where: { id: payload.act.sub }
where: { accountAddress: payload.act.sub }
});

await delRedis(`account:${payload.act.sub}`);
Expand Down
20 changes: 6 additions & 14 deletions apps/api/src/routes/badges/isHeyAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,22 @@ import catchedError from "src/helpers/catchedError";
import { CACHE_AGE_INDEFINITE } from "src/helpers/constants";
import { rateLimiter } from "src/helpers/middlewares/rateLimiter";
import { noBody } from "src/helpers/responses";
import type { Address } from "viem";
import { getAddress } from "viem";

export const get = [
rateLimiter({ requests: 100, within: 1 }),
async (req: Request, res: Response) => {
const { address, id } = req.query;
const { address } = req.query;

if (!id && !address) {
if (!address) {
return noBody(res);
}

try {
const formattedAddress = address
? getAddress(address as Address)
: undefined;

const cacheKey = `badge:hey-account:${id || address}`;
const cacheKey = `badge:hey-account:${address}`;
const cachedData = await getRedis(cacheKey);

if (cachedData === "true") {
logger.info(
`(cached) Hey account badge fetched for ${id || formattedAddress}`
);
logger.info(`(cached) Hey account badge fetched for ${address}`);

return res
.status(200)
Expand All @@ -49,15 +41,15 @@ export const get = [
AND o.onboarded_by_address = $3
) AS exists;
`,
[id, formattedAddress, HEY_LENS_SIGNUP]
[address, HEY_LENS_SIGNUP]
);

const isHeyAccount = onboardingAccount[0]?.exists;

if (isHeyAccount) {
await setRedis(cacheKey, isHeyAccount);
}
logger.info(`Hey account badge fetched for ${id || formattedAddress}`);
logger.info(`Hey account badge fetched for ${address}`);

return res
.status(200)
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/routes/email/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const post = [

if (!resend) {
const foundEmail = await prisma.email.findUnique({
where: { id: payload.act.sub }
where: { accountAddress: payload.act.sub }
});

if (foundEmail?.email === email) {
Expand All @@ -70,9 +70,9 @@ export const post = [
};

const upsertedEmail = await prisma.email.upsert({
create: { id: payload.act.sub, ...baseData },
create: { accountAddress: payload.act.sub, ...baseData },
update: baseData,
where: { id: payload.act.sub }
where: { accountAddress: payload.act.sub }
});

sendEmail({
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/routes/email/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const get = async (req: Request, res: Response) => {
where: { verificationToken: token as string }
});

await delRedis(`preference:${updatedEmail.id}`);
await delRedis(`preference:${updatedEmail.accountAddress}`);
logger.info(`Email verified for ${updatedEmail.email}`);

return res.redirect("https://hey.xyz");
Expand Down
8 changes: 5 additions & 3 deletions apps/api/src/routes/frames/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ export const post = [
const accessToken = req.headers["x-access-token"] as string;
const idToken = req.headers["x-id-token"] as string;
const payload = parseJwt(idToken);
const { id } = payload;
const accountAddress = payload.act.sub;

let request = {
actionResponse: actionResponse || "",
buttonIndex,
inputText: inputText || "",
profileId: id,
profileId: accountAddress,
pubId,
specVersion: "1.0.0",
state: state || "",
Expand Down Expand Up @@ -124,7 +124,9 @@ export const post = [
}
}

logger.info(`Open frame button clicked by ${id} on ${postUrl}`);
logger.info(
`Open frame button clicked by ${accountAddress} on ${postUrl}`
);

if (buttonAction === "tx") {
return res
Expand Down
8 changes: 4 additions & 4 deletions apps/api/src/routes/internal/creator-tools/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const post = [

try {
if (enabled) {
await prisma.profilePermission.create({
data: { permissionId: id, profileId: accountAddress }
await prisma.accountPermission.create({
data: { permissionId: id, accountAddress }
});

await postUpdateTasks(accountAddress, id, true);
Expand All @@ -51,10 +51,10 @@ export const post = [
return res.status(200).json({ enabled, success: true });
}

await prisma.profilePermission.deleteMany({
await prisma.accountPermission.deleteMany({
where: {
permissionId: id as string,
profileId: accountAddress as string
accountAddress
}
});

Expand Down
15 changes: 6 additions & 9 deletions apps/api/src/routes/internal/permissions/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const postUpdateTasks = async (
if (permissionId === PermissionId.StaffPick) {
if (enabled) {
sendEmailToAccount({
id: accountAddress,
accountAddress,
subject: `Your account on ${APP_NAME} has been Staff Picked!`,
body: `
<html>
Expand All @@ -51,7 +51,7 @@ export const postUpdateTasks = async (
if (permissionId === PermissionId.Verified) {
if (enabled) {
sendEmailToAccount({
id: accountAddress,
accountAddress,
subject: `Your account on ${APP_NAME} has been verified!`,
body: `
<html>
Expand Down Expand Up @@ -104,8 +104,8 @@ export const post = [

try {
if (enabled) {
await prisma.profilePermission.create({
data: { permissionId: id, profileId: accountAddress }
await prisma.accountPermission.create({
data: { permissionId: id, accountAddress }
});

await postUpdateTasks(accountAddress, id, true);
Expand All @@ -114,11 +114,8 @@ export const post = [
return res.status(200).json({ enabled, success: true });
}

await prisma.profilePermission.deleteMany({
where: {
permissionId: id as string,
profileId: accountAddress as string
}
await prisma.accountPermission.deleteMany({
where: { permissionId: id as string, accountAddress }
});

await postUpdateTasks(accountAddress, id, false);
Expand Down

0 comments on commit 4d54b83

Please sign in to comment.