@loomcore/api 0.2.9 → 0.2.11

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,9 +1,13 @@
1
- import { type IUser, type IUserContext } from "@loomcore/common/models";
1
+ import { type IQueryOptions, type IUser, type IUserContext } from "@loomcore/common/models";
2
2
  import type { AppIdType } from "@loomcore/common/types";
3
3
  import type { IDatabase } from "../databases/models/index.js";
4
4
  import { MultiTenantApiService } from "./multi-tenant-api.service.js";
5
5
  export declare class UserService extends MultiTenantApiService<IUser> {
6
6
  constructor(database: IDatabase);
7
7
  fullUpdateById(_userContext: IUserContext, _id: AppIdType, _entity: IUser): Promise<IUser>;
8
- preProcessEntity(userContext: IUserContext, entity: Partial<IUser>, isCreate: boolean, _allowId?: boolean): Promise<Partial<IUser>>;
8
+ update(userContext: IUserContext, queryObject: IQueryOptions, entity: Partial<IUser>): Promise<IUser[]>;
9
+ batchUpdate(userContext: IUserContext, entities: Partial<IUser>[]): Promise<IUser[]>;
10
+ partialUpdateById(userContext: IUserContext, id: AppIdType, entity: Partial<IUser>, allowPasswordUpdate?: boolean): Promise<IUser>;
11
+ private assertPasswordUpdateAllowed;
12
+ preProcessEntity(userContext: IUserContext, entity: Partial<IUser>, isCreate: boolean, allowId?: boolean): Promise<Partial<IUser>>;
9
13
  }
@@ -1,5 +1,5 @@
1
1
  import { UserSpec, } from "@loomcore/common/models";
2
- import { ServerError } from "../errors/index.js";
2
+ import { BadRequestError, ServerError } from "../errors/index.js";
3
3
  import { passwordUtils } from "../utils/password.utils.js";
4
4
  import { MultiTenantApiService } from "./multi-tenant-api.service.js";
5
5
  export class UserService extends MultiTenantApiService {
@@ -9,8 +9,36 @@ export class UserService extends MultiTenantApiService {
9
9
  async fullUpdateById(_userContext, _id, _entity) {
10
10
  throw new ServerError("User full update is not allowed.");
11
11
  }
12
- async preProcessEntity(userContext, entity, isCreate, _allowId = false) {
13
- const preparedEntity = await super.preProcessEntity(userContext, entity, isCreate);
12
+ async update(userContext, queryObject, entity) {
13
+ this.assertPasswordUpdateAllowed(userContext, null, entity, false);
14
+ return super.update(userContext, queryObject, entity);
15
+ }
16
+ async batchUpdate(userContext, entities) {
17
+ for (const entity of entities) {
18
+ this.assertPasswordUpdateAllowed(userContext, null, entity, false);
19
+ }
20
+ return super.batchUpdate(userContext, entities);
21
+ }
22
+ async partialUpdateById(userContext, id, entity, allowPasswordUpdate = false) {
23
+ this.assertPasswordUpdateAllowed(userContext, id, entity, allowPasswordUpdate);
24
+ return super.partialUpdateById(userContext, id, entity);
25
+ }
26
+ assertPasswordUpdateAllowed(userContext, id, entity, allowPasswordUpdate) {
27
+ if (!("password" in entity)) {
28
+ return;
29
+ }
30
+ if (!entity.password) {
31
+ throw new BadRequestError("Password cannot be empty.");
32
+ }
33
+ if (!allowPasswordUpdate) {
34
+ throw new ServerError("Use auth change password endpoint to update password.");
35
+ }
36
+ if (userContext.user._id !== id) {
37
+ throw new ServerError("You can only update your own password.");
38
+ }
39
+ }
40
+ async preProcessEntity(userContext, entity, isCreate, allowId = false) {
41
+ const preparedEntity = await super.preProcessEntity(userContext, entity, isCreate, allowId);
14
42
  if (preparedEntity.email) {
15
43
  preparedEntity.email = preparedEntity.email.toLowerCase();
16
44
  }
@@ -6,7 +6,7 @@ export async function changePassword(database, userContext, password) {
6
6
  password: password,
7
7
  _lastPasswordChange: moment().utc().toDate(),
8
8
  };
9
- await userService.partialUpdateById(userContext, userContext.user._id, updates);
9
+ await userService.partialUpdateById(userContext, userContext.user._id, updates, true);
10
10
  return {
11
11
  success: true,
12
12
  count: 1,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
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": {