Einbau Validation finished

This commit is contained in:
2024-08-03 12:16:04 +02:00
parent f58448679d
commit 4c1b1fbc87
19 changed files with 421 additions and 338 deletions

View File

@@ -1,13 +1,14 @@
import { Inject, Injectable } from '@nestjs/common';
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
import { and, count, eq, gte, ilike, inArray, lte, ne, or, SQL, sql } from 'drizzle-orm';
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { ZodError } from 'zod';
import * as schema from '../drizzle/schema.js';
import { businesses, PG_CONNECTION } from '../drizzle/schema.js';
import { FileService } from '../file/file.service.js';
import { GeoService } from '../geo/geo.service.js';
import { BusinessListing, CommercialPropertyListing } from '../models/db.model';
import { BusinessListing, BusinessListingSchema } from '../models/db.model.js';
import { BusinessListingCriteria, emailToDirName, JwtUser } from '../models/main.model.js';
import { getDistanceQuery } from '../utils.js';
@@ -169,17 +170,41 @@ export class BusinessListingService {
}
// #### CREATE ########################################
async createListing(data: BusinessListing): Promise<BusinessListing> {
data.created = new Date();
data.updated = new Date();
const [createdListing] = await this.conn.insert(businesses).values(data).returning();
return createdListing as BusinessListing;
try {
data.created = new Date();
data.updated = new Date();
const validatedBusinessListing = BusinessListingSchema.parse(data);
const [createdListing] = await this.conn.insert(businesses).values(validatedBusinessListing).returning();
return createdListing as BusinessListing;
} catch (error) {
if (error instanceof ZodError) {
const formattedErrors = error.errors.map(err => ({
field: err.path.join('.'),
message: err.message,
}));
throw new BadRequestException(formattedErrors);
}
throw error;
}
}
// #### UPDATE Business ########################################
async updateBusinessListing(id: string, data: BusinessListing): Promise<BusinessListing | CommercialPropertyListing> {
data.updated = new Date();
data.created = new Date(data.created);
const [updateListing] = await this.conn.update(businesses).set(data).where(eq(businesses.id, id)).returning();
return updateListing as BusinessListing | CommercialPropertyListing;
async updateBusinessListing(id: string, data: BusinessListing): Promise<BusinessListing> {
try {
data.updated = new Date();
data.created = new Date(data.created);
const validatedBusinessListing = BusinessListingSchema.parse(data);
const [updateListing] = await this.conn.update(businesses).set(data).where(eq(businesses.id, id)).returning();
return updateListing as BusinessListing;
} catch (error) {
if (error instanceof ZodError) {
const formattedErrors = error.errors.map(err => ({
field: err.path.join('.'),
message: err.message,
}));
throw new BadRequestException(formattedErrors);
}
throw error;
}
}
// #### DELETE ########################################
async deleteListing(id: string): Promise<void> {