-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: create mixin to share helper functions across repos #597
Open
gwynndp
wants to merge
5
commits into
Greenstand:master
Choose a base branch
from
gwynndp:cleanup-helper-fns
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ccd0b17
refactor: create mixin to share helper functions across repos
gwynndp a57a13d
fix: lint warnings and logic for querying planters by organization
gwynndp b11c77e
fix: add modelName to query to avoid ambiguous fields
gwynndp b1eeb08
fix: add modelName to query to avoid ambiguous fields
gwynndp b016d72
feat: add device manufacturer to planter registration data for org ac…
gwynndp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,120 @@ | ||
import { MixinTarget } from '@loopback/core'; | ||
import { CrudRepository, Model } from '@loopback/repository'; | ||
import expect from 'expect-runtime'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export function UtilsRepositoryMixin< | ||
M extends Model, | ||
R extends MixinTarget<CrudRepository<M>>, | ||
>(superClass: R) { | ||
return class extends superClass { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[x: string]: any; | ||
// put the shared code here | ||
async getEntityIdsByOrganizationId( | ||
organizationId: number, | ||
): Promise<Array<number>> { | ||
expect(organizationId).number(); | ||
expect(this).property('execute').defined(); | ||
const result = await this.execute( | ||
`select * from getEntityRelationshipChildren(${organizationId})`, | ||
[], | ||
); | ||
return result.map((e) => e.entity_id); | ||
} | ||
|
||
async applyOrganizationWhereClause( | ||
where: Object | undefined, | ||
organizationId: number | undefined, | ||
model: string, | ||
): Promise<Object | undefined> { | ||
if (!where || organizationId === undefined) { | ||
return Promise.resolve(where); | ||
} | ||
|
||
// if planter or tree repository request | ||
if (model === 'trees' || model === 'planter') { | ||
const organizationWhereClause = await this.getOrganizationWhereClause( | ||
organizationId, | ||
model, | ||
); | ||
return { | ||
and: [where, organizationWhereClause], | ||
}; | ||
} else { | ||
const entityIds = await this.getEntityIdsByOrganizationId( | ||
organizationId, | ||
); | ||
return { | ||
and: [where, { id: { inq: entityIds } }], | ||
}; | ||
} | ||
} | ||
|
||
async getPlanterIdsByOrganizationId( | ||
organizationId: number, | ||
): Promise<Array<number>> { | ||
expect(organizationId).number(); | ||
const result = await this.execute( | ||
`select * from planter where organization_id in (select entity_id from getEntityRelationshipChildren(${organizationId}))`, | ||
[], | ||
); | ||
expect(result).match([{ id: expect.any(Number) }]); | ||
return result.map((e) => e.id); | ||
} | ||
|
||
async getNonOrganizationPlanterIds(): Promise<Array<number>> { | ||
const result = await this.execute( | ||
`select * from planter where organization_id isnull`, | ||
[], | ||
); | ||
expect(result).match([{ id: expect.any(Number) }]); | ||
return result.map((e) => e.id); | ||
} | ||
|
||
async getOrganizationWhereClause( | ||
organizationId: number, | ||
model: string, | ||
): Promise<Object> { | ||
if (organizationId === null) { | ||
const planterIds = await this.getNonOrganizationPlanterIds(); | ||
// if planter repository request | ||
if (model === 'planter') { | ||
return { id: { inq: planterIds } }; | ||
} else { | ||
// if trees or other repository request | ||
return { | ||
and: [ | ||
{ plantingOrganizationId: null }, | ||
{ planterId: { inq: planterIds } }, | ||
], | ||
}; | ||
} | ||
} else { | ||
const planterIds = await this.getPlanterIdsByOrganizationId( | ||
organizationId, | ||
); | ||
const entityIds = await this.getEntityIdsByOrganizationId( | ||
organizationId, | ||
); | ||
// if planter repository request | ||
if (model === 'planter') { | ||
return { | ||
or: [ | ||
{ organizationId: { inq: entityIds } }, | ||
{ 'planter.id': { inq: planterIds } }, | ||
], | ||
}; | ||
} else { | ||
// if trees or other repository request | ||
return { | ||
or: [ | ||
{ plantingOrganizationId: { inq: entityIds } }, | ||
{ planterId: { inq: planterIds } }, | ||
], | ||
}; | ||
} | ||
} | ||
} | ||
}; | ||
} |
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,42 +1,22 @@ | ||
import { Constructor, inject } from '@loopback/core'; | ||
import { DefaultCrudRepository } from '@loopback/repository'; | ||
import { Organization, OrganizationRelations } from '../models'; | ||
import { TreetrackerDataSource } from '../datasources'; | ||
import { inject } from '@loopback/core'; | ||
import expect from 'expect-runtime'; | ||
import { UtilsRepositoryMixin } from '../mixins/utils.repository-mixin'; | ||
import { Organization, OrganizationRelations } from '../models'; | ||
|
||
export class OrganizationRepository extends DefaultCrudRepository< | ||
export class OrganizationRepository extends UtilsRepositoryMixin< | ||
Organization, | ||
typeof Organization.prototype.id, | ||
OrganizationRelations | ||
> { | ||
Constructor< | ||
DefaultCrudRepository< | ||
Organization, | ||
typeof Organization.prototype.id, | ||
OrganizationRelations | ||
> | ||
> | ||
>(DefaultCrudRepository) { | ||
constructor( | ||
@inject('datasources.treetracker') dataSource: TreetrackerDataSource, | ||
) { | ||
super(Organization, dataSource); | ||
} | ||
|
||
async getEntityIdsByOrganizationId( | ||
organizationId: number, | ||
): Promise<Array<number>> { | ||
expect(organizationId).number(); | ||
expect(this).property('execute').defined(); | ||
const result = await this.execute( | ||
`select * from getEntityRelationshipChildren(${organizationId})`, | ||
[], | ||
); | ||
return result.map((e) => e.entity_id); | ||
} | ||
|
||
async applyOrganizationWhereClause( | ||
where: Object | undefined, | ||
organizationId: number | undefined, | ||
): Promise<Object | undefined> { | ||
if (!where || organizationId === undefined) { | ||
return Promise.resolve(where); | ||
} | ||
const entityIds = await this.getEntityIdsByOrganizationId(organizationId); | ||
return { | ||
and: [where, { id: { inq: entityIds } }], | ||
}; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nmcharlton
I added this regex to add the modelName back onto each field because our more complex cross-table queries were resulting in "ambiguous" field errors that I couldn't find any other way of resolving. Is there a better way to resolve those errors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether the LoopBack query builders have a way of including the table name in the query.
This looks ok, but won't catch every
WHERE
clause construction.