Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ class MemberAttributionService {
* @returns {Promise<import('./attribution-builder').AttributionResource|null>}
*/
async getMemberCreatedAttribution(memberId) {
const memberCreatedEvent = await this.models.MemberCreatedEvent.findOne({member_id: memberId}, {require: false, withRelated: []});
// A member can have more than one created event row (member_id is not unique),
// so order deterministically to avoid non-deterministic attribution resolution.
const memberCreatedEvent = await this.models.MemberCreatedEvent
.where('member_id', memberId)
.query(qb => qb.orderBy('created_at', 'desc').orderBy('id', 'desc'))
.fetch({require: false, withRelated: []});
if (!memberCreatedEvent) {
return null;
}
Expand All @@ -163,7 +168,12 @@ class MemberAttributionService {
* @returns {Promise<import('./attribution-builder').AttributionResource|null>}
*/
async getSubscriptionCreatedAttribution(subscriptionId) {
const subscriptionCreatedEvent = await this.models.SubscriptionCreatedEvent.findOne({subscription_id: subscriptionId}, {require: false, withRelated: []});
// A subscription can have more than one created event row (subscription_id is not unique),
// so order deterministically to avoid non-deterministic attribution resolution.
const subscriptionCreatedEvent = await this.models.SubscriptionCreatedEvent
.where('subscription_id', subscriptionId)
.query(qb => qb.orderBy('created_at', 'desc').orderBy('id', 'desc'))
.fetch({require: false, withRelated: []});
if (!subscriptionCreatedEvent) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ const assert = require('node:assert/strict');

const MemberAttributionService = require('../../../../../core/server/services/member-attribution/member-attribution-service');

// Mocks a Bookshelf model that is queried via `.where(...).query(...).fetch(...)`,
// resolving the fetch to the given event (or null).
function createEventModelMock(event) {
const chainable = {
where: () => chainable,
query: () => chainable,
fetch: async () => event
};
return chainable;
}

describe('MemberAttributionService', function () {
describe('Constructor', function () {
it('doesn\'t throw', function () {
Expand Down Expand Up @@ -268,9 +279,7 @@ describe('MemberAttributionService', function () {
it('returns null when no event exists', async function () {
const service = new MemberAttributionService({
models: {
MemberCreatedEvent: {
findOne: () => null
}
MemberCreatedEvent: createEventModelMock(null)
}
});

Expand All @@ -281,21 +290,19 @@ describe('MemberAttributionService', function () {
it('returns attribution from event', async function () {
const service = new MemberAttributionService({
models: {
MemberCreatedEvent: {
findOne: () => ({
get: function (key) {
const values = {
attribution_id: 'attr_123',
attribution_url: '/test',
attribution_type: 'post',
referrer_source: 'source',
referrer_medium: 'medium',
referrer_url: 'https://referrer.com'
};
return values[key];
}
})
}
MemberCreatedEvent: createEventModelMock({
get: function (key) {
const values = {
attribution_id: 'attr_123',
attribution_url: '/test',
attribution_type: 'post',
referrer_source: 'source',
referrer_medium: 'medium',
referrer_url: 'https://referrer.com'
};
return values[key];
}
})
},
attributionBuilder: {
build: function (attribution) {
Expand Down Expand Up @@ -326,9 +333,7 @@ describe('MemberAttributionService', function () {
it('returns null when no event exists', async function () {
const service = new MemberAttributionService({
models: {
SubscriptionCreatedEvent: {
findOne: () => null
}
SubscriptionCreatedEvent: createEventModelMock(null)
}
});

Expand All @@ -339,21 +344,19 @@ describe('MemberAttributionService', function () {
it('returns attribution from event', async function () {
const service = new MemberAttributionService({
models: {
SubscriptionCreatedEvent: {
findOne: () => ({
get: function (key) {
const values = {
attribution_id: 'attr_123',
attribution_url: '/test',
attribution_type: 'post',
referrer_source: 'source',
referrer_medium: 'medium',
referrer_url: 'https://referrer.com'
};
return values[key];
}
})
}
SubscriptionCreatedEvent: createEventModelMock({
get: function (key) {
const values = {
attribution_id: 'attr_123',
attribution_url: '/test',
attribution_type: 'post',
referrer_source: 'source',
referrer_medium: 'medium',
referrer_url: 'https://referrer.com'
};
return values[key];
}
})
},
attributionBuilder: {
build: function (attribution) {
Expand Down
Loading