Skip to content

web-backend


Class: CollabService

Defined in: packages/web-backend/src/collab/collab.service.ts:20

Service for collaboration and user management. Provides user CRUD, validation, and pagination helpers.

Constructors

Constructor

new CollabService(userCounterModel, userModel): CollabService

Defined in: packages/web-backend/src/collab/collab.service.ts:26

Construct the collaboration service with persistence models.

Parameters

userCounterModel

Model<UserCounterDocument>

Mongoose model for tracking incremental user IDs.

userModel

Model<UserDocument>

Mongoose model for user documents.

Returns

CollabService

Methods

createNewUser()

createNewUser(body): Promise<string | User>

Defined in: packages/web-backend/src/collab/collab.service.ts:213

Create a new user with additional defaulted properties:

  1. The password is encrypted using the 'argon2id' method.
  2. The UserID is generated in an incremental order using getNextUserValue() function.
  3. The name of the user is saved by simply joining the first and last names of the user.
  4. The 'recently_accessed' object contains a list of all the Models created by the user. By default its kept empty at first. The Projects and Subsystems have not been implemented yet. Whenever the user creates any new HCL tree inside a Model, the tree's info gets saved inside this Model list. After creating a tree, if it is viewed, edited or quantified - those information get saved as well - to get an idea about the activity of the user.
  5. Whenever a user is interacting with the tree editor, the user can enable or disable certain settings of the editor. Those settings are saved in the 'preferences' object. By default all the settings inside the 'preferences' are set to 'enabled' when a user is created.
  6. The permissions feature has not been implemented yet - so it is kept empty by default. Once implemented, a user will be able to assume one of the two roles - either the role of an administrator (with special access - such as deleting a user from the database) or the role of a general user.

Parameters

body

CreateNewUserSchemaDto

Request body

Returns

Promise<string | User>

A mongoose document of the new user


getNextUserValue()

getNextUserValue(name): Promise<number>

Defined in: packages/web-backend/src/collab/collab.service.ts:59

Generate an ID for a newly created user in incremental order. If no user exists yet, IDs start at 1.

Parameters

name

string

Name of the counter

Returns

Promise<number>

ID number


getUserById()

getUserById(user_id): Promise<User>

Defined in: packages/web-backend/src/collab/collab.service.ts:193

Return a user with a particular id.

Parameters

user_id

string

Id of the user to find

Returns

Promise<User>


getUserPreferences()

getUserPreferences(user_id): Promise<Document<unknown, { }, UserDocument, { }, { }> & User & Document<unknown, any, any, Record<string, any>, { }> & Required<{ }> & object>

Defined in: packages/web-backend/src/collab/collab.service.ts:282

Get preferences for the current user.

Parameters

user_id

string

Current user's ID

Returns

Promise<Document<unknown, { }, UserDocument, { }, { }> & User & Document<unknown, any, any, Record<string, any>, { }> & Required<{ }> & object>

Preferences of the current user


getUsersList()

getUsersList(url, limit?, offset?, role?): Promise<PaginationDto>

Defined in: packages/web-backend/src/collab/collab.service.ts:161

Retrieve the User list in a paginated format.

  1. Count: the number of users found.
  2. Next: if there are additional users that couldn't be shown in the current page, then the link to the next page is shown. For example: if there are 20 users in total, and the current page is showing only 10 users, then the 'next' property is going to give the link to the 2nd page that can show the users with an ID of 11 to 20.
  3. Previous: if the offset is set, then some of the initial users will be skipped automatically. The 'previous' property provides a link to view these skipped users. For example: if there are 20 users and the limit = 5 and the offset = 5, then the current page will show users with an ID of 6 to 10 and skip the first 5 users. The 'previous' property in this case will provide the link for the 1st page that can show the first 5 users.
  4. Result: provides the users list. The 'result' property follows the limit and offset values to decide which users are going to be showed.

Parameters

url

string

Original request URL. See https://expressjs.com/en/api.html#req.originalUrl

limit?

number

How many results can be seen at once

offset?

number

How many initial results will be skipped

role?

string

The role of the user

Returns

Promise<PaginationDto>

List of all users


isEmailValid()

isEmailValid(email): Promise<boolean>

Defined in: packages/web-backend/src/collab/collab.service.ts:257

Return true if email exists in the database.

Parameters

email

string

Email of the user

Returns

Promise<boolean>


isUsernameValid()

isUsernameValid(username): Promise<boolean>

Defined in: packages/web-backend/src/collab/collab.service.ts:269

Return true if username exists in the database.

Parameters

username

string

Username of the user

Returns

Promise<boolean>


loginUser()

loginUser(username): Promise<User>

Defined in: packages/web-backend/src/collab/collab.service.ts:39

Assists the Local Strategy method to determine whether a user exists in the database or not.

Parameters

username

string

Username of the user

Returns

Promise<User>

A mongoose document of the user or undefined


pagination()

pagination(count, url, limit?, offset?): object

Defined in: packages/web-backend/src/collab/collab.service.ts:91

The pagination() method follows a very simple mechanism:

  1. It determines the number of pages its going to take to show all the queried results.
  2. It determines on which page the user is currently on. These 2 parameters are calculated using 3 pieces of information: limit, offset and total number of results (count). By default the limit = 10 and offset = 0. If the user provides different values, they replace the defaults. Let's look at an example: let's say there are 35 users in the database. The current user is trying to see the list of users. The user set the limit to 5 and offset to 10. So, the number of pages required = (number of users / users per page) = (35 / 5) = 7 pages. The current page = (skipped users / users per page) + 1 = (10 / 5) + 1 = 3. After determining these values, the method will handle 4 situations:
  3. There's only 1 page in total where all the results fit; in that case there will be no previous or next page link.
  4. There's more than 1 page in total but the user is on the first result page; in that case - link for the next page will be available only.
  5. There's more than 1 page in total but the user is on the last result page; in that case - link for the previous page will be available only.
  6. There's more than 1 page in total and the user is somewhere between the first and last page; in that case links for both previous and next page will be available. The 4th condition suits the above example. The user is on the 3rd page. The pagination() method will provide links to the 2nd (previous) and 4th (next) pages.

Parameters

count

number

Total number of results

url

string

Original request URL. See https://expressjs.com/en/api.html#req.originalUrl

limit?

unknown

How many results can be seen at once

offset?

unknown

How many initial results will be skipped

Returns

object

The previous/next links and the active limit/offset values

default_limit

default_limit: number

default_offset

default_offset: number

next

next: string

previous

previous: string


updateLastLogin()

updateLastLogin(user_id): Promise<void>

Defined in: packages/web-backend/src/collab/collab.service.ts:49

After each login, update the last login time for the user.

Parameters

user_id

number

Current user's ID

Returns

Promise<void>

void


updateUser()

updateUser(member): Promise<void>

Defined in: packages/web-backend/src/collab/collab.service.ts:329

This is a general update user function. It will allow the following updates: preferences, permissions, lastname, email, firstname, and username

Parameters

member

MemberResult

This is the user schema object

Returns

Promise<void>


updateUserPreferences()

updateUserPreferences(user_id, body): Promise<Document<unknown, { }, UserDocument, { }, { }> & User & Document<unknown, any, any, Record<string, any>, { }> & Required<{ }> & object>

Defined in: packages/web-backend/src/collab/collab.service.ts:316

  1. The lean() method returns a Plain Old JS Object (POJO) instead of a Mongoose document which makes the query much faster.
  2. Since the 'preferences' is a nested object, 2 things are needed to be done to ensure that its properties are updated properly: a. Use the objectID (_id) of the User document instead of using normal UserID(id) inside the findByIdAndUpdate() query method. b. Use 'dot-object' library to break-down the nested object properties. For example dot-object is going to transform:
json
{
  "preferences": {
    "theme": "Light",
    "outlineVisible": false
  }
}

into:

ts
{ 'preferences.theme': 'Light', 'preferences.outlineVisible': false }

Without using dot-object all the 6 preference settings are going to be replaced by these 2 properties only, deleting the rest of the 4 settings. 3. '$set' operator replaces a field's value with the provided value. 4. 'new' is set to true to ensure that the document that is returned after saving the preferences is always the updated one. 5. 'upsert' is a combined action of 'Update and insert'. If a user is not found while updating the preferences with the given UserID, the upsert operation will create a new user document with the given preferences which is not desired. So the upsert is set to false.

Parameters

user_id

string

Current user's ID

body

UserPreferencesDto

Request body

Returns

Promise<Document<unknown, { }, UserDocument, { }, { }> & User & Document<unknown, any, any, Record<string, any>, { }> & Required<{ }> & object>

Updated preferences of the user