Skip to content

Commit

Permalink
Add seedProfilePermission util
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoginth committed Oct 4, 2024
1 parent fba3bdb commit fb3fe64
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 29 deletions.
4 changes: 2 additions & 2 deletions apps/api/src/routes/internal/permissions/assign.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VERIFIED_PERMISSION_ID } from "@hey/db/constants";
import { PermissionId } from "@hey/data/permissions";
import prisma from "@hey/db/prisma/db/client";
import { delRedis } from "@hey/db/redisClient";
import logger from "@hey/helpers/logger";
Expand All @@ -12,7 +12,7 @@ import { boolean, object, string } from "zod";
export const clearCache = async (profileId: string, featureId: string) => {
await delRedis(`preference:${profileId}`);
await delRedis(`profile:${profileId}`);
if (featureId === VERIFIED_PERMISSION_ID) {
if (featureId === PermissionId.Verified) {
await delRedis("verified");
}
};
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/routes/misc/verified.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VERIFIED_PERMISSION_ID } from "@hey/db/constants";
import { PermissionId } from "@hey/data/permissions";
import prisma from "@hey/db/prisma/db/client";
import { getRedis, setRedis } from "@hey/db/redisClient";
import logger from "@hey/helpers/logger";
Expand All @@ -24,7 +24,7 @@ export const get = [

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

const ids = data.map(({ profileId }) => profileId);
Expand Down
7 changes: 3 additions & 4 deletions apps/api/src/routes/profile/get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SUSPENDED_PERMISSION_ID } from "@hey/db/constants";
import { PermissionId } from "@hey/data/permissions";
import prisma from "@hey/db/prisma/db/client";
import { getRedis, setRedis } from "@hey/db/redisClient";
import logger from "@hey/helpers/logger";
Expand Down Expand Up @@ -32,7 +32,7 @@ export const get = [
await prisma.$transaction([
prisma.profilePermission.findFirst({
where: {
permissionId: SUSPENDED_PERMISSION_ID,
permissionId: PermissionId.Suspended,
profileId: id as string
}
}),
Expand All @@ -46,8 +46,7 @@ export const get = [

const isPro = pro?.id;
const response: ProfileDetails = {
isSuspended:
profilePermission?.permissionId === SUSPENDED_PERMISSION_ID,
isSuspended: profilePermission?.permissionId === PermissionId.Suspended,
pro: isPro ? { isPro: true, expiresAt: pro.expiresAt } : null,
status: isPro ? profileStatus || null : null,
theme: isPro ? (profileTheme as ProfileTheme) || null : null
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/routes/staff-picks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { STAFF_PICK_PERMISSION_ID } from "@hey/db/constants";
import { PermissionId } from "@hey/data/permissions";
import prisma from "@hey/db/prisma/db/client";
import { generateMediumExpiry, getRedis, setRedis } from "@hey/db/redisClient";
import logger from "@hey/helpers/logger";
Expand Down Expand Up @@ -32,7 +32,7 @@ export const get = [

const data = await prisma.profilePermission.findMany({
select: { profileId: true },
where: { enabled: true, permissionId: STAFF_PICK_PERMISSION_ID }
where: { enabled: true, permissionId: PermissionId.StaffPick }
});

await setRedis(cacheKey, data, generateMediumExpiry());
Expand Down
10 changes: 3 additions & 7 deletions apps/web/src/components/Profile/CreatorTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import ToggleWrapper from "@components/Staff/Users/Overview/Tool/ToggleWrapper";
import { getAuthApiHeaders } from "@helpers/getAuthApiHeaders";
import { Leafwatch } from "@helpers/leafwatch";
import { HEY_API_URL } from "@hey/data/constants";
import { Permission } from "@hey/data/permissions";
import { Permission, PermissionId } from "@hey/data/permissions";
import { CREATORTOOLS } from "@hey/data/tracking";
import {
STAFF_PICK_PERMISSION_ID,
VERIFIED_PERMISSION_ID
} from "@hey/db/constants";
import getInternalProfile from "@hey/helpers/api/getInternalProfile";
import type { Profile } from "@hey/lens";
import { Toggle } from "@hey/ui";
Expand All @@ -26,8 +22,8 @@ const CreatorTool: FC<CreatorToolProps> = ({ profile }) => {
const [permissions, setPermissions] = useState<string[]>([]);

const allowedPermissions = [
{ id: VERIFIED_PERMISSION_ID, key: Permission.Verified },
{ id: STAFF_PICK_PERMISSION_ID, key: Permission.StaffPick }
{ id: PermissionId.Verified, key: Permission.Verified },
{ id: PermissionId.StaffPick, key: Permission.StaffPick }
];

const { data: preferences, isLoading } = useQuery({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getAuthApiHeaders } from "@helpers/getAuthApiHeaders";
import { ChatBubbleLeftIcon, NoSymbolIcon } from "@heroicons/react/24/outline";
import { HEY_API_URL } from "@hey/data/constants";
import { FeatureFlag } from "@hey/data/feature-flags";
import { PermissionId } from "@hey/data/permissions";
import type { MirrorablePublication } from "@hey/lens";
import { Button } from "@hey/ui";
import { useFlag } from "@unleash/proxy-client-react";
Expand Down Expand Up @@ -42,9 +43,7 @@ const SuspendButtons: FC<SuspendButtonsProps> = ({ onClick, publication }) => {
<Button
className="flex justify-center"
icon={<NoSymbolIcon className="size-4" />}
onClick={() =>
updateFeatureFlag("8ed8b26a-279d-4111-9d39-a40164b273a0")
}
onClick={() => updateFeatureFlag(PermissionId.Suspended)}
size="sm"
variant="danger"
>
Expand All @@ -53,9 +52,7 @@ const SuspendButtons: FC<SuspendButtonsProps> = ({ onClick, publication }) => {
<Button
className="flex justify-center"
icon={<ChatBubbleLeftIcon className="size-4" />}
onClick={() =>
updateFeatureFlag("df931ea4-109f-4fde-a8b5-4b2170730e8c")
}
onClick={() => updateFeatureFlag(PermissionId.CommentSuspended)}
size="sm"
variant="danger"
>
Expand Down
11 changes: 10 additions & 1 deletion packages/data/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@ export enum Permission {
CommentSuspended = "comment-suspended",
StaffPick = "staff-pick",
Suspended = "suspended",
Verified = "verified"
Verified = "verified",
Beta = "beta"
}

export enum PermissionId {
CommentSuspended = "df931ea4-109f-4fde-a8b5-4b2170730e8c",
StaffPick = "73d2f48d-0291-4a36-adc2-9737057ad2b7",
Suspended = "8ed8b26a-279d-4111-9d39-a40164b273a0",
Verified = "a0d6d247-50ef-419f-a045-54fa96054922",
Beta = "9ff8d280-db7b-4075-be0c-980f8293b77e"
}
5 changes: 0 additions & 5 deletions packages/db/constants.ts

This file was deleted.

8 changes: 8 additions & 0 deletions packages/db/prisma/db/seed/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import prisma from "../client";
import seedPermissions from "./seedPermissions";
import seedPreferences from "./seedPreferences";
import seedProfilePermission from "./seedProfilePermission";

async function main() {
const preferences = await seedPreferences();
console.log(`Seeded ${preferences} preferences`);

const permissions = await seedPermissions();
console.log(`Seeded ${permissions} permissions`);

const profilePermissions = await seedProfilePermission();
console.log(`Seeded ${profilePermissions} profile permissions`);
}

main()
Expand Down
22 changes: 22 additions & 0 deletions packages/db/prisma/db/seed/seedPermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Permission, PermissionId } from "@hey/data/permissions";
import prisma from "../client";

const seedPermissions = async (): Promise<number> => {
// Delete all permissions
await prisma.permission.deleteMany();

// Seed permissions
const permissions = await prisma.permission.createMany({
data: [
{ id: PermissionId.CommentSuspended, key: Permission.CommentSuspended },
{ id: PermissionId.Suspended, key: Permission.Suspended },
{ id: PermissionId.Verified, key: Permission.Verified },
{ id: PermissionId.StaffPick, key: Permission.StaffPick },
{ id: PermissionId.Beta, key: Permission.Beta }
]
});

return permissions.count;
};

export default seedPermissions;
17 changes: 17 additions & 0 deletions packages/db/prisma/db/seed/seedProfilePermission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { TEST_LENS_ID } from "@hey/data/constants";
import { PermissionId } from "@hey/data/permissions";
import prisma from "../client";

const seedProfilePermission = async (): Promise<number> => {
// Delete all profile permissions
await prisma.profilePermission.deleteMany();

// Seed profile permissions
const profilePermissions = await prisma.profilePermission.createMany({
data: [{ permissionId: PermissionId.Verified, profileId: TEST_LENS_ID }]
});

return profilePermissions.count;
};

export default seedProfilePermission;

0 comments on commit fb3fe64

Please sign in to comment.