-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
691 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
Oops, something went wrong.