From 57111b82e57cd86d6bc25e59e5d0087bea84fcde Mon Sep 17 00:00:00 2001 From: Redm4x <2829180+Redm4x@users.noreply.github.com> Date: Thu, 22 Aug 2024 00:04:37 -0400 Subject: [PATCH] refactor(api): remove user address code from api --- apps/api/src/db/dbConnection.ts | 3 +- apps/api/src/routers/userRouter.ts | 56 +------------------ apps/api/src/services/db/userDataService.ts | 31 +--------- packages/database/dbSchemas/index.ts | 4 +- packages/database/dbSchemas/user/index.ts | 1 - .../dbSchemas/user/userAddressName.ts | 15 ----- 6 files changed, 5 insertions(+), 105 deletions(-) delete mode 100644 packages/database/dbSchemas/user/userAddressName.ts diff --git a/apps/api/src/db/dbConnection.ts b/apps/api/src/db/dbConnection.ts index dd82647c3..3d36b29fa 100644 --- a/apps/api/src/db/dbConnection.ts +++ b/apps/api/src/db/dbConnection.ts @@ -1,6 +1,6 @@ import { chainDefinitions } from "@akashnetwork/database/chainDefinitions"; import { chainModels, getChainModels, userModels } from "@akashnetwork/database/dbSchemas"; -import { Template, TemplateFavorite, UserAddressName, UserSetting } from "@akashnetwork/database/dbSchemas/user"; +import { Template, TemplateFavorite, UserSetting } from "@akashnetwork/database/dbSchemas/user"; import pg from "pg"; import { Transaction as DbTransaction } from "sequelize"; import { Sequelize } from "sequelize-typescript"; @@ -77,7 +77,6 @@ export const userDb = new Sequelize(env.UserDatabaseCS, { export async function syncUserSchema() { await UserSetting.sync(); - await UserAddressName.sync(); await Template.sync(); await TemplateFavorite.sync(); } diff --git a/apps/api/src/routers/userRouter.ts b/apps/api/src/routers/userRouter.ts index 4e95d7ef4..57a36ce52 100644 --- a/apps/api/src/routers/userRouter.ts +++ b/apps/api/src/routers/userRouter.ts @@ -14,17 +14,7 @@ import { saveTemplate, saveTemplateDesc } from "@src/services/db/templateService"; -import { - checkUsernameAvailable, - getAddressNames, - getSettingsOrInit, - getUserByUsername, - removeAddressName, - saveAddressName, - subscribeToNewsletter, - updateSettings -} from "@src/services/db/userDataService"; -import { isValidBech32Address } from "@src/utils/addresses"; +import { checkUsernameAvailable, getSettingsOrInit, getUserByUsername, subscribeToNewsletter, updateSettings } from "@src/services/db/userDataService"; export const userRouter = new Hono(); @@ -46,50 +36,6 @@ userOptionalRouter.get("/byUsername/:username", async c => { return c.json(user); }); -userRequiredRouter.get("/addressNames", async c => { - const userId = getCurrentUserId(c); - const addressNames = await getAddressNames(userId); - - return c.json(addressNames); -}); - -userRequiredRouter.post("/saveAddressName", async c => { - const userId = getCurrentUserId(c); - const { address, name } = await c.req.json(); - - if (!address) { - return c.text("Address is required", 400); - } - - if (!name) { - return c.text("Name is required", 400); - } - - if (!isValidBech32Address(address, "akash")) { - return c.text("Invalid address", 400); - } - - await saveAddressName(userId, address, name); - - return c.text("Saved"); -}); - -userRequiredRouter.delete("/removeAddressName/:address", async c => { - const userId = getCurrentUserId(c); - - if (!c.req.param("address")) { - return c.text("Address is required", 400); - } - - if (!isValidBech32Address(c.req.param("address"), "akash")) { - return c.text("Invalid address", 400); - } - - await removeAddressName(userId, c.req.param("address")); - - return c.text("Removed"); -}); - userRequiredRouter.post("/tokenInfo", async c => { const userId = getCurrentUserId(c); const { wantedUsername, email, emailVerified, subscribedToNewsletter } = await c.req.json(); diff --git a/apps/api/src/services/db/userDataService.ts b/apps/api/src/services/db/userDataService.ts index 9f30d4c0a..7e3b11f9e 100644 --- a/apps/api/src/services/db/userDataService.ts +++ b/apps/api/src/services/db/userDataService.ts @@ -1,4 +1,4 @@ -import { UserAddressName, UserSetting } from "@akashnetwork/database/dbSchemas/user"; +import { UserSetting } from "@akashnetwork/database/dbSchemas/user"; import pick from "lodash/pick"; import { Transaction } from "sequelize"; import { container } from "tsyringe"; @@ -168,35 +168,6 @@ async function tryToTransferWallet(prevUserId: string, nextUserId: string) { } } -export async function getAddressNames(userId: string) { - const addressNames = await UserAddressName.findAll({ - where: { - userId: userId - } - }); - - return addressNames.reduce((obj, current) => ({ ...obj, [current.address]: current.name }), {}); -} - -export async function saveAddressName(userId: string, address: string, name: string) { - let addressName = await UserAddressName.findOne({ where: { userId: userId, address: address } }); - - if (!addressName) { - addressName = UserAddressName.build({ - userId: userId, - address: address - }); - } - - addressName.name = name; - - await addressName.save(); -} - -export async function removeAddressName(userId: string, address: string) { - await UserAddressName.destroy({ where: { userId: userId, address: address } }); -} - export async function getUserByUsername(username: string) { const user = await UserSetting.findOne({ where: { username: username } }); diff --git a/packages/database/dbSchemas/index.ts b/packages/database/dbSchemas/index.ts index cd89e1983..66018c517 100644 --- a/packages/database/dbSchemas/index.ts +++ b/packages/database/dbSchemas/index.ts @@ -2,7 +2,7 @@ import { Model, ModelCtor } from "sequelize-typescript"; import { activeChain, chainDefinitions } from "../chainDefinitions"; import { AddressReference, Block as BaseBlock, Day, Message as BaseMessage, MonitoredValue, Transaction, Validator } from "./base"; -import { Template, TemplateFavorite, UserAddressName, UserSetting } from "./user"; +import { Template, TemplateFavorite, UserSetting } from "./user"; function getFilteredBaseModel(): ModelCtor>[] { let models: ModelCtor>[] = baseModels; @@ -31,6 +31,6 @@ export function getChainModels(chainName: string) { } export const chainModels = [...getFilteredBaseModel(), ...(activeChain.customModels ?? [])]; -export const userModels: ModelCtor>[] = [UserSetting, Template, TemplateFavorite, UserAddressName]; +export const userModels: ModelCtor>[] = [UserSetting, Template, TemplateFavorite]; export const Block = activeChain.customBlockModel || BaseBlock; export const Message = activeChain.customMessageModel || BaseMessage; diff --git a/packages/database/dbSchemas/user/index.ts b/packages/database/dbSchemas/user/index.ts index 79e7b6439..949281f50 100644 --- a/packages/database/dbSchemas/user/index.ts +++ b/packages/database/dbSchemas/user/index.ts @@ -1,4 +1,3 @@ export { Template } from "./template"; export { TemplateFavorite } from "./templateFavorite"; -export { UserAddressName } from "./userAddressName"; export { UserSetting } from "./userSetting"; diff --git a/packages/database/dbSchemas/user/userAddressName.ts b/packages/database/dbSchemas/user/userAddressName.ts deleted file mode 100644 index 80b93268b..000000000 --- a/packages/database/dbSchemas/user/userAddressName.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { DataTypes, UUIDV4 } from "sequelize"; -import { Column, Default, Model, PrimaryKey, Table } from "sequelize-typescript"; - -import { Required } from "../decorators/requiredDecorator"; - -@Table({ - modelName: "userAddressName", - indexes: [{ unique: true, fields: ["userId", "address"] }] -}) -export class UserAddressName extends Model { - @Required @PrimaryKey @Default(UUIDV4) @Column(DataTypes.UUID) id: string; - @Required @Column userId: string; - @Required @Column address: string; - @Required @Column name: string; -}