Skip to content

Commit

Permalink
feat: email notification
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjdd committed Apr 3, 2024
1 parent b6a452b commit 1f288ee
Show file tree
Hide file tree
Showing 10 changed files with 691 additions and 4 deletions.
92 changes: 88 additions & 4 deletions next/api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions next/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"elastic-builder": "^2.24.0",
"eventemitter3": "^4.0.7",
"form-data": "^4.0.0",
"handlebars": "^4.7.8",
"highlight.js": "^11.4.0",
"html-to-text": "^9.0.5",
"imapflow": "^1.0.158",
Expand Down
61 changes: 61 additions & 0 deletions next/api/src/controller/email-notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { z } from 'zod';

import { Body, Controller, Delete, Get, Put, UseMiddlewares } from '@/common/http';
import { ZodValidationPipe } from '@/common/pipe';
import { adminOnly, auth } from '@/middleware';
import { emailNotificationService } from '@/service/email-notification';
import { EmailNotificationResponse } from '@/response/email-notification';

const SmtpSchema = z.object({
host: z.string(),
port: z.number().int().positive(),
secure: z.boolean(),
username: z.string(),
password: z.string().optional(),
});

const MessageSchema = z.object({
text: z.string().optional(),
html: z.string().optional(),
});

const EventSchema = z.object({
type: z.enum(['ticketRepliedByCustomerService']),
from: z.string().optional(),
to: z.string(),
subject: z.string(),
message: MessageSchema,
});

const SetEmailNotificationSchema = z.object({
send: z.object({
smtp: SmtpSchema,
}),
events: z.array(EventSchema).default([]),
});

@Controller('email-notification')
@UseMiddlewares(auth, adminOnly)
export class EmailNotificationController {
@Put()
async set(
@Body(new ZodValidationPipe(SetEmailNotificationSchema))
data: z.infer<typeof SetEmailNotificationSchema>
) {
await emailNotificationService.set(data);
}

@Get()
async get() {
const value = await emailNotificationService.get(false);
if (value) {
return new EmailNotificationResponse(value);
}
return null;
}

@Delete()
async remove() {
await emailNotificationService.remove();
}
}
42 changes: 42 additions & 0 deletions next/api/src/interfaces/email-notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export type EventType = 'ticketRepliedByCustomerService';

export interface SmtpConfig {
host: string;
port: number;
secure: boolean;
username: string;
password?: string;
}

export interface MessageConfig {
text?: string;
html?: string;
}

export interface EventConfig {
type: EventType;
from?: string;
to: string;
subject: string;
message: MessageConfig;
}

export interface EmailNotification {
send: {
smtp: {
host: string;
port: number;
secure: boolean;
username: string;
password: string;
};
};
events: EventConfig[];
}

export interface SetEmailNotificationData {
send: {
smtp: SmtpConfig;
};
events: EventConfig[];
}
8 changes: 8 additions & 0 deletions next/api/src/model/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ export class Config extends Model {
configCache.delete(key);
return newConfig;
}

static async remove(key: string) {
const config = await this.findOneByKey(key);
if (config) {
await config.delete({ useMasterKey: true });
}
configCache.delete(key);
}
}
19 changes: 19 additions & 0 deletions next/api/src/response/email-notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { EmailNotification } from '@/interfaces/email-notification';

export class EmailNotificationResponse {
constructor(private data: EmailNotification) {}

toJSON() {
return {
...this.data,
send: {
...this.data.send,
smtp: {
...this.data.send.smtp,
// Strip password
password: undefined,
},
},
};
}
}
Loading

0 comments on commit 1f288ee

Please sign in to comment.