generated from Greenstand/treetracker-microservice-template
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #359 from aayushgauba/main
fix: species v2
- Loading branch information
Showing
4 changed files
with
119 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import DbModel from './DbModel'; | ||
|
||
interface SpeciesFilter extends DbModel { | ||
id?: number; | ||
scientific_name?: string; | ||
description?: string; | ||
limit?: number; | ||
offset?: number; | ||
keyword?: string; | ||
morphology?: string; | ||
range?: string; | ||
created_at?: string; | ||
updated_at?: string; | ||
} | ||
|
||
export default SpeciesFilter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,27 @@ | ||
import log from 'loglevel'; | ||
import FilterOptions from 'interfaces/FilterOptions'; | ||
import Species from 'interfaces/Species'; | ||
import SpeciesFilter from 'interfaces/SpeciesFilter'; | ||
import { delegateRepository } from '../infra/database/delegateRepository'; | ||
import SpeciesRepositoryV2 from '../infra/database/SpeciesRepositoryV2'; | ||
|
||
type Filter = Partial<{ | ||
planter_id: number; | ||
organization_id: number; | ||
wallet_id: string; | ||
grower_id: string; | ||
}>; | ||
|
||
function getByFilter( | ||
speciesRepository: SpeciesRepositoryV2, | ||
): (filter: Filter, options: FilterOptions) => Promise<Species[]> { | ||
return async function (filter: Filter, options: FilterOptions) { | ||
if (filter.organization_id) { | ||
log.warn('using org filter...'); | ||
const trees = await speciesRepository.getByOrganization( | ||
filter.organization_id, | ||
options, | ||
); | ||
return trees; | ||
} | ||
if (filter.planter_id) { | ||
log.warn('using planter filter...'); | ||
const trees = await speciesRepository.getByPlanter( | ||
filter.planter_id, | ||
options, | ||
); | ||
return trees; | ||
} | ||
|
||
if (filter.wallet_id) { | ||
log.warn('using wallet filter...'); | ||
const trees = await speciesRepository.getByWallet( | ||
filter.wallet_id, | ||
options, | ||
); | ||
return trees; | ||
} | ||
if (filter.grower_id) { | ||
log.warn('using grower filter...'); | ||
const trees = await speciesRepository.getByGrower( | ||
filter.grower_id, | ||
options, | ||
); | ||
return trees; | ||
} | ||
|
||
const trees = await speciesRepository.getByFilter(filter, options); | ||
return trees; | ||
}; | ||
} | ||
function countByFilter( | ||
speciesRepository: SpeciesRepositoryV2, | ||
): (filter: Filter) => Promise<number> { | ||
return async function (filter: Filter) { | ||
if (filter.organization_id) { | ||
log.warn('using org filter...'); | ||
const total = await speciesRepository.countByOrganization( | ||
filter.organization_id, | ||
); | ||
return total; | ||
} | ||
const total = await speciesRepository.countByFilter(filter); | ||
return total; | ||
): (filter: SpeciesFilter, options: FilterOptions) => Promise<SpeciesFilter[]> { | ||
return async function (filter: SpeciesFilter, options: FilterOptions) { | ||
const result = await speciesRepository.getByFilter(filter, options); | ||
return result; | ||
}; | ||
} | ||
|
||
export default { | ||
getById: delegateRepository<SpeciesRepositoryV2, Species>('getById'), | ||
getByGrower: delegateRepository<SpeciesRepositoryV2, Species>('getByGrower'), | ||
getByFilter, | ||
countByFilter, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters