A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
feat(stripe): add a trpc command for force syncing customer detail
Mohamed Bassema787ee4704b5
diff --git a/apps/cli/src/commands/admin.ts b/apps/cli/src/commands/admin.tsindex 84741bf0..9c3d7c2d 100644--- a/apps/cli/src/commands/admin.ts+++ b/apps/cli/src/commands/admin.ts@@ -535,3 +535,30 @@ jobsCmd});adminCmd.addCommand(jobsCmd);++// --- Subscriptions subcommand (cloud only) ---++if (process.env.KARAKEEP_CLOUD === "1") {+ const subscriptionsCmd = new Command()+ .name("subscriptions")+ .description("admin subscription management commands");++ subscriptionsCmd+ .command("sync")+ .description("force a Stripe sync for a specific user")+ .argument("<userId>", "the id of the user to sync")+ .action(async (userId) => {+ const api = getAPIClient();+ try {+ await api.admin.forceStripeSync.mutate({ userId });+ printStatusMessage(true, "Stripe sync completed successfully");+ } catch (error) {+ printErrorMessageWithReason(+ "Failed to sync user with Stripe",+ error as object,+ );+ }+ });++ adminCmd.addCommand(subscriptionsCmd);+}diff --git a/packages/trpc/routers/admin.ts b/packages/trpc/routers/admin.tsindex cd0a8bf7..76518181 100644--- a/packages/trpc/routers/admin.ts+++ b/packages/trpc/routers/admin.ts@@ -3,7 +3,13 @@ import { TRPCError } from "@trpc/server";import { and, asc, count, eq, gt, inArray, or, sum } from "drizzle-orm";import { z } from "zod";-import { assets, bookmarkLinks, bookmarks, users } from "@karakeep/db/schema";+import {+ assets,+ bookmarkLinks,+ bookmarks,+ subscriptions,+ users,+} from "@karakeep/db/schema";import {AdminMaintenanceQueue,AssetPreprocessingQueue,@@ -37,6 +43,7 @@ import { generatePasswordSalt, hashPassword } from "../auth";import { createAdminScopedProcedure, router } from "../index";import { Bookmark } from "../models/bookmarks";import { User } from "../models/users";+import { syncStripeDataToDatabase } from "./subscriptions";const adminBookmarksProcedure = createAdminScopedProcedure("bookmarks");const adminJobsProcedure = createAdminScopedProcedure("jobs");@@ -926,4 +933,23 @@ export const adminAppRouter = router({},);}),+ forceStripeSync: adminSystemProcedure+ .input(z.object({ userId: z.string() }))+ .output(z.object({ success: z.boolean() }))+ .mutation(async ({ input, ctx }) => {+ const subscription = await ctx.db.query.subscriptions.findFirst({+ where: eq(subscriptions.userId, input.userId),+ });++ if (!subscription?.stripeCustomerId) {+ throw new TRPCError({+ code: "NOT_FOUND",+ message: "No Stripe customer found for the given user",+ });+ }++ await syncStripeDataToDatabase(subscription.stripeCustomerId, ctx.db);++ return { success: true };+ }),});diff --git a/packages/trpc/routers/subscriptions.ts b/packages/trpc/routers/subscriptions.tsindex 01e28f6a..7f74a9c4 100644--- a/packages/trpc/routers/subscriptions.ts+++ b/packages/trpc/routers/subscriptions.ts@@ -93,7 +93,10 @@ function computeSubscriptionTransition(return "no_change";}-async function syncStripeDataToDatabase(customerId: string, db: Context["db"]) {+export async function syncStripeDataToDatabase(+ customerId: string,+ db: Context["db"],+) {return withEventLog("subscription.synced", async () => {addLogFields<"subscription.synced">({"stripe.customer_id": customerId,