Bug Fixing overall

This commit is contained in:
2024-05-06 20:13:09 +02:00
parent bb5a408cdc
commit 6b61c19bd7
52 changed files with 1926 additions and 1048 deletions

View File

@@ -1,29 +1,20 @@
import { Inject, Injectable } from '@nestjs/common';
import {
ListingCriteria,
ListingType,
ImageProperty,
ListingCategory,
ResponseBusinessListing
} from '../models/main.model.js';
import { convertStringToNullUndefined } from '../utils.js';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { EntityData, EntityId, Schema, SchemaDefinition } from 'redis-om';
import { SQL, eq, gte, ilike, lte, sql, and} from 'drizzle-orm';
import { PG_CONNECTION, businesses, commercials, } from '../drizzle/schema.js';
import { and, eq, gte, ilike, lte, sql } from 'drizzle-orm';
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
import * as schema from '../drizzle/schema.js';
import { PgTableFn, PgTableWithColumns, QueryBuilder } from 'drizzle-orm/pg-core';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { BusinessListing, CommercialPropertyListing } from 'src/models/db.model.js';
import { Logger } from 'winston';
import * as schema from '../drizzle/schema.js';
import { PG_CONNECTION, businesses, commercials } from '../drizzle/schema.js';
import { ListingCriteria } from '../models/main.model.js';
@Injectable()
export class ListingsService {
constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
@Inject(PG_CONNECTION) private conn: NodePgDatabase<typeof schema>,) {
}
private getConditions(criteria: ListingCriteria,table: typeof businesses | typeof commercials): any[] {
constructor(
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
@Inject(PG_CONNECTION) private conn: NodePgDatabase<typeof schema>,
) {}
private getConditions(criteria: ListingCriteria, table: typeof businesses | typeof commercials): any[] {
const conditions = [];
if (criteria.type) {
conditions.push(eq(table.type, criteria.type));
@@ -51,36 +42,49 @@ export class ListingsService {
async findListingsByCriteria(criteria: ListingCriteria, table: typeof businesses | typeof commercials): Promise<{ data: Record<string, any>[]; total: number }> {
const start = criteria.start ? criteria.start : 0;
const length = criteria.length ? criteria.length : 12;
return await this.findListings(table, criteria, start, length)
return await this.findListings(table, criteria, start, length);
}
private async findListings(table: typeof businesses | typeof commercials, criteria: ListingCriteria, start = 0, length = 12): Promise<any> {
const conditions = this.getConditions(criteria,table)
const conditions = this.getConditions(criteria, table);
const [data, total] = await Promise.all([
this.conn.select().from(table).where(and(...conditions)).offset(start).limit(length),
this.conn.select({ count: sql`count(*)` }).from(table).where(and(...conditions)).then((result) => Number(result[0].count)),
this.conn
.select()
.from(table)
.where(and(...conditions))
.offset(start)
.limit(length),
this.conn
.select({ count: sql`count(*)` })
.from(table)
.where(and(...conditions))
.then(result => Number(result[0].count)),
]);
return { total, data };
}
async findById(id: string, table: typeof businesses | typeof commercials): Promise<BusinessListing | CommercialPropertyListing> {
const result = await this.conn.select().from(table).where(sql`${table.id} = ${id}`)
return result[0] as BusinessListing | CommercialPropertyListing
const result = await this.conn
.select()
.from(table)
.where(sql`${table.id} = ${id}`);
return result[0] as BusinessListing | CommercialPropertyListing;
}
async findByUserId(userId: string, table: typeof businesses | typeof commercials): Promise<BusinessListing[] | CommercialPropertyListing[]> {
return await this.conn.select().from(table).where(eq(table.userId, userId)) as BusinessListing[] | CommercialPropertyListing[]
return (await this.conn.select().from(table).where(eq(table.userId, userId))) as BusinessListing[] | CommercialPropertyListing[];
}
async createListing(data: BusinessListing | CommercialPropertyListing, table: typeof businesses | typeof commercials): Promise<BusinessListing | CommercialPropertyListing> {
data.created=new Date()
data.updated=new Date()
data.visits=0;
data.lastVisit=null
data.created = new Date();
data.updated = new Date();
data.visits = 0;
data.lastVisit = null;
const [createdListing] = await this.conn.insert(table).values(data).returning();
return createdListing as BusinessListing | CommercialPropertyListing;
}
async updateListing(id: string, data: BusinessListing | CommercialPropertyListing, table: typeof businesses | typeof commercials): Promise<BusinessListing | CommercialPropertyListing> {
data.updated=new Date();
data.updated = new Date();
data.created = new Date(data.created);
const [updateListing] = await this.conn.update(table).set(data).where(eq(table.id, id)).returning();
return updateListing as BusinessListing | CommercialPropertyListing;
}
@@ -89,29 +93,33 @@ export class ListingsService {
await this.conn.delete(table).where(eq(table.id, id));
}
async getStates(table: typeof businesses | typeof commercials): Promise<any[]> {
return await this.conn.select({state: table.state,count: sql<number>`count(${table.id})`.mapWith(Number)}).from(table).groupBy(sql`${table.state}`).orderBy(sql`count desc`);
return await this.conn
.select({ state: table.state, count: sql<number>`count(${table.id})`.mapWith(Number) })
.from(table)
.groupBy(sql`${table.state}`)
.orderBy(sql`count desc`);
}
// ##############################################################
// Images for commercial Properties
// ##############################################################
async updateImageOrder(id: string, imageOrder: string[]) {
const listing = await this.findById(id, commercials) as unknown as CommercialPropertyListing
const listing = (await this.findById(id, commercials)) as unknown as CommercialPropertyListing;
listing.imageOrder = imageOrder;
await this.updateListing(listing.id, listing, commercials)
await this.updateListing(listing.id, listing, commercials);
}
async deleteImage(id: string, name: string,) {
const listing = await this.findById(id, commercials) as unknown as CommercialPropertyListing
async deleteImage(id: string, name: string) {
const listing = (await this.findById(id, commercials)) as unknown as CommercialPropertyListing;
const index = listing.imageOrder.findIndex(im => im === name);
if (index > -1) {
listing.imageOrder.splice(index, 1);
await this.updateListing(listing.id, listing, commercials)
await this.updateListing(listing.id, listing, commercials);
}
}
async addImage(id: string, imagename: string) {
const listing = await this.findById(id, commercials) as unknown as CommercialPropertyListing
const listing = (await this.findById(id, commercials)) as unknown as CommercialPropertyListing;
listing.imageOrder.push(imagename);
await this.updateListing(listing.id, listing, commercials)
listing.imagePath = listing.id;
await this.updateListing(listing.id, listing, commercials);
}
}