This commit is contained in:
eric
2026-03-26 11:32:13 -05:00
parent 8c865cf0e8
commit f5068c0ead
3 changed files with 40 additions and 1 deletions

View File

@@ -35,6 +35,8 @@ type RecoveryResult = {
source?: string;
};
const inflightEmailChecks = new Map<string, Promise<PayjsVipEmailResult | null>>();
const PAYJS_CHECK_BY_EMAIL_TIMEOUT =
Number(process.env.PAYJSAPI_CHECK_BY_EMAIL_TIMEOUT) || 20000;
const PAYJS_CHECK_BY_EMAIL_RETRIES =
@@ -62,6 +64,13 @@ async function checkVipByEmailFromPayjsapi(
password: string,
userId?: string
): Promise<PayjsVipEmailResult | null> {
const inflightKey = `${email}::${userId || ""}`;
const existed = inflightEmailChecks.get(inflightKey);
if (existed) {
return existed;
}
const task = (async () => {
const payConfig = getPaymentConfig();
const primaryUrl = resolvePayApiUrl(request);
const localUrl = getLocalPayApiUrl();
@@ -125,6 +134,14 @@ async function checkVipByEmailFromPayjsapi(
}
}
return null;
})();
inflightEmailChecks.set(inflightKey, task);
try {
return await task;
} finally {
inflightEmailChecks.delete(inflightKey);
}
}
async function loginPocketBaseUser(

View File

@@ -15,6 +15,8 @@ type PayjsVipResult = {
email?: string;
};
const inflightUserChecks = new Map<string, Promise<PayjsVipResult | null>>();
async function fetchPayjsVipWithRetry(
url: string,
timeoutMs: number,
@@ -51,6 +53,12 @@ async function checkVipFromPayjsapi(
request: NextRequest,
userId: string
): Promise<PayjsVipResult | null> {
const existed = inflightUserChecks.get(userId);
if (existed) {
return existed;
}
const task = (async () => {
try {
const apiUrl = resolvePayApiUrl(request);
const url = `${apiUrl}/nomadvip/check_vip?user_id=${encodeURIComponent(
@@ -95,6 +103,14 @@ async function checkVipFromPayjsapi(
);
return null;
}
})();
inflightUserChecks.set(userId, task);
try {
return await task;
} finally {
inflightUserChecks.delete(userId);
}
}
export async function POST(request: NextRequest) {