Skip to content

Commit

Permalink
fix: linking bugs, renameing fields in snakeCase
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp committed Jan 9, 2022
1 parent a5f10e8 commit 80c61fc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 91 deletions.
50 changes: 27 additions & 23 deletions server/models/Stakeholder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-param-reassign */
/* eslint-disable prefer-destructuring */
const { v4: uuidv4 } = require('uuid');
const { camelToSnakeCase } = require('../utils/utils');

const StakeholderPostObject = ({
type,
Expand Down Expand Up @@ -143,36 +142,36 @@ const FilterCriteria = ({
owner_id = null,
organization_id = null,
type = null,
orgName = null,
firstName = null,
lastName = null,
imageUrl = null,
org_name = null,
first_name = null,
last_name = null,
image_url = null,
email = null,
phone = null,
website = null,
logoUrl = null,
logo_url = null,
map = null,
}) => {
return Object.entries({
id,
owner_id,
organization_id,
type,
orgName,
firstName,
lastName,
imageUrl,
org_name,
first_name,
last_name,
image_url,
email,
phone,
website,
logoUrl,
logo_url,
map,
})
.filter(
(entry) => entry[1] !== undefined && entry[1] !== null && entry[1] !== '',
)
.reduce((result, item) => {
result[camelToSnakeCase(item[0])] = item[1];
result[item[0]] = item[1];
return result;
}, {});
};
Expand Down Expand Up @@ -212,20 +211,14 @@ async function getUUID(
id,
options = { limit: 100, offset: 0 },
) {
console.log('getUUID', id);

// check for id in current table first
const stakeholderFound = await stakeholderRepo.getUUIDbyId(id);

console.log('stakeholderFound', stakeholderFound);

if (!stakeholderFound) {
// get organization from old entity table
const { stakeholders } =
await stakeholderRepo.getStakeholderByOrganizationId(id, options);

console.log('from entity table ------> ', stakeholders.length);

const owner_id = stakeholders[0].stakeholder_uuid;

stakeholders.forEach(async (entity, i) => {
Expand Down Expand Up @@ -405,7 +398,9 @@ const getUnlinkedStakeholders =
stakeholders:
stakeholders &&
stakeholders.map((row) => {
return Stakeholder({ ...row });
row.children = [];
row.parents = [];
return StakeholderTree({ ...row });
}),
totalCount: count,
};
Expand All @@ -425,13 +420,21 @@ const updateLinkStakeholder =
: await getUUID(stakeholderRepo, orgId);

try {
const parentStakeholder = await stakeholderRepo.verifyById(id, id);
const relatedStakeholders = await stakeholderRepo.getRelatedIds(id); // linked
const foundStakeholder = await stakeholderRepo.verifyById(
id,
object.data.id,
);

// confirm stakeholder link can be updated
if (foundStakeholder && foundStakeholder.stakeholder.email) {
if (
(id && relatedStakeholders.includes(foundStakeholder.id)) ||
foundStakeholder.owner_id === id ||
parentStakeholder.owner_id === foundStakeholder.owner_id ||
id === null ||
foundStakeholder.owner_id === null
) {
const stakeholderRelation = await stakeholderRepo.updateLink(
id,
object,
Expand Down Expand Up @@ -465,10 +468,11 @@ const updateStakeholder =
const foundStakeholder = await stakeholderRepo.verifyById(id, object.id);

// confirm stakeholder is related (can be edited) if there's an orgId OR just that it exists (if no orgId) before updating

if (
(orgId && relatedStakeholders.includes(object.id)) ||
foundStakeholder.stakeholder.email ||
foundStakeholder.stakeholder.email
(id && relatedStakeholders.includes(object.id)) ||
foundStakeholder.owner_id === id ||
id === null
) {
// remove children and parents temporarily
const { children, parents, ...updateObj } = object;
Expand Down
98 changes: 30 additions & 68 deletions server/repositories/StakeholderRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ class StakeholderRepository extends BaseRepository {
.getDB()(this._tableName)
.select('*')
.where('id', id)
.andWhere('owner_id', orgId)
.orWhereNull('owner_id')
.first();

return { stakeholder };
return stakeholder;
}

async getAllStakeholderTrees(options) {
Expand All @@ -66,7 +64,7 @@ class StakeholderRepository extends BaseRepository {
const stakeholders = await Promise.all(
results.map(async (stakeholder) => {
// eslint-disable-next-line no-param-reassign
stakeholder.parents = await this.getParents(stakeholder.id);
stakeholder.parents = await this.getParents(stakeholder, options);
// eslint-disable-next-line no-param-reassign
stakeholder.children = await this.getChildren(stakeholder, options);
return stakeholder;
Expand Down Expand Up @@ -96,7 +94,7 @@ class StakeholderRepository extends BaseRepository {
const stakeholders = await Promise.all(
results.map(async (stakeholder) => {
// eslint-disable-next-line no-param-reassign
stakeholder.parents = await this.getParents(stakeholder.id);
stakeholder.parents = await this.getParents(stakeholder, options);
// eslint-disable-next-line no-param-reassign
stakeholder.children = await this.getChildren(stakeholder, options);
return stakeholder;
Expand All @@ -122,7 +120,7 @@ class StakeholderRepository extends BaseRepository {
.first();

// only get one step generation difference, no recursion
stakeholder.parents = await this.getParents(id);
stakeholder.parents = await this.getParents(stakeholder, options);
stakeholder.children = await this.getChildren(stakeholder, options);

const count = await this._session
Expand All @@ -139,30 +137,42 @@ class StakeholderRepository extends BaseRepository {
async getParentIds(id) {
const parents = await this._session
.getDB()('stakeholder as s')
.select('stakeholder_relations.parent_id')
.select('sr.parent_id')
.join('stakeholder_relations as sr', 's.id', 'sr.child_id')
.where('s.id', id);

return parents.length ? parents.map((parent) => parent.parent_id) : [];
}

async getParents(id) {
const parentIds = await this.getParentIds(id);
async getParents(child, options) {
const parentIds = await this.getParentIds(child.id);

if (parentIds.length) {
return this._session
const parents = await this._session
.getDB()(this._tableName)
.select('*')
.whereIn('id', parentIds);
}
.whereIn('id', parentIds)
.orderBy('org_name', 'asc')
.limit(options.limit)
.offset(options.offset);

// don't want to keep getting all of the parents and children recursively, but do want to
// include the current stakeholder as child
return parents.map((parent) => {
// eslint-disable-next-line no-param-reassign
parent.parents = [];
// eslint-disable-next-line no-param-reassign
parent.children = [{ ...child }];
return parent;
});
}
return [];
}

async getChildrenIds(id) {
const children = await this._session
.getDB()('stakeholder as s')
.select('stakeholder_relations.child_id')
.select('sr.child_id')
.join('stakeholder_relations as sr', 's.id', 'sr.parent_id')
.where('s.id', id);

Expand Down Expand Up @@ -219,7 +229,7 @@ class StakeholderRepository extends BaseRepository {
const stakeholders = await Promise.all(
results.map(async (stakeholder) => {
// eslint-disable-next-line no-param-reassign
stakeholder.parents = await this.getParents(stakeholder.id);
stakeholder.parents = await this.getParents(stakeholder, options);
// eslint-disable-next-line no-param-reassign
stakeholder.children = await this.getChildren(stakeholder, options);
return stakeholder;
Expand All @@ -230,15 +240,6 @@ class StakeholderRepository extends BaseRepository {
.getDB()(this._tableName)
.count('*')
.where({ ...filter });
// .where((builder) =>
// org_name || first_name || last_name
// ? builder
// .where({ ...otherFilters })
// .orWhere('org_name', 'like', org_name)
// .orWhere('first_name', 'like', first_name)
// .orWhere('last_name', 'like', last_name)
// : builder.where({ ...otherFilters }),
// );

return { stakeholders, count: +count[0].count };
}
Expand All @@ -265,50 +266,15 @@ class StakeholderRepository extends BaseRepository {
}

async getFilterById(id, filter, options) {
// PARTIAL SEARCH IN PROGRESS

// const {
// org_name,
// first_name,
// last_name,
// email,
// phone, ...otherFilters,
// } = filter;

const relatedIds = await this.getRelatedIds(id);

// const searchFields = Object.entries({
// org_name,
// first_name,
// last_name,
// email,
// phone,
// });

// console.log('filter cols -------->', searchFields);
// console.log('other filters -------->', otherFilters);

// const searchString = searchFields
// .reduce((acc, [key, value]) => {
// if (value) {
// acc.push(`"${key}" like "${value}"`);
// }
// return acc;
// }, [])
// .join(' and ');

// console.log('searchString', searchString);

const results = await this._session
.getDB()(this._tableName)
.select('*')
.where((builder) =>
builder.whereIn('id', relatedIds).orWhere('owner_id', id),
)
// .whereIn('id', relatedIds)
// .orWhere('owner_id', id)
.andWhere({ ...filter })
// .andWhere(this._session.getDB().raw(searchString))
.orderBy('org_name', 'asc')
.limit(options.limit)
.offset(options.offset);
Expand All @@ -329,8 +295,6 @@ class StakeholderRepository extends BaseRepository {
.where((builder) =>
builder.whereIn('id', relatedIds).orWhere('owner_id', id),
)
// .whereIn('id', relatedIds)
// .orWhere('owner_id', id)
.andWhere({ ...filter });

return { stakeholders, count: +count[0].count };
Expand Down Expand Up @@ -373,19 +337,17 @@ class StakeholderRepository extends BaseRepository {
const stakeholders = await this._session
.getDB()(this._tableName)
.select('*')
.whereNotIn('id', [...ids, stakeholder_id])
.andWhere('owner_id', id)
// .orWhere('id', id) // include the owner in results
// .orWhere('owner_id', null) // include admin-created stakeholders in results
// .where('owner_id', id) // include all w/ same owner
// .orWhere('owner_id', stakeholder_id) // include their own children
.whereNotIn('id', [...ids, stakeholder_id]) // exclude already linked
.orderBy('org_name', 'asc');

const count = await this._session
.getDB()(this._tableName)
.count('*')
.whereNotIn('id', [...ids, stakeholder_id])
.andWhere('owner_id', id);
// .orWhere('id', id);
// .orWhere('owner_id', null);
// .where('owner_id', id)
// .orWhere('owner_id', stakeholder_id)
.whereNotIn('id', [...ids, stakeholder_id]);

return { stakeholders, count: +count[0].count };
}
Expand Down

0 comments on commit 80c61fc

Please sign in to comment.