138 lines
5.4 KiB
TypeScript
138 lines
5.4 KiB
TypeScript
import { Inject, Injectable } from '@nestjs/common';
|
|
import {
|
|
BusinessListing,
|
|
CommercialPropertyListing,
|
|
ListingCriteria,
|
|
ListingType
|
|
} from '../models/main.model.js';
|
|
import { convertStringToNullUndefined } from '../utils.js';
|
|
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
|
|
import { Logger } from 'winston';
|
|
import { EntityData, EntityId, Repository, Schema, SchemaDefinition } from 'redis-om';
|
|
import { REDIS_CLIENT } from '../redis/redis.module.js';
|
|
|
|
@Injectable()
|
|
export class ListingsService {
|
|
businessListingRepository:Repository;
|
|
commercialPropertyListingRepository:Repository;
|
|
baseListingSchemaDef : SchemaDefinition = {
|
|
id: { type: 'string' },
|
|
userId: { type: 'string' },
|
|
listingsCategory: { type: 'string' },
|
|
title: { type: 'string' },
|
|
description: { type: 'string' },
|
|
country: { type: 'string' },
|
|
state:{ type: 'string' },
|
|
city:{ type: 'string' },
|
|
zipCode: { type: 'number' },
|
|
type: { type: 'string' },
|
|
price: { type: 'number' },
|
|
favoritesForUser:{ type: 'string[]' },
|
|
hideImage:{ type: 'boolean' },
|
|
draft:{ type: 'boolean' },
|
|
created:{ type: 'date' },
|
|
updated:{ type: 'date' }
|
|
}
|
|
businessListingSchemaDef : SchemaDefinition = {
|
|
...this.baseListingSchemaDef,
|
|
salesRevenue: { type: 'number' },
|
|
cashFlow: { type: 'number' },
|
|
employees: { type: 'number' },
|
|
established: { type: 'number' },
|
|
internalListingNumber: { type: 'number' },
|
|
realEstateIncluded:{ type: 'boolean' },
|
|
leasedLocation:{ type: 'boolean' },
|
|
franchiseResale:{ type: 'boolean' },
|
|
supportAndTraining: { type: 'string' },
|
|
reasonForSale: { type: 'string' },
|
|
brokerLicencing: { type: 'string' },
|
|
internals: { type: 'string' },
|
|
}
|
|
commercialPropertyListingSchemaDef : SchemaDefinition = {
|
|
...this.baseListingSchemaDef,
|
|
imageNames:{ type: 'string[]' },
|
|
}
|
|
businessListingSchema = new Schema('businessListing',this.businessListingSchemaDef, {
|
|
dataStructure: 'JSON'
|
|
})
|
|
commercialPropertyListingSchema = new Schema('commercialPropertyListing',this.commercialPropertyListingSchemaDef, {
|
|
dataStructure: 'JSON'
|
|
})
|
|
constructor(@Inject(REDIS_CLIENT) private readonly redis: any){
|
|
this.businessListingRepository = new Repository(this.businessListingSchema, redis);
|
|
this.commercialPropertyListingRepository = new Repository(this.commercialPropertyListingSchema, redis)
|
|
this.businessListingRepository.createIndex();
|
|
this.commercialPropertyListingRepository.createIndex();
|
|
}
|
|
async saveListing(listing: BusinessListing | CommercialPropertyListing) {
|
|
const repo=listing.listingsCategory==='business'?this.businessListingRepository:this.commercialPropertyListingRepository;
|
|
let result
|
|
listing.temporary=false;
|
|
if (listing.id){
|
|
result = await repo.save(listing.id,listing as any)
|
|
} else {
|
|
result = await repo.save(listing as any)
|
|
listing.id=result[EntityId];
|
|
result = await repo.save(listing.id,listing as any)
|
|
}
|
|
return result;
|
|
}
|
|
async getCommercialPropertyListingById(id: string) {
|
|
return await this.commercialPropertyListingRepository.fetch(id)
|
|
}
|
|
async getBusinessListingById(id: string) {
|
|
return await this.businessListingRepository.fetch(id)
|
|
}
|
|
async getBusinessListingByUserId(userid:string){
|
|
return await this.businessListingRepository.search().where('userId').equals(userid).return.all()
|
|
}
|
|
async deleteBusinessListing(id: string){
|
|
return await this.businessListingRepository.remove(id);
|
|
}
|
|
async deleteCommercialPropertyListing(id: string){
|
|
return await this.commercialPropertyListingRepository.remove(id);
|
|
}
|
|
async getAllBusinessListings(start?: number, end?: number) {
|
|
return await this.businessListingRepository.search().return.all()
|
|
}
|
|
async getAllCommercialListings(start?: number, end?: number) {
|
|
return await this.commercialPropertyListingRepository.search().return.all()
|
|
}
|
|
async findBusinessListings(criteria:ListingCriteria): Promise<any> {
|
|
let listings = await this.getAllBusinessListings();
|
|
return this.find(criteria,listings);
|
|
}
|
|
async findCommercialPropertyListings(criteria:ListingCriteria): Promise<any> {
|
|
let listings = await this.getAllCommercialListings();
|
|
return this.find(criteria,listings);
|
|
}
|
|
async find(criteria:ListingCriteria, listings: any[]): Promise<any> {
|
|
listings=listings.filter(l=>l.listingsCategory===criteria.listingsCategory);
|
|
if (convertStringToNullUndefined(criteria.type)){
|
|
console.log(criteria.type);
|
|
listings=listings.filter(l=>l.type===criteria.type);
|
|
}
|
|
if (convertStringToNullUndefined(criteria.state)){
|
|
console.log(criteria.state);
|
|
listings=listings.filter(l=>l.state===criteria.state);
|
|
}
|
|
if (convertStringToNullUndefined(criteria.minPrice)){
|
|
console.log(criteria.minPrice);
|
|
listings=listings.filter(l=>l.price>=Number(criteria.minPrice));
|
|
}
|
|
if (convertStringToNullUndefined(criteria.maxPrice)){
|
|
console.log(criteria.maxPrice);
|
|
listings=listings.filter(l=>l.price<=Number(criteria.maxPrice));
|
|
}
|
|
if (convertStringToNullUndefined(criteria.realEstateChecked)){
|
|
console.log(criteria.realEstateChecked);
|
|
listings=listings.filter(l=>l.realEstateIncluded);
|
|
}
|
|
if (convertStringToNullUndefined(criteria.category)){
|
|
console.log(criteria.category);
|
|
listings=listings.filter(l=>l.category===criteria.category);
|
|
}
|
|
return listings
|
|
}
|
|
}
|