mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-10-24 02:07:00 +08:00
feat(api): decouple api from curriculum (#40703)
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
import { createActiveUsers } from '../utils/about.js';
|
||||
|
||||
module.exports = function(About) {
|
||||
const activeUsers = createActiveUsers();
|
||||
let activeUsersForRendering = 0;
|
||||
About.getActiveUsers = async function getActiveUsers() {
|
||||
// converting to promise automatically will subscribe to Observable
|
||||
// initiating the sequence above
|
||||
const usersActive = await activeUsers.toPromise();
|
||||
activeUsersForRendering = usersActive;
|
||||
return usersActive;
|
||||
};
|
||||
|
||||
About.getActiveUsersForRendering = () => activeUsersForRendering;
|
||||
|
||||
About.remoteMethod('getActiveUsers', {
|
||||
http: {
|
||||
path: '/get-active-users',
|
||||
verb: 'get'
|
||||
},
|
||||
returns: {
|
||||
type: 'number',
|
||||
arg: 'activeUsers'
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -1,28 +0,0 @@
|
||||
{
|
||||
"name": "about",
|
||||
"plural": "about",
|
||||
"base": "PersistedModel",
|
||||
"idInjection": true,
|
||||
"options": {
|
||||
"validateUpsert": true
|
||||
},
|
||||
"properties": {},
|
||||
"validations": [],
|
||||
"relations": {},
|
||||
"acls": [
|
||||
{
|
||||
"accessType": "*",
|
||||
"principalType": "ROLE",
|
||||
"principalId": "$everyone",
|
||||
"permission": "DENY"
|
||||
},
|
||||
{
|
||||
"accessType": "EXECUTE",
|
||||
"principalType": "ROLE",
|
||||
"principalId": "$everyone",
|
||||
"permission": "ALLOW",
|
||||
"property": "getActiveUsers"
|
||||
}
|
||||
],
|
||||
"methods": {}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { Observable } from 'rx';
|
||||
|
||||
export default function(AuthToken) {
|
||||
AuthToken.on('dataSourceAttached', () => {
|
||||
AuthToken.findOne$ = Observable.fromNodeCallback(
|
||||
AuthToken.findOne.bind(AuthToken)
|
||||
);
|
||||
AuthToken.prototype.validate$ = Observable.fromNodeCallback(
|
||||
AuthToken.prototype.validate
|
||||
);
|
||||
AuthToken.prototype.destroy$ = Observable.fromNodeCallback(
|
||||
AuthToken.prototype.destroy
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"name": "AuthToken",
|
||||
"base": "AccessToken",
|
||||
"idInjection": true,
|
||||
"options": {
|
||||
"validateUpsert": true
|
||||
},
|
||||
"properties": {},
|
||||
"validations": [],
|
||||
"relations": {},
|
||||
"acls": [],
|
||||
"methods": {}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
import { Observable } from 'rx';
|
||||
import debug from 'debug';
|
||||
|
||||
import { reportError } from '../middlewares/sentry-error-handler.js';
|
||||
import InMemoryCache from '../utils/in-memory-cache';
|
||||
|
||||
const log = debug('fcc:boot:donate');
|
||||
const fiveMinutes = 1000 * 60 * 5;
|
||||
|
||||
export default function(Donation) {
|
||||
let activeDonationUpdateInterval = null;
|
||||
const activeDonationCountCacheTTL = fiveMinutes;
|
||||
const activeDonationCountCache = InMemoryCache(0, reportError);
|
||||
const activeDonationsQuery$ = () =>
|
||||
Donation.find$({
|
||||
// eslint-disable-next-line no-undefined
|
||||
where: { endDate: undefined }
|
||||
}).map(instances => instances.length);
|
||||
function cleanUp() {
|
||||
if (activeDonationUpdateInterval) {
|
||||
clearInterval(activeDonationUpdateInterval);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
process.on('exit', cleanUp);
|
||||
|
||||
Donation.on('dataSourceAttached', () => {
|
||||
Donation.find$ = Observable.fromNodeCallback(Donation.find.bind(Donation));
|
||||
Donation.findOne$ = Observable.fromNodeCallback(
|
||||
Donation.findOne.bind(Donation)
|
||||
);
|
||||
|
||||
seedTheCache()
|
||||
.then(setupCacheUpdateInterval)
|
||||
.catch(err => {
|
||||
const errMsg = `Error caught seeding the cache: ${err.message}`;
|
||||
err.message = errMsg;
|
||||
reportError(err);
|
||||
});
|
||||
});
|
||||
|
||||
function seedTheCache() {
|
||||
return new Promise((resolve, reject) =>
|
||||
Observable.defer(activeDonationsQuery$).subscribe(count => {
|
||||
log('activeDonor count: %d', count);
|
||||
activeDonationCountCache.update(() => count);
|
||||
return resolve();
|
||||
}, reject)
|
||||
);
|
||||
}
|
||||
|
||||
function setupCacheUpdateInterval() {
|
||||
activeDonationUpdateInterval = setInterval(
|
||||
() =>
|
||||
Observable.defer(activeDonationsQuery$).subscribe(
|
||||
count => {
|
||||
log('activeDonor count: %d', count);
|
||||
return activeDonationCountCache.update(() => count);
|
||||
},
|
||||
err => {
|
||||
const errMsg = `Error caught updating the cache: ${err.message}`;
|
||||
err.message = errMsg;
|
||||
reportError(err);
|
||||
}
|
||||
),
|
||||
activeDonationCountCacheTTL
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
function getCurrentActiveDonationCount$() {
|
||||
return Observable.of(activeDonationCountCache.get());
|
||||
}
|
||||
|
||||
Donation.getCurrentActiveDonationCount$ = getCurrentActiveDonationCount$;
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
{
|
||||
"name": "Donation",
|
||||
"description": "A representation of a donation to freeCodeCamp",
|
||||
"plural": "donations",
|
||||
"base": "PersistedModel",
|
||||
"idInjection": true,
|
||||
"scopes": {},
|
||||
"indexes": {},
|
||||
"options": {
|
||||
"validateUpsert": true
|
||||
},
|
||||
"hidden": [],
|
||||
"remoting": {},
|
||||
"http": {},
|
||||
"properties": {
|
||||
"email": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"description": "The email used to create the donation"
|
||||
},
|
||||
"provider": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"description": "The payment handler, paypal/stripe etc..."
|
||||
},
|
||||
"amount": {
|
||||
"type": "number",
|
||||
"required": true,
|
||||
"description": "The donation amount in cents"
|
||||
},
|
||||
"duration": {
|
||||
"type": "string"
|
||||
},
|
||||
"startDate": {
|
||||
"type": "DateString",
|
||||
"required": true
|
||||
},
|
||||
"endDate": {
|
||||
"type": "DateString"
|
||||
},
|
||||
"subscriptionId": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"description": "The donation subscription id returned from the provider"
|
||||
},
|
||||
"customerId": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"description": "The providers reference for the donor"
|
||||
}
|
||||
},
|
||||
"validations": [
|
||||
{
|
||||
"amount": {
|
||||
"type": "number",
|
||||
"description": "Amount should be >= $1 (100c)",
|
||||
"min": 100
|
||||
},
|
||||
"facetName": "server"
|
||||
}
|
||||
],
|
||||
"relations": {
|
||||
"user": {
|
||||
"type": "belongsTo",
|
||||
"model": "user",
|
||||
"foreignKey": "userId"
|
||||
}
|
||||
},
|
||||
"acls": [],
|
||||
"methods": {}
|
||||
}
|
||||
Reference in New Issue
Block a user