Skip to content

Commit

Permalink
Merge pull request #34 from REAN-Foundation/bug-fix-06
Browse files Browse the repository at this point in the history
Added filter on user role.
  • Loading branch information
rupali-inflection authored Jun 20, 2024
2 parents 6cb9f1b + c15d478 commit 7846caa
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 39 deletions.
13 changes: 11 additions & 2 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ export const actions = {
//Login error, so redirect to the sign-in page
throw redirect(303, '/', errorMessage(response.Message), event);
}
console.log('response ....', response);
console.log('response ....', response.Data.User.Role);

if (!response.Data.User.Role || !response.Data.User.Role.RoleName) {
throw redirect(303, '/', errorMessage("Permission Denied!"), event);
}

if (!['System admin','System user','Tenant admin','Tenant user'].includes(response.Data.User.Role.RoleName)) {
throw redirect(303, '/', errorMessage("Permission Denied!"), event);
}

const user = response.Data.User;
user.SessionId = response.Data.SessionId;
const accessToken = response.Data.AccessToken;
Expand All @@ -59,7 +68,7 @@ export const actions = {
const sessionId = response.Data.SessionId;
const userId: string = response.Data.User.id;

const session = await SessionManager.constructSession(user, accessToken, expiryDate, refreshToken);
const session = await SessionManager.constructSession(user, accessToken, expiryDate, refreshToken);
if (!session) {
console.log(`Session cannot be constructed!`);
throw redirect(303, `/`, errorMessage(`Use login session cannot be created!`), event);
Expand Down
10 changes: 4 additions & 6 deletions src/routes/api/server/users/search/+server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { RequestEvent } from '@sveltejs/kit';
import { searchUsers } from '$routes/api/services/reancare/user';

//////////////////////////////////////////////////////////////

export const GET = async (event: RequestEvent) => {
Expand All @@ -10,6 +9,7 @@ export const GET = async (event: RequestEvent) => {
const firstName = searchParams.get('firstName') ?? undefined;
const email = searchParams.get('email') ?? undefined;
const phone = searchParams.get('phone') ?? undefined;
const roleIds = searchParams.get('roleIds') ?? undefined;
const sortBy = searchParams.get('sortBy') ?? 'CreatedAt';
const sortOrder = searchParams.get('sortOrder') ?? 'ascending';
const itemsPerPage_ = searchParams.get('itemsPerPage');
Expand All @@ -22,17 +22,15 @@ export const GET = async (event: RequestEvent) => {
firstName,
phone,
email,
roleIds,
orderBy: sortBy,
order: sortOrder,
itemsPerPage,
pageIndex
};
console.log('Search parms: ', searchParams);
const response = await searchUsers(sessionId, searchParams);
const items = response.Data.Users;
console.log('res==', response);

return new Response(JSON.stringify(items));
const users = response.Data.Users;
return new Response(JSON.stringify(users));
} catch (err) {
console.error(`Error retriving users: ${err.message}`);
return new Response(err.message);
Expand Down
38 changes: 23 additions & 15 deletions src/routes/api/services/reancare/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,28 @@ export const resetPassword = async (

export const createUser = async (
sessionId: string,
tenantId: string,
tenantId: string,
firstName: string,
lastName: string,
phone: string,
phone: string,
email: string,
role: string,
roleId: string,
roleId: string,
password: string,
defaultTimeZone: string,
currentTimeZone: string
) => {
const body = {
TenantId: tenantId,
TenantId: tenantId,
FirstName: firstName,
LastName: lastName,
Role: role,
RoleId: roleId,
Role: role,
RoleId: roleId,
Phone: phone ? phone : null,
Email: email ? email : null,
Email: email ? email : null,
Password: password,
DefaultTimeZone: defaultTimeZone,
CurrentTimeZone: currentTimeZone
};

if (Helper.isPhone(phone)) {
Expand Down Expand Up @@ -184,24 +188,28 @@ export const searchUsers = async (sessionId: string, searchParams?: any) => {

export const updateUser = async (
sessionId: string,
userId: string,
userId: string,
firstName: string,
lastName: string,
phone: string,
phone: string,
email: string,
roleId:string,
roleId: string,
defaultTimeZone: string,
currentTimeZone: string
// role: string,
// password: string,
imageResourceId: string
// imageResourceId: string
) => {
const body = {
FirstName: firstName,
LastName: lastName,
RoleId: roleId,
RoleId: roleId,
Phone: phone ? phone : null,
Email: email ? email : null,
// Password: password,
ImageResourceId: imageResourceId
Email: email ? email : null,
DefaultTimeZone: defaultTimeZone ? defaultTimeZone : null,
CurrentTimeZone: currentTimeZone ? currentTimeZone : null
// Password: password,
// ImageResourceId: imageResourceId
};
if (Helper.isPhone(phone)) {
body.Phone = Helper.sanitizePhone(phone);
Expand Down
24 changes: 19 additions & 5 deletions src/routes/users/[userId]/users/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RequestEvent, ServerLoadEvent } from '@sveltejs/kit';
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { searchUsers } from '$routes/api/services/reancare/user';
import { searchPersonRoleTypes } from '$routes/api/services/reancare/person-role-types';

////////////////////////////////////////////////////////////////////////////

Expand All @@ -11,19 +12,32 @@ export const load: PageServerLoad = async (event: ServerLoadEvent) => {
event.depends('app:users');
const tenantId = event.locals.sessionUser.tenantId;
console.log("tenantId.....",tenantId);
const searchParams = {
tenantId:tenantId

const personRoles = await searchPersonRoleTypes(sessionId);
if (personRoles.Status === 'failure' || personRoles.HttpCode !== 200) {
throw error(personRoles.HttpCode, personRoles.Message);
}
const response = await searchUsers(sessionId, {
const selectedRoles: string[] = [];
personRoles.Data.PersonRoleTypes.map(personRole => {
if (personRole.RoleName === "System admin" ||
personRole.RoleName === "System user" ||
personRole.RoleName === "Tenant admin" ||
personRole.RoleName === "Tenant user"
) {
selectedRoles.push(personRole.id);
}
});

const response = await searchUsers(sessionId, {
orderBy: 'FirstName',
order : 'ascending'
order : 'ascending',
roleIds: selectedRoles.length ? selectedRoles as string[] : null,
});
if (response.Status === 'failure' || response.HttpCode !== 200) {
throw error(response.HttpCode, response.Message);
}
const users = response.Data.Users;

console.log("users", users.Items)
return {
users,
sessionId,
Expand Down
24 changes: 20 additions & 4 deletions src/routes/users/[userId]/users/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import { Helper } from '$lib/utils/helper';
import Icon from '@iconify/svelte';
import { Paginator, type PaginationSettings } from '@skeletonlabs/skeleton';
import date from 'date-and-time';
import type { PageServerData } from './$types';
import { invalidate } from '$app/navigation';
import { LocalStorageUtils } from '$lib/utils/local.storage.utils';
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -37,6 +37,7 @@
let isSortingEmail = false;
let isSortingPhone = false;
let items = 10;
let selectedRoles = [];
let paginationSettings = {
page: 0,
Expand All @@ -51,7 +52,18 @@
}
}
async function searchUser(model) {
const tmp = LocalStorageUtils.getItem('personRoles');
const personRoles = JSON.parse(tmp);
personRoles?.map((x) => {
if (x.RoleName === "System admin" ||
x.RoleName === "System user" ||
x.RoleName === "Tenant admin" ||
x.RoleName === "Tenant user") {
selectedRoles.push(x.id);
}});
$: console.log("selectedRole", selectedRoles);
async function searchUser(model) {
let url = `/api/server/users/search?`;
if (sortOrder) url += `sortOrder=${sortOrder}`;
else url += `sortOrder=ascending`;
Expand All @@ -61,6 +73,7 @@
if (firstName) url += `&firstName=${firstName}`;
if (email) url += `&email=${email}`;
if (phone) url += `&phone=${phone}`;
if (selectedRoles.length > 0) url += `&roleIds=${selectedRoles}`;
console.log('URL: ' + url);
const res = await fetch(url, {
method: 'GET',
Expand Down Expand Up @@ -189,9 +202,12 @@
<td role="gridcell" aria-colindex={2} tabindex="0">
<a href={viewRoute(row.id)}>{Helper.truncateText(row.Person.FirstName, 20)} </a>
</td>
<td role="gridcell" aria-colindex={4} tabindex="0">{row.Person.LastName}</td>
<td role="gridcell" aria-colindex={4} tabindex="0">
{row.Person.LastName !== null ? row.Person.LastName : 'Not specified'}
</td>
<td role="gridcell" aria-colindex={4} tabindex="0"
>{row.Person.Phone !== null ? row.Person.Phone : 'Not specified'}</td
>{row.Person.Phone !== null ? row.Person.Phone : 'Not specified'}
</td
>
<td role="gridcell" aria-colindex={4} tabindex="0"
>{row.Person.Email !== null ? row.Person.Email : 'Not specified'}</td
Expand Down
5 changes: 5 additions & 0 deletions src/routes/users/[userId]/users/[id]/edit/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export const actions = {
};
}

const defaultTimeZone = result.countryCode === '+1' ? '-05:00' : '+05:30';
const currentTimeZone = result.countryCode === '+1' ? '-05:00' : '+05:30';

const phone = result.countryCode + '-' + result.phone;
const response = await updateUser(
sessionId,
Expand All @@ -71,6 +74,8 @@ export const actions = {
phone,
result.email,
result.selectedUserRoleId,
defaultTimeZone,
currentTimeZone
// result.role,
// result.imageResourceId
);
Expand Down
9 changes: 6 additions & 3 deletions src/routes/users/[userId]/users/create/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { errorMessage, successMessage } from '$lib/utils/message.utils';
import { createUser } from '$routes/api/services/reancare/user';

/////////////////////////////////////////////////////////////////////////

const createUserSchema = zfd.formData({
firstName: z.string().min(3).max(256),
lastName: z.string().min(3).max(256),
Expand Down Expand Up @@ -44,17 +43,21 @@ export const actions = {
};
}

const defaultTimeZone = result.countryCode === '+1' ? '-05:00' : '+05:30';
const currentTimeZone = result.countryCode === '+1' ? '-05:00' : '+05:30';
const phone = result.countryCode + '-' + result.phone;
const response = await createUser(
sessionId,
tenantId,
tenantId,
result.firstName,
result.lastName,
phone,
result.email,
result.role,
result.selectedUserRoleId,
result.selectedUserRoleId,
result.password,
defaultTimeZone,
currentTimeZone
);
const id = response.Data.User.id;

Expand Down
8 changes: 4 additions & 4 deletions src/routes/users/[userId]/users/create/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import { showMessage } from '$lib/utils/message.utils.js';
import Icon from '@iconify/svelte';
import { LocalStorageUtils } from '$lib/utils/local.storage.utils';
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
export let form;
const userId = $page.params.userId;
let imageResourceId = '';
let imageUrl = undefined;
let fileinput;
let selectedUserRoleId = undefined;
let selectedUserRoleId = undefined;
const createRoute = `/users/${userId}/users/create`;
const userRoute = `/users/${userId}/users`;
Expand Down Expand Up @@ -188,10 +188,10 @@
on:change={getRoleIdByRoleName}
placeholder="Select role here..."
>
<option value="Tenant admin">Tenant Admin</option>
<option value="Tenant admin">Tenant Admin</option>
<option value="Tenant user">Tenant User</option>
<option value="System user">System User</option>
<option value="System admin">System Admin</option>
<!-- <option value="System admin">System Admin</option> -->


</select>
Expand Down

0 comments on commit 7846caa

Please sign in to comment.