UNPKG

tithe

Version:

Organize and track the tithe payments.

266 lines (225 loc) 7.41 kB
"use strict"; // Dependencies var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Fs = require("fs"), Ul = require("ul"), Event = require("./event"), abs = require("abs"), rJson = require("r-json"), wJson = require("w-json"), noop = require("noop6"); // Constants var DEFAULT_CONFIG = { events: [], currency: "$" }; var Tithe = function () { /** * Tithe * Creates a new `Tithe` instance. * * @name Tithe * @function * @param {String} conf_path The configuration path (default: `~/.tithe.json`). * @return {Tithe} The `Tithe` instance. */ function Tithe(conf_path) { _classCallCheck(this, Tithe); this.path = abs(conf_path || "~/.tithe.json"); } /** * getData * Fetches tithe data. * * @name getData * @function * @param {Object} options An object containing: * * - `paid` (Boolean): If `true`, all events will be fetched. * * @param {Function} callback The callback function. * @return {Tithe} The `Tithe` instance. */ _createClass(Tithe, [{ key: "getData", value: function getData(options, callback) { if (typeof options === "function") { callback = options; options = {}; } var data = { events: [], total: 0, currency: null }, ev = null; this.read(function (err, content) { if (err) { return callback(err); } data.currency = content.currency; content.events.forEach(function (c, id) { ev = new Tithe.Event(c, id); if (ev.paid && !options.paid) { return; } data.events.push(ev); data.total += ev.val; }); callback(null, data); }); return this; } /** * read * Reads the JSON file. * * @name read * @function * @param {Function} callback The callback function. * @return {Tithe} The `Tithe` instance. */ }, { key: "read", value: function read(callback) { rJson(this.path, function (err, content) { if (err && err.code === "ENOENT") { err = null; content = {}; } content = content || {}; if (err) { return callback(err); } callback(null, Ul.merge(content, DEFAULT_CONFIG)); }); return this; } /** * write * Writes the JSON information in the file. * * @name write * @function * @param {Object} content The content as object. * @param {Function} callback The callback function. * @return {Tithe} The `Tithe` instance. */ }, { key: "write", value: function write(content, callback) { wJson(this.path, content, function (err) { return callback(err, content); }); return this; } /** * insert * Inserts a new event. * * @name insert * @function * @param {Object} data An object which will be passed to the `Tithe.Event` constructor. * @param {Function} callback The callback function. * @return {Tithe} The `Tithe` instance. */ }, { key: "insert", value: function insert(data, callback) { var _this = this; callback = callback || noop; if (isNaN(data.val)) { return callback(new Error("Invalid income value.")); } if (data.tithe) { data.val /= 10; delete data.tithe; } this.read(function (err, content) { if (err) { return callback(err); } content.events.push(new Tithe.Event(data)); _this.write(content, callback); }); return this; } /** * pay * Marks all events as paid. * * @name pay * @function * @param {Function} callback The callback function. * @return {Tithe} The `Tithe` instance. */ }, { key: "pay", value: function pay(callback) { var _this2 = this; callback = callback || noop; this.getData(function (err, tData) { if (err) { return callback(err); } _this2.insert({ val: -tData.total, desc: "Tithe payment: " + (-tData.total).toFixed(2) }, function (err, content) { if (err) { return callback(err); } content.events.forEach(function (c) { c.paid = true; }); _this2.write(content, callback); }); }); return this; } /** * setCurrency * Sets the currency. * * @name setCurrency * @function * @param {String} newCurrency The new currency. * @param {Function} callback The callback function. * @return {Tithe} The `Tithe` instance. */ }, { key: "setCurrency", value: function setCurrency(newCurrency, callback) { var _this3 = this; callback = callback || noop; this.read(function (err, content) { if (err) { return callback(err); } content.currency = newCurrency; _this3.write(content, callback); }); return this; } }]); return Tithe; }(); /** * Event * Creates an event instance. * * @name Event * @function * @param {Object} options An object containing: * * - `desc` (String): The event description. * - `val` (Number): The event value. * - `date` (Date|String): The event date as date or parsable string. * - `paid` (Boolean): A flag representing if the value is paid or not. * * @param {String} id An optional id. * @return {Tithe.Event} The `Event` instance. */ Tithe.Event = Event; module.exports = Tithe;