Skip to content

Commit

Permalink
refactor(api): remove user address code from api
Browse files Browse the repository at this point in the history
  • Loading branch information
Redm4x committed Aug 28, 2024
1 parent 605d0d5 commit 57111b8
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 105 deletions.
3 changes: 1 addition & 2 deletions apps/api/src/db/dbConnection.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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();
}
Expand Down
56 changes: 1 addition & 55 deletions apps/api/src/routers/userRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand Down
31 changes: 1 addition & 30 deletions apps/api/src/services/db/userDataService.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 } });

Expand Down
4 changes: 2 additions & 2 deletions packages/database/dbSchemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Model<any, any>>[] {
let models: ModelCtor<Model<any, any>>[] = baseModels;
Expand Down Expand Up @@ -31,6 +31,6 @@ export function getChainModels(chainName: string) {
}

export const chainModels = [...getFilteredBaseModel(), ...(activeChain.customModels ?? [])];
export const userModels: ModelCtor<Model<any, any>>[] = [UserSetting, Template, TemplateFavorite, UserAddressName];
export const userModels: ModelCtor<Model<any, any>>[] = [UserSetting, Template, TemplateFavorite];
export const Block = activeChain.customBlockModel || BaseBlock;
export const Message = activeChain.customMessageModel || BaseMessage;
1 change: 0 additions & 1 deletion packages/database/dbSchemas/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { Template } from "./template";
export { TemplateFavorite } from "./templateFavorite";
export { UserAddressName } from "./userAddressName";
export { UserSetting } from "./userSetting";
15 changes: 0 additions & 15 deletions packages/database/dbSchemas/user/userAddressName.ts

This file was deleted.

0 comments on commit 57111b8

Please sign in to comment.