0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Extracted context to source mapping logic

refs https://github.com/TryGhost/Toolbox/issues/386

- Before changing the mapping logic it needs to become testable!
This commit is contained in:
Naz 2022-08-23 12:04:43 +08:00
parent 56da2a2be0
commit 32343a7adb
2 changed files with 61 additions and 13 deletions

View file

@ -98,6 +98,24 @@ module.exports = class MemberRepository {
return subscription.plan && subscription.plan.nickname && subscription.plan.nickname.toLowerCase() === 'complimentary';
}
_resolveContextSource(context) {
let source;
if (context.import || context.importer) {
source = 'import';
} else if (context.internal) {
source = 'system';
} else if (context.user) {
source = 'admin';
} else if (context.api_key) {
source = 'api';
} else {
source = 'member';
}
return source;
}
getMRR({interval, amount, status = null, canceled = false, discount = null}) {
if (status === 'trialing') {
return 0;
@ -250,19 +268,7 @@ module.exports = class MemberRepository {
}
const context = options && options.context || {};
let source;
if (context.import || context.importer) {
source = 'import';
} else if (context.internal) {
source = 'system';
} else if (context.user) {
source = 'admin';
} else if (context.api_key) {
source = 'api';
} else {
source = 'member';
}
const source = this._resolveContextSource(context);
const eventData = _.pick(data, ['created_at']);

View file

@ -1,3 +1,4 @@
const assert = require('assert');
const MemberRepository = require('../../../../lib/repositories/member');
describe('MemberRepository', function () {
@ -7,4 +8,45 @@ describe('MemberRepository', function () {
repo.isComplimentarySubscription({});
});
});
describe('#resolveContextSource', function (){
it('Maps context to source', function (){
const repo = new MemberRepository({});
let source = repo._resolveContextSource({
import: true
});
assert.equal(source, 'import');
source = repo._resolveContextSource({
importer: true
});
assert.equal(source, 'import');
source = repo._resolveContextSource({
user: true
});
assert.equal(source, 'admin');
source = repo._resolveContextSource({
user: true,
api_key: true
});
assert.equal(source, 'admin');
source = repo._resolveContextSource({
api_key: true
});
assert.equal(source, 'api');
source = repo._resolveContextSource({
});
assert.equal(source, 'member');
source = repo._resolveContextSource({
generic_context: true
});
assert.equal(source, 'member');
});
});
});