59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import { getDbClient } from "./client.server.js";
|
|
|
|
export type LegacyEventWebhookAttempt = {
|
|
id: string;
|
|
endpoint: string;
|
|
tenantId: string | null;
|
|
};
|
|
|
|
export type LegacyEventWebhookAttemptUpdate = {
|
|
startedAt?: Date;
|
|
finishedAt?: Date;
|
|
success?: boolean;
|
|
status?: number;
|
|
message?: string;
|
|
body?: string;
|
|
};
|
|
|
|
export type LegacyEventWebhookAttemptClient = {
|
|
eventWebhookAttempt: {
|
|
findUnique(args: {
|
|
where: { id: string };
|
|
select: { id: true; endpoint: true; event: { select: { tenantId: true } } };
|
|
}): Promise<{ id: string; endpoint: string; event: { tenantId: string | null } } | null>;
|
|
updateMany(args: {
|
|
where: { id: string; event: { tenantId: string | null } };
|
|
data: LegacyEventWebhookAttemptUpdate;
|
|
}): Promise<{ count: number }>;
|
|
};
|
|
};
|
|
|
|
export async function findLegacyEventWebhookAttemptById(
|
|
id: string,
|
|
client: LegacyEventWebhookAttemptClient = getDbClient(),
|
|
): Promise<LegacyEventWebhookAttempt | null> {
|
|
const attempt = await client.eventWebhookAttempt.findUnique({
|
|
where: { id },
|
|
select: {
|
|
id: true,
|
|
endpoint: true,
|
|
event: { select: { tenantId: true } },
|
|
},
|
|
});
|
|
return attempt ? { id: attempt.id, endpoint: attempt.endpoint, tenantId: attempt.event.tenantId } : null;
|
|
}
|
|
|
|
export async function updateLegacyEventWebhookAttempt(
|
|
{ id, tenantId }: { id: string; tenantId: string | null },
|
|
data: LegacyEventWebhookAttemptUpdate,
|
|
client: LegacyEventWebhookAttemptClient = getDbClient(),
|
|
): Promise<void> {
|
|
const updated = await client.eventWebhookAttempt.updateMany({
|
|
where: { id, event: { tenantId } },
|
|
data,
|
|
});
|
|
if (updated.count !== 1) {
|
|
throw new Error(`Event webhook attempt ${id} not found`);
|
|
}
|
|
}
|