dreame-shopify-app/app/services/idempotency.server.ts

58 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2026-06-19 00:53:13 +08:00
import db from "../db.server";
export async function findSuccessfulPush(shop: string, orderId: string, activityId: number) {
const record = await db.activityPushIdempotency.findUnique({
where: {
shop_orderId_activityId: {
shop,
orderId,
activityId: activityId.toString(),
},
},
});
if (!record) {
return null;
}
return {
eventId: record.eventId,
redirectUrl: record.redirectUrl,
};
}
export async function recordSuccessfulPush(entry: {
shop: string;
orderId: string;
activityId: number;
eventType: "order_end";
eventId: string;
redirectUrl: string;
buttonText: string;
}) {
await db.activityPushIdempotency.upsert({
where: {
shop_orderId_activityId: {
shop: entry.shop,
orderId: entry.orderId,
activityId: entry.activityId.toString(),
},
},
update: {
eventId: entry.eventId,
redirectUrl: entry.redirectUrl,
buttonText: entry.buttonText,
pushedAt: new Date(),
},
create: {
shop: entry.shop,
orderId: entry.orderId,
activityId: entry.activityId.toString(),
eventType: entry.eventType,
eventId: entry.eventId,
redirectUrl: entry.redirectUrl,
buttonText: entry.buttonText,
},
});
}