diff --git a/worker/src/user_api/bind_address.ts b/worker/src/user_api/bind_address.ts index 22a7b92..c21c075 100644 --- a/worker/src/user_api/bind_address.ts +++ b/worker/src/user_api/bind_address.ts @@ -6,6 +6,22 @@ import { unbindTelegramByAddress } from '../telegram_api/common'; import i18n from '../i18n'; import { updateAddressUpdatedAt, commonGetUserRole, handleListQuery, hideObjectFields } from '../common'; +export const getBindedAddressById = async ( + c: Context, + user_id: number | string, + address_id: number | string +): Promise => { + if (!user_id || !address_id) { + return null; + } + const address = await c.env.DB.prepare( + `SELECT a.name FROM users_address ua` + + ` JOIN address a ON a.id = ua.address_id` + + ` WHERE ua.user_id = ? AND ua.address_id = ?` + ).bind(user_id, address_id).first('name'); + return address ?? null; +} + const UserBindAddressModule = { bind: async (c: Context) => { const { user_id } = c.get("userPayload"); @@ -158,17 +174,10 @@ const UserBindAddressModule = { if (!address_id || !user_id) { return c.text(msgs.InvalidAddressOrUserTokenMsg, 400) } - // check users_address if address binded - const db_user_id = await c.env.DB.prepare( - `SELECT user_id FROM users_address WHERE address_id = ? and user_id = ?` - ).bind(address_id, user_id).first("user_id"); - if (!db_user_id) { + const name = await getBindedAddressById(c, user_id, address_id); + if (!name) { return c.text(msgs.AddressNotBindedMsg, 400) } - // generate jwt - const name = await c.env.DB.prepare( - `SELECT name FROM address WHERE id = ? ` - ).bind(address_id).first("name"); const jwt = await Jwt.sign({ address: name, address_id: address_id diff --git a/worker/src/user_api/user_send_mail_api.ts b/worker/src/user_api/user_send_mail_api.ts index abcbed0..d436289 100644 --- a/worker/src/user_api/user_send_mail_api.ts +++ b/worker/src/user_api/user_send_mail_api.ts @@ -8,27 +8,18 @@ import { requestSendMailAccess, } from "../mails_api/send_balance"; import { getBooleanValue } from "../utils"; - -const getBindedAddress = async ( - c: Context -): Promise => { - const addressId = Number(c.req.param("address_id")); - if (!Number.isInteger(addressId) || addressId <= 0) { - return null; - } - const { user_id } = c.get("userPayload"); - const address = await c.env.DB.prepare( - `SELECT a.name FROM users_address ua` - + ` JOIN address a ON a.id = ua.address_id` - + ` WHERE ua.user_id = ? AND a.id = ?` - ).bind(user_id, addressId).first("name"); - return address ?? null; -} +import { getBindedAddressById } from "./bind_address"; const getAddressOrError = async ( c: Context ): Promise => { - const address = await getBindedAddress(c); + const addressId = Number(c.req.param("address_id")); + if (!Number.isInteger(addressId) || addressId <= 0) { + const msgs = i18n.getMessagesbyContext(c); + return c.text(msgs.AddressNotBindedMsg, 400); + } + const { user_id } = c.get("userPayload"); + const address = await getBindedAddressById(c, user_id, addressId); if (address) { return address; }