-
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathcreate-source-users.ts
More file actions
149 lines (127 loc) · 4.02 KB
/
create-source-users.ts
File metadata and controls
149 lines (127 loc) · 4.02 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* 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 { user, feed_sources } from "@/server/db/schema";
import { eq, isNull } from "drizzle-orm";
import crypto from "crypto";
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) {
// Generate a unique username from slug or name
const baseUsername =
source.slug ||
source.name
.toLowerCase()
.replace(/[^a-z0-9]/g, "-")
.substring(0, 30);
const username = `source-${baseUsername}`;
// Generate a unique email (not used for login, just for uniqueness)
const email = `source-${source.id}@feeds.codu.co`;
console.log(` Creating user for: ${source.name} (${username})`);
if (!isDryRun) {
try {
// Create user for this source
const userId = crypto.randomUUID();
await db.insert(user).values({
id: userId,
username: username,
name: source.name,
email: email,
image: source.logoUrl || "/images/person.png",
bio:
source.description?.substring(0, 200) ||
`Content from ${source.name}`,
websiteUrl: source.websiteUrl || "",
// Feed source users don't need email notifications
emailNotifications: false,
newsletter: false,
});
// Link the user to the feed source
await db
.update(feed_sources)
.set({ userId: userId })
.where(eq(feed_sources.id, source.id));
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();