-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathinject-user-data.ts
More file actions
99 lines (82 loc) · 3.27 KB
/
inject-user-data.ts
File metadata and controls
99 lines (82 loc) · 3.27 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
import { Script } from "../scriptRunner";
import { PrismaClient } from "../../dist";
import { confirmAction } from "../utils";
const mockUsers = [
{ name: "Alice Johnson", email: "alice.johnson@example.com" },
{ name: "Bob Smith", email: "bob.smith@example.com" },
{ name: "Charlie Brown", email: "charlie.brown@example.com" },
{ name: "Diana Prince", email: "diana.prince@example.com" },
{ name: "Ethan Hunt", email: "ethan.hunt@example.com" },
{ name: "Fiona Green", email: "fiona.green@example.com" },
{ name: "George Miller", email: "george.miller@example.com" },
{ name: "Hannah Lee", email: "hannah.lee@example.com" },
{ name: "Ivan Petrov", email: "ivan.petrov@example.com" },
{ name: "Julia Chen", email: "julia.chen@example.com" },
];
export const injectUserData: Script = {
run: async (prisma: PrismaClient) => {
const orgId = 1;
// Check if org exists
const org = await prisma.org.findUnique({
where: { id: orgId }
});
if (!org) {
console.error(`Organization with id ${orgId} not found. Please create it first.`);
return;
}
console.log(`Injecting ${mockUsers.length} mock users for organization: ${org.name} (${org.domain})`);
confirmAction();
const createdUsers: { id: string; email: string | null; name: string | null }[] = [];
for (const mockUser of mockUsers) {
// Check if user already exists
const existingUser = await prisma.user.findUnique({
where: { email: mockUser.email }
});
if (existingUser) {
console.log(`User ${mockUser.email} already exists, skipping...`);
createdUsers.push(existingUser);
continue;
}
// Create the user
const user = await prisma.user.create({
data: {
name: mockUser.name,
email: mockUser.email,
}
});
console.log(`Created user: ${user.name} (${user.email})`);
createdUsers.push(user);
}
// Add users to the organization
for (const user of createdUsers) {
// Check if already a member
const existingMembership = await prisma.userToOrg.findUnique({
where: {
orgId_userId: {
orgId,
userId: user.id,
}
}
});
if (existingMembership) {
console.log(`User ${user.email} is already a member of the org, skipping...`);
continue;
}
await prisma.userToOrg.create({
data: {
orgId,
userId: user.id,
role: "MEMBER",
}
});
console.log(`Added ${user.email} to organization`);
}
console.log(`\nUser data injection complete!`);
console.log(`Total users created/found: ${createdUsers.length}`);
// Show org membership count
const memberCount = await prisma.userToOrg.count({
where: { orgId }
});
console.log(`Total org members: ${memberCount}`);
},
};