Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ADR about the Auditable type and helper fn #899

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/architecture/decision-records/Auditable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Auditable

## Context

A historical record needs to be kept related to certain data points in CAMS. This historical record involves tracking when a change was made, and by whom. We were previously managing this for orders and assignments in an ad hoc manner. To ensure consistency and to reduce boilerplate code, we need a way to provide this functionality through code common to all data types that require historical records.

## 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:

```typescript
type Foo = Auditable & {
prop1: string;
prop2: number;
}
```

To create a historical record for an action initiated by a user, call the `createAuditRecord` as follows:

```typescript
createAuditRecord<Foo>(someFoo, userSession);
```

To create a historical record for an action initiated by the system, call the `createAuditRecord` as follows:

```typescript
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);
```
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This highlights a problem with the current implementation. Because the parameters for createAuditRecord contain two optional parameters, we can actually only provide an override if we also provide a session. This could prove to be problematic if for some reason we need to create a record of a system-initiated change while also overriding the date (e.g. we need to use a date from DXTR or the court that is prior to the current date).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written, this example would not work. We should probably convert the function to take a single object for arguments or the item and an object that contains the optional session and optional override.

// option 1
export function createAuditRecord<T extends Auditable>(args: {
  record: Omit<T, 'updatedOn' | 'updatedBy'>,
  session?: CamsSession,
  override?: Partial<Auditable>,
}): T {
  // do stuff
}
// option 2
export function createAuditRecord<T extends Auditable>(
  record: Omit<T, 'updatedOn' | 'updatedBy'>,
  options?: {
    session?: CamsSession,
    override?: Partial<Auditable>,
}): T {
  // do stuff
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I like option 1 better. Session and override seem fundamentally different so grouping just the two of them doesn't make much sense to me.


## Status

Approved

## Consequences

Developers need to remember to make use of this type and the function, but it should reduce the amount of boilerplate/duplicate code we have to write to track history.