Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Commit

Permalink
closes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Jan 5, 2017
1 parent 6e1fb2f commit 15cff97
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 5 deletions.
7 changes: 5 additions & 2 deletions migrations/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ exports.up = (knex) => {
.createTable('errormessage', (table) => {
table.increments('id').primary();
table.string('message');
table.string('foundAt'); // client or server
table.string('stack', 5000);
table.timestamps();
})
.createTable('auditmessage', (table) => {
table.increments('id').primary();
table.string('message');
table.integer('locationId').unsigned().references('location.id');
table.string('terminalId');
table.string('module');
table.jsonb('object');
table.string('message');
table.jsonb('refObject');
table.timestamps();
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/pages/pointofsale/pointofsale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export class PointOfSalePageComponent implements OnInit {
.toPromise()
.then(newInvoice => {

if(!this.settings.canPrint) {
if(!this.settings.canPrint || invoice.purchaseMethod === 'Void') {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions src/server/orm/auditmessage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/* tslint:disable:only-arrow-functions no-invalid-this */

import { bookshelf } from '../server';
import { Location } from './location';

export const AuditMessage = bookshelf.Model.extend({
tableName: 'auditmessage',
hasTimestamps: true,
softDelete: false,
location: function() {
return this.belongsTo(Location, 'locationId');
},
validations: {
}
});
23 changes: 23 additions & 0 deletions src/server/routes/_audit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

import { AuditMessage } from '../orm/auditmessage';

export const AUDIT_CATEGORIES = {
INVOICE: 'Invoice',
INVENTORY: 'Inventory',
LOCATION: 'Location',
OU: 'Category',
PROMOTION: 'Promotion',
REPORT: 'Report',
STOCKITEM: 'StockItem',
SYSTEM: 'System'
};

export const recordAuditMessage = (req, module, message, refObject?) => {
const insertRecord: any = { module, message, refObject };
insertRecord.locationId = +req.header('X-Location');
insertRecord.terminalId = req.header('X-Terminal');

AuditMessage
.forge(insertRecord)
.save();
};
4 changes: 4 additions & 0 deletions src/server/routes/inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { OrganizationalUnit } from '../orm/organizationalunit';
import { OrganizationalUnit as OrganizationalUnitModel } from '../../client/models/organizationalunit';
import { Logger } from '../logger';

import { recordAuditMessage, AUDIT_CATEGORIES } from './_audit';

const getColumnsAndRelated = (columns) => {
const withRelated = [];

Expand All @@ -29,6 +31,7 @@ export default (app) => {
.collection()
.fetch({ columns, withRelated })
.then(collection => {
recordAuditMessage(req, AUDIT_CATEGORIES.INVENTORY, `All inventory was exported.`);
res.json(collection.toJSON());
})
.catch(e => {
Expand Down Expand Up @@ -79,6 +82,7 @@ export default (app) => {
.all(insertPromises)
.then(t.commit, t.rollback)
.then(() => {
recordAuditMessage(req, AUDIT_CATEGORIES.INVENTORY, `New inventory was imported.`);
res.json({ flash: `Import successful.`, data: item });
})
.catch(errorHandler);
Expand Down
8 changes: 8 additions & 0 deletions src/server/routes/invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { InvoicePromo as InvoicePromoModel } from '../../client/models/invoicepr
import { Logger } from '../logger';
import Settings from './_settings';

import { recordAuditMessage, AUDIT_CATEGORIES } from './_audit';

const dateFunctions = require('date-fns');
const thermalPrinter = require('node-thermal-printer');

Expand Down Expand Up @@ -152,6 +154,7 @@ export default (app) => {
.all(itemPromises(item).concat(promoPromises(item)).concat(otherPromises))
.then(t.commit, t.rollback)
.then(() => {
recordAuditMessage(req, AUDIT_CATEGORIES.INVOICE, `A new ${invoice.purchaseMethod} invoice was created for ${(+invoice.purchasePrice).toFixed(2)}.`, { id: item.id });
res.json({ flash: `Transaction completed successfully.`, data: item });
})
.catch(errorHandler);
Expand Down Expand Up @@ -238,9 +241,13 @@ export default (app) => {
const inventoryHash = itemsFromInvoiceToStockable(items);
let inventoryPromises = [];

let type = '';

if(unwrappedItem.isVoided) {
type = 'unvoided';
inventoryPromises = incrementItems(inventoryHash);
} else {
type = 'voided';
inventoryPromises = decrementItems(inventoryHash);
}

Expand All @@ -257,6 +264,7 @@ export default (app) => {
Promise.all([savePromise].concat(inventoryPromises))
.then(resolvedPromises => {
// [0] is the saved item
recordAuditMessage(req, AUDIT_CATEGORIES.INVOICE, `An invoice was ${type}.`, { id: +req.params.id });
res.json(resolvedPromises[0]);
});
})
Expand Down
4 changes: 4 additions & 0 deletions src/server/routes/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Location } from '../orm/location';
import { Logger } from '../logger';

import { Location as LocationModel } from '../../client/models/location';
import { recordAuditMessage, AUDIT_CATEGORIES } from './_audit';

export default (app) => {
app.get('/location', (req, res) => {
Expand All @@ -25,6 +26,7 @@ export default (app) => {
.forge(loca)
.save()
.then(item => {
recordAuditMessage(req, AUDIT_CATEGORIES.LOCATION, `A new location was added (${loca.name}).`, { id: item.id });
res.json(item);
})
.catch(e => {
Expand All @@ -39,6 +41,7 @@ export default (app) => {
.forge({ id: req.params.id })
.save(loca, { patch: true })
.then(item => {
recordAuditMessage(req, AUDIT_CATEGORIES.LOCATION, `A location was changed (${loca.name}).`, { id: item.id });
res.json(item);
})
.catch(e => {
Expand All @@ -56,6 +59,7 @@ export default (app) => {
.forge({ id: req.params.id })
.destroy()
.then(item => {
recordAuditMessage(req, AUDIT_CATEGORIES.LOCATION, `A location was removed.`, { id: +req.params.id, oldId: +req.params.id });
res.json(item);
})
.catch(e => {
Expand Down
4 changes: 4 additions & 0 deletions src/server/routes/organizationalunit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { OrganizationalUnit } from '../orm/organizationalunit';
import { Logger } from '../logger';

import { OrganizationalUnit as OrganizationalUnitModel } from '../../client/models/organizationalunit';
import { recordAuditMessage, AUDIT_CATEGORIES } from './_audit';

export default (app) => {
app.get('/organizationalunit', (req, res) => {
Expand All @@ -25,6 +26,7 @@ export default (app) => {
.forge(ou)
.save()
.then(item => {
recordAuditMessage(req, AUDIT_CATEGORIES.OU, `A category was added (${ou.name}).`, { id: item.id });
res.json(item);
})
.catch(e => {
Expand All @@ -39,6 +41,7 @@ export default (app) => {
.forge({ id: req.params.id })
.save(ou, { patch: true })
.then(item => {
recordAuditMessage(req, AUDIT_CATEGORIES.OU, `A category was changed (${ou.name}).`, { id: item.id });
res.json(item);
})
.catch(e => {
Expand All @@ -56,6 +59,7 @@ export default (app) => {
.forge({ id: req.params.id })
.destroy()
.then(item => {
recordAuditMessage(req, AUDIT_CATEGORIES.OU, `A category was removed.`, { id: +req.params.id, oldId: +req.params.id });
res.json(item);
})
.catch(e => {
Expand Down
12 changes: 10 additions & 2 deletions src/server/routes/promotion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { StockItem as StockItemModel } from '../../client/models/stockitem';

import { Logger } from '../logger';
import Settings from './_settings';
import { recordAuditMessage, AUDIT_CATEGORIES } from './_audit';

const calculatePromotionDiscount = (promo: PromotionModel, validItems: StockItemModel[]) => {

Expand Down Expand Up @@ -143,6 +144,7 @@ export default (app) => {
}))
.then(t.commit, t.rollback)
.then(() => {
recordAuditMessage(req, AUDIT_CATEGORIES.PROMOTION, `A new promotion was added (${newPromo.name}).`, { id: newPromo.id });
res.json({ flash: `Created new promotion successfully`, data: newPromo });
})
.catch(e => {
Expand All @@ -159,12 +161,16 @@ export default (app) => {
const { promo, item } = req.body;
const { discount, applyId } = calculatePromotionDiscount(promo, [item]);

res.json({
const tempPromo = {
promo: new PromotionModel(promo),
skus: [item.sku],
totalDiscount: -discount,
applyId
});
};

recordAuditMessage(req, AUDIT_CATEGORIES.PROMOTION, `A temporary promotion was added (${tempPromo.promo.name}).`, { id: item.id, item: tempPromo });

res.json(tempPromo);

});

Expand Down Expand Up @@ -256,6 +262,7 @@ export default (app) => {
}))
.then(t.commit, t.rollback)
.then(() => {
recordAuditMessage(req, AUDIT_CATEGORIES.PROMOTION, `A promotion was changed (${promo.name}).`, { id: +req.params.id });
res.json({ flash: `Updated promotion "${promo.name}"`, data: item });
});
})
Expand Down Expand Up @@ -285,6 +292,7 @@ export default (app) => {
.destroy({ transacting: t })
.then(t.commit, t.rollback)
.then(() => {
recordAuditMessage(req, AUDIT_CATEGORIES.PROMOTION, `A promotion was removed.`, { id: +req.params.id, oldId: +req.params.id });
res.json({ flash: `Removed promotion successfully.` });
});
})
Expand Down
9 changes: 9 additions & 0 deletions src/server/routes/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ReportConfiguration } from '../orm/reportconfiguration';

import { ReportConfiguration as ReportConfigurationModel } from '../../client/models/reportconfiguration';
import { Logger } from '../logger';
import { recordAuditMessage, AUDIT_CATEGORIES } from './_audit';

const getColumnsAndRelated = (columns) => {
const withRelated = [];
Expand Down Expand Up @@ -74,6 +75,7 @@ export default (app) => {
if(!items.length) {
resObj.flash = 'No data matched your query.';
}
recordAuditMessage(req, AUDIT_CATEGORIES.REPORT, `A current inventory report was run.`);
res.json(resObj);
})
.catch(e => {
Expand Down Expand Up @@ -119,6 +121,7 @@ export default (app) => {
if(!data.length) {
resObj.flash = 'No data matched your query.';
}
recordAuditMessage(req, AUDIT_CATEGORIES.REPORT, `An old inventory report was run.`);
res.json(resObj);
})
.catch(e => {
Expand Down Expand Up @@ -156,6 +159,7 @@ export default (app) => {
if(!data.length) {
resObj.flash = 'No data matched your query.';
}
recordAuditMessage(req, AUDIT_CATEGORIES.REPORT, `A reorder inventory report was run.`);
res.json(resObj);
})
.catch(e => {
Expand Down Expand Up @@ -198,6 +202,7 @@ export default (app) => {
if(resObj.promotions) {
resObj.promotions = { length: resObj.promotions.length };
}
recordAuditMessage(req, AUDIT_CATEGORIES.REPORT, `A completed sales report was run.`);
res.json(resObj);
})
.catch(e => {
Expand Down Expand Up @@ -243,6 +248,7 @@ export default (app) => {
if(resObj.promotions) {
resObj.promotions = { length: resObj.promotions.length };
}
recordAuditMessage(req, AUDIT_CATEGORIES.REPORT, `A voided sales report was run.`);
res.json(resObj);
})
.catch(e => {
Expand Down Expand Up @@ -272,6 +278,7 @@ export default (app) => {
.forge()
.save(req.body)
.then(newReport => {
recordAuditMessage(req, AUDIT_CATEGORIES.REPORT, `A new report configuration was created (${newReport.name}).`, { id: newReport.id });
res.json({ flash: `Created new report successfully`, data: newReport });
})
.catch(e => {
Expand All @@ -287,6 +294,7 @@ export default (app) => {
.forge({ id: req.params.id })
.save(req.body, { patch: true })
.then(newReport => {
recordAuditMessage(req, AUDIT_CATEGORIES.REPORT, `A report configuration was updated (${newReport.name}).`, { id: newReport.id });
res.json({ flash: `Updated report successfully`, data: newReport });
})
.catch(e => {
Expand All @@ -300,6 +308,7 @@ export default (app) => {
.forge({ id: req.params.id })
.destroy()
.then(item => {
recordAuditMessage(req, AUDIT_CATEGORIES.REPORT, `A report configuration was removed.`, { id: +req.params.id, oldId: +req.params.id });
res.json(item);
})
.catch(e => {
Expand Down
6 changes: 6 additions & 0 deletions src/server/routes/stockitem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { StockItem as StockItemModel } from '../../client/models/stockitem';

import { Logger } from '../logger';
import Settings from './_settings';
import { recordAuditMessage, AUDIT_CATEGORIES } from './_audit';

const cleanItem = (item) => {
item.cost = +item.cost;
Expand Down Expand Up @@ -77,6 +78,7 @@ export default (app) => {
.increment('quantity', v);
}))
.then(() => {
recordAuditMessage(req, AUDIT_CATEGORIES.STOCKITEM, `A stockitem import has completed (${_.keys(req.body).length} items, ${numItems} total).`, { items: req.body });
res.json({ flash: `Updated quantities for ${_.keys(req.body).length} stock items (${numItems} total imported)` });
})
.catch(e => {
Expand All @@ -95,6 +97,7 @@ export default (app) => {
.decrement('quantity', v);
}))
.then(() => {
recordAuditMessage(req, AUDIT_CATEGORIES.STOCKITEM, `A stockitem export has completed (${_.keys(req.body).length} items, ${numItems} total).`, { items: req.body });
res.json({ flash: `Updated quantities for ${_.keys(req.body).length} stock items (${numItems} total exported)` });
})
.catch(e => {
Expand All @@ -119,6 +122,7 @@ export default (app) => {
}))
.then(t.commit, t.rollback)
.then(() => {
recordAuditMessage(req, AUDIT_CATEGORIES.STOCKITEM, `A new stockitem was created (${newItem.name}).`, { id: newItem.id });
res.json({ flash: `Created new item successfully`, data: newItem });
})
.catch(e => {
Expand Down Expand Up @@ -172,6 +176,7 @@ export default (app) => {
}))
.then(t.commit, t.rollback)
.then(() => {
recordAuditMessage(req, AUDIT_CATEGORIES.STOCKITEM, `A stockitem was updated (${realItem.name}).`, { id: realItem.id });
res.json({ flash: `Updated item "${realItem.name}"`, data: realItem });
});
})
Expand All @@ -192,6 +197,7 @@ export default (app) => {
.destroy()
.then(item => {
item = item.toJSON();
recordAuditMessage(req, AUDIT_CATEGORIES.STOCKITEM, `A stockitem was removed.`, { id: +req.params.id, oldId: +req.params.id });
res.json({ flash: `Removed item successfully.`, data: item });
})
.catch(e => {
Expand Down
2 changes: 2 additions & 0 deletions src/server/routes/system.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import * as _ from 'lodash';
import { readSettings, writeSettings } from './_settings';
import { recordAuditMessage, AUDIT_CATEGORIES } from './_audit';

const nodePrinter = require('printer');

Expand All @@ -27,6 +28,7 @@ export default (app) => {
}

writeSettings(JSON.stringify(req.body, null, 4), () => {
recordAuditMessage(req, AUDIT_CATEGORIES.SYSTEM, `System settings were updated.`, { settings: req.body });
res.json({ flash: 'Settings updated successfully.', data: req.body });
});
});
Expand Down

0 comments on commit 15cff97

Please sign in to comment.