You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { IDEMPOTENCY_TTL_MS } from "@/lib/constants/quote";
import { prisma } from "@/lib/prisma";
import { getL1, setL1 } from "@/modules/cache/redis-cache";
export type IdempotencyHit = {
hit: true;
quoteId: string;
response: unknown;
};
export type IdempotencyMiss = {
hit: false;
};
export type IdempotencyResult = IdempotencyHit | IdempotencyMiss;
/** 查 L1 Redis → DB 双保险24h 内同 request_id 返回同一 quote_id */
export async function checkIdempotency(
requestId: string,
): Promise<IdempotencyResult> {
const cached = await getL1<{ quoteId: string; response: unknown }>(
requestId,
);
if (cached?.quoteId) {
return {
hit: true,
quoteId: cached.quoteId,
response: cached.response,
};
}
const record = await prisma.idempotencyRecord.findUnique({
where: { requestId },
select: { quoteId: true },
});
if (!record) {
return { hit: false };
}
const quote = await prisma.quoteRecord.findUnique({
where: { quoteId: record.quoteId },
select: {
quoteId: true,
status: true,
sourceType: true,
isRealtime: true,
},
});
const response = {
quote_id: record.quoteId,
status: quote?.status ?? "done",
};
await setL1(requestId, { quoteId: record.quoteId, response });
return {
hit: true,
quoteId: record.quoteId,
response,
};
}
/** 写入 DB + Redis L1处理唯一键冲突 */
export async function saveIdempotency(
requestId: string,
customerId: string,
quoteId: string,
response: unknown,
): Promise<void> {
const expireAt = new Date(Date.now() + IDEMPOTENCY_TTL_MS);
try {
await prisma.idempotencyRecord.create({
data: {
requestId,
customerId,
quoteId,
expireAt,
},
});
} catch (error: unknown) {
const prismaError = error as { code?: string };
if (prismaError.code === "P2002") {
const existing = await prisma.idempotencyRecord.findUnique({
where: { requestId },
select: { quoteId: true },
});
if (existing) {
return;
}
}
throw error;
}
await setL1(requestId, { quoteId, response });
}