@@ -133,6 +133,50 @@ async function insertCollections(
133
133
} ) ,
134
134
) as ( typeof schema . organizationsTable . $inferInsert ) [ ] ;
135
135
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
+ ) ;
136
180
break ;
137
181
}
138
182
case "organization_memberships" : {
0 commit comments