Skip to content

Commit

Permalink
fix: user id in billing & nextCycle detection
Browse files Browse the repository at this point in the history
  • Loading branch information
AVVS committed Feb 25, 2016
1 parent e738273 commit 5d1570a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/actions/agreement/bill.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ const { PLANS_DATA, AGREEMENT_DATA, FREE_PLAN_ID } = require('../../constants.js
// check agreement bill
function agreementBill(input) {
const { agreement: id, subscriptionInterval, username } = input;
const { _config, redis, amqp } = this;
const { _config, redis, amqp, log } = this;
const { users: { prefix, postfix } } = _config;
const start = moment().subtract(2, subscriptionInterval).format('YYYY-MM-DD');
const end = moment().add(1, 'day').format('YYYY-MM-DD');

log.debug('billing %s on %s', username, id);

// pull agreement data
function getAgreement() {
if (id === FREE_PLAN_ID) {
Expand Down Expand Up @@ -77,7 +79,7 @@ function agreementBill(input) {

// bill next free cycle
function billNextFreeCycle(data) {
const nextCycle = moment(input.currentCycle);
const nextCycle = moment(input.nextCycle);
const current = moment();

// 0 or 1
Expand All @@ -96,8 +98,8 @@ function agreementBill(input) {

function billPaidCycle(data) {
// agreement nextCycle date
const nextCycle = moment(data.details.agreement.agreement_details.next_billing_date);
const currentCycle = moment(input.currentCycle).subtract(1, 'day');
const nextCycle = moment(data.details.agreement.agreement_details.next_billing_date || input.nextCycle);
const currentCycle = moment(input.nextCycle).subtract(1, 'day');
const { transactions } = data.details;

// determine how many cycles and next billing date
Expand Down
6 changes: 3 additions & 3 deletions src/actions/agreement/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function agreementSync(message) {
const { users, cursor, page, pages } = response;

users.forEach(user => {
pulledUsers.add(user.username);
pulledUsers.add(user.id);
pool.push(user);
});

Expand Down Expand Up @@ -84,7 +84,7 @@ function agreementSync(message) {
return amqp
.publishAndWait(getMetadata, request, { timeout: 10000 })
.then(metadata => {
pool.push({ username, metadata });
pool.push({ id: username, metadata });
});
});
});
Expand All @@ -93,7 +93,7 @@ function agreementSync(message) {
// 3. bill users
const billUser = user => {
const meta = user.metadata[audience];
return bill.call(this, { ...meta, username: user.username });
return bill.call(this, { ...meta, username: user.id });
};

return getUsers()
Expand Down

0 comments on commit 5d1570a

Please sign in to comment.