1- import { Injectable , Logger , NotFoundException } from '@nestjs/common'
1+ import {
2+ BadGatewayException ,
3+ BadRequestException ,
4+ ConflictException ,
5+ Injectable ,
6+ InternalServerErrorException ,
7+ Logger ,
8+ NotFoundException ,
9+ ServiceUnavailableException ,
10+ } from '@nestjs/common'
211import { ModuleRef } from '@nestjs/core'
312import { ImapFlow } from 'imapflow'
413import { LRUCache } from 'lru-cache'
@@ -8,9 +17,11 @@ import { AccountsFileV1, AccountsMetadataV1, readAccountsFile, writeAccountsFile
817import { PartialType } from '@nestjs/swagger'
918import { MailerService } from '@nestjs-modules/mailer'
1019import { AccountSubmitDto } from '~/accounts/_dto/account-submit.dto'
20+ import { AccountSubmitedDto } from '~/accounts/_dto/account-submited.dto'
21+ import os from 'os'
22+ import gateway from 'default-gateway'
1123
12- class InternalAccountMetadataV1 extends PartialType ( AccountsMetadataV1 ) {
13- }
24+ class InternalAccountMetadataV1 extends PartialType ( AccountsMetadataV1 ) { }
1425
1526@Injectable ( )
1627export class AccountsService extends AbstractService {
@@ -77,15 +88,38 @@ export class AccountsService extends AbstractService {
7788 return account
7889 }
7990
80- public async submit ( id : string , body : AccountSubmitDto , files ?: Express . Multer . File [ ] ) {
91+ public async submit ( id : string , body : AccountSubmitDto , files ?: Express . Multer . File [ ] ) : Promise < AccountSubmitedDto > {
8192 const accounts = await readAccountsFile ( this . cache )
8293 const account = accounts . accounts . find ( ( a ) => a . id === id )
8394 if ( ! account ) throw new NotFoundException ( `Account not found: ${ id } ` )
84- return this . mailerService . sendMail ( {
85- ...body ,
86- attachments : files ,
87- from : account . smtp . from || account . smtp . auth . user ,
88- transporterName : id ,
89- } )
95+ if ( ! body . template && ( ! body . text || ! body . html ) ) {
96+ throw new BadRequestException ( `Template, text or html is required !` )
97+ }
98+ try {
99+ return await this . mailerService . sendMail ( {
100+ ...body ,
101+ attachments : files . map ( ( file ) => ( {
102+ filename : file . originalname ,
103+ content : file . buffer ,
104+ } ) ) ,
105+ from :
106+ account . smtp . from ||
107+ account . smtp ?. auth ?. user ||
108+ ( await ( async ( ) => {
109+ return `${ os . hostname ( ) } @${ ( await gateway . v4 ( ) ) . gateway } `
110+ } ) ( ) ) ,
111+ transporterName : id ,
112+ } )
113+ } catch ( e ) {
114+ if ( ! e . code ) throw new BadRequestException ( `Failed to post message with <${ e . message } >` , e . stack )
115+ switch ( e . code ) {
116+ case 'EDNS' :
117+ throw new BadGatewayException ( `[${ e . code } ] SMTP server connexion failed with <${ e . message } >` , e . stack )
118+ case 'ESOCKET' :
119+ throw new ServiceUnavailableException ( `[${ e . code } ] SMTP server connexion failed with <${ e . message } >` , e . stack )
120+ default :
121+ throw new InternalServerErrorException ( `[${ e . code } ] SMTP connection attempt internal server error with <${ e . message } >` , e . stack )
122+ }
123+ }
90124 }
91125}
0 commit comments