Skip to content

Commit

Permalink
Merge pull request #563 from IABTechLab/sas-UID2-4299-audit-trail-cle…
Browse files Browse the repository at this point in the history
…anup-2

names rather than ids in audit trail.  index on participantId in audit table
  • Loading branch information
ssundahlTTD authored Nov 4, 2024
2 parents b2773f2 + e76e967 commit 600132c
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/api/entities/AuditTrail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { BaseModel } from './BaseModel';
import { ModelObjectOpt } from './ModelObjectOpt';

export enum AuditAction {
Add = 'add',
Delete = 'delete',
Update = 'update',
Add = 'Add',
Delete = 'Delete',
Update = 'Update',
}

export enum AuditTrailEvents {
Expand Down
13 changes: 13 additions & 0 deletions src/api/entities/UserRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ export enum UserRoleId {
UID2Support = 3,
}

export const getUserRoleById = (id: number) => {
switch (id) {
case 1:
return 'Admin';
case 2:
return 'Operations';
case 3:
return 'Support';
default:
return 'Invalid';
}
};

export class UserRole extends BaseModel {
static get tableName() {
return 'userRoles';
Expand Down
14 changes: 9 additions & 5 deletions src/api/routers/participants/participantsApproval.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Response } from 'express';

import { getRoleNamesByIds } from '../../../web/utils/apiRoles';
import { AuditTrailEvents } from '../../entities/AuditTrail';
import { ParticipantApprovalPartial, ParticipantStatus } from '../../entities/Participant';
import { getTraceId } from '../../helpers/loggingHelpers';
import { getKcAdminClient } from '../../keycloakAdminClient';
import { setSiteClientTypes } from '../../services/adminServiceClient';
import { ParticipantApprovalResponse } from '../../services/adminServiceHelpers';
import {
mapClientTypeIdsToAdminEnums,
ParticipantApprovalResponse,
} from '../../services/adminServiceHelpers';
import {
constructAuditTrailObject,
performAsyncOperationWithAuditTrail,
Expand Down Expand Up @@ -56,11 +60,11 @@ export const handleApproveParticipant = async (req: UserParticipantRequest, res:
AuditTrailEvents.ApproveAccount,
{
oldName: participant?.name,
siteId: data.siteId!,
newName: data.name,
oldTypeIds: participant?.types!.map((type) => type.id),
newTypeIds: data.types.map((type) => type.id),
apiRoles: data.apiRoles.map((role) => role.id),
siteId: data.siteId!,
oldTypeIds: mapClientTypeIdsToAdminEnums(participant?.types!.map((type) => type.id) ?? []),
newTypeIds: mapClientTypeIdsToAdminEnums(data.types.map((type) => type.id)),
apiRoles: getRoleNamesByIds(data.apiRoles.map((role) => role.id)),
},
participant!.id
);
Expand Down
8 changes: 6 additions & 2 deletions src/api/routers/participants/participantsCreation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Response } from 'express';
import { z } from 'zod';

import { getRoleNamesByIds } from '../../../web/utils/apiRoles';
import { ApiRole } from '../../entities/ApiRole';
import { AuditAction, AuditTrailEvents } from '../../entities/AuditTrail';
import {
Expand All @@ -15,6 +16,7 @@ import { getTraceId } from '../../helpers/loggingHelpers';
import { getKcAdminClient } from '../../keycloakAdminClient';
import { addSite, getSiteList, setSiteClientTypes } from '../../services/adminServiceClient';
import {
mapClientTypeIdsToAdminEnums,
mapClientTypesToAdminEnums,
SiteCreationRequest,
} from '../../services/adminServiceHelpers';
Expand Down Expand Up @@ -126,9 +128,11 @@ async function createParticipant(
{
action: AuditAction.Add,
siteId: parsedParticipantRequest.siteId!,
apiRoles: parsedParticipantRequest.apiRoles.map((role) => role.id),
apiRoles: getRoleNamesByIds(parsedParticipantRequest.apiRoles.map((role) => role.id)),
participantName: parsedParticipantRequest.name,
participantTypes: parsedParticipantRequest.types.map((type) => type.id),
participantTypes: mapClientTypeIdsToAdminEnums(
parsedParticipantRequest.types.map((type) => type.id)
),
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
Expand Down
3 changes: 2 additions & 1 deletion src/api/routers/participants/participantsUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from 'zod';

import { AuditAction, AuditTrailEvents } from '../../entities/AuditTrail';
import { UserJobFunction } from '../../entities/User';
import { getUserRoleById } from '../../entities/UserRole';
import { getTraceId } from '../../helpers/loggingHelpers';
import {
constructAuditTrailObject,
Expand Down Expand Up @@ -42,7 +43,7 @@ export async function handleInviteUserToParticipant(req: UserParticipantRequest,
lastName: userPartial.lastName,
email: userPartial.email,
jobFunction: userPartial.jobFunction,
userRoleId: userRoleIdData.userRoleId,
userRoleId: getUserRoleById(userRoleIdData.userRoleId),
},
participant!.id
);
Expand Down
11 changes: 8 additions & 3 deletions src/api/services/participantsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Request } from 'express';
import { TransactionOrKnex } from 'objection';
import { z } from 'zod';

import { getRoleNamesByIds } from '../../web/utils/apiRoles';
import { ApiRole } from '../entities/ApiRole';
import { AuditAction, AuditTrailEvents } from '../entities/AuditTrail';
import {
Expand All @@ -16,7 +17,11 @@ import { User, UserDTO } from '../entities/User';
import { SSP_WEB_BASE_URL } from '../envars';
import { getTraceId } from '../helpers/loggingHelpers';
import { getSharingList, setSiteClientTypes, updateSharingList } from './adminServiceClient';
import { ClientType, SharingListResponse } from './adminServiceHelpers';
import {
ClientType,
mapClientTypeIdsToAdminEnums,
SharingListResponse,
} from './adminServiceHelpers';
import { findApproversByType, getApprovableParticipantTypeIds } from './approversService';
import {
constructAuditTrailObject,
Expand Down Expand Up @@ -285,9 +290,9 @@ export const updateParticipant = async (participant: Participant, req: UserParti
AuditTrailEvents.ManageParticipant,
{
action: AuditAction.Update,
apiRoles,
apiRoles: getRoleNamesByIds(apiRoles),
participantName,
participantTypes: participantTypeIds,
participantTypes: mapClientTypeIdsToAdminEnums(participantTypeIds),
crmAgreementNumber,
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/api/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { z } from 'zod';
import { AuditAction, AuditTrailEvents } from '../entities/AuditTrail';
import { ParticipantType } from '../entities/ParticipantType';
import { UserJobFunction } from '../entities/User';
import { UserRoleId } from '../entities/UserRole';
import { getUserRoleById, UserRoleId } from '../entities/UserRole';
import { UserToParticipantRole } from '../entities/UserToParticipantRole';
import { getTraceId } from '../helpers/loggingHelpers';
import { mapClientTypeToParticipantType } from '../helpers/siteConvertingHelpers';
Expand Down Expand Up @@ -112,7 +112,7 @@ export class UserService {
firstName: userData.firstName,
lastName: userData.lastName,
jobFunction: userData.jobFunction,
userRoleId: userRoleData.userRoleId,
userRoleId: getUserRoleById(userRoleData.userRoleId),
},
participant!.id
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
// add index to participantId column
await knex.schema.alterTable('auditTrails', (table) => {
table.index('participantId');
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('auditTrails', (table) => {
table.dropIndex('participantId');
});
}
3 changes: 1 addition & 2 deletions src/web/components/AuditTrail/AuditTrailRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ function AuditTrailRow({ log }: AuditTrailProps) {

return (
<tr>
<td>{log.id}</td>
<td>{eventDate}</td>
<td>{log.userEmail}</td>
<td>{log.event.replace(/([a-z])([A-Z])/g, '$1 $2')}</td>
<td className='event-data'>{formattedEventData}</td>
<td>{eventDate}</td>
<td className='succeeded'>
<FontAwesomeIcon
className={log.succeeded ? 'icon_check' : 'icon_x'}
Expand Down
3 changes: 1 addition & 2 deletions src/web/components/AuditTrail/AuditTrailTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ function AuditTrailTableComponent({ auditTrail }: AuditTrailTableProps) {
<table className='audit-log-table'>
<thead>
<tr>
<th>ID</th>
<SortableTableHeader<AuditTrailDTO> sortKey='updated_at' header='Date' />
<SortableTableHeader<AuditTrailDTO> sortKey='userEmail' header='User' />
<SortableTableHeader<AuditTrailDTO> sortKey='event' header='Event' />
<th>Event Data</th>
<SortableTableHeader<AuditTrailDTO> sortKey='updated_at' header='Date' />
<th>Succeeded</th>
</tr>
</thead>
Expand Down

0 comments on commit 600132c

Please sign in to comment.