@loomcore/api 0.1.80 → 0.1.84

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.
@@ -19,15 +19,18 @@ import { setBaseApiConfig, config } from '../config/index.js';
19
19
  import { entityUtils } from '@loomcore/common/utils';
20
20
  import { getTestMetaOrgUserPerson, getTestOrgUser, getTestOrgUserPerson, setTestMetaOrgUserPersonId, setTestOrgUserPersonId } from './test-objects.js';
21
21
  import { TestEmailClient } from './test-email-client.js';
22
+ import { PersonService } from '../services/person.service.js';
22
23
  let deviceIdCookie;
23
24
  let authService;
24
25
  let organizationService;
26
+ let personService;
25
27
  const JWT_SECRET = 'test-secret';
26
28
  const newUser1Email = 'one@test.com';
27
29
  const newUser1Password = 'testone';
28
30
  const constDeviceIdCookie = crypto.randomBytes(16).toString('hex');
29
31
  function initialize(database) {
30
32
  authService = new AuthService(database);
33
+ personService = new PersonService(database);
31
34
  organizationService = new OrganizationService(database);
32
35
  deviceIdCookie = constDeviceIdCookie;
33
36
  }
@@ -90,7 +93,7 @@ async function setupTestUsers() {
90
93
  }
91
94
  }
92
95
  async function createTestUsers() {
93
- if (!authService || !organizationService) {
96
+ if (!authService || !organizationService || !personService) {
94
97
  throw new Error('Database not initialized. Call initialize() first.');
95
98
  }
96
99
  try {
@@ -110,15 +113,23 @@ async function createTestUsers() {
110
113
  else {
111
114
  setTestOrgId(existingTestOrg._id);
112
115
  }
113
- const createdTestOrgUser = await authService.createUser(getTestOrgUserContext(), getTestOrgUser(), getTestOrgUserPerson());
114
- const createdMetaOrgUser = await authService.createUser(getTestMetaOrgUserContext(), getTestMetaOrgUser(), getTestMetaOrgUserPerson());
116
+ const createdTestOrgUserPerson = await personService.create(getTestOrgUserContext(), getTestOrgUserPerson());
117
+ if (!createdTestOrgUserPerson) {
118
+ throw new Error('Failed to create test organization user person');
119
+ }
120
+ setTestOrgUserPersonId(createdTestOrgUserPerson._id);
121
+ const createdMetaOrgUserPerson = await personService.create(getTestMetaOrgUserContext(), getTestMetaOrgUserPerson());
122
+ if (!createdMetaOrgUserPerson) {
123
+ throw new Error('Failed to create meta organization user person');
124
+ }
125
+ setTestMetaOrgUserPersonId(createdMetaOrgUserPerson._id);
126
+ const createdTestOrgUser = await authService.createUser(getTestOrgUserContext(), getTestOrgUser());
127
+ const createdMetaOrgUser = await authService.createUser(getTestMetaOrgUserContext(), getTestMetaOrgUser());
115
128
  if (!createdTestOrgUser || !createdMetaOrgUser) {
116
129
  throw new Error('Failed to create test user');
117
130
  }
118
131
  setTestMetaOrgUserId(createdMetaOrgUser._id);
119
- setTestMetaOrgUserPersonId(createdMetaOrgUser.personId);
120
132
  setTestOrgUserId(createdTestOrgUser._id);
121
- setTestOrgUserPersonId(createdTestOrgUser.personId);
122
133
  return { metaOrgUser: createdMetaOrgUser, testOrgUser: createdTestOrgUser };
123
134
  }
124
135
  catch (error) {
@@ -93,35 +93,6 @@ export const getPostgresTestSchema = (config) => {
93
93
  await pool.query('DROP TABLE IF EXISTS "testItems"');
94
94
  }
95
95
  });
96
- migrations.push({
97
- name: '00000000000105_schema-persons',
98
- up: async ({ context: pool }) => {
99
- const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER,' : '';
100
- await pool.query(`
101
- CREATE TABLE IF NOT EXISTS "persons" (
102
- "_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
103
- ${orgColumnDef}
104
- "external_id" VARCHAR UNIQUE,
105
- "is_agent" BOOLEAN NOT NULL DEFAULT FALSE,
106
- "is_client" BOOLEAN NOT NULL DEFAULT FALSE,
107
- "is_employee" BOOLEAN NOT NULL DEFAULT FALSE,
108
- "first_name" VARCHAR NOT NULL,
109
- "middle_name" VARCHAR,
110
- "date_of_birth" DATE,
111
- "last_name" VARCHAR NOT NULL,
112
- "_created" TIMESTAMPTZ NOT NULL,
113
- "_createdBy" INTEGER NOT NULL,
114
- "_updated" TIMESTAMPTZ NOT NULL,
115
- "_updatedBy" INTEGER NOT NULL,
116
- "_deleted" TIMESTAMPTZ,
117
- "_deletedBy" INTEGER
118
- )
119
- `);
120
- },
121
- down: async ({ context: pool }) => {
122
- await pool.query('DROP TABLE IF EXISTS "persons"');
123
- }
124
- });
125
96
  migrations.push({
126
97
  name: '00000000000105_5_schema-agents',
127
98
  up: async ({ context: pool }) => {
@@ -6,7 +6,6 @@ export declare class TestExpressApp {
6
6
  private static database;
7
7
  private static testDatabase;
8
8
  private static initPromise;
9
- private static databaseName;
10
9
  static init(useMongoDb?: boolean): Promise<{
11
10
  app: Application;
12
11
  database: IDatabase;
@@ -13,7 +13,6 @@ export class TestExpressApp {
13
13
  static database;
14
14
  static testDatabase;
15
15
  static initPromise = null;
16
- static databaseName = 'test-db';
17
16
  static async init(useMongoDb) {
18
17
  if (useMongoDb === undefined) {
19
18
  const testDb = process.env.TEST_DATABASE;
@@ -41,23 +41,21 @@ export function getTestMetaOrgUser() {
41
41
  };
42
42
  }
43
43
  export function getTestMetaOrgUserPerson() {
44
- return {
44
+ const person = {
45
45
  _id: TEST_META_ORG_USER_PERSON_ID,
46
46
  _orgId: getTestMetaOrg()._id,
47
47
  externalId: 'test-meta-org-user-person-external-id',
48
- middleName: null,
49
48
  firstName: 'Test',
50
49
  lastName: 'User',
51
50
  isAgent: false,
52
51
  isClient: false,
53
52
  isEmployee: false,
54
- dateOfBirth: null,
55
- extendedTypes: 0,
56
53
  _created: new Date(),
57
54
  _createdBy: 'system',
58
55
  _updated: new Date(),
59
56
  _updatedBy: 'system',
60
57
  };
58
+ return person;
61
59
  }
62
60
  export function getTestMetaOrgUserContext() {
63
61
  return {
@@ -120,14 +118,11 @@ export function getTestOrgUserPerson() {
120
118
  _id: TEST_ORG_USER_PERSON_ID,
121
119
  _orgId: getTestOrg()._id,
122
120
  externalId: 'test-org-user-person-external-id',
123
- middleName: null,
124
121
  firstName: 'Test',
125
122
  lastName: 'User',
126
123
  isAgent: false,
127
124
  isClient: false,
128
125
  isEmployee: false,
129
- dateOfBirth: null,
130
- extendedTypes: 0,
131
126
  _created: new Date(),
132
127
  _createdBy: 'system',
133
128
  _updated: new Date(),
@@ -36,8 +36,10 @@ export class AuthController {
36
36
  const body = req.body;
37
37
  let validationErrors = this.authService.validate(body.user);
38
38
  entityUtils.handleValidationResult(validationErrors, 'AuthController.registerUser');
39
- validationErrors = this.personService.validate(body.person);
40
- entityUtils.handleValidationResult(validationErrors, 'AuthController.registerUser');
39
+ if (body.person) {
40
+ validationErrors = this.personService.validate(body.person);
41
+ entityUtils.handleValidationResult(validationErrors, 'AuthController.registerUser');
42
+ }
41
43
  const user = await this.authService.createUser(userContext, body.user, body.person);
42
44
  apiUtils.apiResponse(res, 201, { data: user || undefined }, UserSpec, PublicUserSpec);
43
45
  }
@@ -184,14 +184,11 @@ export const getMongoInitialSchema = (config) => {
184
184
  _id: personId,
185
185
  _orgId: systemUserContext.organization?._id,
186
186
  externalId: 'admin-user-person-external-id',
187
- middleName: null,
188
187
  firstName: 'Admin',
189
188
  lastName: 'User',
190
189
  isAgent: false,
191
190
  isClient: false,
192
191
  isEmployee: false,
193
- dateOfBirth: null,
194
- extendedTypes: 0,
195
192
  _created: new Date(),
196
193
  _createdBy: 'system',
197
194
  _updated: new Date(),
@@ -58,7 +58,7 @@ export const getPostgresInitialSchema = (config) => {
58
58
  "isAgent" BOOLEAN NOT NULL DEFAULT FALSE,
59
59
  "isClient" BOOLEAN NOT NULL DEFAULT FALSE,
60
60
  "isEmployee" BOOLEAN NOT NULL DEFAULT FALSE,
61
- "extendedTypes" INTEGER NOT NULL DEFAULT 0,
61
+ "extendedTypes" INTEGER,
62
62
  "_created" TIMESTAMPTZ NOT NULL,
63
63
  "_createdBy" INTEGER NOT NULL,
64
64
  "_updated" TIMESTAMPTZ NOT NULL,
@@ -23,7 +23,7 @@ export declare class AuthService extends MultiTenantApiService<IUser> {
23
23
  userContext: IUserContext;
24
24
  } | null>;
25
25
  getUserByEmail(email: string): Promise<IUser | null>;
26
- createUser(userContext: IUserContext, user: Partial<IUser>, person: Partial<IPersonModel>): Promise<IUser | null>;
26
+ createUser(userContext: IUserContext, user: Partial<IUser>, person?: Partial<IPersonModel>): Promise<IUser | null>;
27
27
  requestTokenUsingRefreshToken(refreshToken: string, deviceId: string): Promise<ITokenResponse | null>;
28
28
  changeLoggedInUsersPassword(userContext: IUserContext, body: any): Promise<UpdateResult>;
29
29
  changePassword(userContext: IUserContext, queryObject: any, password: string): Promise<UpdateResult>;
@@ -100,13 +100,16 @@ export class AuthService extends MultiTenantApiService {
100
100
  }
101
101
  let personId = user.personId;
102
102
  if (personId && person) {
103
- const updatePersonResult = await this.personService.partialUpdateById(userContext, personId, person);
103
+ await this.personService.partialUpdateById(userContext, personId, person);
104
104
  }
105
- if (!personId) {
105
+ if (!personId && person) {
106
106
  const newPerson = await this.personService.create(userContext, person);
107
107
  if (newPerson) {
108
108
  personId = newPerson._id;
109
109
  }
110
+ else {
111
+ throw new ServerError('authService.createUser: Failed to create person');
112
+ }
110
113
  }
111
114
  user.personId = personId;
112
115
  return await this.create(userContext, user);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.80",
3
+ "version": "0.1.84",
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": {
@@ -57,7 +57,7 @@
57
57
  "qs": "^6.14.1"
58
58
  },
59
59
  "peerDependencies": {
60
- "@loomcore/common": "^0.0.46",
60
+ "@loomcore/common": "^0.0.49",
61
61
  "@sinclair/typebox": "0.34.33",
62
62
  "cookie-parser": "^1.4.6",
63
63
  "cors": "^2.8.5",