Skip to content

Commit

Permalink
feat(api): use organization identity model in usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
er-lim committed Sep 23, 2024
1 parent f5be284 commit bced0f9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OrganizationLearnerIdentity } from '../../../src/identity-access-management/domain/models/OrganizationLearnerIdentity.js';
import { OrganizationIdentity } from '../../../src/identity-access-management/domain/models/OrganizationIdentity.js';
import { UserNotAuthorizedToUpdatePasswordError } from '../../../src/shared/domain/errors.js';
import { OrganizationLearnerPasswordResetDTO } from '../../../src/shared/domain/models/OrganizationLearnerPasswordResetDTO.js';
import {
Expand All @@ -15,23 +15,20 @@ const generateOrganizationLearnersUsernameAndTemporaryPassword = async function
userReconciliationService,
authenticationMethodRepository,
organizationRepository,
organizationLearnerRepository,
organizationLearnerIdentityRepository,
userRepository,
}) {
const errorMessage = `User ${userId} cannot reset passwords of some students in organization ${organizationId}`;
const organization = await organizationRepository.get(organizationId);
const organizationLearners = await organizationLearnerRepository.findByIds({ ids: organizationLearnersId });
const userIds = organizationLearners.map((organizationLearner) => organizationLearner.userId);
const users = await userRepository.getByIds(userIds);
let organizationLearnerIdentities = _buildOrganizationLearnerIdentities({ organizationLearners, users });

_assertEachOrganizationLearnersBelongToOrganization({
const organizationIdentity = await _buildOrganizationIdentity({
errorMessage,
organizationId,
organizationLearners,
organization,
organizationLearnersId,
organizationLearnerIdentityRepository,
});
let organizationLearnerIdentities = organizationIdentity.organizationLearnerIdentities;

if (!organization.isGarIdentityProvider) {
if (!organizationIdentity.isScoGarIdentityProvider) {
organizationLearnerIdentities = await _generateAndUpdateUsernameForOrganizationLearnerIdentities({
organizationLearnerIdentities,
userReconciliationService,
Expand All @@ -50,33 +47,27 @@ const generateOrganizationLearnersUsernameAndTemporaryPassword = async function
return _buildOrganizationLearnerPasswordResetDTOs({ organizationLearnerIdentities, userIdWithPasswords });
};

function _assertEachOrganizationLearnersBelongToOrganization({ errorMessage, organizationId, organizationLearners }) {
const organizationLearnersBelongsToOrganization = organizationLearners.every(
(organizationLearner) => organizationLearner.organizationId === organizationId,
);

if (!organizationLearnersBelongsToOrganization) {
async function _buildOrganizationIdentity({
errorMessage,
organization,
organizationLearnersId,
organizationLearnerIdentityRepository,
}) {
try {
const organizationLearnerIdentities = await organizationLearnerIdentityRepository.getByIds(organizationLearnersId);
return new OrganizationIdentity({
id: organization.id,
isScoGarIdentityProvider: organization.isGarIdentityProvider,
organizationLearnerIdentities,
});
} catch (error) {
throw new UserNotAuthorizedToUpdatePasswordError(
errorMessage,
ORGANIZATION_LEARNER_DOES_NOT_BELONG_TO_ORGANIZATION_CODE,
);
}
}

function _buildOrganizationLearnerIdentities({ organizationLearners, users }) {
return organizationLearners.map(({ division, firstName, lastName, birthdate, userId }) => {
const user = users.find((user) => user.id === userId);
return new OrganizationLearnerIdentity({
division,
firstName,
lastName,
birthdate,
userId,
username: user.username,
});
});
}

async function _generateAndUpdateUsernameForOrganizationLearnerIdentities({
organizationLearnerIdentities,
userReconciliationService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
} from '../../../../lib/domain/constants/generate-organization-learners-username-and-temporary-password-errors.js';
import * as userReconciliationService from '../../../../lib/domain/services/user-reconciliation-service.js';
import { generateOrganizationLearnersUsernameAndTemporaryPassword } from '../../../../lib/domain/usecases/generate-organization-learners-username-and-temporary-password.js';
import * as organizationLearnerRepository from '../../../../lib/infrastructure/repositories/organization-learner-repository.js';
import { NON_OIDC_IDENTITY_PROVIDERS } from '../../../../src/identity-access-management/domain/constants/identity-providers.js';
import * as authenticationMethodRepository from '../../../../src/identity-access-management/infrastructure/repositories/authentication-method.repository.js';
import { organizationLearnerIdentityRepository } from '../../../../src/identity-access-management/infrastructure/repositories/organization-learner-identity.repository.js';
import * as userRepository from '../../../../src/identity-access-management/infrastructure/repositories/user.repository.js';
import { UserNotAuthorizedToUpdatePasswordError } from '../../../../src/shared/domain/errors.js';
import { OrganizationLearnerPasswordResetDTO } from '../../../../src/shared/domain/models/OrganizationLearnerPasswordResetDTO.js';
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('Integration | UseCases | generate organization learners username and t
userId,
authenticationMethodRepository,
organizationRepository,
organizationLearnerRepository,
organizationLearnerIdentityRepository,
userRepository,
cryptoService,
passwordGenerator,
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('Integration | UseCases | generate organization learners username and t
userId,
authenticationMethodRepository,
organizationRepository,
organizationLearnerRepository,
organizationLearnerIdentityRepository,
userRepository,
cryptoService,
passwordGenerator,
Expand Down Expand Up @@ -242,7 +242,7 @@ describe('Integration | UseCases | generate organization learners username and t
userId,
authenticationMethodRepository,
organizationRepository,
organizationLearnerRepository,
organizationLearnerIdentityRepository,
userRepository,
cryptoService,
passwordGenerator,
Expand Down Expand Up @@ -284,7 +284,7 @@ describe('Integration | UseCases | generate organization learners username and t
userId,
authenticationMethodRepository,
organizationRepository,
organizationLearnerRepository,
organizationLearnerIdentityRepository,
userRepository,
cryptoService,
passwordGenerator,
Expand Down

0 comments on commit bced0f9

Please sign in to comment.