@loomcore/api 0.1.58 → 0.1.59

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,10 +1,18 @@
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) => {
6
6
  const migrations = [];
7
7
  const isMultiTenant = config.app.isMultiTenant === true;
8
+ console.log('📋 Migration Config Diagnostic:');
9
+ console.log(' isMultiTenant:', isMultiTenant);
10
+ console.log(' config.app.metaOrgName:', config.app.metaOrgName ?? '(undefined)');
11
+ console.log(' config.app.metaOrgCode:', config.app.metaOrgCode ?? '(undefined)');
12
+ console.log(' config.auth?.adminUser?.email:', config.auth?.adminUser?.email ?? '(undefined)');
13
+ console.log(' config.auth?.adminUser?.password:', config.auth?.adminUser?.password ? '(set)' : '(undefined)');
14
+ console.log(' Will add meta-org migration:', isMultiTenant && !!config.app.metaOrgName && !!config.app.metaOrgCode);
15
+ console.log(' Will add admin-user migration:', !!config.auth?.adminUser?.email && !!config.auth?.adminUser?.password);
8
16
  if (isMultiTenant) {
9
17
  migrations.push({
10
18
  name: '00000000000001_schema-organizations',
@@ -150,11 +158,19 @@ export const getMongoInitialSchema = (config) => {
150
158
  migrations.push({
151
159
  name: '00000000000009_data-admin-user',
152
160
  up: async ({ context: db }) => {
153
- if (config.auth?.adminUser?.email || !config.auth?.adminUser?.password) {
161
+ if (!config.auth?.adminUser?.email || !config.auth?.adminUser?.password) {
154
162
  throw new Error('Admin user email and password must be provided in config');
155
163
  }
156
164
  const database = new MongoDBDatabase(db);
157
165
  const authService = new AuthService(database);
166
+ if (isMultiTenant && !isSystemUserContextInitialized()) {
167
+ throw new Error('SystemUserContext has not been initialized. The meta-org migration (00000000000008_data-meta-org) should have run before this migration. ' +
168
+ 'This migration only runs if config.app.metaOrgName and config.app.metaOrgCode are provided. ' +
169
+ 'Please ensure both values are set in your config.');
170
+ }
171
+ if (!isMultiTenant && !isSystemUserContextInitialized()) {
172
+ initializeSystemUserContext(config.email?.systemEmailAddress || 'system@example.com', undefined);
173
+ }
158
174
  const systemUserContext = getSystemUserContext();
159
175
  const _id = randomUUID().toString();
160
176
  await authService.createUser(systemUserContext, {
@@ -1,9 +1,17 @@
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) => {
5
5
  const migrations = [];
6
6
  const isMultiTenant = config.app.isMultiTenant === true;
7
+ console.log('📋 Migration Config Diagnostic:');
8
+ console.log(' isMultiTenant:', isMultiTenant);
9
+ console.log(' config.app.metaOrgName:', config.app.metaOrgName ?? '(undefined)');
10
+ console.log(' config.app.metaOrgCode:', config.app.metaOrgCode ?? '(undefined)');
11
+ console.log(' config.auth?.adminUser?.email:', config.auth?.adminUser?.email ?? '(undefined)');
12
+ console.log(' config.auth?.adminUser?.password:', config.auth?.adminUser?.password ? '(set)' : '(undefined)');
13
+ console.log(' Will add meta-org migration:', isMultiTenant && !!config.app.metaOrgName && !!config.app.metaOrgCode);
14
+ console.log(' Will add admin-user migration:', !!config.auth?.adminUser?.email && !!config.auth?.adminUser?.password);
7
15
  if (isMultiTenant) {
8
16
  migrations.push({
9
17
  name: '00000000000001_schema-organizations',
@@ -218,23 +226,15 @@ export const getPostgresInitialSchema = (config) => {
218
226
  try {
219
227
  const database = new PostgresDatabase(client);
220
228
  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.');
229
+ if (isMultiTenant && !isSystemUserContextInitialized()) {
230
+ throw new Error('SystemUserContext has not been initialized. The meta-org migration (00000000000008_data-meta-org) should have run before this migration. ' +
231
+ 'This migration only runs if config.app.metaOrgName and config.app.metaOrgCode are provided. ' +
232
+ 'Please ensure both values are set in your config.');
224
233
  }
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
- }
234
+ if (!isMultiTenant && !isSystemUserContextInitialized()) {
235
+ initializeSystemUserContext(config.email?.systemEmailAddress || 'system@example.com', undefined);
237
236
  }
237
+ const systemUserContext = getSystemUserContext();
238
238
  const userData = {
239
239
  email: config.auth?.adminUser?.email,
240
240
  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.59",
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": {