49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import { Authorized, Body, JsonController, Post, QueryParam } from 'routing-controllers';
|
|
import { OpenAPI } from 'routing-controllers-openapi';
|
|
import { Mailer } from '../Mailer';
|
|
import { locales } from '../models/LocaleEnum';
|
|
import { ResetMail } from '../models/ResetMail';
|
|
import { SuccessResponse } from '../models/SuccessResponse';
|
|
|
|
/**
|
|
* The mail controller handels all endpoints concerning Mail sending.
|
|
*/
|
|
@JsonController()
|
|
@Authorized()
|
|
@OpenAPI({ security: [{ "AuthToken": [] }] })
|
|
export class MailController {
|
|
private mailer: Mailer = new Mailer();
|
|
private initialized: boolean = false;
|
|
|
|
@Post('/reset')
|
|
@OpenAPI({ description: "Sends reset mails", parameters: [{ in: "query", name: "locale", schema: { type: "string", enum: ["de", "en"] } }] })
|
|
async sendReset(@Body({ validate: true }) mailOptions: ResetMail, @QueryParam("locale") locale: locales) {
|
|
if (!this.initialized) {
|
|
await this.mailer.init();
|
|
this.initialized = true;
|
|
}
|
|
try {
|
|
this.mailer.sendResetMail(mailOptions.address, mailOptions.resetKey, locale?.toString())
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
return new SuccessResponse(locale);
|
|
}
|
|
|
|
@Post('/test')
|
|
@OpenAPI({ description: "Sends test mails", parameters: [{ in: "query", name: "locale", schema: { type: "string", enum: ["de", "en"] } }] })
|
|
async sendTest(@QueryParam("locale") locale: locales) {
|
|
if (!this.initialized) {
|
|
await this.mailer.init();
|
|
this.initialized = true;
|
|
}
|
|
try {
|
|
this.mailer.sendTestMail(locale?.toString())
|
|
} catch (error) {
|
|
console.log(error)
|
|
throw error;
|
|
}
|
|
return new SuccessResponse(locale);
|
|
}
|
|
}
|