This commit is contained in:
2025-03-26 19:34:53 +01:00
parent 923040f487
commit 715fbdf2f5
10 changed files with 34 additions and 20 deletions

View File

@@ -1,4 +1,4 @@
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
import { BadRequestException, Inject, Injectable, NotFoundException } from '@nestjs/common';
import { and, arrayContains, asc, count, desc, 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';
@@ -246,10 +246,18 @@ export class BusinessListingService {
}
}
// #### UPDATE Business ########################################
async updateBusinessListing(id: string, data: BusinessListing): Promise<BusinessListing> {
async updateBusinessListing(id: string, data: BusinessListing, user: JwtUser): Promise<BusinessListing> {
try {
const [existingListing] = await this.conn.select().from(businesses).where(eq(businesses.id, id));
if (!existingListing) {
throw new NotFoundException(`Business listing with id ${id} not found`);
}
data.updated = new Date();
data.created = data.created ? (typeof data.created === 'string' ? new Date(data.created) : data.created) : new Date();
if (existingListing.email === user?.email) {
data.favoritesForUser = existingListing.favoritesForUser;
}
BusinessListingSchema.parse(data);
const convertedBusinessListing = data;
const [updateListing] = await this.conn.update(businesses).set(convertedBusinessListing).where(eq(businesses.id, id)).returning();