Skip to content

Commit

Permalink
Merge pull request aesirxio#250 from vietredweb/member
Browse files Browse the repository at this point in the history
Roles, Member Role
  • Loading branch information
lunguyenhat authored Nov 29, 2023
2 parents a3d05ef + 38cd5bc commit 3ffaf41
Show file tree
Hide file tree
Showing 14 changed files with 836 additions and 16 deletions.
32 changes: 29 additions & 3 deletions src/Constant/OrganizationContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,34 @@ const ORGANISATION_MEMBER_FIELD: any = {

const ORGANISATION_ROLE_FIELD: any = {
ID: 'id',
NAME: 'role_name',
DESC: 'description',
ROLE_NAME: 'role_name',
DESCRIPTION: 'description',
};

export { ORGANISATION_MEMBER_FIELD, ORGANISATION_ROLE_FIELD };
const MEMBER_ROLE_FIELD: any = {
ID: 'id',
MEMBER_ID: 'member_id',
MEMBER_NAME: 'member_name',
ROLE_ID: 'role_id',
ROLE_NAME: 'role_name',
ORGANISATION_ID: 'organisation_id',
NAME: 'name',
STATE: 'state',
PUBLISHED: 'published',
CREATED_USER_NAME: 'created_user_name',
MODIFIED_USER_NAME: 'modified_user_name',
CREATED_TIME: 'created_time',
MODIFIED_TIME: 'modified_time',
};

const PERMISSION_FIELD: any = {
ROLE_NAME: 'role_name',
GROUP_ID: 'group_id',
RULES: 'rules',
ASSET_ID: 'asset_id',
ACTION: 'action',
VALUE: 'value',
ENTITY: 'entity',
};

export { ORGANISATION_MEMBER_FIELD, ORGANISATION_ROLE_FIELD, MEMBER_ROLE_FIELD, PERMISSION_FIELD };
101 changes: 101 additions & 0 deletions src/Organization/MemberRole/Model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* @copyright Copyright (C) 2022 AesirX. All rights reserved.
* @license GNU General Public License version 3, see LICENSE.
*/

import BaseItemModel from '../../Abstract/BaseItemModel';
import BaseModel from '../../Abstract/BaseModel';
import { MEMBER_ROLE_FIELD } from '../../Constant/OrganizationContent';

class MemberRoleModel extends BaseModel {
constructor(entities: any) {
super(entities);
if (entities) {
this.items = entities._embedded?.item.map((element: any) => {
return new MemberRoleItemModel(element);
});
}
}
}

class MemberRoleItemModel extends BaseItemModel {
id: number = 0;
member_id = null;
member_name = null;
role_id = null;
role_name = null;
organisation_id = null;
name = null;
state = null;
published = null;
created_user_name = null;
modified_user_name = null;
created_time = null;
modified_time = null;
constructor(entity: any) {
super(entity);
if (entity) {
this.id = entity[MEMBER_ROLE_FIELD.ID] ?? 0;
this.member_id = entity[MEMBER_ROLE_FIELD.MEMBER_ID] ?? 0;
this.member_name = entity[MEMBER_ROLE_FIELD.MEMBER_NAME] ?? '';
this.role_id = entity[MEMBER_ROLE_FIELD.ROLE_ID] ?? 0;
this.role_name = entity[MEMBER_ROLE_FIELD.ROLE_NAME] ?? '';
this.name = entity[MEMBER_ROLE_FIELD.NAME] ?? '';
this.state = entity[MEMBER_ROLE_FIELD.STATE] ?? '';
this.published = entity[MEMBER_ROLE_FIELD.PUBLISHED] ?? 0;
this.organisation_id = entity[MEMBER_ROLE_FIELD.ORGANISATION_ID] ?? 0;
this.created_user_name = entity[MEMBER_ROLE_FIELD.CREATED_USER_NAME] ?? '';
this.modified_user_name = entity[MEMBER_ROLE_FIELD.MODIFIED_USER_NAME] ?? '';
this.created_time = entity[MEMBER_ROLE_FIELD.CREATED_TIME] ?? '';
this.modified_time = entity[MEMBER_ROLE_FIELD.MODIFIED_TIME] ?? '';
}
}

toJSON = () => {
return {
...this.baseToJSON(),
[MEMBER_ROLE_FIELD.ID]: this.id,
[MEMBER_ROLE_FIELD.MEMBER_ID]: this.member_id,
[MEMBER_ROLE_FIELD.MEMBER_NAME]: this.member_name,
[MEMBER_ROLE_FIELD.ROLE_ID]: this.role_id,
[MEMBER_ROLE_FIELD.ROLE_NAME]: this.role_name,
[MEMBER_ROLE_FIELD.NAME]: this.name,
[MEMBER_ROLE_FIELD.STATE]: this.state,
[MEMBER_ROLE_FIELD.PUBLISHED]: this.published,
[MEMBER_ROLE_FIELD.ORGANISATION_ID]: this.organisation_id,
[MEMBER_ROLE_FIELD.CREATED_USER_NAME]: this.created_user_name,
[MEMBER_ROLE_FIELD.MODIFIED_USER_NAME]: this.modified_user_name,
[MEMBER_ROLE_FIELD.CREATED_TIME]: this.created_time,
[MEMBER_ROLE_FIELD.MODIFIED_TIME]: this.modified_time,
[MEMBER_ROLE_FIELD.NAME]: this.name,
};
};

toObject = () => {
return {};
};

static __transformItemToApiOfCreation = (data: any) => {
let formData = new FormData();
const excluded = [MEMBER_ROLE_FIELD.ID];
Object.keys(MEMBER_ROLE_FIELD).forEach((index) => {
if (!excluded.includes(MEMBER_ROLE_FIELD[index]) && data[MEMBER_ROLE_FIELD[index]]) {
formData.append(MEMBER_ROLE_FIELD[index], data[MEMBER_ROLE_FIELD[index]]);
}
});
return formData;
};

static __transformItemToApiOfUpdation = (data: any) => {
let formData: any = {};
const excluded = [MEMBER_ROLE_FIELD.CUSTOM_FIELDS];
Object.keys(MEMBER_ROLE_FIELD).forEach((index) => {
if (!excluded.includes(MEMBER_ROLE_FIELD[index]) && data[MEMBER_ROLE_FIELD[index]]) {
formData[MEMBER_ROLE_FIELD[index]] = data[MEMBER_ROLE_FIELD[index]];
}
});
return formData;
};
}

export { MemberRoleModel, MemberRoleItemModel };
69 changes: 69 additions & 0 deletions src/Organization/MemberRole/Route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* @copyright Copyright (C) 2022 AesirX. All rights reserved.
* @license GNU General Public License version 3, see LICENSE.
*/

import AesirXApiInstance from '../../Gateway/Instance';
import BaseRoute from '../../Abstract/BaseRoute';
import Utils from '../../Utils/Utils';

class MemberRoleRouter extends BaseRoute {
option = 'reditem-member_role';

create = (data: any) => {
return AesirXApiInstance.post(
this.createRequestURL({
option: this.option,
}),
data
);
};

update = (data: any) => {
return AesirXApiInstance.put(
this.createRequestURL({
option: this.option,
}),
data,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);
};

delete = (ids: any) => {
return AesirXApiInstance.delete(
this.createRequestURL({
option: this.option,
}),
{
data: { ids: Array.isArray(ids) ? ids : [ids] },
}
);
};

getList = (filters: any) => {
const buildFilters = Utils.createFilters(filters);

return AesirXApiInstance.get(
this.createRequestURL({
option: this.option,
...buildFilters,
})
);
};

getDetail = (id = 0, dataFilter = {}) => {
return AesirXApiInstance.get(
this.createRequestURL({
option: this.option,
id: id,
...dataFilter,
})
);
};
}

export default MemberRoleRouter;
122 changes: 122 additions & 0 deletions src/Organization/MemberRole/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { OrganizationMemberApiService } from '../Member';
import { OrganizationRoleApiService } from '../Role';
import { MemberRoleApiService } from './index';
import { describe, expect } from '@jest/globals';
let createID: any = '';
let createIDMember: any = '';
let createIDRole: any = '';
describe('MemberRole', () => {
it('Get List', async () => {
const service = new MemberRoleApiService();

const filters = {
'list[limitstart]': 0,
'list[limit]': 2,
};

const data = await service.getList(filters);

expect(data?.listItems?.length).toBeGreaterThan(0);
});

it('Create Member', async () => {
const service = new OrganizationMemberApiService();

const data = {
member_name: 'Member 0000',
password: 'unittest1',
email: '[email protected]',
role_id: '2',
};

const response = await service.create(data);

createIDMember = response?.result;

expect(createIDMember).not.toBeNull();
expect(createIDMember).toBeDefined();
expect(createIDMember).not.toBeUndefined();
expect(createIDMember).not.toBe('false');
});

it('Create Role', async () => {
const service = new OrganizationRoleApiService();

const data = {
role_name: 'Role 0000',
description: 'description',
};

const response = await service.create(data);

createIDRole = response?.result;

expect(createIDRole).not.toBeNull();
expect(createIDRole).toBeDefined();
expect(createIDRole).not.toBeUndefined();
expect(createIDRole).not.toBe('false');
});

it('Create', async () => {
const service = new MemberRoleApiService();

const data = {
name: 'Member Role 0000',
role_id: createIDRole,
member_id: createIDMember,
state: '1',
};

const response = await service.create(data);

createID = response?.result;

expect(createID).not.toBeNull();
expect(createID).toBeDefined();
expect(createID).not.toBeUndefined();
expect(createID).not.toBe('false');
});

it('Update', async () => {
const service = new MemberRoleApiService();

const data = {
id: createID,
name: 'Member Role 0001',
};

const response = await service.update(data);

expect(response).toBeTruthy();
});

it('Get Detail', async () => {
const service = new MemberRoleApiService();

const response: any = await service.getDetail(createID);

expect(response?.id).toEqual(createID);
});

it('Delete', async () => {
const service = new MemberRoleApiService();

const response = await service.delete([createID]);

expect(response).toBeTruthy();
});
it('DeleteMember', async () => {
const service = new OrganizationMemberApiService();

const response = await service.delete([createIDMember]);

expect(response).toBeTruthy();
});
it('DeleteRole', async () => {
const service = new OrganizationRoleApiService();

const response = await service.delete([createIDRole]);

expect(response).toBeTruthy();
});
});
Loading

0 comments on commit 3ffaf41

Please sign in to comment.