Skip to content

Commit 61d1a44

Browse files
authored
feat: add entity verification and migrate audit logs to actor model (CM-966) (#3863)
1 parent 4a84072 commit 61d1a44

File tree

74 files changed

+736
-1303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+736
-1303
lines changed

backend/src/api/auditLog/auditLogList.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

backend/src/api/auditLog/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { safeWrap } from '../../middlewares/errorMiddleware'
22

33
export default (app) => {
4-
app.get(`/audit-log`, safeWrap(require('./auditLogList').default))
5-
64
app.post(`/audit-logs/query`, safeWrap(require('./auditLogsQuery').default))
75
}

backend/src/bin/jobs/cleanUp.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { getServiceChildLogger } from '@crowd/logging'
22

3-
import AuditLogRepository from '../../database/repositories/auditLogRepository'
43
import IncomingWebhookRepository from '../../database/repositories/incomingWebhookRepository'
54
import IntegrationRunRepository from '../../database/repositories/integrationRunRepository'
65
import SequelizeRepository from '../../database/repositories/sequelizeRepository'
@@ -40,13 +39,6 @@ export const cleanUpOldWebhooks = async () => {
4039
await repo.cleanUpOldWebhooks(MAX_MONTHS_TO_KEEP)
4140
}
4241

43-
export const cleanUpOldAuditLogs = async () => {
44-
const dbOptions = await SequelizeRepository.getDefaultIRepositoryOptions()
45-
46-
log.info(`Cleaning up audit logs that are older than 1 month!`)
47-
await AuditLogRepository.cleanUpOldAuditLogs(1, dbOptions)
48-
}
49-
5042
const job: CrowdJob = {
5143
name: 'Clean up old data',
5244
// run once every week on Sunday at 1AM
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
-- ---------------------------------------------------------------------------
2+
-- Add verification metadata for member identities
3+
-- ---------------------------------------------------------------------------
4+
5+
-- Track the original source of the identity and which source performed the latest verification
6+
alter table "memberIdentities" add column if not exists "source" varchar(255);
7+
alter table "memberIdentities" add column if not exists "verifiedBy" varchar(255);
8+
9+
-- ---------------------------------------------------------------------------
10+
-- Add verification metadata for organization identities
11+
-- ---------------------------------------------------------------------------
12+
13+
-- Track the original source of the identity
14+
alter table "organizationIdentities" add column if not exists "source" varchar(255);
15+
16+
-- ---------------------------------------------------------------------------
17+
-- Add verification metadata for work experiences
18+
-- ---------------------------------------------------------------------------
19+
20+
-- Track if the work experience has been verified and by which source
21+
alter table "memberOrganizations" add column if not exists "verified" boolean not null default false;
22+
alter table "memberOrganizations" add column if not exists "verifiedBy" varchar(255);
23+
24+
-- ---------------------------------------------------------------------------
25+
-- Align audit logging with a generic actor model
26+
-- ---------------------------------------------------------------------------
27+
28+
-- Add actorId and actorType as nullable initially
29+
alter table "auditLogAction" add column if not exists "actorId" varchar(255);
30+
alter table "auditLogAction" add column if not exists "actorType" varchar(255);
31+
32+
-- Backfill historical rows (map old userId into new columns)
33+
update "auditLogAction"
34+
set "actorId" = "userId"::text,
35+
"actorType" = 'user'
36+
where "actorId" is null;
37+
38+
-- Enforce NOT NULL after backfill
39+
alter table "auditLogAction" alter column "actorId" set not null;
40+
alter table "auditLogAction" alter column "actorType" set not null;
41+
42+
-- Add index on actorId to speed up queries
43+
create index if not exists "auditLogAction_actorId" on "auditLogAction" ("actorId");
44+
45+
-- Remove old userId column
46+
alter table "auditLogAction" drop column if exists "userId";
47+
48+
-- ---------------------------------------------------------------------------
49+
-- Remove legacy auditLogs table
50+
-- ---------------------------------------------------------------------------
51+
-- This table is no longer used; all audit actions are tracked in auditLogAction
52+
drop table if exists "auditLogs";

backend/src/database/models/auditLog.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

backend/src/database/models/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ async function models(queryTimeoutMilliseconds: number, databaseHostnameOverride
117117
// }
118118

119119
const modelClasses = [
120-
require('./auditLog').default,
121120
require('./member').default,
122121
require('./memberIdentity').default,
123122
require('./file').default,

backend/src/database/repositories/activityRepository.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ import { IIntegrationResult, IntegrationResultState } from '@crowd/types'
1414
import { QUEUE_CLIENT } from '@/serverless/utils/queueService'
1515

1616
import { IRepositoryOptions } from './IRepositoryOptions'
17-
import AuditLogRepository from './auditLogRepository'
1817
import SegmentRepository from './segmentRepository'
1918
import SequelizeRepository from './sequelizeRepository'
2019

21-
const log: boolean = false
22-
2320
class ActivityRepository {
2421
static async create(data, options: IRepositoryOptions) {
2522
const currentUser = SequelizeRepository.getCurrentUser(options)
@@ -80,8 +77,6 @@ class ActivityRepository {
8077

8178
const record = await this.findById(ids[0], options)
8279

83-
await this._createAuditLog(AuditLogRepository.CREATE, record, data, options)
84-
8580
return record
8681
}
8782

@@ -216,28 +211,6 @@ class ActivityRepository {
216211
return results[0][0].id
217212
}
218213

219-
static async _createAuditLog(action, record, data, options: IRepositoryOptions) {
220-
if (log) {
221-
let values = {}
222-
223-
if (data) {
224-
values = {
225-
...record.get({ plain: true }),
226-
}
227-
}
228-
229-
await AuditLogRepository.log(
230-
{
231-
entityName: 'activity',
232-
entityId: record.id,
233-
action,
234-
values,
235-
},
236-
options,
237-
)
238-
}
239-
}
240-
241214
static async _populateRelationsForRows(rows, options: IRepositoryOptions) {
242215
if (!rows) {
243216
return rows

backend/src/database/repositories/auditLogRepository.ts

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)