@loomcore/api 0.0.27 → 0.0.29

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.
@@ -56,6 +56,7 @@ export class TestExpressApp {
56
56
  if (!this.db) {
57
57
  this.mongoServer = await MongoMemoryServer.create({
58
58
  instance: {
59
+ ip: '127.0.0.1',
59
60
  port: 0,
60
61
  },
61
62
  binary: {
@@ -1,7 +1,7 @@
1
1
  import { ObjectId } from 'mongodb';
2
2
  import moment from 'moment';
3
3
  import crypto from 'crypto';
4
- import { EmptyUserContext, UserSpec, getSystemUserContext } from '@loomcore/common/models';
4
+ import { EmptyUserContext, passwordValidator, UserSpec, getSystemUserContext } from '@loomcore/common/models';
5
5
  import { entityUtils } from '@loomcore/common/utils';
6
6
  import { BadRequestError, ServerError } from '../errors/index.js';
7
7
  import { JwtService, EmailService } from './index.js';
@@ -93,7 +93,8 @@ export class AuthService extends GenericApiService {
93
93
  return result;
94
94
  }
95
95
  async changePassword(userContext, queryObject, password) {
96
- const updates = { password: password, _lastPasswordChange: moment().utc().toDate() };
96
+ const hashedPassword = await passwordUtils.hashPassword(password);
97
+ const updates = { password: hashedPassword, _lastPasswordChange: moment().utc().toDate() };
97
98
  const updatedUsers = await super.update(userContext, queryObject, updates);
98
99
  const result = {
99
100
  acknowledged: true,
@@ -180,6 +181,8 @@ export class AuthService extends GenericApiService {
180
181
  if (retrievedPasswordResetToken.token !== passwordResetToken || retrievedPasswordResetToken.expiresOn < Date.now()) {
181
182
  throw new BadRequestError('Invalid password reset token');
182
183
  }
184
+ const validationErrors = entityUtils.validate(passwordValidator, { password: password });
185
+ entityUtils.handleValidationResult(validationErrors, 'AuthService.resetPassword');
183
186
  const result = await this.changePassword(EmptyUserContext, { email: lowerCaseEmail }, password);
184
187
  console.log(`password changed using forgot-password for email: ${lowerCaseEmail}`);
185
188
  await this.passwordResetTokenService.deleteById(EmptyUserContext, retrievedPasswordResetToken._id.toString());
@@ -1,6 +1,10 @@
1
1
  import { IAddress } from '@loomcore/common/models';
2
- declare function getSingleLineAddress(address: IAddress): string;
2
+ declare function standardizeField(field: string | undefined | null): string | null;
3
+ declare function getSingleLineAddress(address: IAddress): string | null;
4
+ declare function addFormattedAddress(address: IAddress): IAddress;
3
5
  export declare const addressUtils: {
4
6
  getSingleLineAddress: typeof getSingleLineAddress;
7
+ standardizeField: typeof standardizeField;
8
+ addFormattedAddress: typeof addFormattedAddress;
5
9
  };
6
10
  export {};
@@ -1,15 +1,61 @@
1
+ function isEmptyValue(value) {
2
+ let result = false;
3
+ if (value === null || value === undefined) {
4
+ result = true;
5
+ }
6
+ else if (typeof value === 'string' && value.trim() === '') {
7
+ result = true;
8
+ }
9
+ return result;
10
+ }
11
+ function standardizeField(field) {
12
+ let result = null;
13
+ if (!isEmptyValue(field)) {
14
+ const standardized = field.trim().toUpperCase();
15
+ if (standardized.length > 0) {
16
+ result = standardized;
17
+ }
18
+ }
19
+ return result;
20
+ }
1
21
  function getSingleLineAddress(address) {
2
- const street = address.address1;
3
- let singleLineAddress = `${street}`;
4
- if (address.address2) {
5
- singleLineAddress += ` ${address.address2}`;
22
+ let result = null;
23
+ if (address) {
24
+ const street = standardizeField(address.address1);
25
+ const address2 = standardizeField(address.address2);
26
+ const address3 = standardizeField(address.address3);
27
+ const city = standardizeField(address.city);
28
+ const state = standardizeField(address.state);
29
+ const postalCode = standardizeField(address.postalCode);
30
+ if (street && city && postalCode) {
31
+ let parts = [street];
32
+ if (address2) {
33
+ parts.push(address2);
34
+ }
35
+ if (address3) {
36
+ parts.push(address3);
37
+ }
38
+ parts.push(city);
39
+ let statePostalPart = state;
40
+ statePostalPart += ' ' + postalCode;
41
+ parts.push(statePostalPart);
42
+ result = parts.join(', ');
43
+ }
6
44
  }
7
- if (address.address3) {
8
- singleLineAddress += ` ${address.address3}`;
45
+ return result;
46
+ }
47
+ function addFormattedAddress(address) {
48
+ const formattedAddress = getSingleLineAddress(address);
49
+ if (formattedAddress) {
50
+ return {
51
+ ...address,
52
+ formattedAddress
53
+ };
9
54
  }
10
- singleLineAddress += `, ${address.city}, ${address.state} ${address.postalCode}`;
11
- return singleLineAddress;
55
+ return address;
12
56
  }
13
57
  export const addressUtils = {
14
- getSingleLineAddress
58
+ getSingleLineAddress,
59
+ standardizeField,
60
+ addFormattedAddress
15
61
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb",
6
6
  "scripts": {