Skip to content
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

fix: planter filter to allow filtering parent orgs by name and all orgs by id #589

Merged
merged 2 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/controllers/organization.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
import { Organization } from '../models';
import { OrganizationRepository } from '../repositories';

// Extend the LoopBack filter types for the Planter model to include type
type OrganizationWhere = (Where<Organization> & { type?: string }) | undefined;
export type OrganizationFilter = Filter<Organization> & {
where: OrganizationWhere;
};
export class OrganizationController {
constructor(
@repository(OrganizationRepository)
Expand Down Expand Up @@ -69,18 +74,24 @@ export class OrganizationController {
async findByParentOrg(
@param.path.number('organizationId') organizationId: Number,
@param.query.object('filter', getFilterSchemaFor(Organization))
filter?: Filter<Organization>,
filter?: OrganizationFilter,
): Promise<Organization[]> {
// if logged in as an org-account then filter for the sub-orgs
if (organizationId && filter?.where) {
filter.where = { ...filter.where, type: 'o' };
}

// create query to get all orgs and their planters
if (filter?.where) {
filter.where = await this.organizationRepository.applyOrganizationWhereClause(
filter.where,
organizationId.valueOf(),
);
}
// console.log('/organizations ?filter --> ', organizationId, filter, filter?.where);
const result = await this.organizationRepository.find(filter);
// console.log('/organizations res --> ', result);
return result;

const childOrgs = await this.organizationRepository.find(filter);

return childOrgs;
}

@get('/organizations/{id}', {
Expand Down
31 changes: 27 additions & 4 deletions src/controllers/planter.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import { Planter, Trees } from '../models';
import { TreesFilter } from './trees.controller';
import { PlanterRepository, TreesRepository } from '../repositories';

// Extend the LoopBack filter types for the Planter model to include organizationId
type PlanterWhere = (Where<Planter> & { organizationId?: number }) | undefined;
export type PlanterFilter = Filter<Planter> & { where: PlanterWhere };

export class PlanterController {
constructor(
@repository(PlanterRepository)
Expand All @@ -38,9 +42,19 @@ export class PlanterController {
})
async count(
@param.query.object('where', getWhereSchemaFor(Planter))
where?: Where<Planter>,
where?: PlanterWhere,
): Promise<Count> {
return await this.planterRepository.count(where);
// Replace organizationId with full entity tree and planter
if (where) {
const { organizationId, ...whereWithoutOrganizationId } = where;
where = await this.planterRepository.applyOrganizationWhereClause(
whereWithoutOrganizationId,
organizationId,
);
}
// console.log('get /planter/count where -->', where);

return await this.planterRepository.countWithOrg(where);
}

@get('/planter', {
Expand All @@ -57,9 +71,18 @@ export class PlanterController {
})
async find(
@param.query.object('filter', getFilterSchemaFor(Planter))
filter?: Filter<Planter>,
filter?: PlanterFilter,
): Promise<Planter[]> {
return await this.planterRepository.find(filter);
// Replace organizationId with full entity tree and planter
if (filter?.where) {
const { organizationId, ...whereWithoutOrganizationId } = filter.where;
filter.where = await this.planterRepository.applyOrganizationWhereClause(
whereWithoutOrganizationId,
organizationId,
);
}

return await this.planterRepository.findWithOrg(filter);
}

@get('/planter/{id}', {
Expand Down
70 changes: 47 additions & 23 deletions src/controllers/planterOrganization.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import {
} from '../repositories';
// import expect from "expect-runtime";

// Extend the LoopBack filter types for the Planter model to include organizationId
// This is a workaround for the lack of proper join support in LoopBack
type PlanterWhere = (Where<Planter> & { organizationId?: number }) | undefined;
export type PlanterFilter = Filter<Planter> & { where: PlanterWhere };
export class PlanterOrganizationController {
constructor(
@repository(PlanterRepository)
Expand All @@ -48,17 +52,29 @@ export class PlanterOrganizationController {
async count(
@param.path.number('organizationId') organizationId: Number,
@param.query.object('where', getWhereSchemaFor(Planter))
where?: Where<Planter>,
where?: PlanterWhere,
): Promise<Count> {
const entityIds = await this.treesRepository.getEntityIdsByOrganizationId(
organizationId.valueOf(),
);
where = {
...where,
organizationId: {
inq: entityIds,
},
};
//override organization id if a sub-org id is in filter AND it matches one of the organization's entityIds
let orgId = organizationId.valueOf();

if (where) {
const { organizationId, ...whereWithoutOrganizationId } = where;
const filterOrgId = organizationId;

if (filterOrgId && filterOrgId !== orgId) {
const entityIds = await this.planterRepository.getEntityIdsByOrganizationId(
orgId,
);
orgId = entityIds.includes(filterOrgId) ? filterOrgId : orgId;
}

// Replace organizationId with full entity tree and planter query
where = await this.planterRepository.applyOrganizationWhereClause(
whereWithoutOrganizationId,
orgId,
);
}

return await this.planterRepository.count(where);
}

Expand All @@ -77,21 +93,29 @@ export class PlanterOrganizationController {
async find(
@param.path.number('organizationId') organizationId: Number,
@param.query.object('filter', getFilterSchemaFor(Planter))
filter?: Filter<Planter>,
filter?: PlanterFilter,
): Promise<Planter[]> {
const entityIds = await this.treesRepository.getEntityIdsByOrganizationId(
organizationId?.valueOf() || -1,
);
if (filter) {
//filter should be to deal with the organization, but here is just for
//demonstration
filter.where = {
...filter.where,
organizationId: {
inq: entityIds,
},
};
//override organization id if a sub-org id is in filter AND it matches one of the organization's entityIds
let orgId = organizationId.valueOf();

if (filter?.where) {
const { organizationId, ...whereWithoutOrganizationId } = filter.where;
const filterOrgId = organizationId;

if (filterOrgId && filterOrgId !== orgId) {
const entityIds = await this.planterRepository.getEntityIdsByOrganizationId(
orgId,
);
orgId = entityIds.includes(filterOrgId) ? filterOrgId : orgId;
}

// Replace organizationId with full entity tree and planter query
filter.where = await this.planterRepository.applyOrganizationWhereClause(
whereWithoutOrganizationId,
orgId,
);
}

return await this.planterRepository.find(filter);
}

Expand Down
5 changes: 3 additions & 2 deletions src/controllers/trees.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export class TreesController {
);
}

// console.log('get /trees/count where --> ', where);

return await this.treesRepository.countWithTagId(
where as Where<Trees>,
tagId,
Expand All @@ -82,8 +84,6 @@ export class TreesController {
@param.query.object('filter', getFilterSchemaFor(Trees))
filter?: TreesFilter,
): Promise<Trees[]> {
console.log('get /trees filter --> ', filter ? filter.where : null);

const tagId = filter?.where?.tagId;

// Replace organizationId with full entity tree and planter
Expand All @@ -95,6 +95,7 @@ export class TreesController {
);
}

// console.log('get /trees filter --> ', filter?.where);
// In order to filter by tagId (treeTags relation), we need to bypass the LoopBack find()
return await this.treesRepository.findWithTagId(filter, tagId);
}
Expand Down
42 changes: 35 additions & 7 deletions src/controllers/treesOrganization.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,26 @@ export class TreesOrganizationController {
@param.path.number('organizationId') organizationId: number,
@param.query.object('where', getWhereSchemaFor(Trees)) where?: TreesWhere,
): Promise<Count> {
const orgWhere = await this.treesRepository.applyOrganizationWhereClause(
where,
organizationId,
);
const tagId = where?.tagId;
return await this.treesRepository.countWithTagId(orgWhere, tagId);
//override organization id if a sub-org id is in filter
let orgId = organizationId.valueOf();

if (where) {
const { organizationId, ...whereWithoutOrganizationId } = where;
const filterOrgId = organizationId;

if (filterOrgId && filterOrgId !== orgId) {
orgId = filterOrgId;
}

// Replace organizationId with full entity tree and planter query
where = await this.treesRepository.applyOrganizationWhereClause(
whereWithoutOrganizationId,
orgId,
);
}

return await this.treesRepository.countWithTagId(where, tagId);
}

@get('/organization/{organizationId}/trees', {
Expand All @@ -71,12 +85,26 @@ export class TreesOrganizationController {
filter?: TreesFilter,
): Promise<Trees[]> {
const tagId = filter?.where?.tagId;
//override organization id if a sub-org id is in filter
let orgId = organizationId.valueOf();

if (filter?.where) {
const { organizationId, ...whereWithoutOrganizationId } = filter.where;
const filterOrgId = organizationId;

if (filterOrgId && filterOrgId !== orgId) {
orgId = filterOrgId;
}

// Replace organizationId with full entity tree and planter query
filter.where = await this.treesRepository.applyOrganizationWhereClause(
filter.where,
organizationId,
whereWithoutOrganizationId,
orgId,
);
}

// console.log('treesOrganization filter ---', filter?.where);

return await this.treesRepository.findWithTagId(filter, tagId);
}

Expand Down
8 changes: 6 additions & 2 deletions src/js/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ router.get('/admin_users/', async (req, res) => {
});

router.post('/validate/', async (req, res) => {
console.log('validate');
try {
const { password } = req.body;
const token = req.headers.authorization;
Expand Down Expand Up @@ -473,6 +474,7 @@ router.post('/init', async (req, res) => {
});

const isAuth = async (req, res, next) => {
console.log('isAuth...');
//white list
const url = req.originalUrl;
if (url.match(/\/auth\/(login|test|init|validate)/)) {
Expand All @@ -484,11 +486,12 @@ const isAuth = async (req, res, next) => {
const token = req.headers.authorization;
const decodedToken = jwt.verify(token, jwtSecret);
const userSession = decodedToken;
//inject the user extract from token to request object
req.user = userSession;

// console.log('userSession', userSession);
console.log('url', url);

// VALIDATE USER DATA
// const roles = userSession.role;
expect(userSession.policy).toBeInstanceOf(Object);
const policies = userSession.policy.policies;
expect(policies).toBeInstanceOf(Array);
Expand All @@ -503,6 +506,7 @@ const isAuth = async (req, res, next) => {
if (url.match(/\/auth\/check_session/)) {
const user_id = req.query.id;
const result = await helper.getActiveAdminUser(userSession.userName);
console.log('check_session user_id', user_id);

if (result.rows.length) {
const update_userSession = utils.convertCamel(result.rows[0]);
Expand Down
Loading