@friggframework/core 2.0.0--canary.393.5a4db58.0 → 2.0.0--canary.395.d07514c.0

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.
Files changed (31) hide show
  1. package/handlers/app-handler-helpers.js +0 -3
  2. package/handlers/backend-utils.js +34 -16
  3. package/handlers/routers/auth.js +18 -9
  4. package/handlers/routers/integration-defined-routers.js +7 -2
  5. package/handlers/routers/user.js +25 -5
  6. package/handlers/workers/integration-defined-workers.js +5 -1
  7. package/index.js +0 -2
  8. package/integrations/index.js +0 -2
  9. package/integrations/integration-factory.js +5 -6
  10. package/integrations/integration-router.js +133 -43
  11. package/module-plugin/auther.js +3 -2
  12. package/module-plugin/test/mock-api/api.js +8 -3
  13. package/module-plugin/test/mock-api/definition.js +12 -8
  14. package/package.json +5 -5
  15. package/user/tests/doubles/test-user-repository.js +115 -0
  16. package/user/tests/use-cases/create-individual-user.test.js +24 -0
  17. package/user/tests/use-cases/create-organization-user.test.js +28 -0
  18. package/user/tests/use-cases/create-token-for-user-id.test.js +19 -0
  19. package/user/tests/use-cases/get-user-from-bearer-token.test.js +64 -0
  20. package/user/tests/use-cases/login-user.test.js +148 -0
  21. package/user/use-cases/create-individual-user.js +52 -0
  22. package/user/use-cases/create-organization-user.js +39 -0
  23. package/user/use-cases/create-token-for-user-id.js +30 -0
  24. package/user/use-cases/get-user-from-bearer-token.js +50 -0
  25. package/user/use-cases/login-user.js +88 -0
  26. package/user/user-repository.js +97 -0
  27. package/user/user.js +77 -0
  28. package/handlers/routers/middleware/loadUser.js +0 -15
  29. package/handlers/routers/middleware/requireLoggedInUser.js +0 -12
  30. package/integrations/create-frigg-backend.js +0 -31
  31. package/integrations/integration-user.js +0 -144
@@ -0,0 +1,24 @@
1
+ const {
2
+ CreateIndividualUser,
3
+ } = require('../../use-cases/create-individual-user');
4
+ const { TestUserRepository } = require('../doubles/test-user-repository');
5
+
6
+ describe('CreateIndividualUser Use Case', () => {
7
+ it('should create and return an individual user via the repository', async () => {
8
+ const userConfig = { usePassword: true };
9
+ const userRepository = new TestUserRepository({ userConfig });
10
+ const createIndividualUser = new CreateIndividualUser({
11
+ userRepository,
12
+ userConfig,
13
+ });
14
+
15
+ const params = {
16
+ username: 'test-user',
17
+ password: 'password123',
18
+ };
19
+ const user = await createIndividualUser.execute(params);
20
+
21
+ expect(user).toBeDefined();
22
+ expect(user.getIndividualUser().username).toBe(params.username);
23
+ });
24
+ });
@@ -0,0 +1,28 @@
1
+ const {
2
+ CreateOrganizationUser,
3
+ } = require('../../use-cases/create-organization-user');
4
+ const { TestUserRepository } = require('../doubles/test-user-repository');
5
+
6
+ describe('CreateOrganizationUser Use Case', () => {
7
+ it('should create and return an organization user via the repository', async () => {
8
+ const userConfig = {
9
+ primary: 'organization',
10
+ organizationUserRequired: true,
11
+ individualUserRequired: false,
12
+ };
13
+ const userRepository = new TestUserRepository({ userConfig });
14
+ const createOrganizationUser = new CreateOrganizationUser({
15
+ userRepository,
16
+ userConfig,
17
+ });
18
+
19
+ const params = {
20
+ name: 'Test Org',
21
+ appOrgId: 'org-123',
22
+ };
23
+ const user = await createOrganizationUser.execute(params);
24
+
25
+ expect(user).toBeDefined();
26
+ expect(user.getOrganizationUser().name).toBe(params.name);
27
+ });
28
+ });
@@ -0,0 +1,19 @@
1
+ const {
2
+ CreateTokenForUserId,
3
+ } = require('../../use-cases/create-token-for-user-id');
4
+ const { TestUserRepository } = require('../doubles/test-user-repository');
5
+
6
+ describe('CreateTokenForUserId Use Case', () => {
7
+ it('should create and return a token via the repository', async () => {
8
+ const userConfig = {}; // Not used by this use case, but required by the test repo
9
+ const userRepository = new TestUserRepository({ userConfig });
10
+ const createTokenForUserId = new CreateTokenForUserId({ userRepository, userConfig });
11
+
12
+ const userId = 'user-123';
13
+ const token = await createTokenForUserId.execute(userId);
14
+
15
+ expect(token).toBeDefined();
16
+ // The mock token is deterministic, so we can check it
17
+ expect(token).toContain(`token-for-${userId}`);
18
+ });
19
+ });
@@ -0,0 +1,64 @@
1
+ const {
2
+ GetUserFromBearerToken,
3
+ } = require('../../use-cases/get-user-from-bearer-token');
4
+ const { TestUserRepository } = require('../doubles/test-user-repository');
5
+
6
+ describe('GetUserFromBearerToken Use Case', () => {
7
+ let userRepository;
8
+ let getUserFromBearerToken;
9
+ let userConfig;
10
+
11
+ beforeEach(() => {
12
+ userConfig = {
13
+ usePassword: true,
14
+ primary: 'individual',
15
+ individualUserRequired: true,
16
+ organizationUserRequired: false,
17
+ };
18
+ userRepository = new TestUserRepository({ userConfig });
19
+ getUserFromBearerToken = new GetUserFromBearerToken({
20
+ userRepository,
21
+ userConfig
22
+ });
23
+ });
24
+
25
+ it('should retrieve a user for a valid bearer token', async () => {
26
+ const userId = 'user-123';
27
+ const token = await userRepository.createToken(userId);
28
+ const createdUser = await userRepository.createIndividualUser({
29
+ id: userId,
30
+ });
31
+
32
+ const user = await getUserFromBearerToken.execute(`Bearer ${token}`);
33
+
34
+ expect(user).toBeDefined();
35
+ expect(user.getId()).toBe(createdUser.getId());
36
+ });
37
+
38
+ it('should throw an unauthorized error if the bearer token is missing', async () => {
39
+ await expect(getUserFromBearerToken.execute(null)).rejects.toThrow(
40
+ 'Missing Authorization Header'
41
+ );
42
+ });
43
+
44
+ it('should throw an unauthorized error for an invalid token format', async () => {
45
+ await expect(
46
+ getUserFromBearerToken.execute('InvalidToken')
47
+ ).rejects.toThrow('Invalid Token Format');
48
+ });
49
+
50
+ it('should throw an unauthorized error if the Session Token is not found', async () => {
51
+ userRepository.getSessionToken = jest.fn().mockResolvedValue(null);
52
+ await expect(
53
+ getUserFromBearerToken.execute('Bearer invalid-token')
54
+ ).rejects.toThrow('Session Token Not Found');
55
+ });
56
+
57
+ it('should throw an unauthorized error if the token is valid but finds no user', async () => {
58
+ userRepository.getSessionToken = jest.fn().mockResolvedValue(null);
59
+ const token = await userRepository.createToken('user-dne');
60
+ await expect(
61
+ getUserFromBearerToken.execute(`Bearer ${token}`)
62
+ ).rejects.toThrow('Session Token Not Found');
63
+ });
64
+ });
@@ -0,0 +1,148 @@
1
+ const bcrypt = require('bcryptjs');
2
+ const { LoginUser } = require('../../use-cases/login-user');
3
+ const { TestUserRepository } = require('../doubles/test-user-repository');
4
+
5
+ jest.mock('bcryptjs', () => ({
6
+ compareSync: jest.fn(),
7
+ }));
8
+
9
+ describe('LoginUser Use Case', () => {
10
+ let userRepository;
11
+ let loginUser;
12
+ let userConfig;
13
+
14
+ beforeEach(() => {
15
+ userConfig = { usePassword: true, individualUserRequired: true, organizationUserRequired: false };
16
+ userRepository = new TestUserRepository({ userConfig });
17
+ loginUser = new LoginUser({ userRepository, userConfig });
18
+
19
+ bcrypt.compareSync.mockClear();
20
+ });
21
+
22
+ describe('With Password Authentication', () => {
23
+ it('should successfully log in a user with correct credentials', async () => {
24
+ const username = 'test-user';
25
+ const password = 'password123';
26
+ await userRepository.createIndividualUser({
27
+ username,
28
+ hashword: 'hashed-password',
29
+ });
30
+
31
+ bcrypt.compareSync.mockReturnValue(true);
32
+
33
+ const user = await loginUser.execute({ username, password });
34
+
35
+ expect(bcrypt.compareSync).toHaveBeenCalledWith(
36
+ password,
37
+ 'hashed-password'
38
+ );
39
+ expect(user).toBeDefined();
40
+ expect(user.getIndividualUser().username).toBe(username);
41
+ });
42
+
43
+ it('should throw an unauthorized error for an incorrect password', async () => {
44
+ const username = 'test-user';
45
+ const password = 'wrong-password';
46
+ await userRepository.createIndividualUser({
47
+ username,
48
+ hashword: 'hashed-password',
49
+ });
50
+
51
+ bcrypt.compareSync.mockReturnValue(false);
52
+
53
+ await expect(
54
+ loginUser.execute({ username, password })
55
+ ).rejects.toThrow('Incorrect username or password');
56
+ });
57
+
58
+ it('should throw an unauthorized error for a non-existent user', async () => {
59
+ const username = 'non-existent-user';
60
+ const password = 'password123';
61
+ userRepository.findIndividualUserByUsername = jest
62
+ .fn()
63
+ .mockRejectedValue(new Error('user not found'));
64
+
65
+ await expect(
66
+ loginUser.execute({ username, password })
67
+ ).rejects.toThrow('user not found');
68
+ });
69
+ });
70
+
71
+ describe('Without Password (appUserId)', () => {
72
+ beforeEach(() => {
73
+ userConfig = { usePassword: false, individualUserRequired: true, organizationUserRequired: false };
74
+ userRepository = new TestUserRepository({ userConfig });
75
+ loginUser = new LoginUser({
76
+ userRepository,
77
+ userConfig,
78
+ });
79
+ });
80
+
81
+ it('should successfully retrieve a user by appUserId', async () => {
82
+ const appUserId = 'app-user-123';
83
+ const createdUser = await userRepository.createIndividualUser({
84
+ appUserId,
85
+ });
86
+
87
+ const result = await loginUser.execute({ appUserId });
88
+ expect(result.getId()).toBe(createdUser.getId());
89
+ });
90
+ });
91
+
92
+ describe('With Organization User', () => {
93
+ beforeEach(() => {
94
+ userConfig = {
95
+ individualUserRequired: false,
96
+ organizationUserRequired: true,
97
+ };
98
+ userRepository = new TestUserRepository({ userConfig });
99
+ loginUser = new LoginUser({
100
+ userRepository,
101
+ userConfig,
102
+ });
103
+ });
104
+
105
+ it('should successfully retrieve an organization user by appOrgId', async () => {
106
+ const appOrgId = 'app-org-123';
107
+ const createdUser = await userRepository.createOrganizationUser({
108
+ name: 'Test Org',
109
+ appOrgId,
110
+ });
111
+
112
+ const result = await loginUser.execute({ appOrgId });
113
+ expect(result.getId()).toBe(createdUser.getId());
114
+ });
115
+
116
+ it('should throw an unauthorized error for a non-existent organization user', async () => {
117
+ const appOrgId = 'non-existent-org';
118
+ userRepository.findOrganizationUserByAppOrgId = jest
119
+ .fn()
120
+ .mockRejectedValue(new Error('user not found'));
121
+
122
+ await expect(loginUser.execute({ appOrgId })).rejects.toThrow(
123
+ 'user not found'
124
+ );
125
+ });
126
+ });
127
+
128
+ describe('Required User Checks', () => {
129
+ it('should throw an error if a required individual user is not found', async () => {
130
+ userConfig = {
131
+ individualUserRequired: true,
132
+ usePassword: false,
133
+ };
134
+ userRepository = new TestUserRepository({ userConfig });
135
+ userRepository.findIndividualUserByAppUserId = jest
136
+ .fn()
137
+ .mockRejectedValue(new Error('user not found'));
138
+ loginUser = new LoginUser({
139
+ userRepository,
140
+ userConfig,
141
+ });
142
+
143
+ await expect(
144
+ loginUser.execute({ appUserId: 'a-non-existent-user-id' })
145
+ ).rejects.toThrow('user not found');
146
+ });
147
+ });
148
+ });
@@ -0,0 +1,52 @@
1
+ const { get } = require('../../assertions');
2
+ const Boom = require('@hapi/boom');
3
+
4
+ /**
5
+ * Use case for creating an individual user.
6
+ * @class CreateIndividualUser
7
+ */
8
+ class CreateIndividualUser {
9
+ /**
10
+ * Creates a new CreateIndividualUser instance.
11
+ * @param {Object} params - Configuration parameters.
12
+ * @param {import('../user-repository').UserRepository} params.userRepository - Repository for user data operations.
13
+ * @param {Object} params.userConfig - The user properties inside of the app definition.
14
+ */
15
+ constructor({ userRepository, userConfig }) {
16
+ this.userRepository = userRepository;
17
+ this.userConfig = userConfig;
18
+ }
19
+
20
+ /**
21
+ * Executes the use case.
22
+ * @async
23
+ * @param {Object} params - The parameters for creating the user.
24
+ * @returns {Promise<import('../user').User>} The newly created user object.
25
+ */
26
+ async execute(params) {
27
+ let hashword;
28
+ if (this.userConfig.usePassword) {
29
+ hashword = get(params, 'password');
30
+ }
31
+
32
+ const email = get(params, 'email', null);
33
+ const username = get(params, 'username', null);
34
+ if (!email && !username) {
35
+ throw Boom.badRequest('email or username is required');
36
+ }
37
+
38
+ const appUserId = get(params, 'appUserId', null);
39
+ const organizationUserId = get(params, 'organizationUserId', null);
40
+
41
+ const individualUser = await this.userRepository.createIndividualUser({
42
+ email,
43
+ username,
44
+ hashword,
45
+ appUserId,
46
+ organizationUser: organizationUserId,
47
+ });
48
+ return individualUser;
49
+ }
50
+ }
51
+
52
+ module.exports = { CreateIndividualUser };
@@ -0,0 +1,39 @@
1
+ const { get } = require('../../assertions');
2
+
3
+ // todo: this is not used anywhere, check if needed
4
+ /**
5
+ * Use case for creating an organization user.
6
+ * @class CreateOrganizationUser
7
+ */
8
+ class CreateOrganizationUser {
9
+ /**
10
+ * Creates a new CreateOrganizationUser instance.
11
+ * @param {Object} params - Configuration parameters.
12
+ * @param {import('../user-repository').UserRepository} params.userRepository - Repository for user data operations.
13
+ * @param {Object} params.userConfig - The user properties inside of the app definition.
14
+ */
15
+ constructor({ userRepository, userConfig }) {
16
+ this.userRepository = userRepository;
17
+ this.userConfig = userConfig;
18
+ }
19
+
20
+ /**
21
+ * Executes the use case.
22
+ * @async
23
+ * @param {Object} params - The parameters for creating the user.
24
+ * @returns {Promise<import('../user').User>} The newly created user object.
25
+ */
26
+ async execute(params) {
27
+ const name = get(params, 'name');
28
+ const appOrgId = get(params, 'appOrgId');
29
+
30
+ const organizationUser =
31
+ await this.userRepository.createOrganizationUser({
32
+ name,
33
+ appOrgId,
34
+ });
35
+ return organizationUser;
36
+ }
37
+ }
38
+
39
+ module.exports = { CreateOrganizationUser };
@@ -0,0 +1,30 @@
1
+ const crypto = require('crypto');
2
+
3
+ /**
4
+ * Use case for creating a token for a user ID.
5
+ * @class CreateTokenForUserId
6
+ */
7
+ class CreateTokenForUserId {
8
+ /**
9
+ * Creates a new CreateTokenForUserId instance.
10
+ * @param {Object} params - Configuration parameters.
11
+ * @param {import('../user-repository').UserRepository} params.userRepository - Repository for user data operations.
12
+ */
13
+ constructor({ userRepository }) {
14
+ this.userRepository = userRepository;
15
+ }
16
+
17
+ /**
18
+ * Executes the use case.
19
+ * @async
20
+ * @param {string} userId - The ID of the user to create a token for.
21
+ * @param {number} minutes - The number of minutes until the token expires.
22
+ * @returns {Promise<string>} The user token.
23
+ */
24
+ async execute(userId, minutes) {
25
+ const rawToken = crypto.randomBytes(20).toString('hex');
26
+ return this.userRepository.createToken(userId, rawToken, minutes);
27
+ }
28
+ }
29
+
30
+ module.exports = { CreateTokenForUserId };
@@ -0,0 +1,50 @@
1
+ const Boom = require('@hapi/boom');
2
+
3
+ /**
4
+ * Use case for retrieving a user from a bearer token.
5
+ * @class GetUserFromBearerToken
6
+ */
7
+ class GetUserFromBearerToken {
8
+ /**
9
+ * Creates a new GetUserFromBearerToken instance.
10
+ * @param {Object} params - Configuration parameters.
11
+ * @param {import('../user-repository').UserRepository} params.userRepository - Repository for user data operations.
12
+ * @param {Object} params.userConfig - The user config in the app definition.
13
+ */
14
+ constructor({ userRepository, userConfig }) {
15
+ this.userRepository = userRepository;
16
+ this.userConfig = userConfig;
17
+ }
18
+
19
+ /**
20
+ * Executes the use case.
21
+ * @async
22
+ * @param {string} bearerToken - The bearer token from the authorization header.
23
+ * @returns {Promise<import('../user').User>} The authenticated user object.
24
+ * @throws {Boom} 401 Unauthorized if the token is missing, malformed, or invalid.
25
+ */
26
+ async execute(bearerToken) {
27
+ if (!bearerToken) {
28
+ throw Boom.unauthorized('Missing Authorization Header');
29
+ }
30
+
31
+ const token = bearerToken.split(' ')[1]?.trim();
32
+ if (!token) {
33
+ throw Boom.unauthorized('Invalid Token Format');
34
+ }
35
+
36
+ const sessionToken = await this.userRepository.getSessionToken(token);
37
+
38
+ if (!sessionToken) {
39
+ throw Boom.unauthorized('Session Token Not Found');
40
+ }
41
+
42
+ if (this.userConfig.primary === 'organization') {
43
+ return this.userRepository.findOrganizationUserById(sessionToken.user);
44
+ }
45
+
46
+ return this.userRepository.findIndividualUserById(sessionToken.user);
47
+ }
48
+ }
49
+
50
+ module.exports = { GetUserFromBearerToken };
@@ -0,0 +1,88 @@
1
+ const bcrypt = require('bcryptjs');
2
+ const Boom = require('@hapi/boom');
3
+ const {
4
+ RequiredPropertyError,
5
+ } = require('../../errors');
6
+
7
+ /**
8
+ * Use case for logging in a user.
9
+ * @class LoginUser
10
+ */
11
+ class LoginUser {
12
+ /**
13
+ * Creates a new LoginUser instance.
14
+ * @param {Object} params - Configuration parameters.
15
+ * @param {import('../user-repository').UserRepository} params.userRepository - Repository for user data operations.
16
+ * @param {Object} params.userConfig - The user properties inside of the app definition.
17
+ */
18
+ constructor({ userRepository, userConfig }) {
19
+ this.userRepository = userRepository;
20
+ this.userConfig = userConfig;
21
+ }
22
+
23
+ /**
24
+ * Executes the use case.
25
+ * @async
26
+ * @param {Object} userCredentials - The user's credentials for authentication.
27
+ * @param {string} [userCredentials.username] - The username for authentication.
28
+ * @param {string} [userCredentials.password] - The password for authentication.
29
+ * @param {string} [userCredentials.appUserId] - The app user id for authentication if no username and password are provided.
30
+ * @param {string} [userCredentials.appOrgId] - The app organization id for authentication if no username and password are provided.
31
+ * @returns {Promise<import('../user').User>} The authenticated user object.
32
+ */
33
+ async execute(userCredentials) {
34
+ const { username, password, appUserId, appOrgId } = userCredentials;
35
+ if (this.userConfig.individualUserRequired) {
36
+ if (this.userConfig.usePassword) {
37
+ if (!username) {
38
+ throw new RequiredPropertyError({
39
+ parent: this,
40
+ key: 'username',
41
+ });
42
+ }
43
+ if (!password) {
44
+ throw new RequiredPropertyError({
45
+ parent: this,
46
+ key: 'password',
47
+ });
48
+ }
49
+
50
+ const individualUser =
51
+ await this.userRepository.findIndividualUserByUsername(
52
+ username
53
+ );
54
+
55
+ if (!individualUser.isPasswordValid(password)) {
56
+ throw Boom.unauthorized('Incorrect username or password');
57
+ }
58
+
59
+ return individualUser;
60
+ } else {
61
+ const individualUser =
62
+ await this.userRepository.findIndividualUserByAppUserId(
63
+ appUserId
64
+ );
65
+
66
+ return individualUser;
67
+ }
68
+ }
69
+
70
+
71
+ if (this.userConfig.organizationUserRequired) {
72
+
73
+ const organizationUser =
74
+ await this.userRepository.findOrganizationUserByAppOrgId(appOrgId);
75
+
76
+ if (!organizationUser) {
77
+ throw Boom.unauthorized(`org user ${appOrgId} not found`);
78
+ }
79
+
80
+ return organizationUser;
81
+ }
82
+
83
+ // todo: check if organizationUserRequired and individualUserRequired can be used at the same time.
84
+ return null;
85
+ }
86
+ }
87
+
88
+ module.exports = { LoginUser };
@@ -0,0 +1,97 @@
1
+ const { Token } = require('../database/models/Token');
2
+ const { IndividualUser } = require('../database/models/IndividualUser');
3
+ const { OrganizationUser } = require('../database/models/OrganizationUser');
4
+ const { User } = require('./user');
5
+
6
+
7
+ //todo: the user class instantiation needs to happen in each use case and not here.
8
+ class UserRepository {
9
+ /**
10
+ * @param {Object} userConfig - The user config in the app definition.
11
+ */
12
+ constructor({ userConfig }) {
13
+ this.IndividualUser = IndividualUser;
14
+ this.OrganizationUser = OrganizationUser;
15
+ this.Token = Token;
16
+ this.userConfig = userConfig;
17
+ }
18
+
19
+ async getSessionToken(token) {
20
+ const jsonToken =
21
+ this.Token.getJSONTokenFromBase64BufferToken(token);
22
+ const sessionToken =
23
+ await this.Token.validateAndGetTokenFromJSONToken(jsonToken);
24
+ return sessionToken;
25
+ }
26
+
27
+ async findOrganizationUserById(userId) {
28
+ const organizationUser = await this.OrganizationUser.findById(userId);
29
+
30
+ if (!organizationUser) {
31
+ throw Boom.unauthorized('Organization User Not Found');
32
+ }
33
+
34
+ return new User(null, organizationUser, this.userConfig.usePassword, this.userConfig.primary, this.userConfig.individualUserRequired, this.userConfig.organizationUserRequired);
35
+ }
36
+
37
+ async findIndividualUserById(userId) {
38
+ const individualUser = await this.IndividualUser.findById(userId);
39
+
40
+ if (!individualUser) {
41
+ throw Boom.unauthorized('Individual User Not Found');
42
+ }
43
+
44
+ return new User(individualUser, null, this.userConfig.usePassword, this.userConfig.primary, this.userConfig.individualUserRequired, this.userConfig.organizationUserRequired);
45
+ }
46
+
47
+ async createToken(userId, rawToken, minutes = 120) {
48
+ const createdToken = await this.Token.createTokenWithExpire(
49
+ userId,
50
+ rawToken,
51
+ minutes
52
+ );
53
+ return this.Token.createBase64BufferToken(createdToken, rawToken);
54
+ }
55
+
56
+ async createIndividualUser(params) {
57
+ const individualUser = await this.IndividualUser.create(params);
58
+ return new User(individualUser, null, this.userConfig.usePassword, this.userConfig.primary, this.userConfig.individualUserRequired, this.userConfig.organizationUserRequired);
59
+ }
60
+
61
+ async createOrganizationUser(params) {
62
+ const organizationUser = await this.OrganizationUser.create(params);
63
+ return new User(null, organizationUser, this.userConfig.usePassword, this.userConfig.primary, this.userConfig.individualUserRequired, this.userConfig.organizationUserRequired);
64
+ }
65
+
66
+ async findIndividualUserByUsername(username) {
67
+ const individualUser = await this.IndividualUser.findOne({ username });
68
+
69
+ if (!individualUser) {
70
+ throw Boom.unauthorized('user not found');
71
+ }
72
+
73
+ return new User(individualUser, null, this.userConfig.usePassword, this.userConfig.primary, this.userConfig.individualUserRequired, this.userConfig.organizationUserRequired);
74
+ }
75
+
76
+ async findIndividualUserByAppUserId(appUserId) {
77
+ const individualUser = await this.IndividualUser.getUserByAppUserId(appUserId);
78
+
79
+ if (!individualUser) {
80
+ throw Boom.unauthorized('user not found');
81
+ }
82
+
83
+ return new User(individualUser, null, this.userConfig.usePassword, this.userConfig.primary, this.userConfig.individualUserRequired, this.userConfig.organizationUserRequired);
84
+ }
85
+
86
+ async findOrganizationUserByAppOrgId(appOrgId) {
87
+ const organizationUser = await this.OrganizationUser.getUserByAppOrgId(appOrgId);
88
+
89
+ if (!organizationUser) {
90
+ throw Boom.unauthorized('user not found');
91
+ }
92
+
93
+ return new User(null, organizationUser, this.userConfig.usePassword, this.userConfig.primary, this.userConfig.individualUserRequired, this.userConfig.organizationUserRequired);
94
+ }
95
+ }
96
+
97
+ module.exports = { UserRepository };