Skip to content

Commit

Permalink
Remove override capability from createAuditRecord
Browse files Browse the repository at this point in the history
We want to avoid a number of issues related to the ability to handle
multiple optional parameters. We now only accept a record and the user.

Jira ticket: CAMS-282

Co-authored-by: Fritz Madden <[email protected]>
Co-authored-by: Arthur Morrow <[email protected]>
Co-authored-by: Brian Posey <[email protected]>,
  • Loading branch information
3 people committed Sep 16, 2024
1 parent 0ef9be4 commit a1b2ae8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions backend/functions/lib/use-cases/case-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export class CaseAssignmentUseCase {
role: CamsRole[role],
assignedOn: currentDate,
},
context.session,
{ updatedOn: currentDate },
context.session?.user,
);
assignment.updatedOn = currentDate;
listOfAssignments.push(assignment);
});
const listOfAssignmentIdsCreated: string[] = [];
Expand Down Expand Up @@ -122,9 +122,9 @@ export class CaseAssignmentUseCase {
before: existingAssignmentRecords,
after: newAssignmentRecords,
},
context.session,
{ updatedOn: currentDate },
context.session?.user,
);
history.updatedOn = currentDate;
await this.casesRepository.createCaseHistory(context, history);

context.logger.info(
Expand Down
8 changes: 4 additions & 4 deletions backend/functions/lib/use-cases/orders/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class OrdersUseCase {
before: initialOrder as TransferOrder,
after: order,
},
context.session,
context.session?.user,
);
await this.casesRepo.createCaseHistory(context, caseHistory);
}
Expand Down Expand Up @@ -212,7 +212,7 @@ export class OrdersUseCase {
before: null,
after: order,
},
context.session,
context.session?.user,
);
await this.casesRepo.createCaseHistory(context, caseHistory);
}
Expand All @@ -238,7 +238,7 @@ export class OrdersUseCase {
before: null,
after: history,
},
context.session,
context.session?.user,
);
await this.casesRepo.createCaseHistory(context, caseHistory);
}
Expand Down Expand Up @@ -314,7 +314,7 @@ export class OrdersUseCase {
before: isConsolidationHistory(before) ? before : null,
after,
},
context.session,
context.session?.user,
);
}

Expand Down
17 changes: 11 additions & 6 deletions common/src/cams/auditable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CamsSession, getCamsUserReference } from './session';
import { CamsUserReference } from './users';
import { getCamsUserReference } from './session';

export type Auditable = {
updatedOn: string;
Expand All @@ -8,15 +8,20 @@ export type Auditable = {

export const SYSTEM_USER_REFERENCE: CamsUserReference = { id: 'SYSTEM', name: 'SYSTEM' };

/**
* Decorates the record with the Auditable properties. Any necessary overriding of the properties must be completed
* after or in lieu of calling this function.
* @param record T extends Auditable required The record to be decorated.
* @param camsUser CamsUserReference optional The user to be assigned to `updatedBy`. Defaults to the system user, so
* this parameter MUST be provided for user-initiated actions.
*/
export function createAuditRecord<T extends Auditable>(
record: Omit<T, 'updatedOn' | 'updatedBy'>,
session?: CamsSession,
override?: Partial<Auditable>,
camsUser: CamsUserReference = SYSTEM_USER_REFERENCE,
): T {
return {
updatedOn: new Date().toISOString(),
updatedBy: session ? getCamsUserReference(session.user) : SYSTEM_USER_REFERENCE,
...record,
...override,
updatedOn: new Date().toISOString(),
updatedBy: getCamsUserReference(camsUser),
} as T;
}
13 changes: 4 additions & 9 deletions docs/architecture/decision-records/Auditable.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ A historical record needs to be kept related to certain data points in CAMS. Thi

## Decision

We have created the `Auditable` type which contains the common properties for history. We have also created the `createAuditRecord` function which provides default values for the properties and the ability to override if needed. Consider the example type—`Foo`—as follows:
We have created the `Auditable` type which contains the common properties for history. We have also created the `createAuditRecord` function which provides default values for the properties. Overriding any of the properties of `Auditable` must be completed aside from calling this helper function but prior to persisting the record. This avoids issues with having two optional parameters.

Consider the example type—`Foo`—as follows:

```typescript
type Foo = Auditable & {
Expand All @@ -18,7 +20,7 @@ type Foo = Auditable & {
To create a historical record for an action initiated by a user, call the `createAuditRecord` as follows:
```typescript
createAuditRecord<Foo>(someFoo, userSession);
createAuditRecord<Foo>(someFoo, context.session.user);
```

To create a historical record for an action initiated by the system, call the `createAuditRecord` as follows:
Expand All @@ -27,13 +29,6 @@ To create a historical record for an action initiated by the system, call the `c
createAuditRecord<Foo>(someFoo);
```

To create a historical record with an override, call the `createAuditRecord` as follows:

```typescript
const override = { updatedOn: someDate, updatedBy: someUser };
createAuditRecord<Foo>(someFoo, override);
```

## Status

Approved
Expand Down

0 comments on commit a1b2ae8

Please sign in to comment.