forked from supafull/magnetic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-seed.ts
226 lines (194 loc) · 6.08 KB
/
db-seed.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { createClient } from "@supabase/supabase-js";
import dotenv from "dotenv";
import { generateCompanies } from "./db/dataGenerator/companies";
import { generateContactNotes } from "./db/dataGenerator/contactNotes";
import { generateContacts } from "./db/dataGenerator/contacts";
import { generateDealNotes } from "./db/dataGenerator/dealNotes";
import { generateDeals } from "./db/dataGenerator/deals";
import { generateSales } from "./db/dataGenerator/sales";
import { generateTags } from "./db/dataGenerator/tags";
import { generateTasks } from "./db/dataGenerator/tasks";
import { ContactNote } from "./frontend/src/types";
import { removeDupes } from "./frontend/src/utils";
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";
dotenv.config();
console.log("Seeding data...");
const supabase = createClient(
process.env.VITE_SUPABASE_URL as string,
process.env.SUPABASE_SERVICE_KEY as string
);
export async function seed() {
// reseed data, first delete all data
const deleteResults = await Promise.all([
supabase
.from("sales")
.delete()
.neq("id", "00000000-0000-0000-0000-000000000000"),
supabase
.from("tags")
.delete()
.neq("id", "00000000-0000-0000-0000-000000000000"),
supabase
.from("companies")
.delete()
.neq("id", "00000000-0000-0000-0000-000000000000"),
supabase
.from("contacts")
.delete()
.neq("id", "00000000-0000-0000-0000-000000000000"),
supabase
.from("contact_notes")
.delete()
.neq("id", "00000000-0000-0000-0000-000000000000"),
supabase
.from("deals")
.delete()
.neq("id", "00000000-0000-0000-0000-000000000000"),
supabase
.from("deal_notes")
.delete()
.neq("id", "00000000-0000-0000-0000-000000000000"),
supabase
.from("tasks")
.delete()
.neq("id", "00000000-0000-0000-0000-000000000000"),
]);
for (const result of deleteResults) {
if (result.error) {
throw result.error;
}
}
await new Promise((r) => setTimeout(r, 1000));
const users = await supabase.auth.admin.listUsers();
// console.log("Deleting users...", users);
const deleteUsers = await Promise.all(
users.data?.users.map((user) =>
supabase.auth.admin.deleteUser(user.id, false)
)
);
for (const result of deleteUsers) {
if (result.error) {
throw result.error;
}
}
console.log("Deleted existing data... Now adding new data...");
// supabase needs to think for a bit unfortunately ;-(
await new Promise((r) => setTimeout(r, 1000));
const sales = generateSales();
console.log(`Adding ${sales.length} sales...`);
const { data: persistedSales, error: errorSales } = await supabase
.from("sales")
.insert(sales)
.select();
if (errorSales) {
throw errorSales;
}
const tags = generateTags();
console.log(`Adding ${tags.length} tags...`);
const { data: persistedTags, error: errorTags } = await supabase
.from("tags")
.insert(tags)
.select();
if (errorTags) {
throw errorTags;
}
const companies = generateCompanies({ sales: persistedSales });
console.log(`Adding ${companies.length} companies...`);
const { data: persistedCompanies, error: errorCompanies } = await supabase
.from("companies")
.insert(companies)
.select();
if (errorCompanies) {
throw errorCompanies;
}
const contacts = generateContacts({
tags: persistedTags,
companies: persistedCompanies,
});
console.log(`Adding ${contacts.length} contacts...`);
const { data: persistedContacts, error: errorContacts } = await supabase
.from("contacts")
.insert(contacts)
.select();
if (errorContacts) {
throw errorContacts;
}
const contactNotes = generateContactNotes({
contacts: persistedContacts,
});
console.log(`Adding ${contactNotes.length} contact notes...`);
const { data: persistedContactNotes, error: errorContactNotes } =
await supabase.from("contact_notes").insert(contactNotes).select();
if (errorContactNotes) {
throw errorContactNotes;
}
const updatedContacts = removeDupes(
(persistedContactNotes as ContactNote[])
.sort((a, b) => new Date(a.date).valueOf() - new Date(b.date).valueOf())
.map((note) =>
persistedContacts.find((contact) => contact.id === note.contact_id)
)
.map((contact) => ({
...contact,
status: persistedContactNotes.find(
(note) => note.contact_id === contact.id
).status,
}))
);
console.log(`Updating ${updatedContacts.length} contacts statuses...`);
const { error } = await supabase.from("contacts").upsert(updatedContacts);
if (error) {
throw error;
}
const deals = generateDeals({
companies: persistedCompanies,
contacts: persistedContacts,
});
console.log(`Adding ${deals.length} deals...`);
const { data: persistedDeals, error: errorDeals } = await supabase
.from("deals")
.insert(deals)
.select();
if (errorDeals) {
throw errorDeals;
}
const dealNotes = generateDealNotes({
companies: persistedCompanies,
deals: persistedDeals,
});
console.log(`Adding ${dealNotes.length} deal notes...`);
const { error: errorDealNotes } = await supabase
.from("deal_notes")
.insert(dealNotes);
if (errorDealNotes) {
throw errorDealNotes;
}
const tasks = generateTasks({
contacts: persistedContacts,
});
console.log(`Adding ${tasks.length} tasks...`);
const { error: errorTasks } = await supabase.from("tasks").insert(tasks);
if (errorTasks) {
throw errorTasks;
}
console.log(`Adding ${sales.length} users...`);
for (const sale of sales) {
const users = await supabase.auth.admin.listUsers();
if (!users.data?.users.find((user) => user.email === sale.email)) {
// console.log("Adding user...", sale);
const { error } = await supabase.auth.admin.createUser({
email: sale.email,
password: "a_good_password",
email_confirm: true,
});
if (error) {
throw error;
}
}
}
console.log("Data seeded successfully");
}
seed().catch((error) => {
console.error(error);
process.exit(1);
});