@magic.batua/points
Advanced tools
Comparing version
@@ -13,2 +13,3 @@ "use strict"; | ||
const Transaction_1 = require("./Source/Transaction"); | ||
exports.Transaction = Transaction_1.Transaction; | ||
/** | ||
@@ -40,2 +41,4 @@ * The Points Ledger keeps track of a user's Magic Points, their status, their availability | ||
constructor(transactions) { | ||
/** Number of points available for redemption */ | ||
this.available = 0; | ||
/** | ||
@@ -51,7 +54,7 @@ * Points earned at signup. Becomes redeemable once the account has spent | ||
this.referral = 0; | ||
/** Points that can be redeemed immediately. */ | ||
/** Number of points accured since the creation of account */ | ||
this.redeemable = 0; | ||
/** Points that have expired. */ | ||
this.expired = 0; | ||
/** Points that have been redeemed */ | ||
/** Points that have been redeemed since the creation of account */ | ||
this.redeemed = 0; | ||
@@ -66,9 +69,3 @@ /** Points that have been refunded */ | ||
// Count all points | ||
let countingResult = this.count(this.transactions); | ||
this.expired = countingResult.expired; | ||
this.redeemable = countingResult.redeemable; | ||
this.redeemed = countingResult.redeemed; | ||
this.referral = countingResult.referral; | ||
this.refunded = countingResult.refunded; | ||
this.signup = countingResult.signup; | ||
this.count(); | ||
} | ||
@@ -81,40 +78,36 @@ /** | ||
*/ | ||
count(transactions) { | ||
let signup = 0, referral = 0, redeemable = 0, expired = 0, redeemed = 0, refunded = 0; | ||
for (var transaction of transactions) { | ||
if (transaction.expiryDate) { | ||
} | ||
count() { | ||
this.signup = 0; | ||
this.referral = 0; | ||
this.expired = 0; | ||
this.redeemable = 0; | ||
this.refunded = 0; | ||
this.redeemed = 0; | ||
for (var transaction of this.transactions) { | ||
switch (transaction.type) { | ||
case "Issue": | ||
if (transaction.expiryDate.valueOf() <= Date.now()) { | ||
expired += transaction.points; | ||
this.expired += transaction.points; | ||
break; | ||
} | ||
if (transaction.notes == "Signup") { | ||
signup += transaction.points; | ||
this.signup += transaction.points; | ||
break; | ||
} | ||
if (transaction.notes == "Referral") { | ||
referral += transaction.points; | ||
this.referral += transaction.points; | ||
break; | ||
} | ||
else { | ||
redeemable += transaction.points; | ||
this.redeemable += transaction.points; | ||
break; | ||
} | ||
case "Redeem": | ||
redeemed += transaction.points; | ||
this.redeemed += transaction.points; | ||
break; | ||
case "Refund": | ||
refunded += transaction.points; | ||
this.refunded += transaction.points; | ||
} | ||
} | ||
return { | ||
signup: signup, | ||
referral: referral, | ||
redeemable: redeemable, | ||
expired: expired, | ||
redeemed: redeemed, | ||
refunded: refunded | ||
}; | ||
this.available = this.redeemable - this.redeemed; | ||
} | ||
@@ -128,12 +121,11 @@ /** Issues a Magic Point for the given `reason` */ | ||
}); | ||
// Add this transaction to the transactions array and count | ||
// the points again | ||
this.transactions.push(transaction); | ||
this.count(); | ||
return transaction; | ||
} | ||
/** Adds a `Transaction` to the ledger */ | ||
Add(transaction) { | ||
this.transactions.push(transaction); | ||
this.count(this.transactions); | ||
} | ||
/** Redeems `points` from the balance */ | ||
Redeem(points, reason) { | ||
if (points > this.redeemable) { | ||
if (points > this.available) { | ||
throw new error_1.ClientError(error_2.Code.BadRequest, "Trying to redeem " + points + " points, but the account has " + this.redeemable + " redeemable points only."); | ||
@@ -146,6 +138,40 @@ } | ||
}); | ||
// Add this transaction to the transactions array and count | ||
// the points again | ||
this.transactions.push(transaction); | ||
this.count(); | ||
return transaction; | ||
} | ||
/** | ||
* This method will be called by the `Account` module when the condition for | ||
* unlocking signup bonus is met. | ||
* To move points from Signup bucket `this.signup` to Redeemable bucket `this.redeemable`, | ||
* we change the `notes` property of signup points issuance from "Signup" to | ||
* "Signup Points become active". | ||
*/ | ||
MakeSignupRedeemable() { | ||
for (var transaction of this.transactions) { | ||
if (transaction.notes == "Signup" && transaction.expiryDate.valueOf() >= Date.now()) { | ||
transaction.notes = "Signup points can be redeemed now."; | ||
} | ||
} | ||
this.count(); | ||
} | ||
/** | ||
* This method will be called by the `Account` module when the condition for | ||
* unlocking signup bonus is met. | ||
* To move points from Referrals bucket `this.signup` to Redeemable bucket `this.redeemable`, | ||
* we change the `notes` property of signup points issuance from "Signup" to | ||
* "Referral points can be redeemed now.". | ||
*/ | ||
MakeReferralsRedeemable() { | ||
for (var transaction of this.transactions) { | ||
if (transaction.notes == "Referral" && transaction.expiryDate.valueOf() >= Date.now()) { | ||
transaction.notes = "Referral points can now be redeemed."; | ||
} | ||
} | ||
this.count(); | ||
} | ||
} | ||
exports.Ledger = Ledger; | ||
//# sourceMappingURL=index.js.map |
@@ -23,5 +23,5 @@ "use strict"; | ||
this.type = json.type; | ||
this.date = json.date || new Date(); | ||
this.date = json.date ? new Date(json.date) : new Date(); | ||
this.notes = json.notes; | ||
this.expiryDate = json.expiryDate || new Date(Date.now() + milliSecondsInAYear); | ||
this.expiryDate = json.expiryDate ? new Date(json.expiryDate) : new Date(Date.now() + milliSecondsInAYear); | ||
} | ||
@@ -28,0 +28,0 @@ } |
@@ -15,15 +15,31 @@ "use strict"; | ||
before(function () { | ||
let transaction = { | ||
points: 600, | ||
type: "Issue", | ||
notes: "Signup" | ||
}; | ||
ledger = new index_1.Ledger([transaction]); | ||
let transactions = [ | ||
{ | ||
points: 600, | ||
type: "Issue", | ||
notes: "Signup" | ||
}, | ||
{ | ||
points: 400, | ||
type: "Issue", | ||
notes: "Referral" | ||
}, | ||
{ | ||
_id: '5aad35c684ec63317d13d7f0', | ||
points: 400, | ||
type: 'Issue', | ||
date: "2014-03-17T15:35:34.648Z", | ||
notes: 'Referral', | ||
expiryDate: "2015-03-17T21:35:34.648Z" | ||
} | ||
]; | ||
ledger = new index_1.Ledger(transactions); | ||
}); | ||
describe("◊◊◊ Points ◊◊◊", function () { | ||
this.timeout(10000); | ||
it("Should issue a Magic Point correctly", async function () { | ||
it("Should initialise ledger correctly", function () { | ||
try { | ||
expect(ledger).to.have.property("signup").equal(600); | ||
console.log(ledger); | ||
expect(ledger).to.have.property("referral").equal(400); | ||
expect(ledger).to.have.property("expired").equal(400); | ||
} | ||
@@ -35,11 +51,6 @@ catch (exception) { | ||
}); | ||
it("Should initialise Ledger correctly", async function () { | ||
it("Should issue a Point correctly", function () { | ||
try { | ||
let issue = ledger.Issue(20, "Tuition Fee"); | ||
ledger.Add(issue); | ||
console.log(ledger); | ||
let redeem = ledger.Redeem(20, "Recharge"); | ||
ledger.Add(redeem); | ||
let newLedger = new index_1.Ledger(ledger.transactions); | ||
console.log(newLedger); | ||
expect(ledger).to.have.property("redeemable").equal(20); | ||
} | ||
@@ -51,14 +62,36 @@ catch (exception) { | ||
}); | ||
// it("Should get User ledger correctly", async function() { | ||
// try { | ||
// let ledger = await manager.GetPointsLedger("testaccount") | ||
// expect(ledger).to.be.instanceof(Ledger) | ||
// // expect(ledger.transactions).to.not.be.empty | ||
// } | ||
// catch (exception) { | ||
// console.log(exception) | ||
// Chai.assert(false) | ||
// } | ||
// }) | ||
it("Should redeem points correctly", function () { | ||
try { | ||
let redeem = ledger.Redeem(8, "Recharge"); | ||
expect(ledger).to.have.property("redeemed").equal(8); | ||
expect(ledger).to.have.property("available").equal(12); | ||
} | ||
catch (exception) { | ||
console.log(exception); | ||
Chai.assert(false); | ||
} | ||
}); | ||
it("Should make signup redeemable.", function () { | ||
try { | ||
ledger.MakeSignupRedeemable(); | ||
expect(ledger).to.have.property("available").equal(612); | ||
expect(ledger).to.have.property("signup").equal(0); | ||
} | ||
catch (exception) { | ||
console.log(exception); | ||
Chai.assert(false); | ||
} | ||
}); | ||
it("Should make referrals redeemable.", function () { | ||
try { | ||
ledger.MakeReferralsRedeemable(); | ||
expect(ledger).to.have.property("available").equal(1012); | ||
expect(ledger).to.have.property("referral").equal(0); | ||
} | ||
catch (exception) { | ||
console.log(exception); | ||
Chai.assert(false); | ||
} | ||
}); | ||
}); | ||
//# sourceMappingURL=index.spec.js.map |
@@ -11,2 +11,3 @@ // | ||
import * as Request from "request-promise-native" | ||
import * as Points from "./Source/Points" | ||
import { ObjectID } from "mongodb" | ||
@@ -16,3 +17,3 @@ import { ExternalError } from "@magic.batua/error" | ||
import { Code } from "@magic.batua/error" | ||
import { Transaction, InitTransaction } from "./Source/Transaction" | ||
import { Transaction, InitTransaction } from "./Source/Transaction" | ||
@@ -180,2 +181,6 @@ /** | ||
} | ||
} | ||
} | ||
export { Transaction } | ||
export { InitTransaction } | ||
export { Points } |
{ | ||
"name": "@magic.batua/points", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "The Points module powers the loyalty points features of the Magic Batua platform.", | ||
@@ -5,0 +5,0 @@ "author": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
32071
19.02%18
5.88%598
18.42%0
-100%