Skip to content

Commit

Permalink
global serarch api
Browse files Browse the repository at this point in the history
  • Loading branch information
saigonbitmaster committed Apr 14, 2023
1 parent fd438f0 commit f798ee6
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 13 deletions.
2 changes: 1 addition & 1 deletion api/src/app.module.ts
Expand Up @@ -15,7 +15,7 @@ import { QueueModule } from './queue/queue.module';
import { BullModule } from '@nestjs/bull';
import { JobTaskModule } from './jobtask/module';
import { AdminWalletModule } from './adminwallet/module';
import { PublicModule } from './public/module';
import { PublicModule } from './customapi/module';

@Module({
imports: [
Expand Down
@@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PublicController } from './controller';
import { PublicController } from './public.controller';

describe('PublicController', () => {
let controller: PublicController;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 11 additions & 8 deletions api/src/public/module.ts → api/src/customapi/module.ts
@@ -1,25 +1,28 @@
import { Module } from '@nestjs/common';
import { PublicService } from './service';
import { PublicController } from './controller';
import { PublicService } from './public.service';
import { SearchService } from './search.service';
import { SearchController } from './search.controller';
import { PublicController } from './public.controller';
import { MongooseModule } from '@nestjs/mongoose';
import {
TokenReceiver,
TokenReceiverSchema,
} from './schemas/token-receiver.schema';
import { Campaign, CampaignSchema } from './schemas/campaign.schema';

import { PostJobModule } from '../postjob/module';
import { JobBidModule } from '../jobbid/module';
//apis for homepage & other public requests
@Module({
providers: [PublicService],
controllers: [PublicController],
providers: [PublicService, SearchService],
controllers: [PublicController, SearchController],
imports: [
MongooseModule.forFeature([
{ name: TokenReceiver.name, schema: TokenReceiverSchema },
]),
MongooseModule.forFeature([
{ name: Campaign.name, schema: CampaignSchema },
]),
JobBidModule,
PostJobModule,
],
exports: [PublicService],
exports: [PublicService, SearchService],
})
export class PublicModule {}
Expand Up @@ -11,7 +11,7 @@ import {
} from '@nestjs/common';
import { CreateTokenReceiverDto } from './dto/create.token-receiver.dto';
import { UpdateTokenReceiverDto } from './dto/update.token-receiver.dto';
import { PublicService } from './service';
import { PublicService } from './public.service';
import { queryTransform, formatRaList } from '../flatworks/utils/getlist';

import { CreateCampaignDto } from './dto/create.campaign.dto';
Expand Down
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions api/src/customapi/search.controller.ts
@@ -0,0 +1,25 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
Response,
Query,
} from '@nestjs/common';
import { SearchService } from './search.service';
import { queryTransform, formatRaList } from '../flatworks/utils/getlist';

@Controller('customapis')
export class SearchController {
constructor(private readonly service: SearchService) {}

@Get('searchall')
async indexTokenReceiver(@Response() res: any, @Query() query) {
const mongooseQuery = queryTransform(query);
const result = await this.service.findAll(mongooseQuery);
return formatRaList(res, result);
}
}
112 changes: 112 additions & 0 deletions api/src/customapi/search.service.ts
@@ -0,0 +1,112 @@
import { PostJobService } from '../postjob/service';
import { JobBidService } from '../jobbid/service';
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';

@Injectable()
export class SearchService {
constructor(
private readonly postJobService: PostJobService,
private readonly jobBidService: JobBidService,
) {}

async findAll(filter): Promise<any> {
const config = {
baseUrl: 'http://localhost:3001/#',
priority: [
{
priority: 1,
collection: 'postjobs',
serviceName: 'postJobService',
totalRecords: 0,
limit: 0,
skip: 0,
},
{
priority: 2,
collection: 'jobbids',
serviceName: 'jobBidService',
totalRecords: 0,
limit: 0,
skip: 0,
},
],
};

const text = filter.text;
let _limit = filter.limit;
let _skip = filter.skip;
const _searchCols = await Promise.all(
config.priority.map(async (item, index) => {
const _ = await this[item.serviceName].findAll({
$text: {
$search: text,
},
});
const totalRecords = _.count;
return { ...item, totalRecords: totalRecords };
}),
);

const searchCols = _searchCols.map((item, index) => {
_limit == 0
? null
: item.totalRecords - _skip > _limit
? ((item.limit = _limit), (_limit = 0), (item.skip = _skip))
: item.totalRecords < _skip
? ((item.skip = item.totalRecords),
(item.limit = 0),
(_limit = _limit),
(_skip = _skip - item.totalRecords))
: ((item.skip = _skip),
(item.limit = item.totalRecords - _skip),
(_limit = _limit - item.totalRecords - _skip),
(_skip = 0));

return item;
});

const results = await Promise.all(
searchCols.map(async (item, index) => {
let result;
try {
result = await this[item.serviceName].findAll(
{
$text: {
$search: text,
},
},
{},
item.skip,
item.limit,
);
} catch (error) {
console.log(error);
}
return {
count: result.count,
data: result.data.map((record) => ({
...record._doc,
collectionName: item.collection,
})),
};
}),
);
let data = [];
let count = 0;

results.forEach((item) => {
const _data = item.data.map((item) => ({
text: item.name || item.username || item.title,
link: `${config.baseUrl}/${item.collectionName}/${item._id}/show`,
_id: item._id,
}));
data = [...data, ..._data];
count = (count + item.count) as number;
});

return {
data,
count,
};
}
}
@@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PublicService } from './service';
import { PublicService } from './public.service';

describe('PublicService', () => {
let service: PublicService;
Expand Down
1 change: 0 additions & 1 deletion api/src/flatworks/utils/roles.ts
@@ -1,5 +1,4 @@
import { SetMetadata } from '@nestjs/common';

import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';

Expand Down
1 change: 1 addition & 0 deletions api/src/postjob/module.ts
Expand Up @@ -13,5 +13,6 @@ import { User, UserSchema } from '../user/schemas/user.schema';
MongooseModule.forFeature([{ name: PostJob.name, schema: PostJobSchema }]),
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]),
],
exports: [PostJobService],
})
export class PostJobModule {}

0 comments on commit f798ee6

Please sign in to comment.