Skip to content

Commit 3f1e458

Browse files
authored
fix: Added API_ADMINISTRATOR_USER as administrator for all organizations while loading sample data (#3208)
1 parent 51c11d2 commit 3f1e458

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/utilities/loadSampleData.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,50 @@ async function insertCollections(
133133
}),
134134
) as (typeof schema.organizationsTable.$inferInsert)[];
135135
await db.insert(schema.organizationsTable).values(organizations);
136+
137+
// Add API_ADMINISTRATOR_USER_EMAIL_ADDRESS as administrator of the all organization
138+
const API_ADMINISTRATOR_USER_EMAIL_ADDRESS =
139+
process.env.API_ADMINISTRATOR_USER_EMAIL_ADDRESS;
140+
if (!API_ADMINISTRATOR_USER_EMAIL_ADDRESS) {
141+
console.error(
142+
"\x1b[31m",
143+
"API_ADMINISTRATOR_USER_EMAIL_ADDRESS is not defined in .env file",
144+
);
145+
return;
146+
}
147+
148+
const API_ADMINISTRATOR_USER = await db.query.usersTable.findFirst({
149+
columns: {
150+
id: true,
151+
},
152+
where: (fields, operators) =>
153+
operators.eq(
154+
fields.emailAddress,
155+
API_ADMINISTRATOR_USER_EMAIL_ADDRESS,
156+
),
157+
});
158+
if (!API_ADMINISTRATOR_USER) {
159+
console.error(
160+
"\x1b[31m",
161+
"API_ADMINISTRATOR_USER_EMAIL_ADDRESS is not found in users table",
162+
);
163+
return;
164+
}
165+
166+
const organizationAdminMembership = organizations.map((org) => ({
167+
organizationId: org.id,
168+
memberId: API_ADMINISTRATOR_USER.id,
169+
creatorId: API_ADMINISTRATOR_USER.id,
170+
createdAt: new Date(),
171+
role: "administrator",
172+
})) as (typeof schema.organizationMembershipsTable.$inferInsert)[];
173+
await db
174+
.insert(schema.organizationMembershipsTable)
175+
.values(organizationAdminMembership);
176+
console.log(
177+
"\x1b[35m",
178+
"Added API_ADMINISTRATOR_USER as administrator of the all organization",
179+
);
136180
break;
137181
}
138182
case "organization_memberships": {

0 commit comments

Comments
 (0)