@cocalc/server
Version:
CoCalc server functionality: functions used by either the hub and the next.js server
43 lines (42 loc) • 1.88 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stripe_sync = void 0;
/*
Sync remote stripe view of all users with our local via (in our database).
Should get done eventually mostly via webhooks, etc., -- but for now this is OK.
*/
const awaiting_1 = require("awaiting");
const connection_1 = __importDefault(require("./connection"));
const async_utils_1 = require("@cocalc/util/async-utils");
async function stripe_sync({ logger, database, delay_ms = 500, }) {
if (!delay_ms) {
delay_ms = 100;
}
const dbg = (m) => logger.debug(`stripe_sync: ${m}`);
dbg("get all customers from the database with stripe that have been active in the last month");
const users = (await database.async_query({
query: "SELECT account_id, stripe_customer_id FROM accounts WHERE stripe_customer_id IS NOT NULL AND banned IS NOT TRUE AND deleted IS NOT TRUE AND last_active >= NOW() - INTERVAL '1 MONTH'",
})).rows;
dbg(`got ${users.length} users with stripe info`);
const stripe = await (0, connection_1.default)();
for (const user of users) {
dbg(`updating customer ${user.account_id} data to our local database`);
await (0, async_utils_1.callback2)(database.stripe_update_customer, {
account_id: user.account_id,
customer_id: user.stripe_customer_id,
stripe,
});
// rate limiting
await (0, awaiting_1.delay)(delay_ms);
}
dbg("updated all customer info successfully");
}
exports.stripe_sync = stripe_sync;
//# sourceMappingURL=sync.js.map