From 66fde832a7812d985373b97611a3a1932900a864 Mon Sep 17 00:00:00 2001 From: bigint <69431456+bigint@users.noreply.github.com> Date: Mon, 8 Jan 2024 11:31:44 +0530 Subject: [PATCH] feat: remove profile restriction table and migrate them to features --- .../migration.sql | 8 ++ apps/api/src/db/schema.prisma | 11 --- .../src/routes/internal/cron/cleanPostgres.ts | 5 -- .../routes/internal/restrictions/update.ts | 53 ------------ apps/api/src/routes/preferences/get.ts | 39 ++++----- apps/api/tests/preferences/get.spec.ts | 2 - .../Common/Providers/PreferencesProvider.tsx | 4 +- .../Shared/GlobalBanners/Flagged.tsx | 4 +- .../Shared/GlobalBanners/ProtectProfile.tsx | 4 +- .../Shared/GlobalBanners/Suspended.tsx | 4 +- .../Users/Overview/Tool/Restrictions.tsx | 84 ------------------- .../Staff/Users/Overview/Tool/index.tsx | 2 - packages/data/feature-flags.ts | 4 +- packages/lib/api/getPreferences.ts | 2 - packages/types/hey.d.ts | 2 - 15 files changed, 34 insertions(+), 194 deletions(-) create mode 100644 apps/api/src/db/migrations/20240108060056_remove_profile_restrictions/migration.sql delete mode 100644 apps/api/src/routes/internal/restrictions/update.ts delete mode 100644 apps/web/src/components/Staff/Users/Overview/Tool/Restrictions.tsx diff --git a/apps/api/src/db/migrations/20240108060056_remove_profile_restrictions/migration.sql b/apps/api/src/db/migrations/20240108060056_remove_profile_restrictions/migration.sql new file mode 100644 index 00000000000..2e05c764d27 --- /dev/null +++ b/apps/api/src/db/migrations/20240108060056_remove_profile_restrictions/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - You are about to drop the `ProfileRestriction` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropTable +DROP TABLE "ProfileRestriction"; diff --git a/apps/api/src/db/schema.prisma b/apps/api/src/db/schema.prisma index 39e29918d36..68f19993bde 100644 --- a/apps/api/src/db/schema.prisma +++ b/apps/api/src/db/schema.prisma @@ -40,17 +40,6 @@ model Preference { createdAt DateTime @default(now()) } -model ProfileRestriction { - id String @id - isFlagged Boolean @default(false) - isSuspended Boolean @default(false) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - - // Indexes - @@index([isFlagged, isSuspended]) -} - model MembershipNft { id String @id dismissedOrMinted Boolean @default(false) diff --git a/apps/api/src/routes/internal/cron/cleanPostgres.ts b/apps/api/src/routes/internal/cron/cleanPostgres.ts index 9514850fc7c..940cba7ce08 100644 --- a/apps/api/src/routes/internal/cron/cleanPostgres.ts +++ b/apps/api/src/routes/internal/cron/cleanPostgres.ts @@ -37,11 +37,6 @@ export const post: Handler = async (req, res) => { } try { - // Cleanup ProfileRestriction - await prisma.profileRestriction.deleteMany({ - where: { isFlagged: false, isSuspended: false } - }); - // Cleanup Preference await prisma.preference.deleteMany({ where: { highSignalNotificationFilter: false, isPride: false } diff --git a/apps/api/src/routes/internal/restrictions/update.ts b/apps/api/src/routes/internal/restrictions/update.ts deleted file mode 100644 index 27d85889188..00000000000 --- a/apps/api/src/routes/internal/restrictions/update.ts +++ /dev/null @@ -1,53 +0,0 @@ -import type { Handler } from 'express'; - -import logger from '@hey/lib/logger'; -import catchedError from '@utils/catchedError'; -import validateIsStaff from '@utils/middlewares/validateIsStaff'; -import prisma from '@utils/prisma'; -import { invalidBody, noBody, notAllowed } from '@utils/responses'; -import { boolean, object, string } from 'zod'; - -type ExtensionRequest = { - id: string; - isFlagged: boolean; - isSuspended: boolean; -}; - -const validationSchema = object({ - id: string(), - isFlagged: boolean(), - isSuspended: boolean() -}); - -export const post: Handler = async (req, res) => { - const { body } = req; - - if (!body) { - return noBody(res); - } - - const validation = validationSchema.safeParse(body); - - if (!validation.success) { - return invalidBody(res); - } - - if (!(await validateIsStaff(req))) { - return notAllowed(res); - } - - const { id, isFlagged, isSuspended } = body as ExtensionRequest; - - try { - const restrictions = await prisma.profileRestriction.upsert({ - create: { id, isFlagged, isSuspended, updatedAt: new Date() }, - update: { isFlagged, isSuspended, updatedAt: new Date() }, - where: { id: id } - }); - logger.info(`Updated restrictions for ${id}`); - - return res.status(200).json({ restrictions, success: true }); - } catch (error) { - return catchedError(res, error); - } -}; diff --git a/apps/api/src/routes/preferences/get.ts b/apps/api/src/routes/preferences/get.ts index 33ea0763ce2..1e11410889a 100644 --- a/apps/api/src/routes/preferences/get.ts +++ b/apps/api/src/routes/preferences/get.ts @@ -19,28 +19,21 @@ export const get: Handler = async (req, res) => { } try { - const [ - preference, - pro, - features, - membershipNft, - restriction, - trustedProfile - ] = await prisma.$transaction([ - prisma.preference.findUnique({ where: { id: id as string } }), - prisma.pro.findFirst({ where: { profileId: id as string } }), - prisma.profileFeature.findMany({ - select: { feature: { select: { key: true } } }, - where: { - enabled: true, - feature: { enabled: true }, - profileId: id as string - } - }), - prisma.membershipNft.findUnique({ where: { id: id as string } }), - prisma.profileRestriction.findUnique({ where: { id: id as string } }), - prisma.trustedProfile.findUnique({ where: { id: id as string } }) - ]); + const [preference, pro, features, membershipNft, trustedProfile] = + await prisma.$transaction([ + prisma.preference.findUnique({ where: { id: id as string } }), + prisma.pro.findFirst({ where: { profileId: id as string } }), + prisma.profileFeature.findMany({ + select: { feature: { select: { key: true } } }, + where: { + enabled: true, + feature: { enabled: true }, + profileId: id as string + } + }), + prisma.membershipNft.findUnique({ where: { id: id as string } }), + prisma.trustedProfile.findUnique({ where: { id: id as string } }) + ]); const response: Preferences = { features: features.map((feature: any) => feature.feature?.key), @@ -50,10 +43,8 @@ export const get: Handler = async (req, res) => { highSignalNotificationFilter: Boolean( preference?.highSignalNotificationFilter ), - isFlagged: Boolean(restriction?.isFlagged), isPride: Boolean(preference?.isPride), isPro: Boolean(pro), - isSuspended: Boolean(restriction?.isSuspended), isTrusted: Boolean(trustedProfile) }; diff --git a/apps/api/tests/preferences/get.spec.ts b/apps/api/tests/preferences/get.spec.ts index 93523a00e18..e45cab55208 100644 --- a/apps/api/tests/preferences/get.spec.ts +++ b/apps/api/tests/preferences/get.spec.ts @@ -21,7 +21,5 @@ describe('preferences/get', () => { expect(response.data.result.isTrusted).toBeTruthy(); expect(response.data.result.highSignalNotificationFilter).toBeTruthy(); expect(response.data.result.isPride).toBeTruthy(); - expect(response.data.result.isFlagged).toBeFalsy(); - expect(response.data.result.isSuspended).toBeFalsy(); }); }); diff --git a/apps/web/src/components/Common/Providers/PreferencesProvider.tsx b/apps/web/src/components/Common/Providers/PreferencesProvider.tsx index 3f8b6ed4707..3044079cb60 100644 --- a/apps/web/src/components/Common/Providers/PreferencesProvider.tsx +++ b/apps/web/src/components/Common/Providers/PreferencesProvider.tsx @@ -59,8 +59,8 @@ const PreferencesProvider: FC = () => { // Restriction setRestriction({ - isFlagged: preferences.isFlagged, - isSuspended: preferences.isSuspended + isFlagged: preferences.features.includes(FeatureFlag.Flagged), + isSuspended: preferences.features.includes(FeatureFlag.Suspended) }); // Feature flags diff --git a/apps/web/src/components/Shared/GlobalBanners/Flagged.tsx b/apps/web/src/components/Shared/GlobalBanners/Flagged.tsx index 534b45115e7..0e4afadfafc 100644 --- a/apps/web/src/components/Shared/GlobalBanners/Flagged.tsx +++ b/apps/web/src/components/Shared/GlobalBanners/Flagged.tsx @@ -15,8 +15,8 @@ const Flagged: FC = () => { return (
- - + +
diff --git a/apps/web/src/components/Shared/GlobalBanners/ProtectProfile.tsx b/apps/web/src/components/Shared/GlobalBanners/ProtectProfile.tsx index f6f595d3c23..9606cde91ad 100644 --- a/apps/web/src/components/Shared/GlobalBanners/ProtectProfile.tsx +++ b/apps/web/src/components/Shared/GlobalBanners/ProtectProfile.tsx @@ -69,8 +69,8 @@ const ProtectProfile: FC = () => { return (
- - + +
diff --git a/apps/web/src/components/Shared/GlobalBanners/Suspended.tsx b/apps/web/src/components/Shared/GlobalBanners/Suspended.tsx index 386696d566c..a59cd5d3354 100644 --- a/apps/web/src/components/Shared/GlobalBanners/Suspended.tsx +++ b/apps/web/src/components/Shared/GlobalBanners/Suspended.tsx @@ -15,8 +15,8 @@ const Suspended: FC = () => { return (
- - + +
diff --git a/apps/web/src/components/Staff/Users/Overview/Tool/Restrictions.tsx b/apps/web/src/components/Staff/Users/Overview/Tool/Restrictions.tsx deleted file mode 100644 index 168651f30a5..00000000000 --- a/apps/web/src/components/Staff/Users/Overview/Tool/Restrictions.tsx +++ /dev/null @@ -1,84 +0,0 @@ -import type { Preferences } from '@hey/types/hey'; - -import { NoSymbolIcon } from '@heroicons/react/24/outline'; -import { HEY_API_URL } from '@hey/data/constants'; -import { Toggle } from '@hey/ui'; -import getAuthWorkerHeaders from '@lib/getAuthWorkerHeaders'; -import axios from 'axios'; -import { type FC, useEffect, useState } from 'react'; -import toast from 'react-hot-toast'; - -import ToggleWrapper from './ToggleWrapper'; - -interface RestrictionsProps { - preferences: Preferences; - profileId: string; -} - -const Restrictions: FC = ({ preferences, profileId }) => { - const [disabled, setDisabled] = useState(false); - const [isFlagged, setIsFlagged] = useState(false); - const [isSuspended, setIsSuspended] = useState(false); - - useEffect(() => { - if (preferences) { - setIsFlagged(preferences.isFlagged); - setIsSuspended(preferences.isSuspended); - } - }, [preferences]); - - const updateRestriction = async ( - isFlagged: boolean, - isSuspended: boolean - ) => { - setDisabled(true); - toast.promise( - axios.post( - `${HEY_API_URL}/internal/restrictions/update`, - { id: profileId, isFlagged, isSuspended }, - { headers: getAuthWorkerHeaders() } - ), - { - error: () => { - setDisabled(false); - return 'Error updating restriction'; - }, - loading: 'Updating restriction...', - success: () => { - setDisabled(false); - setIsFlagged(isFlagged); - setIsSuspended(isSuspended); - - return 'Restriction updated'; - } - } - ); - }; - - return ( - <> -
- -
Restrictions
-
-
- - updateRestriction(!isFlagged, isSuspended)} - /> - - - updateRestriction(isFlagged, !isSuspended)} - /> - -
- - ); -}; - -export default Restrictions; diff --git a/apps/web/src/components/Staff/Users/Overview/Tool/index.tsx b/apps/web/src/components/Staff/Users/Overview/Tool/index.tsx index 658ec1e1c0e..b81e96b39f4 100644 --- a/apps/web/src/components/Staff/Users/Overview/Tool/index.tsx +++ b/apps/web/src/components/Staff/Users/Overview/Tool/index.tsx @@ -27,7 +27,6 @@ import LeafwatchDetails from './LeafwatchDetails'; import ManagedProfiles from './ManagedProfiles'; import OnchainIdentities from './OnchainIdentities'; import Rank from './Rank'; -import Restrictions from './Restrictions'; interface ProfileStaffToolProps { profile: Profile; @@ -161,7 +160,6 @@ const ProfileStaffTool: FC = ({ profile }) => { {preferences ? ( <> -