From 1b9f3adfc5072034684d287d199cbcf3182d50cd Mon Sep 17 00:00:00 2001 From: bigint <69431456+bigint@users.noreply.github.com> Date: Mon, 8 Jan 2024 11:39:04 +0530 Subject: [PATCH] feat: remove trusted profile table and migrate them to features --- .../migration.sql | 8 +++ apps/api/src/db/schema.prisma | 5 -- .../api/src/routes/internal/trusted/update.ts | 54 ------------------ apps/api/src/routes/preferences/get.ts | 8 +-- apps/api/src/utils/constants.ts | 2 + .../utils/middlewares/validateIsTrusted.ts | 11 +++- apps/api/tests/preferences/get.spec.ts | 1 - .../Common/Providers/PreferencesProvider.tsx | 14 ++--- .../Users/Overview/Tool/Access/Trusted.tsx | 55 ------------------- .../Users/Overview/Tool/Access/index.tsx | 2 - packages/data/feature-flags.ts | 3 +- packages/lib/api/getPreferences.ts | 3 +- packages/types/hey.d.ts | 1 - 13 files changed, 29 insertions(+), 138 deletions(-) create mode 100644 apps/api/src/db/migrations/20240108060821_remove_trusted_profile/migration.sql delete mode 100644 apps/api/src/routes/internal/trusted/update.ts delete mode 100644 apps/web/src/components/Staff/Users/Overview/Tool/Access/Trusted.tsx diff --git a/apps/api/src/db/migrations/20240108060821_remove_trusted_profile/migration.sql b/apps/api/src/db/migrations/20240108060821_remove_trusted_profile/migration.sql new file mode 100644 index 000000000000..573f97dd4233 --- /dev/null +++ b/apps/api/src/db/migrations/20240108060821_remove_trusted_profile/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - You are about to drop the `TrustedProfile` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropTable +DROP TABLE "TrustedProfile"; diff --git a/apps/api/src/db/schema.prisma b/apps/api/src/db/schema.prisma index 68f19993bdea..47a07e82c8a5 100644 --- a/apps/api/src/db/schema.prisma +++ b/apps/api/src/db/schema.prisma @@ -12,11 +12,6 @@ model Verified { createdAt DateTime @default(now()) } -model TrustedProfile { - id String @id - createdAt DateTime @default(now()) -} - model StaffPick { id String @id type StaffPickType diff --git a/apps/api/src/routes/internal/trusted/update.ts b/apps/api/src/routes/internal/trusted/update.ts deleted file mode 100644 index 104ad855cf09..000000000000 --- a/apps/api/src/routes/internal/trusted/update.ts +++ /dev/null @@ -1,54 +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 = { - enabled: boolean; - id: string; -}; - -const validationSchema = object({ - enabled: boolean(), - id: string() -}); - -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 { enabled, id } = body as ExtensionRequest; - - try { - if (enabled) { - await prisma.trustedProfile.create({ data: { id } }); - logger.info(`Enabled trusted profile for ${id}`); - - return res.status(200).json({ enabled, success: true }); - } - - await prisma.trustedProfile.delete({ where: { id } }); - logger.info(`Disabled trusted profile for ${id}`); - - return res.status(200).json({ enabled, 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 1e11410889a1..70091b6edf67 100644 --- a/apps/api/src/routes/preferences/get.ts +++ b/apps/api/src/routes/preferences/get.ts @@ -19,7 +19,7 @@ export const get: Handler = async (req, res) => { } try { - const [preference, pro, features, membershipNft, trustedProfile] = + const [preference, pro, features, membershipNft] = await prisma.$transaction([ prisma.preference.findUnique({ where: { id: id as string } }), prisma.pro.findFirst({ where: { profileId: id as string } }), @@ -31,8 +31,7 @@ export const get: Handler = async (req, res) => { profileId: id as string } }), - prisma.membershipNft.findUnique({ where: { id: id as string } }), - prisma.trustedProfile.findUnique({ where: { id: id as string } }) + prisma.membershipNft.findUnique({ where: { id: id as string } }) ]); const response: Preferences = { @@ -44,8 +43,7 @@ export const get: Handler = async (req, res) => { preference?.highSignalNotificationFilter ), isPride: Boolean(preference?.isPride), - isPro: Boolean(pro), - isTrusted: Boolean(trustedProfile) + isPro: Boolean(pro) }; logger.info('Profile preferences fetched'); diff --git a/apps/api/src/utils/constants.ts b/apps/api/src/utils/constants.ts index e61a2a30e1ee..98a97e1923db 100644 --- a/apps/api/src/utils/constants.ts +++ b/apps/api/src/utils/constants.ts @@ -8,6 +8,8 @@ export const STAFF_FEATURE_ID = 'eea3b2d2-a60c-4e41-8130-1cb34cc37810'; export const STAFF_MODE_FEATURE_ID = '0e588583-b347-4752-9e1e-0ad4128348e8'; export const GARDENER_FEATURE_ID = '0a441129-182a-4a3f-83cf-a13c5ad8282b'; export const GARDENER_MODE_FEATURE_ID = '9f66a465-e1d7-4123-b329-ddd14fd85510'; +export const TRUSTED_PROFILE_FEATURE_ID = + '266c7bc3-93ae-4565-8065-de636bce58b3'; // Cache // Cache for 1 minute, stale for 30 days diff --git a/apps/api/src/utils/middlewares/validateIsTrusted.ts b/apps/api/src/utils/middlewares/validateIsTrusted.ts index d87a45ee7b3d..aac68dea66e4 100644 --- a/apps/api/src/utils/middlewares/validateIsTrusted.ts +++ b/apps/api/src/utils/middlewares/validateIsTrusted.ts @@ -1,6 +1,7 @@ import type { Request } from 'express'; import parseJwt from '@hey/lib/parseJwt'; +import { TRUSTED_PROFILE_FEATURE_ID } from '@utils/constants'; import prisma from '@utils/prisma'; import validateLensAccount from './validateLensAccount'; @@ -23,11 +24,15 @@ const validateIsTrusted = async (request: Request) => { } const payload = parseJwt(accessToken); - const data = await prisma.trustedProfile.findFirst({ - where: { id: payload.id } + const data = await prisma.profileFeature.findFirst({ + where: { + enabled: true, + featureId: TRUSTED_PROFILE_FEATURE_ID, + profileId: payload.id + } }); - if (data?.id) { + if (data?.enabled) { return true; } diff --git a/apps/api/tests/preferences/get.spec.ts b/apps/api/tests/preferences/get.spec.ts index e45cab552081..1ffcc8e1e1ed 100644 --- a/apps/api/tests/preferences/get.spec.ts +++ b/apps/api/tests/preferences/get.spec.ts @@ -18,7 +18,6 @@ describe('preferences/get', () => { expect(response.data.result.features).toBeInstanceOf(Array); expect(response.data.result.hasDismissedOrMintedMembershipNft).toBeTruthy(); expect(response.data.result.isPro).toBeTruthy(); - expect(response.data.result.isTrusted).toBeTruthy(); expect(response.data.result.highSignalNotificationFilter).toBeTruthy(); expect(response.data.result.isPride).toBeTruthy(); }); diff --git a/apps/web/src/components/Common/Providers/PreferencesProvider.tsx b/apps/web/src/components/Common/Providers/PreferencesProvider.tsx index 3044079cb609..2696ca9abe63 100644 --- a/apps/web/src/components/Common/Providers/PreferencesProvider.tsx +++ b/apps/web/src/components/Common/Providers/PreferencesProvider.tsx @@ -54,21 +54,17 @@ const PreferencesProvider: FC = () => { // Pro setIsPro(preferences.isPro); - // Trusted - setIsTrusted(preferences.isTrusted); - - // Restriction - setRestriction({ - isFlagged: preferences.features.includes(FeatureFlag.Flagged), - isSuspended: preferences.features.includes(FeatureFlag.Suspended) - }); - // Feature flags setFeatureFlags(preferences.features); setStaffMode(preferences.features.includes(FeatureFlag.StaffMode)); setGardenerMode( preferences?.features.includes(FeatureFlag.GardenerMode) ); + setRestriction({ + isFlagged: preferences.features.includes(FeatureFlag.Flagged), + isSuspended: preferences.features.includes(FeatureFlag.Suspended) + }); + setIsTrusted(preferences.features.includes(FeatureFlag.TrustedProfile)); // Membership NFT setHasDismissedOrMintedMembershipNft( diff --git a/apps/web/src/components/Staff/Users/Overview/Tool/Access/Trusted.tsx b/apps/web/src/components/Staff/Users/Overview/Tool/Access/Trusted.tsx deleted file mode 100644 index 9375a8592201..000000000000 --- a/apps/web/src/components/Staff/Users/Overview/Tool/Access/Trusted.tsx +++ /dev/null @@ -1,55 +0,0 @@ -import type { FC } from 'react'; - -import { HEY_API_URL } from '@hey/data/constants'; -import { Toggle } from '@hey/ui'; -import getAuthWorkerHeaders from '@lib/getAuthWorkerHeaders'; -import axios from 'axios'; -import { useEffect, useState } from 'react'; -import { toast } from 'react-hot-toast'; - -import ToggleWrapper from '../ToggleWrapper'; - -interface TrustedProps { - isTrusted: boolean; - profileId: string; -} - -const Trusted: FC = ({ isTrusted: enabled, profileId }) => { - const [disabled, setDisabled] = useState(false); - const [isTrusted, setIsTrusted] = useState(false); - - useEffect(() => { - setIsTrusted(enabled); - }, [enabled]); - - const updateTrusted = async () => { - setDisabled(true); - toast.promise( - axios.post( - `${HEY_API_URL}/internal/trusted/update`, - { enabled: !isTrusted, id: profileId }, - { headers: getAuthWorkerHeaders() } - ), - { - error: () => { - setDisabled(false); - return 'Error updating trusted status'; - }, - loading: 'Updating trusted status...', - success: () => { - setIsTrusted(!isTrusted); - setDisabled(false); - return 'Trusted status updated'; - } - } - ); - }; - - return ( - - - - ); -}; - -export default Trusted; diff --git a/apps/web/src/components/Staff/Users/Overview/Tool/Access/index.tsx b/apps/web/src/components/Staff/Users/Overview/Tool/Access/index.tsx index c5da1afbe23b..34085647b2ca 100644 --- a/apps/web/src/components/Staff/Users/Overview/Tool/Access/index.tsx +++ b/apps/web/src/components/Staff/Users/Overview/Tool/Access/index.tsx @@ -4,7 +4,6 @@ import type { FC } from 'react'; import { AdjustmentsVerticalIcon } from '@heroicons/react/24/solid'; import ActivateLifetimePro from './ActivateLifetimePro'; -import Trusted from './Trusted'; import Verify from './Verify'; interface AccessProps { @@ -22,7 +21,6 @@ const Access: FC = ({ preferences, profileId }) => {
-
); diff --git a/packages/data/feature-flags.ts b/packages/data/feature-flags.ts index 16b09f7833b6..84a1c3b0c4b5 100644 --- a/packages/data/feature-flags.ts +++ b/packages/data/feature-flags.ts @@ -5,5 +5,6 @@ export enum FeatureFlag { LensMember = 'lens-member', Staff = 'staff', StaffMode = 'staff-mode', - Suspended = 'suspended' + Suspended = 'suspended', + TrustedProfile = 'trusted-profile' } diff --git a/packages/lib/api/getPreferences.ts b/packages/lib/api/getPreferences.ts index 5130702eb8bd..591e38dac3b4 100644 --- a/packages/lib/api/getPreferences.ts +++ b/packages/lib/api/getPreferences.ts @@ -26,8 +26,7 @@ const getPreferences = async ( hasDismissedOrMintedMembershipNft: false, highSignalNotificationFilter: false, isPride: false, - isPro: false, - isTrusted: false + isPro: false }; } }; diff --git a/packages/types/hey.d.ts b/packages/types/hey.d.ts index de6378785609..ed6546361d76 100644 --- a/packages/types/hey.d.ts +++ b/packages/types/hey.d.ts @@ -88,5 +88,4 @@ export type Preferences = { highSignalNotificationFilter: boolean; isPride: boolean; isPro: boolean; - isTrusted: boolean; };