Skip to content

Commit

Permalink
resolves comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dopeamin committed Sep 10, 2024
1 parent a639a4c commit e79e579
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 129 deletions.
69 changes: 5 additions & 64 deletions src/services/identifierService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,9 @@ export interface IdentifierInterface {
create(userId: string, req: IdentifierCreateReq): Promise<IdentifierRsp>;
delete(userId: string, identifierId: string): Promise<GenericRsp>;
list(sort?: string, filter?: string[], page?: number, pageSize?: number): Promise<IdentifierList>;
listAllWithPaging(page?: number, pageSize?: number): Promise<IdentifierList>;
listByValueAndType(value: string, type: IdentifierType): Promise<IdentifierList>;
listByValueAndTypeWithPaging(
value: string,
type: IdentifierType,
sort?: string,
page?: number,
pageSize?: number,
): Promise<IdentifierList>;
existsByValueAndType(value: string, type: IdentifierType): Promise<boolean>;
listAllByUserIdWithPaging(userId: string, page?: number, pageSize?: number): Promise<IdentifierList>;
listAllByUserIdAndTypeWithPaging(
userId: string,
type: IdentifierType,
page?: number,
pageSize?: number,
): Promise<IdentifierList>;
listAllEmailsByUserId(userId: string): Promise<IdentifierRsp[]>;
listByUserId(userId: string, page?: number, pageSize?: number): Promise<IdentifierList>;
listByUserIdAndType(userId: string, type: IdentifierType, page?: number, pageSize?: number): Promise<IdentifierList>;
updateIdentifier(
userId: string,
identifierId: string,
Expand Down Expand Up @@ -119,15 +104,7 @@ class Identifier implements IdentifierInterface {
}
}

async listAllWithPaging(page?: number, pageSize?: number): Promise<IdentifierList> {
return this.list(undefined, undefined, page, pageSize);
}

async listByValueAndType(value: string, type: IdentifierType): Promise<IdentifierList> {
return this.list(undefined, [`identifierValue:eq:${value}`, `identifierType:eq:${type}`], undefined, undefined);
}

async listByValueAndTypeWithPaging(
async listByValueAndType(
value: string,
type: IdentifierType,
sort?: string,
Expand All @@ -137,13 +114,7 @@ class Identifier implements IdentifierInterface {
return this.list(sort, [`identifierValue:eq:${value}`, `identifierType:eq:${type}`], page, pageSize);
}

async existsByValueAndType(value: string, type: IdentifierType): Promise<boolean> {
const list = await this.listByValueAndType(value, type);

return list.identifiers.length > 0;
}

async listAllByUserIdWithPaging(userId: string, page?: number, pageSize?: number): Promise<IdentifierList> {
async listByUserId(userId: string, page?: number, pageSize?: number): Promise<IdentifierList> {
let id = userId;

// filter queries are using userID without prefix
Expand All @@ -154,12 +125,7 @@ class Identifier implements IdentifierInterface {
return this.list(undefined, [`userID:eq:${id}`], page, pageSize);
}

listAllByUserIdAndTypeWithPaging(
userId: string,
type: IdentifierType,
page?: number,
pageSize?: number,
): Promise<IdentifierList> {
listByUserIdAndType(userId: string, type: IdentifierType, page?: number, pageSize?: number): Promise<IdentifierList> {
let id = userId;

// filter queries are using userID without prefix
Expand All @@ -170,31 +136,6 @@ class Identifier implements IdentifierInterface {
return this.list(undefined, [`userID:eq:${id}`, `identifierType:eq:${type}`], page, pageSize);
}

async listAllEmailsByUserId(userId: string): Promise<IdentifierRsp[]> {
const list: IdentifierRsp[] = [];

const firstRes = await this.listAllByUserIdAndTypeWithPaging(userId, IdentifierType.Email);

list.push(...firstRes.identifiers);

const { paging } = firstRes;

const listPromise: Promise<IdentifierList>[] = [];
while (paging.page < paging.totalPages) {
const currentPage = paging.page;
paging.page = currentPage + 1;

const temp = this.listAllByUserIdAndTypeWithPaging(userId, IdentifierType.Email, paging.page);

listPromise.push(temp);
}

const listRes = await Promise.all(listPromise);
list.push(...listRes.flatMap((res) => res.identifiers));

return list;
}

async updateIdentifier(
userId: string,
identifierId: string,
Expand Down
18 changes: 1 addition & 17 deletions src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { UserCreateReq, UsersApi, User, UserStatus, GenericRsp } from '../genera

export interface UserInterface {
create(req: UserCreateReq): Promise<User>;
create(fullName: string, status: UserStatus, explicitWebauthnID: string): Promise<User>;
createActiveByName(fullName: string): Promise<User>;
delete(id: string): Promise<GenericRsp>;
get(id: string): Promise<User>;
Expand All @@ -19,23 +18,8 @@ class UserService implements UserInterface {
this.client = new UsersApi(undefined, '', axios);
}

async create(req: UserCreateReq): Promise<User>;
async create(fullName: string, status: UserStatus, explicitWebauthnID: string): Promise<User>;
async create(arg1: UserCreateReq | string, arg2?: UserStatus, arg3?: string): Promise<User> {
if (typeof arg1 === 'string' && arg2 && arg3) {
Assert.notEmptyString(arg1, 'User.create() "fullName" param must not be null');

const request: UserCreateReq = {
fullName: arg1,
status: arg2,
explicitWebauthnID: arg3,
};

return this.create(request);
}

async create(req: UserCreateReq): Promise<User> {
try {
const req = arg1 as UserCreateReq;
Assert.notNull(req, 'User.create() "req" param must not be null');
Assert.notEmptyString(req.fullName ? req.fullName : '', 'User.create() "fullName" param must not be empty');
Assert.notNull(req.status, 'User.create() "status" param must not be null');
Expand Down
45 changes: 11 additions & 34 deletions tests/integration/services/identifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
IdentifierCreateReq,
IdentifierStatus,
IdentifierType,
IdentifierList,
Identifier as IdentifierRsp,
} from '../../../src/generated';
import Utils from '../../utils';
Expand All @@ -19,7 +18,7 @@ describe('Identifier Service Tests', () => {
sdk = Utils.SDK();

// Create a test user and email identifier
TEST_USER_ID = await Utils.createUserId();
TEST_USER_ID = (await Utils.createUser()).userID;
TEST_USER_EMAIL = Utils.createRandomTestEmail();

// Create an email identifier for the user
Expand All @@ -30,26 +29,10 @@ describe('Identifier Service Tests', () => {
});
});

test('should check if existing email identifier is present', async () => {
// Check if email identifier is present
let ret: IdentifierList = await sdk.identifiers().listByValueAndType(TEST_USER_EMAIL, IdentifierType.Email);
expect(ret.identifiers.length).not.toEqual(0);

// Check if email is listed under phone identifiers (should not be found)
ret = await sdk.identifiers().listByValueAndType(TEST_USER_EMAIL, IdentifierType.Phone);
expect(ret.identifiers.length).toEqual(0);

// Check existence using existsByValueAndType
const emailExists = await sdk.identifiers().existsByValueAndType(TEST_USER_EMAIL, IdentifierType.Email);
const phoneExists = await sdk.identifiers().existsByValueAndType(TEST_USER_EMAIL, IdentifierType.Phone);
expect(emailExists).toBe(true);
expect(phoneExists).toBe(false);
});

test('should throw error on empty identifier creation', async () => {
expect.assertions(3);

const userId = await Utils.createUserId();
const userId = (await Utils.createUser()).userID;
const req: IdentifierCreateReq = {
identifierType: IdentifierType.Email,
identifierValue: '', // Empty email value
Expand All @@ -66,7 +49,7 @@ describe('Identifier Service Tests', () => {
});

test('should successfully create an identifier', async () => {
const userId = await Utils.createUserId();
const userId = (await Utils.createUser()).userID;
const email = Utils.createRandomTestEmail();
const req: IdentifierCreateReq = {
identifierType: IdentifierType.Email,
Expand All @@ -81,7 +64,7 @@ describe('Identifier Service Tests', () => {
});

test('should list all identifiers by userId', async () => {
const ret = await sdk.identifiers().listAllByUserIdWithPaging(TEST_USER_ID);
const ret = await sdk.identifiers().listByUserId(TEST_USER_ID);
const identifierExists = ret.identifiers.some(
(identifier) => identifier.identifierID === TEST_USER_EMAIL_IDENTIFIER.identifierID,
);
Expand All @@ -96,19 +79,11 @@ describe('Identifier Service Tests', () => {
});

test('should list identifiers by value and type with paging', async () => {
const ret = await sdk
.identifiers()
.listByValueAndTypeWithPaging(TEST_USER_EMAIL, IdentifierType.Email, undefined, 1, 10);
const ret = await sdk.identifiers().listByValueAndType(TEST_USER_EMAIL, IdentifierType.Email, undefined, 1, 10);
expect(ret.identifiers.length).toBeGreaterThan(0);
expect(ret.identifiers[0].value).toEqual(TEST_USER_EMAIL);
});

test('should list identifiers with paging', async () => {
const ret = await sdk.identifiers().listAllWithPaging(1, 10);
expect(ret.identifiers).toBeDefined();
expect(ret.identifiers.length).toBeGreaterThan(0);
});

test('should list all identifiers', async () => {
const ret = await sdk.identifiers().list(undefined, undefined, 1, 100);
expect(ret).not.toBeNull();
Expand Down Expand Up @@ -141,7 +116,7 @@ describe('Identifier Service Tests', () => {
});

test('should list all emails by userId', async () => {
const testSize = 11;
const testSize = 3;

// Create multiple email identifiers for the same user
const promises: Promise<IdentifierRsp>[] = [];
Expand All @@ -158,8 +133,10 @@ describe('Identifier Service Tests', () => {

await Promise.all(promises);

const allEmails = await sdk.identifiers().listAllEmailsByUserId(TEST_USER_ID);
expect(allEmails.length).toEqual(testSize + 1); // One email was already created before
const allEmails = await sdk
.identifiers()
.listByUserIdAndType(TEST_USER_ID, IdentifierType.Email, undefined, undefined);
expect(allEmails.identifiers.length).toEqual(testSize + 1); // One email was already created before
});

test('should successfully delete an identifier', async () => {
Expand Down Expand Up @@ -215,7 +192,7 @@ describe('Identifier Service Tests', () => {
test('should fail to create identifier with invalid type', async () => {
expect.assertions(2);

const userId = await Utils.createUserId();
const userId = (await Utils.createUser()).userID;
const req: IdentifierCreateReq = {
identifierType: 'invalid_type' as IdentifierType, // Invalid identifier type
identifierValue: Utils.createRandomTestEmail(),
Expand Down
14 changes: 2 additions & 12 deletions tests/integration/services/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ describe('User Validation Tests', () => {
expect(sendResponse.fullName).toEqual(req.fullName);
});

test('should handle successful create using fullName and userStatus', async () => {
expect.assertions(2);

const fullName = Utils.createRandomTestName();
const sendResponse = await sdk.users().create(fullName, UserStatus.Active, Utils.generateString(10));

expect(sendResponse.fullName).toEqual(fullName);
expect(sendResponse.status).toEqual(UserStatus.Active);
});

test('should handle not found delete', async () => {
expect.assertions(3);
try {
Expand All @@ -56,7 +46,7 @@ describe('User Validation Tests', () => {
test('should handle successful delete', async () => {
expect.assertions(2);

const userId = await Utils.createUserId();
const userId = (await Utils.createUser()).userID;

await sdk.users().delete(userId);

Expand All @@ -82,7 +72,7 @@ describe('User Validation Tests', () => {
test('should handle successful get', async () => {
expect.assertions(1);

const userId = await Utils.createUserId();
const userId = (await Utils.createUser()).userID;
const getResponse = await sdk.users().get(userId);
expect(getResponse.userID).toEqual(userId);
});
Expand Down
5 changes: 3 additions & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AxiosMockAdapter from 'axios-mock-adapter';
import axios, { AxiosInstance } from 'axios';
import { SDK, Config } from '../src';
import { BaseError, httpStatusCodes } from '../src/errors';
import { User } from '../src/generated';

class Utils {
public static SDK(): SDK {
Expand Down Expand Up @@ -88,10 +89,10 @@ class Utils {
return `+491509${this.generateNumber(7)}`;
}

public static async createUserId(): Promise<string> {
public static async createUser(): Promise<User> {
const rsp = await this.SDK().users().createActiveByName(this.createRandomTestName());

return rsp.userID;
return rsp;
}

public static testConstants = {
Expand Down

0 comments on commit e79e579

Please sign in to comment.