/ opencodehub / src / db / seed-notifications.ts
swadhinbiswas Fix: Add date validation in analysis.ts to prevent RangeError Β· 1/20/2026
a7662cd
72 lines Β· 3.0 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
/**
 * Seed demo notifications for testing
 */
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { getDatabase, schema } from "@/db";
import { notifications, users, repositories } from "@/db/schema";
import { generateId, now } from "@/lib/utils";
import { faker } from "@faker-js/faker";

async function seedNotifications() {
    console.log("πŸ”” Seeding notifications...");
    const db = getDatabase() as NodePgDatabase<typeof schema>;

    const allUsers = await db.query.users.findMany();
    const allRepos = await db.query.repositories.findMany({ limit: 50 });

    if (allUsers.length === 0) {
        console.error("❌ No users found.");
        process.exit(1);
    }

    const notificationTypes = [
        { type: "mention", reason: "mention", titlePrefix: "You were mentioned in" },
        { type: "assign", reason: "assign", titlePrefix: "You were assigned to" },
        { type: "review_request", reason: "review_requested", titlePrefix: "Review requested for" },
        { type: "comment", reason: "comment", titlePrefix: "New comment on" },
        { type: "ci_failed", reason: "ci_activity", titlePrefix: "CI failed for" },
        { type: "ci_success", reason: "ci_activity", titlePrefix: "CI passed for" },
        { type: "star", reason: "subscribed", titlePrefix: "New star on" },
    ];

    const notifsToCreate = 100;
    console.log(`Creating ${notifsToCreate} notifications...`);

    for (let i = 0; i < notifsToCreate; i++) {
        const targetUser = faker.helpers.arrayElement(allUsers);
        const actor = faker.helpers.arrayElement(allUsers.filter(u => u.id !== targetUser.id)) || allUsers[0];
        const repo = allRepos.length > 0 ? faker.helpers.arrayElement(allRepos) : null;
        const notifType = faker.helpers.arrayElement(notificationTypes);

        const subjectTypes = ["issue", "pull_request", "commit"];
        const subjectType = faker.helpers.arrayElement(subjectTypes);
        const subjectId = `${subjectType}_${faker.string.alphanumeric(8)}`;

        const title = repo
            ? `${notifType.titlePrefix} ${repo.name}#${faker.number.int({ min: 1, max: 500 })}`
            : `${notifType.titlePrefix} a ${subjectType.replace("_", " ")}`;

        await db.insert(notifications).values({
            id: generateId("notif"),
            userId: targetUser.id,
            repositoryId: repo?.id,
            type: notifType.type,
            title,
            body: faker.lorem.sentence(),
            url: repo ? `/${repo.name}/${subjectType}s/${faker.number.int({ min: 1, max: 100 })}` : "#",
            actorId: actor.id,
            subjectType,
            subjectId,
            reason: notifType.reason,
            isRead: faker.datatype.boolean({ probability: 0.3 }),
            isArchived: faker.datatype.boolean({ probability: 0.1 }),
            createdAt: faker.date.recent({ days: 14 }),
            updatedAt: new Date(),
        });
    }

    console.log("βœ… Notifications seeded!");
}

seedNotifications().catch(console.error);