Moved the DomainEvents service as a dependency of members-events-service

refs https://github.com/TryGhost/Ghost/pull/14197

- Using the package directly was creating a second instance and was never triggering the subscriber
- Passing DomainEvents as a dependency solves this issue
This commit is contained in:
Thibaut Patel 2022-03-01 17:12:59 +01:00
parent 88fb84027c
commit 88f8b622f1
2 changed files with 15 additions and 7 deletions

View File

@ -1,4 +1,3 @@
const DomainEvents = require('@tryghost/domain-events');
const {MemberPageViewEvent} = require('@tryghost/member-events');
const moment = require('moment-timezone');
@ -12,6 +11,7 @@ class LastSeenAtUpdater {
* @param {Object} deps.models The list of model dependencies
* @param {any} deps.models.Member The Member model
* @param {Object} deps.services The list of service dependencies
* @param {any} deps.services.domainEvent The DomainEvent service
* @param {any} deps.services.settingsCache The settings service
*/
constructor({
@ -19,12 +19,15 @@ class LastSeenAtUpdater {
Member
},
services: {
domainEvent,
settingsCache
}
}) {
this._memberModel = Member;
this._domainEventsService = domainEvent;
this._settingsCacheService = settingsCache;
DomainEvents.subscribe(MemberPageViewEvent, async (event) => {
this._domainEventsService.subscribe(MemberPageViewEvent, async (event) => {
await this.updateLastSeenAt(event.data.memberId, event.data.memberLastSeenAt, event.timestamp);
});
}

View File

@ -24,7 +24,8 @@ describe('LastSeenAtUpdater', function () {
services: {
settingsCache: {
get: settingsCache
}
},
domainEvent: DomainEvents
}
});
DomainEvents.dispatch(MemberPageViewEvent.create({memberId: '1', memberLastSeenAt: previousLastSeen, url: '/'}, now.toDate()));
@ -49,7 +50,8 @@ describe('LastSeenAtUpdater', function () {
services: {
settingsCache: {
get: settingsCache
}
},
domainEvent: DomainEvents
}
});
DomainEvents.dispatch(MemberPageViewEvent.create({memberId: '1', memberLastSeenAt: previousLastSeen, url: '/'}, now.toDate()));
@ -70,7 +72,8 @@ describe('LastSeenAtUpdater', function () {
services: {
settingsCache: {
get: settingsCache
}
},
domainEvent: DomainEvents
}
});
DomainEvents.dispatch(MemberPageViewEvent.create({memberId: '1', memberLastSeenAt: previousLastSeen, url: '/'}, now.toDate()));
@ -95,7 +98,8 @@ describe('LastSeenAtUpdater', function () {
services: {
settingsCache: {
get: settingsCache
}
},
domainEvent: DomainEvents
}
});
DomainEvents.dispatch(MemberPageViewEvent.create({memberId: '1', memberLastSeenAt: previousLastSeen, url: '/'}, now.toDate()));
@ -115,7 +119,8 @@ describe('LastSeenAtUpdater', function () {
services: {
settingsCache: {
get: settingsCache
}
},
domainEvent: DomainEvents
}
});
DomainEvents.dispatch(MemberSubscribeEvent.create({memberId: '1', source: 'api'}, now.toDate()));