From 3861162772bebc222985cd5ae69a2be3d469ba5b Mon Sep 17 00:00:00 2001 From: Innei Date: Sat, 11 Sep 2021 10:48:39 +0800 Subject: [PATCH] feat: add email ejs template route --- ecosystem.config.js | 7 +++-- package.json | 1 + pnpm-lock.yaml | 6 ++++ scripts/deploy.js | 3 +- src/modules/option/dtos/email.dto.ts | 12 ++++++++ src/modules/option/option.controller.ts | 30 +++++++++++++++++++ src/processors/helper/helper.email.service.ts | 29 ++++++++++++++++-- 7 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 src/modules/option/dtos/email.dto.ts diff --git a/ecosystem.config.js b/ecosystem.config.js index 0ee95f9b..659968f9 100644 --- a/ecosystem.config.js +++ b/ecosystem.config.js @@ -1,13 +1,16 @@ module.exports = { apps: [ { - name: 'mx-server', - script: 'index.js', + name: 'mx-space-server@next', + script: 'dist/src/main.js', autorestart: true, exec_mode: 'cluster', watch: false, instances: 2, max_memory_restart: '230M', + env: { + NODE_ENV: 'production', + }, }, ], } diff --git a/package.json b/package.json index 724818af..b4e2c7a9 100644 --- a/package.json +++ b/package.json @@ -105,6 +105,7 @@ "@nestjs/testing": "8.0.6", "@types/bcrypt": "5.0.0", "@types/cache-manager": "3.4.2", + "@types/ejs": "^3.1.0", "@types/ioredis": "4.27.2", "@types/jest": "27.0.1", "@types/lodash": "4.14.172", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d9da43ac..8a1332db 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -25,6 +25,7 @@ specifiers: '@typegoose/typegoose': 8.2.0 '@types/bcrypt': 5.0.0 '@types/cache-manager': 3.4.2 + '@types/ejs': ^3.1.0 '@types/ioredis': 4.27.2 '@types/jest': 27.0.1 '@types/lodash': 4.14.172 @@ -162,6 +163,7 @@ devDependencies: '@nestjs/testing': 8.0.6_67f7e5db8827badcb202b1d38f6b1aea '@types/bcrypt': 5.0.0 '@types/cache-manager': 3.4.2 + '@types/ejs': 3.1.0 '@types/ioredis': 4.27.2 '@types/jest': 27.0.1 '@types/lodash': 4.14.172 @@ -1771,6 +1773,10 @@ packages: /@types/cors/2.8.12: resolution: {integrity: sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==} + /@types/ejs/3.1.0: + resolution: {integrity: sha512-DCg+Ka+uDQ31lJ/UtEXVlaeV3d6t81gifaVWKJy4MYVVgvJttyX/viREy+If7fz+tK/gVxTGMtyrFPnm4gjrVA==} + dev: true + /@types/eslint-scope/3.7.1: resolution: {integrity: sha512-SCFeogqiptms4Fg29WpOTk5nHIzfpKCemSN63ksBQYKTcXoJEmJagV+DhVmbapZzY4/5YaOV1nZwrsU79fFm1g==} dependencies: diff --git a/scripts/deploy.js b/scripts/deploy.js index e612f55b..3cca9791 100644 --- a/scripts/deploy.js +++ b/scripts/deploy.js @@ -33,7 +33,8 @@ async function main() { await $`unzip /tmp/${tmpName}.zip -d ./run` await $`rm /tmp/${tmpName}.zip` cd('./run') - + process.env.NODE_ENV = 'production' + await $`export NODE_ENV=production` await nothrow($`pm2 reload ecosystem.config.js -- ${argv}`) console.log('等待 15 秒') await sleep(15000) diff --git a/src/modules/option/dtos/email.dto.ts b/src/modules/option/dtos/email.dto.ts new file mode 100644 index 00000000..89d3959c --- /dev/null +++ b/src/modules/option/dtos/email.dto.ts @@ -0,0 +1,12 @@ +import { IsEnum, IsString } from 'class-validator' +import { ReplyMailType } from '~/processors/helper/helper.email.service' + +export class ReplyEmailTypeDto { + @IsEnum(ReplyMailType) + type: ReplyMailType +} + +export class ReplyEmailBodyDto { + @IsString() + source: string +} diff --git a/src/modules/option/option.controller.ts b/src/modules/option/option.controller.ts index a4616f74..1a3a51f2 100644 --- a/src/modules/option/option.controller.ts +++ b/src/modules/option/option.controller.ts @@ -5,13 +5,20 @@ import { Get, Param, Patch, + Put, + Query, UnprocessableEntityException, } from '@nestjs/common' import { ApiTags } from '@nestjs/swagger' import { IsNotEmpty, IsString } from 'class-validator' import { Auth } from '~/common/decorator/auth.decorator' +import { + EmailService, + ReplyMailType, +} from '~/processors/helper/helper.email.service' import { IConfig } from '../configs/configs.interface' import { ConfigsService } from '../configs/configs.service' +import { ReplyEmailBodyDto, ReplyEmailTypeDto } from './dtos/email.dto' import { OptionService } from './option.service' class ConfigKeyDto { @@ -27,6 +34,7 @@ export class OptionController { constructor( private readonly optionService: OptionService, private readonly configs: ConfigsService, + private readonly emailService: EmailService, ) {} @Get('/') @@ -55,4 +63,26 @@ export class OptionController { } return this.optionService.patchAndValid(params.key, body) } + + @Get('/email/template/reply') + getEmailReplyTemplate(@Query() { type }: ReplyEmailTypeDto) { + const template = this.emailService.readTemplate( + type === 'guest' ? ReplyMailType.Guest : ReplyMailType.Owner, + ) + return template + } + + @Put('/email/template/reply') + writeEmailReplyTemplate( + @Query() { type }: ReplyEmailTypeDto, + @Body() body: ReplyEmailBodyDto, + ) { + this.emailService.writeTemplate( + type === 'guest' ? ReplyMailType.Guest : ReplyMailType.Owner, + body.source, + ) + return { + source: body.source, + } + } } diff --git a/src/processors/helper/helper.email.service.ts b/src/processors/helper/helper.email.service.ts index 8473ccdf..dd47c583 100644 --- a/src/processors/helper/helper.email.service.ts +++ b/src/processors/helper/helper.email.service.ts @@ -1,14 +1,14 @@ import { Injectable, Logger } from '@nestjs/common' import { render } from 'ejs' -import { readFileSync } from 'fs' +import { readFileSync, writeFileSync } from 'fs' import { createTransport } from 'nodemailer' import path from 'path' import { ConfigsService } from '~/modules/configs/configs.service' import { LinkModel } from '~/modules/link/link.model' export enum ReplyMailType { - Owner, - Guest, + Owner = 'owner', + Guest = 'guest', } export enum LinkApplyEmailType { @@ -46,6 +46,29 @@ export class EmailService { } } + writeTemplate(type: ReplyMailType, source: string) { + switch (type) { + case ReplyMailType.Guest: + return writeFileSync( + path.resolve( + process.cwd(), + 'assets/email-template/guest.template.ejs', + ), + source, + { encoding: 'utf-8' }, + ) + case ReplyMailType.Owner: + return writeFileSync( + path.resolve( + process.cwd(), + 'assets/email-template/owner.template.ejs', + ), + source, + { encoding: 'utf-8' }, + ) + } + } + init() { this.getConfigFromConfigService().then((config) => { this.instance = createTransport({