Skip to content

Commit

Permalink
Merge pull request #2 from mlakdawala-sf/RPMS-4260
Browse files Browse the repository at this point in the history
added audit creation optional flag
  • Loading branch information
samarpan-b authored Mar 17, 2021
2 parents 1f20b28 + 4653c0e commit 0da5181
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ Make sure you provide `getCurrentUser` and `getAuditLogRepository` Getter functi

This will create all insert, update, delete audits for this model.

- Option to disable audit logging on specific functions by just passing `noAudit:true` flag with options

```ts
create(data, {noAudit: true});
```

## Feedback

If you've noticed a bug or have a question or have a feature request, [search the issue tracker](https://github.com/sourcefuse/loopback4-audit-log/issues) to see if someone else in the community has already created a ticket.
Expand Down
45 changes: 34 additions & 11 deletions src/mixins/audit.mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import {
DataObject,
Entity,
EntityCrudRepository,
Options,
Where,
} from '@loopback/repository';
import {keyBy} from 'lodash';

import {Action, AuditLog} from '../models';
import {AuditLogRepository} from '../repositories';
import {IAuditMixin, IAuditMixinOptions} from '../types';
import {AuditOptions, IAuditMixin, IAuditMixinOptions} from '../types';

export function AuditRepositoryMixin<
M extends Entity,
Expand All @@ -26,9 +25,12 @@ export function AuditRepositoryMixin<

/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
// @ts-ignore
async create(dataObject: DataObject<M>, options?: Options): Promise<M> {
async create(
dataObject: DataObject<M>,
options?: AuditOptions,
): Promise<M> {
const created = await super.create(dataObject, options);
if (this.getCurrentUser) {
if (this.getCurrentUser && !options?.noAudit) {
const user = await this.getCurrentUser();
const auditRepo = await this.getAuditLogRepository();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -58,10 +60,10 @@ export function AuditRepositoryMixin<
// @ts-ignore
async createAll(
dataObjects: DataObject<M>[],
options?: Options,
options?: AuditOptions,
): Promise<M[]> {
const created = await super.createAll(dataObjects, options);
if (this.getCurrentUser) {
if (this.getCurrentUser && !options?.noAudit) {
const user = await this.getCurrentUser();
const auditRepo = await this.getAuditLogRepository();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -96,8 +98,11 @@ export function AuditRepositoryMixin<
async updateAll(
dataObject: DataObject<M>,
where?: Where<M>,
options?: Options,
options?: AuditOptions,
): Promise<Count> {
if (options?.noAudit) {
return super.updateAll(dataObject, where, options);
}
const toUpdate = await this.find({where});
const beforeMap = keyBy(toUpdate, d => d.getId());
const updatedCount = await super.updateAll(dataObject, where, options);
Expand Down Expand Up @@ -137,7 +142,10 @@ export function AuditRepositoryMixin<

/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
// @ts-ignore
async deleteAll(where?: Where<M>, options?: Options): Promise<Count> {
async deleteAll(where?: Where<M>, options?: AuditOptions): Promise<Count> {
if (options?.noAudit) {
return super.deleteAll(where, options);
}
const toDelete = await this.find({where});
const beforeMap = keyBy(toDelete, d => d.getId());
const deletedCount = await super.deleteAll(where, options);
Expand Down Expand Up @@ -178,9 +186,18 @@ export function AuditRepositoryMixin<
async updateById(
id: ID,
data: DataObject<M>,
options?: Options,
options?: AuditOptions,
): Promise<void> {
if (options?.noAudit) {
return super.updateById(id, data, options);
}
const before = await this.findById(id);
// loopback repository internally calls updateAll so we don't want to create another log
if (options) {
options.noAudit = true;
} else {
options = {noAudit: true};
}
await super.updateById(id, data, options);
const after = await this.findById(id);

Expand Down Expand Up @@ -216,8 +233,11 @@ export function AuditRepositoryMixin<
async replaceById(
id: ID,
data: DataObject<M>,
options?: Options,
options?: AuditOptions,
): Promise<void> {
if (options?.noAudit) {
return super.replaceById(id, data, options);
}
const before = await this.findById(id);
await super.replaceById(id, data, options);
const after = await this.findById(id);
Expand Down Expand Up @@ -251,7 +271,10 @@ export function AuditRepositoryMixin<

/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
// @ts-ignore
async deleteById(id: ID, options?: Options): Promise<void> {
async deleteById(id: ID, options?: AuditOptions): Promise<void> {
if (options?.noAudit) {
return super.deleteById(id, options);
}
const before = await this.findById(id);
await super.deleteById(id, options);

Expand Down
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {AuditLogRepository} from './repositories';
import {Options} from '@loopback/repository';

export const AuditDbSourceName = 'AuditDB';

export interface IAuditMixin<UserID> {
getAuditLogRepository: () => Promise<AuditLogRepository>;
getCurrentUser?: () => Promise<{id?: UserID}>;
Expand All @@ -12,3 +12,7 @@ export interface IAuditMixinOptions {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}
export interface AuditLogOption {
noAudit: boolean;
}
export declare type AuditOptions = Options & AuditLogOption;

0 comments on commit 0da5181

Please sign in to comment.