Skip to content

Commit

Permalink
Rename package
Browse files Browse the repository at this point in the history
  • Loading branch information
ahtinurme authored and ahtinurme committed Mar 26, 2020
1 parent b06d051 commit 5ab1aac
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 132 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
# adonis-auditable
# adonis-audit
Audit models in AdonisJS

## DISCLAIMER

This is just a release for using it through NPM. Not claiming any rights over the original code developed.

## How to use

Install npm module:

```bash
$ adonis install adonis-auditable
$ adonis install adonis-audit
```

## Register provider

Once you have installed adonis-auditable, make sure to register the provider inside `start/app.js` in order to make use of it.
Once you have installed adonis-audit, make sure to register the provider inside `start/app.js` in order to make use of it.

```js
const providers = [
'adonis-auditable/providers/AuditableProvider'
'adonis-audit/providers/AuditProvider'
]
```

Expand Down
40 changes: 20 additions & 20 deletions instructions.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
'use strict'
"use strict";

const path = require('path')
const path = require("path");

async function copyAuditMigration (cli) {
try {
const migrationsFile = cli.helpers.migrationsPath(`${new Date().getTime()}_audit.js`)
const migrationsFile = cli.helpers.migrationsPath(
`${new Date().getTime()}_audit.js`);
await cli.copy(
path.join(__dirname, 'templates', 'AuditSchema.js'),
path.join(migrationsFile)
)
cli.command.completed('create', migrationsFile.replace(cli.helpers.appRoot(), '').replace(path.sep, ''))
}
catch (error) {
console.log(error)
path.join(__dirname, "templates", "AuditSchema.js"),
path.join(migrationsFile),
);
cli.command.completed("create",
migrationsFile.replace(cli.helpers.appRoot(), "").replace(path.sep, ""));
} catch (error) {
console.log(error);
}
}

async function copyAuditModel (cli) {
try {
await cli.copy(
path.join(__dirname, 'templates', 'Audit.js'),
path.join(cli.appDir, 'Models/Audit.js')
)
cli.command.completed('create', 'Models/Audit.js')
}
catch (error) {
console.log(error)
path.join(__dirname, "templates", "Audit.js"),
path.join(cli.appDir, "Models/Audit.js"),
);
cli.command.completed("create", "Models/Audit.js");
} catch (error) {
console.log(error);
}
}

module.exports = async (cli) => {
await copyAuditModel(cli)
await copyAuditMigration(cli)
}
await copyAuditModel(cli);
await copyAuditMigration(cli);
};
2 changes: 1 addition & 1 deletion instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Start by registering the provider inside `start/app.js` file.

```js
const providers = [
'adonis-auditable/providers/AuditableProvider'
'adonis-audit/providers/AuditProvider'
]
```

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "adonis-auditable",
"name": "adonis-audit",
"version": "2.0.1",
"description": "Audit AdonisJS models",
"main": " ",
Expand All @@ -8,7 +8,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/simontong/adonis-auditable.git"
"url": "git+https://github.com/ahtinurme/adonis-audit.git"
},
"keywords": [
"adonis",
Expand All @@ -18,9 +18,9 @@
"author": "Simon Tong <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/simontong/adonis-auditable/issues"
"url": "https://github.com/ahtinurme/adonis-audit/issues"
},
"homepage": "https://github.com/simontong/adonis-auditable#readme",
"homepage": "https://github.com/ahtinurme/adonis-audit#readme",
"dependencies": {
"lodash": "^4.17.4"
}
Expand Down
24 changes: 24 additions & 0 deletions providers/AuditProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { ServiceProvider } = require("@adonisjs/fold");

class AuditProvider extends ServiceProvider {
register () {
this.app.singleton("Adonis/Traits/Auditable", () => {
// const Config = this.app.use('Adonis/Src/Config')
const Auditable = require("../src/Traits/Auditable");
return new Auditable();
});
this.app.alias("Adonis/Traits/Auditable", "Auditable");
}

boot () {
const Context = this.app.use("Adonis/Src/HttpContext");
const Auditable = this.app.use("Auditable");

// add ctx to datagrid
Context.onReady(ctx => {
Auditable.ctx = ctx;
});
}
}

module.exports = AuditProvider;
24 changes: 0 additions & 24 deletions providers/AuditableProvider.js

This file was deleted.

106 changes: 55 additions & 51 deletions src/Traits/Auditable.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
'use strict'
"use strict";

const _ = require('lodash')
const Audit = use('App/Models/Audit')
const _ = require("lodash");
const Audit = use("App/Models/Audit");

class Auditable {
register (Model) {
// create methods
const self = this
const self = this;
Model.audit = function () {
return {
create: createWithAudit(self.ctx).bind(this)
}
}
create: createWithAudit(self.ctx).bind(this),
};
};

// update/delete methods
Model.prototype.audit = function () {
return {
update: updateWithAudit(self.ctx).bind(this),
delete: deleteWithAudit(self.ctx).bind(this)
}
}
delete: deleteWithAudit(self.ctx).bind(this),
};
};
}
}

Expand All @@ -30,20 +30,21 @@ class Auditable {
* @param request
* @returns {*}
*/
function createWithAudit ({request, auth}) {
function createWithAudit ({ request, auth }) {
return async function (data) {
const model = await this.create(data)
const newModel = (await this.find(model.primaryKeyValue))
const auditable = newModel.constructor.name
const auditableId = newModel.id
const newData = newModel.$attributes
const event = Audit.events.CREATE
const model = await this.create(data);
const newModel = (await this.find(model.primaryKeyValue));
const auditable = newModel.constructor.name;
const auditableId = newModel.id;
const newData = newModel.$attributes;
const event = Audit.events.CREATE;

// save audit
await createAudit(event, {request, auth}, auditable, auditableId, null, newData)
await createAudit(event, { request, auth }, auditable, auditableId, null,
newData);

return model
}
return model;
};
}

/**
Expand All @@ -53,33 +54,34 @@ function createWithAudit ({request, auth}) {
* @param request
* @returns {*}
*/
function updateWithAudit ({request, auth}) {
return async function (data, ignoreDiff = ['updated_at']) {
const auditable = this.constructor.name
const auditableId = this.id
const oldData = this.$originalAttributes
this.merge(data)
const result = await this.save()
const newModel = (await this.constructor.find(this.primaryKeyValue))
const newData = newModel.$attributes
function updateWithAudit ({ request, auth }) {
return async function (data, ignoreDiff = ["updated_at"]) {
const auditable = this.constructor.name;
const auditableId = this.id;
const oldData = this.$originalAttributes;
this.merge(data);
const result = await this.save();
const newModel = (await this.constructor.find(this.primaryKeyValue));
const newData = newModel.$attributes;

// if new and old are equal then don't bother updating
const isEqual = _.isEqual(
_.omit(newData, ignoreDiff),
_.omit(oldData, ignoreDiff)
)
_.omit(oldData, ignoreDiff),
);
if (isEqual) {
return result
return result;
}

// update / patch are shared
const event = Audit.events.UPDATE
const event = Audit.events.UPDATE;

// save audit
await createAudit(event, {request, auth}, auditable, auditableId, oldData, newData)
await createAudit(event, { request, auth }, auditable, auditableId, oldData,
newData);

return result
}
return result;
};
}

/**
Expand All @@ -89,18 +91,19 @@ function updateWithAudit ({request, auth}) {
* @param request
* @returns {*}
*/
function deleteWithAudit ({request, auth}) {
function deleteWithAudit ({ request, auth }) {
return async function () {
const auditable = this.constructor.name
const auditableId = this.id
const oldData = this.$originalAttributes
const result = await this.delete()
const auditable = this.constructor.name;
const auditableId = this.id;
const oldData = this.$originalAttributes;
const result = await this.delete();

// save audit
await createAudit(Audit.events.DELETE, {request, auth}, auditable, auditableId, oldData)
await createAudit(Audit.events.DELETE, { request, auth }, auditable,
auditableId, oldData);

return result
}
return result;
};
}

/**
Expand All @@ -115,16 +118,17 @@ function deleteWithAudit ({request, auth}) {
* @param request
* @returns {Promise<void>}
*/
async function createAudit (event, {request, auth}, auditable, auditableId, oldData, newData) {
async function createAudit (
event, { request, auth }, auditable, auditableId, oldData, newData) {
// check request was passed
if (!request) {
throw new Error('Request param is empty')
throw new Error("Request param is empty");
}

// get user data to store
const userId = _.get(auth, 'user.id', null)
const url = request.originalUrl()
const ip = request.ip()
const userId = _.get(auth, "user.id", null);
const url = request.originalUrl();
const ip = request.ip();

// save audit
await Audit.create({
Expand All @@ -136,7 +140,7 @@ async function createAudit (event, {request, auth}, auditable, auditableId, oldD
ip,
old_data: oldData,
new_data: newData,
})
});
}

module.exports = Auditable
module.exports = Auditable;
Loading

0 comments on commit 5ab1aac

Please sign in to comment.