aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/notifier/shared/instance/registration-request-for-moderators.ts
blob: 79920245a2c268838ceba738d5994481c96a463c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { logger } from '@server/helpers/logger'
import { UserModel } from '@server/models/user/user'
import { UserNotificationModel } from '@server/models/user/user-notification'
import { MRegistration, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models'
import { UserNotificationType, UserRight } from '@shared/models'
import { AbstractNotification } from '../common/abstract-notification'

export class RegistrationRequestForModerators extends AbstractNotification <MRegistration> {
  private moderators: MUserDefault[]

  async prepare () {
    this.moderators = await UserModel.listWithRight(UserRight.MANAGE_REGISTRATIONS)
  }

  log () {
    logger.info('Notifying %s moderators of new user registration request of %s.', this.moderators.length, this.payload.username)
  }

  getSetting (user: MUserWithNotificationSetting) {
    return user.NotificationSetting.newUserRegistration
  }

  getTargetUsers () {
    return this.moderators
  }

  createNotification (user: MUserWithNotificationSetting) {
    const notification = UserNotificationModel.build<UserNotificationModelForApi>({
      type: UserNotificationType.NEW_USER_REGISTRATION_REQUEST,
      userId: user.id,
      userRegistrationId: this.payload.id
    })
    notification.UserRegistration = this.payload

    return notification
  }

  createEmail (to: string) {
    return {
      template: 'user-registration-request',
      to,
      subject: `A new user wants to register: ${this.payload.username}`,
      locals: {
        registration: this.payload
      }
    }
  }
}