added merchant filtering

This commit is contained in:
Joe Arndt 2026-02-27 21:41:31 -06:00
parent 846e3e6368
commit 9daca90d6b
6 changed files with 32 additions and 12 deletions

View file

@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm'; import { DataSource, ILike, Repository } from 'typeorm';
import { Merchant } from './entities/merchant.entity'; import { Merchant } from './entities/merchant.entity';
import { UpdateMerchantDto } from './dto/update-merchant.dto'; import { UpdateMerchantDto } from './dto/update-merchant.dto';
import { GetMerchantFilters } from './merchants.controller';
@Injectable() @Injectable()
export class MerchantDataService { export class MerchantDataService {
@ -11,8 +12,14 @@ export class MerchantDataService {
this.merchants = this.dataSource.getRepository(Merchant); this.merchants = this.dataSource.getRepository(Merchant);
} }
public async getAll(): Promise<Merchant[]> { public async get(filters: GetMerchantFilters): Promise<Merchant[]> {
return await this.merchants.find(); const idFilters = filters.ids?.map(id => { return { id }}) ?? [];
const nameFilters = filters.merchants?.map(tag => { return { name: ILike('%' + tag + '%') }}) ?? [];
return await this.merchants.find({
where: [...idFilters, ...nameFilters],
order: { name: filters.sort === 'desc' ? 'desc' : 'asc' },
});
} }
public async getById(id: string): Promise<Merchant | null> { public async getById(id: string): Promise<Merchant | null> {

View file

@ -6,6 +6,7 @@ import {
Delete, Delete,
Body, Body,
Param, Param,
Query,
HttpCode, HttpCode,
HttpStatus, HttpStatus,
NotFoundException, NotFoundException,
@ -23,8 +24,12 @@ export class MerchantsController {
@Get() @Get()
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
public async findAll(): Promise<Merchant[]> { public async find(@Query() filters: GetMerchantFilters): Promise<Merchant[]> {
return await this.merchantsService.findAll(); const ids = Array.isArray(filters.ids) ? filters.ids : filters.ids ? [filters.ids] : [];
const merchants = Array.isArray(filters.merchants) ? filters.merchants : filters.merchants ? [filters.merchants] : [];
const sort = filters.sort?.toLowerCase() === 'desc' ? 'desc' : 'asc';
return await this.merchantsService.find({ ids, merchants, sort });
} }
@Get(':id') @Get(':id')
@ -73,3 +78,9 @@ export class MerchantsController {
return await this.merchantsService.remove(id); return await this.merchantsService.remove(id);
} }
} }
export interface GetMerchantFilters {
ids?: string[];
merchants?: string[];
sort?: 'asc' | 'desc';
}

View file

@ -3,13 +3,14 @@ import { CreateMerchantDto } from './dto/create-merchant.dto';
import { UpdateMerchantDto } from './dto/update-merchant.dto'; import { UpdateMerchantDto } from './dto/update-merchant.dto';
import { MerchantDataService } from './merchant-data.service'; import { MerchantDataService } from './merchant-data.service';
import { Merchant } from './entities/merchant.entity'; import { Merchant } from './entities/merchant.entity';
import { GetMerchantFilters } from './merchants.controller';
@Injectable() @Injectable()
export class MerchantsService { export class MerchantsService {
public constructor(private merchantDataService: MerchantDataService) { } public constructor(private merchantDataService: MerchantDataService) { }
public async findAll(): Promise<Merchant[]> { public async find(filters: GetMerchantFilters): Promise<Merchant[]> {
return await this.merchantDataService.getAll(); return await this.merchantDataService.get(filters);
} }
public async findById(id: string): Promise<Merchant> { public async findById(id: string): Promise<Merchant> {

View file

@ -12,9 +12,10 @@ export class TagDataService {
this.tags = this.dataSource.getRepository(Tag); this.tags = this.dataSource.getRepository(Tag);
} }
public async getAll(filters: GetTagFilters): Promise<Tag[]> { public async get(filters: GetTagFilters): Promise<Tag[]> {
const idFilters = filters.ids?.map(id => { return { id }}) ?? []; const idFilters = filters.ids?.map(id => { return { id }}) ?? [];
const nameFilters = filters.tags?.map(tag => { return { name: ILike('%' + tag + '%') }}) ?? []; const nameFilters = filters.tags?.map(tag => { return { name: ILike('%' + tag + '%') }}) ?? [];
return await this.tags.find({ return await this.tags.find({
where: [...idFilters, ...nameFilters], where: [...idFilters, ...nameFilters],
order: { name: filters.sort === 'desc' ? 'desc' : 'asc' }, order: { name: filters.sort === 'desc' ? 'desc' : 'asc' },

View file

@ -24,12 +24,12 @@ export class TagsController {
@Get() @Get()
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
public async findAll(@Query() filters: GetTagFilters): Promise<Tag[]> { public async find(@Query() filters: GetTagFilters): Promise<Tag[]> {
const ids = Array.isArray(filters.ids) ? filters.ids : filters.ids ? [filters.ids] : []; const ids = Array.isArray(filters.ids) ? filters.ids : filters.ids ? [filters.ids] : [];
const tags = Array.isArray(filters.tags) ? filters.tags : filters.tags ? [filters.tags] : []; const tags = Array.isArray(filters.tags) ? filters.tags : filters.tags ? [filters.tags] : [];
const sort = filters.sort?.toLowerCase() === 'desc' ? 'desc' : 'asc'; const sort = filters.sort?.toLowerCase() === 'desc' ? 'desc' : 'asc';
return await this.tagsService.findAll({ ids, tags, sort }); return await this.tagsService.find({ ids, tags, sort });
} }
@Get(':id') @Get(':id')

View file

@ -9,8 +9,8 @@ import { GetTagFilters } from './tags.controller';
export class TagsService { export class TagsService {
public constructor(private tagDataService: TagDataService) { } public constructor(private tagDataService: TagDataService) { }
public async findAll(filters: GetTagFilters): Promise<Tag[]> { public async find(filters: GetTagFilters): Promise<Tag[]> {
return await this.tagDataService.getAll(filters); return await this.tagDataService.get(filters);
} }
public async findById(id: string): Promise<Tag> { public async findById(id: string): Promise<Tag> {