@loomcore/api 0.1.58 → 0.1.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { randomUUID } from 'crypto';
2
- import { initializeSystemUserContext, EmptyUserContext, getSystemUserContext } from '@loomcore/common/models';
2
+ import { initializeSystemUserContext, EmptyUserContext, getSystemUserContext, isSystemUserContextInitialized } from '@loomcore/common/models';
3
3
  import { MongoDBDatabase } from '../mongo-db.database.js';
4
4
  import { AuthService, OrganizationService } from '../../../services/index.js';
5
5
  export const getMongoInitialSchema = (config) => {
@@ -150,11 +150,20 @@ export const getMongoInitialSchema = (config) => {
150
150
  migrations.push({
151
151
  name: '00000000000009_data-admin-user',
152
152
  up: async ({ context: db }) => {
153
- if (config.auth?.adminUser?.email || !config.auth?.adminUser?.password) {
153
+ if (!config.auth?.adminUser?.email || !config.auth?.adminUser?.password) {
154
154
  throw new Error('Admin user email and password must be provided in config');
155
155
  }
156
156
  const database = new MongoDBDatabase(db);
157
157
  const authService = new AuthService(database);
158
+ if (!isSystemUserContextInitialized()) {
159
+ const errorMessage = isMultiTenant
160
+ ? 'SystemUserContext has not been initialized. The meta-org migration (00000000000008_data-meta-org) should have run before this migration. ' +
161
+ 'This migration only runs if config.app.metaOrgName and config.app.metaOrgCode are provided. ' +
162
+ 'Please ensure both values are set in your config.'
163
+ : 'BUG: SystemUserContext has not been initialized. For non-multi-tenant setups, SystemUserContext should be initialized before migrations run.';
164
+ console.error('❌ Migration Error:', errorMessage);
165
+ throw new Error(errorMessage);
166
+ }
158
167
  const systemUserContext = getSystemUserContext();
159
168
  const _id = randomUUID().toString();
160
169
  await authService.createUser(systemUserContext, {
@@ -1,4 +1,4 @@
1
- import { initializeSystemUserContext, EmptyUserContext, getSystemUserContext } from '@loomcore/common/models';
1
+ import { initializeSystemUserContext, EmptyUserContext, getSystemUserContext, isSystemUserContextInitialized } from '@loomcore/common/models';
2
2
  import { PostgresDatabase } from '../postgres.database.js';
3
3
  import { AuthService, OrganizationService } from '../../../services/index.js';
4
4
  export const getPostgresInitialSchema = (config) => {
@@ -218,23 +218,16 @@ export const getPostgresInitialSchema = (config) => {
218
218
  try {
219
219
  const database = new PostgresDatabase(client);
220
220
  const authService = new AuthService(database);
221
- let systemUserContext = getSystemUserContext();
222
- if (!systemUserContext) {
223
- throw new Error('SystemUserContext has not been initialized. For non-multi-tenant setups, initialize it before running migrations.');
224
- }
225
- if (isMultiTenant) {
226
- if (!systemUserContext.organization?._id) {
227
- const organizationService = new OrganizationService(database);
228
- const metaOrg = await organizationService.getMetaOrg(EmptyUserContext);
229
- if (metaOrg) {
230
- initializeSystemUserContext(config.email?.systemEmailAddress || 'system@example.com', metaOrg);
231
- systemUserContext = getSystemUserContext();
232
- }
233
- if (!systemUserContext?.organization?._id) {
234
- throw new Error('Cannot create admin user: Multi-tenant mode is enabled but meta-org does not exist. Ensure metaOrgName and metaOrgCode are provided in config so the meta-org migration runs before the admin-user migration.');
235
- }
236
- }
221
+ if (!isSystemUserContextInitialized()) {
222
+ const errorMessage = isMultiTenant
223
+ ? 'SystemUserContext has not been initialized. The meta-org migration (00000000000008_data-meta-org) should have run before this migration. ' +
224
+ 'This migration only runs if config.app.metaOrgName and config.app.metaOrgCode are provided. ' +
225
+ 'Please ensure both values are set in your config.'
226
+ : 'BUG: SystemUserContext has not been initialized. For non-multi-tenant setups, SystemUserContext should be initialized before migrations run.';
227
+ console.error('❌ Migration Error:', errorMessage);
228
+ throw new Error(errorMessage);
237
229
  }
230
+ const systemUserContext = getSystemUserContext();
238
231
  const userData = {
239
232
  email: config.auth?.adminUser?.email,
240
233
  password: config.auth?.adminUser?.password,
@@ -174,7 +174,7 @@ export class AuthService extends MultiTenantApiService {
174
174
  }
175
175
  const httpOrHttps = config.env === 'local' ? 'http' : 'https';
176
176
  const urlEncodedEmail = encodeURIComponent(emailAddress);
177
- const resetPasswordLink = `${httpOrHttps}://${config.network.hostName}/reset-password/${passwordResetToken.token}/${urlEncodedEmail}`;
177
+ const resetPasswordLink = `${httpOrHttps}://${config.app.name}/reset-password/${passwordResetToken.token}/${urlEncodedEmail}`;
178
178
  const htmlEmailBody = `<strong><a href="${resetPasswordLink}">Reset Password</a></strong>`;
179
179
  await this.emailService.sendHtmlEmail(emailAddress, `Reset Password for ${config.app.name}`, htmlEmailBody);
180
180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.58",
3
+ "version": "0.1.60",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb or PostgreSQL",
6
6
  "scripts": {