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 4d54b83 commit 05a68d8
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 47 deletions.
4 changes: 2 additions & 2 deletions apps/api/src/routes/internal/permissions/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const get = [
async (_: Request, res: Response) => {
try {
const permissions = await prisma.permission.findMany({
include: { _count: { select: { profiles: true } } },
orderBy: { profiles: { _count: "desc" } }
include: { _count: { select: { accounts: true } } },
orderBy: { accounts: { _count: "desc" } }
});

logger.info("All permissions fetched");
Expand Down
17 changes: 10 additions & 7 deletions apps/api/src/routes/internal/permissions/bulkAssign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,26 @@ export const post = [
try {
const parsedIds = JSON.parse(ids) as string[];

const accountPermissions = await prisma.profilePermission.findMany({
where: { permissionId, profileId: { in: parsedIds } }
const accountPermissions = await prisma.accountPermission.findMany({
where: { permissionId, accountAddress: { in: parsedIds } }
});

const idsToAssign = parsedIds.filter(
(profileId) =>
(address) =>
!accountPermissions.some(
(accountPermission) => accountPermission.profileId === profileId
(accountPermission) => accountPermission.accountAddress === address
)
);

const accountPermission = await prisma.profilePermission.createMany({
data: idsToAssign.map((profileId) => ({ permissionId, profileId })),
const accountPermission = await prisma.accountPermission.createMany({
data: idsToAssign.map((address) => ({
permissionId,
accountAddress: address
})),
skipDuplicates: true
});

logger.info(`Bulk assigned permissions for ${parsedIds.length} profiles`);
logger.info(`Bulk assigned permissions for ${parsedIds.length} accounts`);

return res
.status(200)
Expand Down
10 changes: 5 additions & 5 deletions apps/api/src/routes/internal/stats/overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ export const get = [
async (_: Request, res: Response) => {
try {
const result = await prisma.$transaction([
prisma.profilePermission.count(),
prisma.accountPermission.count(),
prisma.email.count(),
prisma.membershipNft.count(),
prisma.poll.count(),
prisma.pollOption.count(),
prisma.pollResponse.count(),
prisma.preference.count(),
prisma.profileStatus.count(),
prisma.profileTheme.count()
prisma.accountStatus.count(),
prisma.accountTheme.count()
]);

logger.info("Fetched overview stats");

return res.status(200).json({
result: {
profilePermissions: result[0],
accountPermissions: result[0],
emails: result[1],
membershipNfts: result[2],
polls: result[3],
pollOptions: result[4],
pollResponses: result[5],
preferences: result[6],
accountStatuses: result[7],
profileThemes: result[8]
accountThemes: result[8]
},
success: true
});
Expand Down
8 changes: 5 additions & 3 deletions apps/api/src/routes/misc/verified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ export const get = [
.json({ result: JSON.parse(cachedData), success: true });
}

const accountPermission = await prisma.profilePermission.findMany({
select: { profileId: true },
const accountPermission = await prisma.accountPermission.findMany({
select: { accountAddress: true },
where: { enabled: true, permissionId: PermissionId.Verified }
});

const result = accountPermission.map(({ profileId }) => profileId);
const result = accountPermission.map(
({ accountAddress }) => accountAddress
);
await setRedis(cacheKey, result);
logger.info("Verified accounts fetched");

Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/routes/polls/act.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const post = [
const existingPollResponse = await prisma.pollResponse.findFirst({
where: {
option: { pollId: poll as string },
profileId: payload.act.sub
accountAddress: payload.act.sub
}
});

Expand All @@ -68,7 +68,7 @@ export const post = [
// End: Check if the poll exists and delete the existing response

const pollResponse = await prisma.pollResponse.create({
data: { optionId: option, profileId: payload.act.sub }
data: { optionId: option, accountAddress: payload.act.sub }
});

await delRedis(`poll:${poll}`);
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/routes/polls/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const get = [
option: true,
responses: {
select: { id: true },
where: { profileId: payload.act.sub }
where: { accountAddress: payload.act.sub }
}
}
}
Expand Down
26 changes: 14 additions & 12 deletions apps/api/src/routes/preferences/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,35 @@ export const get = [
try {
const idToken = req.headers["x-id-token"] as string;
const payload = parseJwt(idToken);
const { id } = payload;
const accountAddress = payload.act.sub;

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

const cacheKey = `preference:${id}`;
const cacheKey = `preference:${accountAddress}`;
const cachedData = await getRedis(cacheKey);

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

const [preference, permissions, email, membershipNft, theme, mutedWords] =
await prisma.$transaction([
prisma.preference.findUnique({ where: { id: id as string } }),
prisma.profilePermission.findMany({
prisma.preference.findUnique({ where: { accountAddress } }),
prisma.accountPermission.findMany({
include: { permission: { select: { key: true } } },
where: { enabled: true, profileId: id as string }
where: { enabled: true, accountAddress }
}),
prisma.email.findUnique({ where: { id: id as string } }),
prisma.membershipNft.findUnique({ where: { id: id as string } }),
prisma.profileTheme.findUnique({ where: { id: id as string } }),
prisma.mutedWord.findMany({ where: { profileId: id as string } })
prisma.email.findUnique({ where: { accountAddress } }),
prisma.membershipNft.findUnique({ where: { accountAddress } }),
prisma.accountTheme.findUnique({ where: { accountAddress } }),
prisma.mutedWord.findMany({ where: { accountAddress } })
]);

const response: Preferences = {
Expand All @@ -66,7 +68,7 @@ export const get = [
};

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

return res.status(200).json({ result: response, success: true });
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/routes/preferences/mute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const post = [
const payload = parseJwt(idToken);

const mutedWord = await prisma.mutedWord.create({
data: { word, expiresAt, profileId: payload.act.sub }
data: { word, expiresAt, accountAddress: payload.act.sub }
});

await delRedis(`preference:${payload.act.sub}`);
Expand Down
8 changes: 4 additions & 4 deletions apps/api/src/routes/preferences/theme/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export const post = [
const idToken = req.headers["x-id-token"] as string;
const payload = parseJwt(idToken);

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

await delRedis(`preference:${payload.act.sub}`);
logger.info(`Reset profile theme for ${payload.act.sub}`);
logger.info(`Reset account theme for ${payload.act.sub}`);

return res.status(200).json({ result: profileTheme, success: true });
return res.status(200).json({ result: accountTheme, success: true });
} catch (error) {
return catchedError(res, error);
}
Expand Down
10 changes: 5 additions & 5 deletions apps/api/src/routes/preferences/theme/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ export const post = [
const payload = parseJwt(idToken);
const dbPayload = { fontStyle };

const profileTheme = await prisma.profileTheme.upsert({
create: { id: payload.act.sub, ...dbPayload },
const accountTheme = await prisma.accountTheme.upsert({
create: { accountAddress: payload.act.sub, ...dbPayload },
update: dbPayload,
where: { id: payload.act.sub }
where: { accountAddress: payload.act.sub }
});

await delRedis(`preference:${payload.act.sub}`);
logger.info(`Updated profile theme for ${payload.act.sub}`);
logger.info(`Updated account theme for ${payload.act.sub}`);

return res.status(200).json({ result: profileTheme, success: true });
return res.status(200).json({ result: accountTheme, success: true });
} catch (error) {
return catchedError(res, error);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/routes/preferences/unmute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const post = [
const payload = parseJwt(idToken);

const mutedWord = await prisma.mutedWord.delete({
where: { id, profileId: payload.act.sub }
where: { id, accountAddress: payload.act.sub }
});

await delRedis(`preference:${payload.act.sub}`);
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/routes/preferences/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export const post = [

const data = { appIcon, highSignalNotificationFilter, developerMode };
const preference = await prisma.preference.upsert({
create: { ...data, id: payload.act.sub },
create: { ...data, accountAddress: payload.act.sub },
update: data,
where: { id: payload.act.sub }
where: { accountAddress: payload.act.sub }
});

await delRedis(`preference:${payload.act.sub}`);
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/routes/preferences/updateNftStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export const post = [
const payload = parseJwt(idToken);

const membershipNft = await prisma.membershipNft.upsert({
create: { dismissedOrMinted: true, id: payload.act.sub },
create: { dismissedOrMinted: true, accountAddress: payload.act.sub },
update: { dismissedOrMinted: true },
where: { id: payload.act.sub }
where: { accountAddress: payload.act.sub }
});
logger.info(`Updated membership nft status for ${payload.act.sub}`);

Expand Down

0 comments on commit 05a68d8

Please sign in to comment.