Skip to content

Commit

Permalink
Update .env.sample (#343)
Browse files Browse the repository at this point in the history
* Update .env.sample

* Reset billing period for a new workspace

* now change plan resets lastChargeDate and billingPeriodEventsCount

* Create 20210805130054-add-lastChargeDate.js
  • Loading branch information
talyguryn authored Aug 5, 2021
1 parent e97ae5e commit 31d62bd
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ SMTP_SENDER_NAME=
SMTP_SENDER_ADDRESS=

# AMQP URL
AMQP_URL=
AMQP_URL=amqp://guest:guest@rabbitmq

# Billing settings
BILLING_DEBUG=true
Expand Down Expand Up @@ -93,7 +93,7 @@ TELEGRAM_MAIN_CHAT_URL=
TELEGRAM_MONEY_CHAT_URL=

# Cloudpayments public id
CLOUDPAYMENTS_PUBLIC_ID=
CLOUDPAYMENTS_PUBLIC_ID=test_api_00000000000000000000001

# Cloudpayments secret string
CLOUDPAYMENTS_SECRET=
Expand Down
134 changes: 134 additions & 0 deletions migrations/20210805130054-add-lastChargeDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* Get date for the current day without hours, minutes and seconds
* @return {Date}
*/
const getCurrentDay = () => {
const thisDay = new Date();

thisDay.setHours(0);
thisDay.setMinutes(0);
thisDay.setSeconds(0);
thisDay.setMilliseconds(0);

return thisDay;
};

/**
* @file Add missing lastChargeDate field for workspaces
*/
module.exports = {
async up(db, client) {
/**
* Use one transaction for all requests
*/
const session = client.startSession();

console.log('Add missing lastChargeDate field for workspaces...');

try {
await session.withTransaction(async () => {
/**
* Get all workspaces
*/
const workspaces = await db.collection('workspaces').find()
.toArray();

/**
* Each workspace should have a lastChargeDate param
*/
await Promise.all(workspaces.map(async workspace => {
const workspaceId = workspace._id;

/**
* Skip workspaces, which already have plan
*/
if (workspace.lastChargeDate) {
console.log(`Workspace ${workspaceId} already have lastChargeDate. Skipped.`);

return Promise.resolve();
}

/**
* Set up lastChargeDate to current Date
*/
const result = await db.collection('workspaces').findOneAndUpdate(
{
_id: workspace._id,
},
{
$set: {
lastChargeDate: getCurrentDay(),
},
}
);

if (result.ok === 1) {
console.log(`Workspace ${workspaceId} updated`);
} else {
console.log(`Workspace ${workspaceId} failed updating`);
}

return result;
}));
});
} finally {
await session.endSession();
}

return true;
},

async down(db, client) {
/**
* Use one transaction for all requests
*/
const session = client.startSession();

console.log('Start to rollback adding lastChargeDate for existing workspaces...');

try {
await session.withTransaction(async () => {
/**
* Find workspaces with a 0 billingPeriodEventsCount
*/
const workspaces = await db.collection('workspaces').find({
billingPeriodEventsCount: {
$eq: getCurrentDay(),
},
})
.toArray();

/**
* Remove lastChargeDate field from a few workspaces
*/
await Promise.all(workspaces.map(async workspace => {
const workspaceId = workspace._id;

/**
* Remove lastChargeDate
*/
const result = await db.collection('workspaces').findOneAndUpdate(
{
_id: workspace._id,
},
{
$unset: {
lastChargeDate: '',
},
}
);

if (result.ok === 1) {
console.log(`Workspace ${workspaceId} now has no lastChargeDate field`);
} else {
console.log(`Workspace ${workspaceId} failed to remove lastChargeDate field`);
}

return result;
}));
});
} finally {
await session.endSession();
}
},
};
7 changes: 6 additions & 1 deletion src/models/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export default class WorkspaceModel extends AbstractModel<WorkspaceDBScheme> imp

/**
* Change plan for current workspace
* Reset billing period events count, reset last charge date
*
* @param planId - id of plan to be enabled
*/
Expand All @@ -296,7 +297,11 @@ export default class WorkspaceModel extends AbstractModel<WorkspaceDBScheme> imp
_id: new ObjectId(this._id),
},
{
$set: { tariffPlanId: new ObjectId(planId) },
$set: {
tariffPlanId: new ObjectId(planId),
billingPeriodEventsCount: 0,
lastChargeDate: new Date(),
},
}
)).modifiedCount;
}
Expand Down

0 comments on commit 31d62bd

Please sign in to comment.