mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
Fifty-nine 422s raised off a Zod `safeParse` answered with one opaque sentence and nothing a client could map onto a field, while the other 166 sent `details.issues` — the multi-issue envelope that exists so a caller with three bad fields fixes all three in one round-trip. Twelve of the fifty-nine put a dotted machine token in `error` where the sentence belongs, so `error` was sometimes text to show a person and sometimes a token that must not be shown, with nothing in the payload to tell them apart. Every one of them now answers through `apiValidationError`, which keeps the existing `error` string and the existing `meta` byte for byte and adds the issue list beside them. The dotted tokens keep their place in `error` for the clients that already match on them and are published in `meta.errorCode` as well, which is the field a machine code belongs in and the one a new client should read. A guard reads the route tree per refusal site rather than per file — per file is what let this grow, since a route that used the helper once looked compliant to a `grep -l` sweep — and asserts it still finds the sites it judges, so a matcher that stops matching fails instead of reporting a clean zero.
101 lines
3.0 KiB
TypeScript
101 lines
3.0 KiB
TypeScript
import { prisma } from "@/lib/db";
|
|
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
|
import { auditLog } from "@/lib/auth/audit";
|
|
import {
|
|
apiSuccess,
|
|
apiValidationError,
|
|
safeJson,
|
|
sanitiseZodIssues,
|
|
} from "@/lib/api-response";
|
|
import { annotate } from "@/lib/logging/context";
|
|
import { markDisconnected } from "@/lib/integrations/status";
|
|
import {
|
|
storePolarClientCredentials,
|
|
clearPolarClientCredentials,
|
|
} from "@/lib/polar/credentials";
|
|
import { polarCredentialsSchema } from "@/lib/validations/polar";
|
|
import { NextRequest } from "next/server";
|
|
import { z } from "zod/v4";
|
|
|
|
/**
|
|
* v1.17.1 — per-user Polar BYO-key credentials.
|
|
*
|
|
* Whether the user has their own Polar AccessLink client id/secret stored. The
|
|
* connect/callback/status routes resolve credentials DB-first then env, so an
|
|
* unset pair simply falls back to the shared env app.
|
|
*/
|
|
export const GET = apiHandler(async () => {
|
|
const { user } = await requireAuth();
|
|
annotate({ action: { name: "polar.credentials.get" } });
|
|
|
|
const dbUser = await prisma.user.findUnique({
|
|
where: { id: user.id },
|
|
select: {
|
|
polarClientIdEncrypted: true,
|
|
polarClientSecretEncrypted: true,
|
|
},
|
|
});
|
|
|
|
return apiSuccess({
|
|
hasCredentials:
|
|
!!dbUser?.polarClientIdEncrypted && !!dbUser?.polarClientSecretEncrypted,
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Save Polar OAuth client credentials (encrypted at rest).
|
|
*/
|
|
export const PUT = apiHandler(async (request: NextRequest) => {
|
|
const { user } = await requireAuth();
|
|
annotate({ action: { name: "polar.credentials.update" } });
|
|
|
|
const { data: body, error: jsonError } = await safeJson(request, {
|
|
maxBytes: 64 * 1024,
|
|
});
|
|
if (jsonError) return jsonError;
|
|
|
|
const result = z.safeParse(polarCredentialsSchema, body);
|
|
if (!result.success) {
|
|
return apiValidationError(
|
|
"Client ID and Client Secret are required",
|
|
sanitiseZodIssues(result.error.issues),
|
|
422,
|
|
);
|
|
}
|
|
|
|
await storePolarClientCredentials(
|
|
user.id,
|
|
result.data.clientId,
|
|
result.data.clientSecret,
|
|
);
|
|
await auditLog("polar.credentials.update", { userId: user.id });
|
|
|
|
return apiSuccess({ updated: true });
|
|
});
|
|
|
|
/**
|
|
* Delete Polar credentials and the active connection.
|
|
*
|
|
* Removing the BYO client id/secret also drops the live grant: a token minted
|
|
* under the user's own AccessLink app would be orphaned once the keys are gone.
|
|
* Parks the integration ledger at `disconnected` for parity with the dedicated
|
|
* disconnect route so the status snapshot does not linger at a stale state.
|
|
*/
|
|
export const DELETE = apiHandler(async () => {
|
|
const { user } = await requireAuth();
|
|
annotate({ action: { name: "polar.credentials.delete" } });
|
|
|
|
await prisma.user.update({
|
|
where: { id: user.id },
|
|
data: {
|
|
polarAccessTokenEncrypted: null,
|
|
polarUserIdEncrypted: null,
|
|
},
|
|
});
|
|
await clearPolarClientCredentials(user.id);
|
|
await auditLog("polar.credentials.delete", { userId: user.id });
|
|
await markDisconnected(user.id, "polar");
|
|
|
|
return apiSuccess({ deleted: true });
|
|
});
|