Skip to content

Commit

Permalink
fix(mixin): pass the extra values via options (#142)
Browse files Browse the repository at this point in the history
* fix(mixin): pass the extra values via options

dynamically pass extra values to add to audit log model

GH-141

* fix(ci-cd): updated the documentation

update the readme

GH-141
  • Loading branch information
yeshamavani authored Jan 7, 2025
1 parent 8c480b0 commit 6c5f636
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,20 @@ export class GroupRepository extends AuditRepositoryMixin<
}
```

You can pass any extra attributes to save into audit table using the `IAuditMixinOptions` parameter of mixin function.
You can pass any extra attributes to save into audit table using the `IAuditMixinOptions` parameter of mixin function. You can also pass some dynamic values to the extra columns of the audit-log table via the method options.

```ts
const groupAuditOpts: IAuditMixinOptions = {
actionKey: 'Group_Logs',
extra: 'static value',
};
```

```ts
repo.create(data, {extra: 'random calculated value'});
```

So the method options will have a priority over repository mixin options.

Make sure you provide `getCurrentUser` and `getAuditLogRepository` Getter functions in constructor.

Expand Down
15 changes: 14 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,20 @@ export class GroupRepository extends AuditRepositoryMixin<
}
```

You can pass any extra attributes to save into audit table using the `IAuditMixinOptions` parameter of mixin function.
You can pass any extra attributes to save into audit table using the `IAuditMixinOptions` parameter of mixin function. You can also pass some dynamic values to the extra columns of the audit-log table via the method options.

```ts
const groupAuditOpts: IAuditMixinOptions = {
actionKey: 'Group_Logs',
extra: 'static value',
};
```

```ts
repo.create(data, {extra: 'random calculated value'});
```

So the method options will have a priority over repository mixin options.

Make sure you provide `getCurrentUser` and `getAuditLogRepository` Getter functions in constructor.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/mixins/audit.mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ export function AuditRepositoryMixin<
options?: AuditOptions,
): AuditLog {
const {before, after} = modification;
const extras: Omit<IAuditMixinOptions, 'actionKey'> = {...opts};
//dynamic will take precedence over static
const extras: AnyObject = {
...opts, // passing options to the model helps in adding custom and static values to it if needed
...options, // passing options to the model helps in adding custom and dynamic values to it if needed
};
delete extras.actionKey;
delete extras.noAudit;
return new AuditLog({
actedAt: new Date(),
actor: this.getActor(user, options?.actorId),
Expand Down

0 comments on commit 6c5f636

Please sign in to comment.