first validation

This commit is contained in:
2024-05-17 16:46:17 -05:00
parent 782c254a33
commit 747435bfba
7 changed files with 70 additions and 19 deletions

View File

@@ -1,13 +1,12 @@
import { Body, Controller, Post } from '@nestjs/common';
import { User } from 'src/models/db.model.js';
import { MailInfo } from 'src/models/main.model.js';
import { ErrorResponse, MailInfo } from 'src/models/main.model.js';
import { MailService } from './mail.service.js';
@Controller('mail')
export class MailController {
constructor(private mailService: MailService) {}
@Post()
sendEMail(@Body() mailInfo: MailInfo): Promise<User> {
sendEMail(@Body() mailInfo: MailInfo): Promise<void | ErrorResponse> {
return this.mailService.sendInquiry(mailInfo);
}
}

View File

@@ -2,8 +2,7 @@ import { MailerService } from '@nestjs-modules/mailer';
import { Injectable } from '@nestjs/common';
import path, { join } from 'path';
import { fileURLToPath } from 'url';
import { User } from '../models/db.model.js';
import { MailInfo } from '../models/main.model.js';
import { ErrorResponse, MailInfo, isEmpty } from '../models/main.model.js';
import { UserService } from '../user/user.service.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -15,10 +14,13 @@ export class MailService {
private userService: UserService,
) {}
async sendInquiry(mailInfo: MailInfo): Promise<User> {
async sendInquiry(mailInfo: MailInfo): Promise<void | ErrorResponse> {
//const user = await this.authService.getUser(mailInfo.userId) as KeycloakUser;
const user = await this.userService.getUserByMail(mailInfo.email);
console.log(JSON.stringify(user));
if (isEmpty(mailInfo.sender.name)) {
return { fields: [{ fieldname: 'name', message: 'Required' }] };
}
await this.mailerService.sendMail({
to: user.email,
from: '"Bizmatch Team" <info@bizmatch.net>', // override default from
@@ -38,6 +40,5 @@ export class MailService {
url: mailInfo.url,
},
});
return user;
}
}

View File

@@ -165,3 +165,42 @@ export interface ImageProperty {
code: string;
name: string;
}
export interface ErrorResponse {
fields?: FieldError[];
general?: string[];
}
export interface FieldError {
fieldname: string;
message: string;
}
export function isEmpty(value: any): boolean {
// Check for undefined or null
if (value === undefined || value === null) {
return true;
}
// Check for empty string or string with only whitespace
if (typeof value === 'string') {
return value.trim().length === 0;
}
// Check for number and NaN
if (typeof value === 'number') {
return isNaN(value);
}
// If it's not a string or number, it's not considered empty by this function
return false;
}
export function emailToDirName(email: string): string {
// Entferne ungültige Zeichen und ersetze sie durch Unterstriche
const sanitizedEmail = email.replace(/[^a-zA-Z0-9_-]/g, '_');
// Entferne führende und nachfolgende Unterstriche
const trimmedEmail = sanitizedEmail.replace(/^_+|_+$/g, '');
// Ersetze mehrfache aufeinanderfolgende Unterstriche durch einen einzelnen Unterstrich
const normalizedEmail = trimmedEmail.replace(/_+/g, '_');
return normalizedEmail;
}