Repository navigation
Expand file tree
/
Copy pathcreate-source-users.ts
More file actions
117 lines (98 loc) · 3.05 KB
/
Copy pathcreate-source-users.ts
File metadata and controls
117 lines (98 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Create Users for Feed Sources Script
*
* This script creates user profiles for RSS feed sources that don't have one yet.
* Each feed source needs a linked user to serve as the author for aggregated articles.
*
* Run with: npx tsx scripts/create-source-users.ts
* Dry run: npx tsx scripts/create-source-users.ts --dry-run
*/
import { db } from "@/server/db";
import { feed_sources } from "@/server/db/schema";
import { isNull } from "drizzle-orm";
import { buildSourceUsername, createSourceUser } from "@/server/lib/sourceUser";
const isDryRun = process.argv.includes("--dry-run");
interface SourceWithoutUser {
id: number;
name: string;
slug: string | null;
logoUrl: string | null;
websiteUrl: string | null;
description: string | null;
}
async function createSourceUsers(): Promise<{
checked: number;
created: number;
sources: SourceWithoutUser[];
}> {
console.log("Finding feed sources without user profiles...");
// Get all feed sources without a linked user
const sourcesWithoutUsers = await db
.select({
id: feed_sources.id,
name: feed_sources.name,
slug: feed_sources.slug,
logoUrl: feed_sources.logoUrl,
websiteUrl: feed_sources.websiteUrl,
description: feed_sources.description,
})
.from(feed_sources)
.where(isNull(feed_sources.userId));
console.log(
`Found ${sourcesWithoutUsers.length} sources without user profiles`,
);
let created = 0;
for (const source of sourcesWithoutUsers) {
const username = buildSourceUsername(source);
console.log(` Creating user for: ${source.name} (${username})`);
if (!isDryRun) {
try {
const userId = await createSourceUser(db, source);
created++;
console.log(` ✓ Created user ${userId} for source ${source.id}`);
} catch (error) {
console.error(` ✗ Error creating user for ${source.name}:`, error);
}
} else {
console.log(` [DRY RUN] Would create user: ${username}`);
}
}
return {
checked: sourcesWithoutUsers.length,
created,
sources: sourcesWithoutUsers,
};
}
async function main() {
console.log("=".repeat(60));
console.log("Feed Source User Creation Script");
console.log(
isDryRun ? "[DRY RUN MODE - No changes will be made]" : "[LIVE MODE]",
);
console.log("=".repeat(60));
console.log();
try {
const result = await createSourceUsers();
console.log();
console.log("=".repeat(60));
console.log("Summary:");
console.log(` Sources checked: ${result.checked}`);
console.log(` Users created: ${result.created}`);
if (result.sources.length > 0) {
console.log();
console.log("Sources processed:");
for (const source of result.sources) {
console.log(` - ${source.name} (ID: ${source.id})`);
}
}
if (isDryRun && result.sources.length > 0) {
console.log();
console.log("Run without --dry-run to create these users.");
}
console.log("=".repeat(60));
} catch (error) {
console.error("Fatal error:", error);
process.exit(1);
}
}
main();