mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-08-28 19:48:01 +08:00
refactor: reuse bound address lookup
This commit is contained in:
@@ -6,6 +6,22 @@ import { unbindTelegramByAddress } from '../telegram_api/common';
|
|||||||
import i18n from '../i18n';
|
import i18n from '../i18n';
|
||||||
import { updateAddressUpdatedAt, commonGetUserRole, handleListQuery, hideObjectFields } from '../common';
|
import { updateAddressUpdatedAt, commonGetUserRole, handleListQuery, hideObjectFields } from '../common';
|
||||||
|
|
||||||
|
export const getBindedAddressById = async (
|
||||||
|
c: Context<HonoCustomType>,
|
||||||
|
user_id: number | string,
|
||||||
|
address_id: number | string
|
||||||
|
): Promise<string | null> => {
|
||||||
|
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<string>('name');
|
||||||
|
return address ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
const UserBindAddressModule = {
|
const UserBindAddressModule = {
|
||||||
bind: async (c: Context<HonoCustomType>) => {
|
bind: async (c: Context<HonoCustomType>) => {
|
||||||
const { user_id } = c.get("userPayload");
|
const { user_id } = c.get("userPayload");
|
||||||
@@ -158,17 +174,10 @@ const UserBindAddressModule = {
|
|||||||
if (!address_id || !user_id) {
|
if (!address_id || !user_id) {
|
||||||
return c.text(msgs.InvalidAddressOrUserTokenMsg, 400)
|
return c.text(msgs.InvalidAddressOrUserTokenMsg, 400)
|
||||||
}
|
}
|
||||||
// check users_address if address binded
|
const name = await getBindedAddressById(c, user_id, address_id);
|
||||||
const db_user_id = await c.env.DB.prepare(
|
if (!name) {
|
||||||
`SELECT user_id FROM users_address WHERE address_id = ? and user_id = ?`
|
|
||||||
).bind(address_id, user_id).first("user_id");
|
|
||||||
if (!db_user_id) {
|
|
||||||
return c.text(msgs.AddressNotBindedMsg, 400)
|
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({
|
const jwt = await Jwt.sign({
|
||||||
address: name,
|
address: name,
|
||||||
address_id: address_id
|
address_id: address_id
|
||||||
|
|||||||
@@ -8,27 +8,18 @@ import {
|
|||||||
requestSendMailAccess,
|
requestSendMailAccess,
|
||||||
} from "../mails_api/send_balance";
|
} from "../mails_api/send_balance";
|
||||||
import { getBooleanValue } from "../utils";
|
import { getBooleanValue } from "../utils";
|
||||||
|
import { getBindedAddressById } from "./bind_address";
|
||||||
const getBindedAddress = async (
|
|
||||||
c: Context<HonoCustomType>
|
|
||||||
): Promise<string | null> => {
|
|
||||||
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<string>("name");
|
|
||||||
return address ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const getAddressOrError = async (
|
const getAddressOrError = async (
|
||||||
c: Context<HonoCustomType>
|
c: Context<HonoCustomType>
|
||||||
): Promise<string | Response> => {
|
): Promise<string | Response> => {
|
||||||
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) {
|
if (address) {
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user