Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/flat-frogs-go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 1 addition & 1 deletion server/src/routes/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function createAdminRouter(): { pageRouter: Router; apiRouter: Router } {
// =========================================================================

// Prospect management routes
setupProspectRoutes(apiRouter);
setupProspectRoutes(apiRouter, { workos });

// Organization detail and management routes
setupOrganizationRoutes(pageRouter, apiRouter, { workos });
Expand Down
32 changes: 31 additions & 1 deletion server/src/routes/admin/prospects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { Router } from "express";
import { WorkOS } from "@workos-inc/node";
import { getPool } from "../../db/client.js";
import { createLogger } from "../../logger.js";
import { requireAuth, requireAdmin } from "../../middleware/auth.js";
Expand All @@ -12,7 +13,12 @@ import { COMPANY_TYPE_VALUES } from "../../config/company-types.js";

const logger = createLogger("admin-prospects");

export function setupProspectRoutes(apiRouter: Router): void {
interface ProspectRoutesConfig {
workos: WorkOS | null;
}

export function setupProspectRoutes(apiRouter: Router, config: ProspectRoutesConfig): void {
const { workos } = config;

// GET /api/admin/prospects - List all prospects with action-based views
apiRouter.get("/prospects", requireAuth, requireAdmin, async (req, res) => {
Expand Down Expand Up @@ -601,6 +607,30 @@ export function setupProspectRoutes(apiRouter: Router): void {
});
}

// If name is being updated, sync to WorkOS first
if (updates.name && typeof updates.name === "string" && updates.name.trim()) {
const trimmedName = updates.name.trim();
if (workos) {
try {
await workos.organizations.updateOrganization({
organization: orgId,
name: trimmedName,
});
logger.info({ orgId, newName: trimmedName }, "Organization name synced to WorkOS");
} catch (workosError) {
logger.error({ err: workosError, orgId }, "Failed to update organization name in WorkOS");
return res.status(500).json({
error: "Failed to update organization name",
message: `Could not sync name change to WorkOS: ${workosError instanceof Error ? workosError.message : 'Unknown error'}`,
});
}
} else {
logger.warn({ orgId }, "WorkOS not configured - organization name change will not be synced");
}
// Use trimmed name for local DB update
updates.name = trimmedName;
}

// Build dynamic UPDATE query
const allowedFields = [
"name",
Expand Down