Skip to content

Let's use AI to Earn!

feat(user): user mail to listmonk (#274)

nevin5f681f01fcb8
diff --git a/project/backend/apps/aitoearn-server/src/user/login.controller.ts b/project/backend/apps/aitoearn-server/src/user/login.controller.ts
index 0337d558..e41db8dd 100644
--- a/project/backend/apps/aitoearn-server/src/user/login.controller.ts
+++ b/project/backend/apps/aitoearn-server/src/user/login.controller.ts
@@ -2,7 +2,6 @@ import { Body, Controller, Delete, Get, Logger, Post, Put } from '@nestjs/common
import { ApiTags } from '@nestjs/swagger'
import { AitoearnAuthService, GetToken, Public, TokenInfo } from '@yikart/aitoearn-auth'
import { ApiDoc, AppException, ResponseCode } from '@yikart/common'
-import { MailService } from '@yikart/mail'
import { UserStatus } from '@yikart/mongodb'
import { RedisService } from '@yikart/redis'
import { getRandomString } from '../common/utils'
@@ -15,6 +14,7 @@ import {
RegistByMailDto,
UserCancelDto,
} from './dto/login.dto'
+import { LoginService } from './login.service'
import { UserService } from './user.service'
interface UserMailRegistCache {
@@ -30,7 +30,7 @@ export class LoginController {
private readonly authService: AitoearnAuthService,
private readonly userService: UserService,
private readonly redisService: RedisService,
- private readonly mailService: MailService,
+ private readonly loginService: LoginService,
) { }
@ApiDoc({
@@ -72,15 +72,10 @@ export class LoginController {
// No existing account, proceed with registration code flow
const code = getRandomString(6, true)
- const mailRes = await this.mailService.sendEmail({
- to: mail,
- subject: 'aitoearn regist',
- template: 'mail/regist',
- context: {
- code,
- mail,
- },
- })
+ const mailRes = await this.loginService.sendRegisterMail(
+ mail,
+ code,
+ )
if (!mailRes)
throw new AppException(ResponseCode.MailSendFail, 'Mail sending failed')
@@ -177,15 +172,10 @@ export class LoginController {
throw new AppException(ResponseCode.MailSendFail, 'Mail code add failed')
// Send verification email with email and code
- const mailRes = await this.mailService.sendEmail({
- to: mail,
- subject: 'aitoearn repassword',
- template: 'mail/repassword',
- context: {
- code,
- mail,
- },
- })
+ const mailRes = await this.loginService.sendRepasswordMail(
+ mail,
+ code,
+ )
if (!mailRes)
throw new AppException(ResponseCode.MailSendFail, 'Mail sending failed')
@@ -282,14 +272,10 @@ export class LoginController {
const code = getRandomString(6, true)
// Send verification email with code
- const mailRes = await this.mailService.sendEmail({
- to: userInfo.mail,
- subject: 'aitoearn cancel',
- template: 'mail/cancel',
- context: {
- code,
- },
- })
+ const mailRes = await this.loginService.sendCancelMail(
+ userInfo.mail,
+ code,
+ )
void this.redisService.set(
`userCancelCode:${userInfo.mail}`,
diff --git a/project/backend/apps/aitoearn-server/src/user/login.service.ts b/project/backend/apps/aitoearn-server/src/user/login.service.ts
new file mode 100644
index 00000000..9dd16de2
--- /dev/null
+++ b/project/backend/apps/aitoearn-server/src/user/login.service.ts
@@ -0,0 +1,73 @@
+import { Injectable, Logger, OnModuleInit } from '@nestjs/common'
+import { AppException, ResponseCode } from '@yikart/common'
+import { SubscribersService, SubscriberStatus, TransactionalService } from '@yikart/listmonk'
+
+@Injectable()
+export class LoginService implements OnModuleInit {
+ logger = new Logger(LoginService.name)
+
+ onModuleInit() {
+ // this.sendRegisterMail('[email protected]', '11111')
+ }
+
+ constructor(
+ private readonly transactionalService: TransactionalService,
+ private readonly subscribersService: SubscribersService,
+ ) { }
+
+ async sendRegisterMail(mail: string, code: string, name?: string): Promise<boolean> {
+ let subscriber = await this.subscribersService.findByEmail(mail)
+ if (!subscriber) {
+ subscriber = await this.subscribersService.create({
+ email: mail,
+ status: SubscriberStatus.ENABLED,
+ name: name || mail,
+ })
+ }
+
+ const res = await this.transactionalService.sendTransactionalMessage({
+ subscriber_id: subscriber.id,
+ template_id: 5,
+ data:
+ {
+ code,
+ },
+ subject: 'AiToEarn Verification Code',
+ })
+ return res.data
+ }
+
+ async sendRepasswordMail(mail: string, code: string): Promise<boolean> {
+ const subscriber = await this.subscribersService.findByEmail(mail)
+ if (!subscriber) {
+ throw new AppException(ResponseCode.UserStatusError, 'The account does not exist')
+ }
+ const res = await this.transactionalService.sendTransactionalMessage({
+ subscriber_id: subscriber.id,
+ template_id: 6,
+ data:
+ {
+ code,
+ },
+ subject: 'AiToEarn Verification Code',
+ })
+ return res.data
+ }
+
+ async sendCancelMail(mail: string, code: string): Promise<boolean> {
+ const subscriber = await this.subscribersService.findByEmail(mail)
+ if (!subscriber) {
+ throw new AppException(ResponseCode.UserStatusError, 'The account does not exist')
+ }
+ const res = await this.transactionalService.sendTransactionalMessage({
+ subscriber_id: subscriber.id,
+ template_id: 7,
+ data:
+ {
+ code,
+ },
+ subject: 'AiToEarn Verification Code',
+ })
+ return res.data
+ }
+}
diff --git a/project/backend/apps/aitoearn-server/src/user/user.module.ts b/project/backend/apps/aitoearn-server/src/user/user.module.ts
index 7b5a0285..9baa5489 100644
--- a/project/backend/apps/aitoearn-server/src/user/user.module.ts
+++ b/project/backend/apps/aitoearn-server/src/user/user.module.ts
@@ -1,5 +1,6 @@
import { Global, Module } from '@nestjs/common'
import { LoginController } from './login.controller'
+import { LoginService } from './login.service'
import { PointsService } from './points.service'
import { StorageService } from './storage.service'
import { UserController } from './user.controller'
@@ -11,7 +12,7 @@ import { VipService } from './vip.service'
@Global()
@Module({
controllers: [UserController, LoginController, UserPopController, VipController],
- providers: [UserService, PointsService, VipService, StorageService],
+ providers: [UserService, PointsService, VipService, StorageService, LoginService],
exports: [UserService, VipService, StorageService, PointsService],
})
export class UserModule { }
diff --git a/project/backend/apps/aitoearn-server/src/user/vip.controller.ts b/project/backend/apps/aitoearn-server/src/user/vip.controller.ts
index f572e2bd..91e8ff26 100644
--- a/project/backend/apps/aitoearn-server/src/user/vip.controller.ts
+++ b/project/backend/apps/aitoearn-server/src/user/vip.controller.ts
@@ -1,65 +1,14 @@
-import { Controller, OnModuleInit } from '@nestjs/common'
+import { Controller } from '@nestjs/common'
// import { Cron } from '@nestjs/schedule'
// import { RedisService } from '@yikart/redis'
// import { VipService } from './vip.service'
// const VipAddPointsLockKey = 'vip:add:points:lock:'
-import { SubscribersService, SubscriberStatus, TemplatesService, TransactionalService } from '@yikart/listmonk'
@Controller()
-export class VipController implements OnModuleInit {
+export class VipController {
constructor(
// private readonly vipService: VipService,
// private readonly redisService: RedisService,
- private readonly transactionalService: TransactionalService,
- private readonly templatesService: TemplatesService,
- private readonly subscribersService: SubscribersService,
) {}
-
- onModuleInit() {
- // this.testListmonk()
- // this.test2()
- // this.test3()
- }
- // @Cron('0 30 0 * * *')
- // async dispatchVipIntegral() {
- // const theKeyHad = await this.redisService.get(VipAddPointsLockKey)
-
- // if (theKeyHad)
- // return
-
- // this.redisService.set(VipAddPointsLockKey, '1', 60 * 60 * 24)
- // const userList = await this.vipService.findAllNormelVipUsers()
-
- // userList.forEach(async (user) => {
- // this.vipService.addVipPoints(user)
- // })
- // }
-
- testListmonk() {
- return this.transactionalService.sendTransactionalMessage({
- template_id: 3,
- subscriber_email: '[email protected]',
- subject: 'Test',
- data: {
- name: 'Nevin',
- content: '<p>Test</p>',
- },
- content_type: 'html',
- })
- }
-
- async test2() {
- const res = await this.templatesService.retrieveAllTemplates()
- return res
- }
-
- async test3() {
- const res = await this.subscribersService.create({
- email: '[email protected]',
- name: 'Nevin',
- status: SubscriberStatus.ENABLED,
- })
- return res
- }
}
diff --git a/project/backend/libs/listmonk/src/api/subscribers.service.ts b/project/backend/libs/listmonk/src/api/subscribers.service.ts
index ffe6e378..7511c523 100644
--- a/project/backend/libs/listmonk/src/api/subscribers.service.ts
+++ b/project/backend/libs/listmonk/src/api/subscribers.service.ts
@@ -1,13 +1,28 @@
import { Injectable } from '@nestjs/common'
-import { CreateSubscriberDto, Subscriber } from '../interfaces'
+import { CreateSubscriberDto, GetSubscribersDto, GetSubscribersResponse, Subscriber } from '../interfaces'
import { BaseService } from './base.service'
@Injectable()
export class SubscribersService extends BaseService {
+ async findAll(params?: GetSubscribersDto): Promise<GetSubscribersResponse> {
+ return this.request<GetSubscribersResponse>('/api/subscribers', {
+ method: 'GET',
+ params,
+ })
+ }
+
async create(data: CreateSubscriberDto): Promise<Subscriber> {
return this.request<Subscriber>('/api/subscribers', {
method: 'POST',
data,
})
}
+
+ async findByEmail(email: string): Promise<Subscriber | null> {
+ const { results } = await this.findAll({
+ query: `subscribers.email = '${email}'`,
+ per_page: 1,
+ })
+ return results[0] || null
+ }
}
diff --git a/project/backend/libs/listmonk/src/api/templates.service.ts b/project/backend/libs/listmonk/src/api/templates.service.ts
index 90b31c45..29a8fae9 100644
--- a/project/backend/libs/listmonk/src/api/templates.service.ts
+++ b/project/backend/libs/listmonk/src/api/templates.service.ts
@@ -1,15 +1,12 @@
import { Injectable } from '@nestjs/common'
+import { Template } from '../interfaces'
import { BaseService } from './base.service'
@Injectable()
export class TemplatesService extends BaseService {
- async retrieveAllTemplates(): Promise<any> {
- const res = await this.request<any>(
- '/api/templates',
- {
- method: 'GET',
- },
- )
- return res
+ async retrieveAllTemplates(): Promise<Template[]> {
+ return this.request<Template[]>('/api/templates', {
+ method: 'GET',
+ })
}
}
diff --git a/project/backend/libs/listmonk/src/interfaces/index.ts b/project/backend/libs/listmonk/src/interfaces/index.ts
index 6a5ede3f..afcfa409 100644
--- a/project/backend/libs/listmonk/src/interfaces/index.ts
+++ b/project/backend/libs/listmonk/src/interfaces/index.ts
@@ -1,3 +1,4 @@
export * from './common.interface'
export * from './subscribers.interface'
+export * from './templates.interface'
export * from './transactional.interface'
diff --git a/project/backend/libs/listmonk/src/interfaces/subscribers.interface.ts b/project/backend/libs/listmonk/src/interfaces/subscribers.interface.ts
index 9bced55a..9817ea00 100644
--- a/project/backend/libs/listmonk/src/interfaces/subscribers.interface.ts
+++ b/project/backend/libs/listmonk/src/interfaces/subscribers.interface.ts
@@ -7,6 +7,23 @@ export enum SubscriberStatus {
BLOCKLISTED = 'blocklisted',
}
+export interface GetSubscribersDto {
+ query?: string
+ list_id?: number[]
+ subscription_status?: string
+ order_by?: 'name' | 'status' | 'created_at' | 'updated_at'
+ order?: 'ASC' | 'DESC'
+ page?: number
+ per_page?: number | 'all'
+}
+
+export interface GetSubscribersResponse {
+ results: Subscriber[]
+ total: number
+ per_page: number
+ page: number
+}
+
export interface CreateSubscriberDto {
email: string
name: string
diff --git a/project/backend/libs/listmonk/src/interfaces/templates.interface.ts b/project/backend/libs/listmonk/src/interfaces/templates.interface.ts
new file mode 100644
index 00000000..b7eb59e4
--- /dev/null
+++ b/project/backend/libs/listmonk/src/interfaces/templates.interface.ts
@@ -0,0 +1,11 @@
+export interface Template {
+ id: number
+ created_at: string
+ updated_at: string
+ name: string
+ body: string
+ body_source: string | null
+ type: 'campaign' | 'campaign_visual' | 'tx'
+ is_default: boolean
+ subject?: string
+}