Skip to content

Commit

Permalink
feat(api): add organization identity model
Browse files Browse the repository at this point in the history
  • Loading branch information
er-lim committed Sep 23, 2024
1 parent 6ecc962 commit ca05fa6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/src/identity-access-management/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class DifferentExternalIdentifierError extends DomainError {
}
}

class OrganizationLearnerNotBelongToOrganizationIdentityError extends DomainError {
constructor(message = 'Organization Learner identity does not belong to Organization Identity') {
super(message);
}
}

class OrganizationLearnerIdentityNotFoundError extends DomainError {
constructor(message = 'Organization Learner Identity not found.') {
super(message);
Expand Down Expand Up @@ -71,6 +77,7 @@ export {
MissingOrInvalidCredentialsError,
MissingUserAccountError,
OrganizationLearnerIdentityNotFoundError,
OrganizationLearnerNotBelongToOrganizationIdentityError,
PasswordNotMatching,
PasswordResetDemandNotFoundError,
UserCantBeCreatedError,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { OrganizationLearnerNotBelongToOrganizationIdentityError } from '../errors.js';

export class OrganizationIdentity {
/**
*
* @param {number} id
* @param {boolean} isScoGarIdentityProvider
* @param {OrganizationLearnerIdentity} organizationLearnerIdentity
*/
constructor({ id, isScoGarIdentityProvider, organizationLearnerIdentities }) {
this.id = id;
this.isScoGarIdentityProvider = isScoGarIdentityProvider;
this.organizationLearnerIdentities = organizationLearnerIdentities;

this.#assertAllOrganizationLearnerIdentitiesBelongsToOrganization();
}

#assertAllOrganizationLearnerIdentitiesBelongsToOrganization() {
const allOrganizationLearnerIdentitiesBelongsToOrganization = this.organizationLearnerIdentities.every(
(organizationLearnerIdentity) => organizationLearnerIdentity.organizationId === this.id,
);

if (!allOrganizationLearnerIdentitiesBelongsToOrganization) {
throw new OrganizationLearnerNotBelongToOrganizationIdentityError();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { OrganizationLearnerNotBelongToOrganizationIdentityError } from '../../../../../src/identity-access-management/domain/errors.js';
import { OrganizationIdentity } from '../../../../../src/identity-access-management/domain/models/OrganizationIdentity.js';
import { catchErrSync, expect } from '../../../../test-helper.js';

describe('Unit | Identity Access Management | Domain | Model | OrganizationIdentity', function () {
describe('#constructor', function () {
let organizationLearnerIdentities;
const organizationId = 42;

beforeEach(function () {
const organizationLearnerIdentity = {
id: 32,
birthdate: '01/01/2022',
division: '6B',
firstName: 'Holly',
lastName: 'Garchie',
organizationId,
userId: 12,
username: 'holly.garchie',
};

const otherOrganizationLearnerIdentity = {
id: 32,
birthdate: '01/01/2022',
division: '6B',
firstName: 'Lee',
lastName: 'Tige',
organizationId,
userId: 12,
username: 'holly.garchie',
};

organizationLearnerIdentities = [organizationLearnerIdentity, otherOrganizationLearnerIdentity];
});

it('builds a OrganizationIdentity model', function () {
// when
const organizationIdentity = new OrganizationIdentity({
id: organizationId,
organizationLearnerIdentities,
});

// then
expect(organizationIdentity.id).to.eq(organizationId);
expect(organizationIdentity.organizationLearnerIdentities).to.deep.equal(organizationLearnerIdentities);
});

context("when one of organizationLearnerIdentities does not belong to 'organizationId'", function () {
it('throws an NotBelongingToOrganizationIdentityError', function () {
// given
const otherOrganizationId = 100;
organizationLearnerIdentities[0].organizationId = otherOrganizationId;

// when
const error = catchErrSync(
() =>
new OrganizationIdentity({
id: organizationId,
organizationLearnerIdentities,
}),
)();

// then
expect(error).to.be.instanceOf(OrganizationLearnerNotBelongToOrganizationIdentityError);
});
});
});
});

0 comments on commit ca05fa6

Please sign in to comment.