Repository navigation
Expand file tree
/
Copy pathseed.ts
More file actions
294 lines (272 loc) · 7.48 KB
/
Copy pathseed.ts
File metadata and controls
294 lines (272 loc) · 7.48 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { exit } from "process";
import { hash } from "argon2";
import { UserType } from "@prisma/client";
import { db } from "@/db";
import type { Prisma } from "@prisma/client";
import StreamIoService from "@/services/streamIoService";
import FileService from "@/services/fileService";
import UserService from "@/services/userService";
const existingStreamTokens: { [email: string]: string } = {};
async function fetchExistingStreamTokens() {
const users = await db.user.findMany({
where: {
streamUserToken: {
not: null,
},
},
});
users.forEach((user) => {
existingStreamTokens[user.email] = user.streamUserToken!;
});
}
/**
* Creates a user with a Stream Chat user token in the DB
*/
async function createUser(
user: Omit<Prisma.UserCreateInput, "streamUserToken">
): Promise<ReturnType<typeof db.user.create>> {
const streamUser = existingStreamTokens[user.email]
? {
userId: StreamIoService.getUserIdFromUser({ email: user.email }),
userToken: existingStreamTokens[user.email],
}
: await StreamIoService.createUser(user, UserService.isStaff(user));
return await db.user.create({
data: {
...user,
streamUserId: streamUser.userId,
streamUserToken: streamUser.userToken,
},
});
}
const buildPartnerDetails = (
siteName: string,
department: string,
contactPrefix: string,
latitude: number,
longitude: number
): Prisma.JsonObject =>
({
siteName,
address: `${siteName}, ${department}, Haiti`,
department,
latitude,
longitude,
website: "https://hopeforhaiti.org",
socialMedia: `@${contactPrefix}`,
regionalContact: {
firstName: "Marie",
lastName: "Joseph",
orgTitle: "Regional Director",
primaryTelephone: "+509 1234-5678",
email: `${contactPrefix}.regional@test.com`,
},
medicalContact: {
firstName: "Jean",
lastName: "Baptiste",
orgTitle: "Chief Medical Officer",
primaryTelephone: "+509 2345-6789",
email: `${contactPrefix}.medical@test.com`,
},
pharmacyContact: {
firstName: "Claire",
lastName: "Dumas",
orgTitle: "Pharmacy Lead",
primaryTelephone: "+509 3456-7890",
email: `${contactPrefix}.pharmacy@test.com`,
},
facilityType: ["clinic", "primary_care"],
organizationType: ["non_profit"],
cleanWaterAccessible: true,
medicationDisposalProcessDefined: true,
programs: [
"maternal_health",
"chronic_disease_support",
"mobile_outreach",
"emergency_response",
],
}) as Prisma.JsonObject;
async function buildSeedData() {
await fetchExistingStreamTokens();
// Clear all Stream Chat channels before seeding
await StreamIoService.deleteAllChannels();
// Get all signoffs with signature URLs before deleting
const signOffs = await db.signOff.findMany({
select: {
signatureUrl: true,
},
});
// Delete signatures from Azure Storage
for (const signOff of signOffs) {
if (signOff.signatureUrl) {
try {
await FileService.deleteSignature(signOff.signatureUrl);
} catch (error) {
console.warn(
`Failed to delete signature: ${signOff.signatureUrl}`,
error
);
}
}
}
// Delete all data
await db.allocation.deleteMany();
await db.generalItemRequest.deleteMany();
await db.lineItem.deleteMany();
await db.generalItem.deleteMany();
await db.donorOffer.deleteMany();
await db.distribution.deleteMany();
await db.signOff.deleteMany();
await db.wishlist.deleteMany();
await db.shippingStatus.deleteMany();
await db.userInvite.deleteMany();
await db.notification.deleteMany();
await db.passwordResetToken.deleteMany();
await db.itemPrice.deleteMany();
await db.user.deleteMany();
await db.tag.deleteMany();
const passwordHash = await hash("root");
const internalTag = await db.tag.upsert({
where: { name: "internal" },
create: { name: "internal" },
update: {},
});
const externalTag = await db.tag.upsert({
where: { name: "external" },
create: { name: "external" },
update: {},
});
// Create super admin staff user
const superAdmin = await createUser({
email: "superadmin@test.com",
name: "Super Admin",
passwordHash,
type: UserType.STAFF,
enabled: true,
pending: false,
isSuper: true,
userRead: true,
userWrite: true,
itemNotify: true,
offerWrite: true,
requestRead: true,
requestWrite: true,
allocationRead: true,
allocationWrite: true,
archivedRead: true,
distributionRead: true,
distributionWrite: true,
shipmentRead: true,
shipmentWrite: true,
signoffWrite: true,
wishlistRead: true,
supportRead: true,
supportWrite: true,
supportNotify: true,
});
// Standard Admin (Has userWrite, but NOT isSuper)
// Use this account to see if it can deactivate partners but NOT Super Admins
const staffAdmin = await createUser({
email: "staffadmin@test.com",
name: "Staff Admin",
passwordHash,
type: UserType.STAFF,
enabled: true,
pending: false,
isSuper: false,
userRead: true,
userWrite: true,
itemNotify: true,
offerWrite: true,
requestRead: true,
requestWrite: true,
allocationRead: true,
allocationWrite: true,
archivedRead: true,
distributionRead: true,
distributionWrite: true,
shipmentRead: true,
shipmentWrite: true,
signoffWrite: true,
wishlistRead: true,
supportRead: true,
supportWrite: true,
supportNotify: true,
});
// Create distribution lead staff user
const distributionLead = await createUser({
email: "distribution@test.com",
name: "Distribution Lead",
passwordHash,
type: UserType.STAFF,
enabled: true,
pending: false,
userRead: false,
userWrite: false,
requestRead: true,
allocationRead: true,
allocationWrite: true,
archivedRead: true,
distributionRead: true,
distributionWrite: true,
shipmentRead: true,
shipmentWrite: true,
signoffWrite: true,
wishlistRead: false,
offerWrite: false,
requestWrite: false,
itemNotify: false,
supportRead: true,
supportWrite: false,
supportNotify: false,
});
// Create first partner user
const partner1 = await createUser({
email: "partner1@test.com",
name: "Hope Medical Center",
passwordHash,
type: UserType.PARTNER,
tags: { connect: [{ id: internalTag.id }] },
enabled: true,
pending: false,
partnerDetails: buildPartnerDetails(
"Hope Medical Center",
"Ouest",
"hopecenter",
18.5392,
-72.335
),
});
// Create second partner user
const partner2 = await createUser({
email: "partner2@test.com",
name: "Les Cayes Community Hospital",
passwordHash,
type: UserType.PARTNER,
tags: { connect: [{ id: externalTag.id }] },
enabled: true,
pending: false,
partnerDetails: buildPartnerDetails(
"Les Cayes Community Hospital",
"Sud",
"lescayes",
18.25,
-73.75
),
});
console.log("✓ Created users");
console.log(` - Super Admin: ${superAdmin.email}`);
console.log(` - Staff Admin: ${staffAdmin.email}`);
console.log(` - Distribution Lead: ${distributionLead.email}`);
console.log(` - Partner 1: ${partner1.email}`);
console.log(` - Partner 2: ${partner2.email}`);
}
buildSeedData()
.then(() => {
console.info("\n✅ Database seeded successfully!");
exit(0);
})
.catch((error) => {
console.error("❌ Error seeding database", error);
exit(1);
});