feat(auth): add publishing mute emails setting changed event

This commit is contained in:
Karol Sójko 2022-12-06 11:00:09 +01:00
parent a521894d7c
commit 6928988f78
No known key found for this signature in database
GPG key ID: A50543BF560BDEB0
7 changed files with 86 additions and 0 deletions

1
.pnp.cjs generated
View file

@ -2649,6 +2649,7 @@ const RAW_RUNTIME_STATE =
["@sentry/node", "npm:7.19.0"],\
["@standardnotes/api", "npm:1.19.0"],\
["@standardnotes/common", "workspace:packages/common"],\
["@standardnotes/domain-core", "workspace:packages/domain-core"],\
["@standardnotes/domain-events", "workspace:packages/domain-events"],\
["@standardnotes/domain-events-infra", "workspace:packages/domain-events-infra"],\
["@standardnotes/features", "npm:1.53.1"],\

View file

@ -35,6 +35,7 @@
"@sentry/node": "^7.19.0",
"@standardnotes/api": "^1.19.0",
"@standardnotes/common": "workspace:*",
"@standardnotes/domain-core": "workspace:^",
"@standardnotes/domain-events": "workspace:*",
"@standardnotes/domain-events-infra": "workspace:*",
"@standardnotes/features": "^1.52.1",

View file

@ -20,6 +20,7 @@ import {
WebSocketMessageRequestedEvent,
ExitDiscountApplyRequestedEvent,
UserContentSizeRecalculationRequestedEvent,
MuteEmailsSettingChangedEvent,
} from '@standardnotes/domain-events'
import { Predicate, PredicateVerificationResult } from '@standardnotes/predicates'
import { TimerInterface } from '@standardnotes/time'
@ -32,6 +33,25 @@ import { DomainEventFactoryInterface } from './DomainEventFactoryInterface'
export class DomainEventFactory implements DomainEventFactoryInterface {
constructor(@inject(TYPES.Timer) private timer: TimerInterface) {}
createMuteEmailsSettingChangedEvent(dto: {
username: string
mute: boolean
emailSubscriptionRejectionLevel: string
}): MuteEmailsSettingChangedEvent {
return {
type: 'MUTE_EMAILS_SETTING_CHANGED',
createdAt: this.timer.getUTCDate(),
meta: {
correlation: {
userIdentifier: dto.username,
userIdentifierType: 'email',
},
origin: DomainEventService.Auth,
},
payload: dto,
}
}
createUserContentSizeRecalculationRequestedEvent(userUuid: string): UserContentSizeRecalculationRequestedEvent {
return {
type: 'USER_CONTENT_SIZE_RECALCULATION_REQUESTED',

View file

@ -18,6 +18,7 @@ import {
WebSocketMessageRequestedEvent,
ExitDiscountApplyRequestedEvent,
UserContentSizeRecalculationRequestedEvent,
MuteEmailsSettingChangedEvent,
} from '@standardnotes/domain-events'
import { InviteeIdentifierType } from '../SharedSubscription/InviteeIdentifierType'
@ -91,4 +92,9 @@ export interface DomainEventFactoryInterface {
userEmail: string
discountCode: string
}): ExitDiscountApplyRequestedEvent
createMuteEmailsSettingChangedEvent(dto: {
username: string
mute: boolean
emailSubscriptionRejectionLevel: string
}): MuteEmailsSettingChangedEvent
}

View file

@ -2,11 +2,13 @@ import {
CloudBackupRequestedEvent,
DomainEventPublisherInterface,
EmailBackupRequestedEvent,
MuteEmailsSettingChangedEvent,
UserDisabledSessionUserAgentLoggingEvent,
} from '@standardnotes/domain-events'
import {
EmailBackupFrequency,
LogSessionUserAgentOption,
MuteMarketingEmailsOption,
OneDriveBackupFrequency,
SettingName,
} from '@standardnotes/settings'
@ -57,6 +59,9 @@ describe('SettingInterpreter', () => {
domainEventFactory.createUserDisabledSessionUserAgentLoggingEvent = jest
.fn()
.mockReturnValue({} as jest.Mocked<UserDisabledSessionUserAgentLoggingEvent>)
domainEventFactory.createMuteEmailsSettingChangedEvent = jest
.fn()
.mockReturnValue({} as jest.Mocked<MuteEmailsSettingChangedEvent>)
logger = {} as jest.Mocked<Logger>
logger.debug = jest.fn()
@ -201,6 +206,23 @@ describe('SettingInterpreter', () => {
)
})
it('should trigger mute subscription emails rejection if mute setting changed', async () => {
const setting = {
name: SettingName.MuteMarketingEmails,
value: MuteMarketingEmailsOption.Muted,
} as jest.Mocked<Setting>
settingRepository.findOneByNameAndUserUuid = jest.fn().mockReturnValue(null)
await createInterpreter().interpretSettingUpdated(setting, user, MuteMarketingEmailsOption.Muted)
expect(domainEventPublisher.publish).toHaveBeenCalled()
expect(domainEventFactory.createMuteEmailsSettingChangedEvent).toHaveBeenCalledWith({
emailSubscriptionRejectionLevel: 'MARKETING',
mute: true,
username: 'test@test.te',
})
})
it('should trigger cloud backup if backup frequency setting is updated and a backup token setting is present', async () => {
settingRepository.findLastByNameAndUserUuid = jest.fn().mockReturnValueOnce({
name: SettingName.OneDriveBackupToken,

View file

@ -1,4 +1,5 @@
import { DomainEventPublisherInterface } from '@standardnotes/domain-events'
import { EmailSubscriptionRejectionLevel } from '@standardnotes/domain-core'
import {
DropboxBackupFrequency,
EmailBackupFrequency,
@ -39,6 +40,13 @@ export class SettingInterpreter implements SettingInterpreterInterface {
OneDriveBackupFrequency.Disabled,
]
private readonly emailSettingToSubscriptionRejectionLevelMap: Map<SettingName, string> = new Map([
[SettingName.MuteFailedBackupsEmails, EmailSubscriptionRejectionLevel.LEVELS.FailedEmailBackup],
[SettingName.MuteFailedCloudBackupsEmails, EmailSubscriptionRejectionLevel.LEVELS.FailedCloudBackup],
[SettingName.MuteMarketingEmails, EmailSubscriptionRejectionLevel.LEVELS.Marketing],
[SettingName.MuteSignInEmails, EmailSubscriptionRejectionLevel.LEVELS.SignIn],
])
constructor(
@inject(TYPES.DomainEventPublisher) private domainEventPublisher: DomainEventPublisherInterface,
@inject(TYPES.DomainEventFactory) private domainEventFactory: DomainEventFactoryInterface,
@ -48,6 +56,10 @@ export class SettingInterpreter implements SettingInterpreterInterface {
) {}
async interpretSettingUpdated(updatedSetting: Setting, user: User, unencryptedValue: string | null): Promise<void> {
if (this.isChangingMuteEmailsSetting(updatedSetting)) {
await this.triggerEmailSubscriptionChange(user, updatedSetting.name as SettingName, unencryptedValue)
}
if (this.isEnablingEmailBackupSetting(updatedSetting)) {
await this.triggerEmailBackup(user.uuid)
}
@ -78,6 +90,15 @@ export class SettingInterpreter implements SettingInterpreterInterface {
)
}
private isChangingMuteEmailsSetting(setting: Setting): boolean {
return [
SettingName.MuteFailedBackupsEmails,
SettingName.MuteFailedCloudBackupsEmails,
SettingName.MuteMarketingEmails,
SettingName.MuteSignInEmails,
].includes(setting.name as SettingName)
}
private isEnablingEmailBackupSetting(setting: Setting): boolean {
return setting.name === SettingName.EmailBackupFrequency && setting.value !== EmailBackupFrequency.Disabled
}
@ -96,6 +117,20 @@ export class SettingInterpreter implements SettingInterpreterInterface {
return SettingName.LogSessionUserAgent === setting.name && LogSessionUserAgentOption.Disabled === setting.value
}
private async triggerEmailSubscriptionChange(
user: User,
settingName: SettingName,
unencryptedValue: string | null,
): Promise<void> {
await this.domainEventPublisher.publish(
this.domainEventFactory.createMuteEmailsSettingChangedEvent({
username: user.email,
mute: unencryptedValue === 'muted',
emailSubscriptionRejectionLevel: this.emailSettingToSubscriptionRejectionLevelMap.get(settingName) as string,
}),
)
}
private async triggerSessionUserAgentCleanup(user: User) {
await this.domainEventPublisher.publish(
this.domainEventFactory.createUserDisabledSessionUserAgentLoggingEvent({

View file

@ -1888,6 +1888,7 @@ __metadata:
"@sentry/node": "npm:^7.19.0"
"@standardnotes/api": "npm:^1.19.0"
"@standardnotes/common": "workspace:*"
"@standardnotes/domain-core": "workspace:^"
"@standardnotes/domain-events": "workspace:*"
"@standardnotes/domain-events-infra": "workspace:*"
"@standardnotes/features": "npm:^1.52.1"