From ffb193bc060dcf2031651a2c47f684a6c6a1c84c Mon Sep 17 00:00:00 2001 From: Gwynn DP Date: Sun, 10 Apr 2022 16:23:27 -0700 Subject: [PATCH] fix: lint errors and remove commented code --- server/handlers/stakeholderHandler.js | 19 ---- server/models/Stakeholder.js | 110 +++---------------- server/repositories/StakeholderRepository.js | 107 +----------------- 3 files changed, 19 insertions(+), 217 deletions(-) diff --git a/server/handlers/stakeholderHandler.js b/server/handlers/stakeholderHandler.js index 3420a63..a5a194e 100644 --- a/server/handlers/stakeholderHandler.js +++ b/server/handlers/stakeholderHandler.js @@ -8,23 +8,16 @@ const { deleteStakeholder, updateStakeholder, getRelations, - // getNonRelations, createRelation, deleteRelation, } = require('../models/Stakeholder'); -// const { dispatch } = require('../models/DomainEvent'); const Session = require('../models/Session'); -// const { publishMessage } = require('../infra/messaging/RabbitMQMessaging'); const StakeholderRepository = require('../repositories/StakeholderRepository'); const stakeholderGetQuerySchema = Joi.object({ id: Joi.string().uuid(), - // organization_id: Joi.number().integer(), - // owner_id: Joi.string().uuid(), - // limit: Joi.number().integer().greater(0).less(101), - // offset: Joi.number().integer().greater(-1), type: Joi.string(), logo_url: Joi.string(), org_name: Joi.string(), @@ -93,11 +86,9 @@ const stakeholderGetAllById = async function (req, res, next) { const stakeholderGetRelations = async function (req, res, next) { const { id } = req.params; - // const { isRelation = true, org_id = null } = req.query; const session = new Session(false); const repo = new StakeholderRepository(session); - // if (isRelation !== 'false') { const executeGetRelations = getRelations(repo, id); try { const result = await executeGetRelations(); @@ -106,16 +97,6 @@ const stakeholderGetRelations = async function (req, res, next) { } catch (e) { next(e); } - // } else { - // const executeGetNonRelations = getNonRelations(repo, id); - // try { - // const result = await executeGetNonRelations(org_id); - // res.send(result); - // res.end(); - // } catch (e) { - // next(e); - // } - // } }; const stakeholderCreateRelation = async function (req, res, next) { diff --git a/server/models/Stakeholder.js b/server/models/Stakeholder.js index 1d3f4a4..5d18987 100644 --- a/server/models/Stakeholder.js +++ b/server/models/Stakeholder.js @@ -12,15 +12,6 @@ const StakeholderPostObject = ({ website, logo_url, map, - // pwd_reset_required, - // password, - // wallet, - // salt, - // active_contract_id, - // offering_pay_to_plant, - // tree_validation_contract_id, - // organization_id, - // owner_id, }) => { return Object.freeze({ id: uuidv4(), // give it a uuid, @@ -33,15 +24,6 @@ const StakeholderPostObject = ({ website, logo_url, map, - // pwd_reset_required: pwd_reset_required || false, - // password, - // wallet, - // salt, - // active_contract_id: active_contract_id || null, - // offering_pay_to_plant, - // tree_validation_contract_id: tree_validation_contract_id || null, - // organization_id, - // owner_id, }); }; @@ -95,8 +77,6 @@ const StakeholderTree = ({ const FilterCriteria = ({ id = null, - // owner_id = null, - // organization_id = null, type = null, org_name = null, first_name = null, @@ -111,8 +91,6 @@ const FilterCriteria = ({ }) => { return Object.entries({ id, - // owner_id, - // organization_id, type, org_name, first_name, @@ -165,20 +143,8 @@ const makeNextPrevUrls = (url, filter, options) => { }; const createRelation = (repo, org_id) => async (stakeholder) => { + // eslint-disable-next-line no-use-before-define const id = await getUUID(repo, org_id); - - // check both stakeholders exist - // const current = await repo.getById(id); - // const newRelation = await repo.getById(stakeholder.data.id); - - // confirm there's permission to create - // note: owners should have their own id as owner_id - // if ( - // newRelation.owner_id === id || - // current.owner_id === newRelation.owner_id || - // id === null || - // newRelation.owner_id === null - // ) { const { type, data } = stakeholder; const insertObj = {}; @@ -191,10 +157,6 @@ const createRelation = (repo, org_id) => async (stakeholder) => { const stakeholderRelation = await repo.createRelation(insertObj); return stakeholderRelation; - // } - // throw new Error({ - // message: "Whoops! That stakeholder link can't be updated, no permission", - // }); }; async function getUUIDbyId(repo, id, options = { limit: 100, offset: 0 }) { @@ -379,7 +341,6 @@ const getAllStakeholdersById = }; const getRelations = (repo, current_id) => async () => { - // const id = await getUUID(repo, org_id); const { stakeholders, count } = await repo.getRelations(current_id); return { @@ -394,6 +355,7 @@ const getRelations = (repo, current_id) => async () => { }; }; +// SAVE IN CASE WE NEED TO ADD AGAIN // const getNonRelations = (repo, current_id) => async (org_id) => { // const id = await getUUID(repo, org_id); // const { stakeholders, count } = await repo.getNonRelations(current_id, id); @@ -412,24 +374,6 @@ const getRelations = (repo, current_id) => async () => { const deleteRelation = (repo, current_id) => async (stakeholder) => { const id = await getUUID(repo, current_id); - - // get relations for owner - // const relatedStakeholders = await repo.getRelatedIds(id); - - // check both stakeholders exist - // const current = await repo.getById(id); - // const delRelation = await repo.getById(stakeholder.data.id); - - // confirm there's permission to delete - // note: owners should have their own id as owner_id - // if ( - // relatedStakeholders.includes(delRelation.id) && - // (delRelation.owner_id === id || - // current.owner_id === delRelation.owner_id || - // id === null || - // delRelation.owner_id === null || - // current.owner_id === null) - // ) { const { type, data } = stakeholder; const removeObj = {}; @@ -441,36 +385,17 @@ const deleteRelation = (repo, current_id) => async (stakeholder) => { const stakeholderRelation = await repo.deleteRelation(removeObj); return stakeholderRelation; - // } - // throw new Error( - // "Whoops! That stakeholder link can't be updated, no permission", - // ); }; -const updateStakeholder = - (repo, org_id = null) => - async (data) => { - // const id = await getUUID(repo, org_id); - const editedStakeholder = StakeholderTree({ ...data }); - // const relatedStakeholders = await repo.getRelatedIds(id); - // const foundStakeholder = await repo.getById(editedStakeholder.id); - - // confirm they have right to edit - // if ( - // (id && relatedStakeholders.includes(editedStakeholder.id)) || - // foundStakeholder.owner_id === id || - // id === null - // ) { - // remove children and parents temporarily to update - const { children, parents, ...updateObj } = editedStakeholder; - const stakeholder = await repo.updateStakeholder(updateObj); - - return StakeholderTree({ ...stakeholder, children, parents }); - // } - // throw new Error({ - // message: "Whoops! That stakeholder can't be edited, no permission", - // }); - }; +const updateStakeholder = (repo) => async (data) => { + const editedStakeholder = StakeholderTree({ ...data }); + + // remove children and parents temporarily to update + const { children, parents, ...updateObj } = editedStakeholder; + const stakeholder = await repo.updateStakeholder(updateObj); + + return StakeholderTree({ ...stakeholder, children, parents }); +}; const createStakeholder = (repo, org_id = null) => @@ -478,8 +403,6 @@ const createStakeholder = const id = await getUUID(repo, org_id); const stakeholderObj = StakeholderPostObject({ ...newStakeholder, - // organization_id: orgId || id, // to prevent it from being 0 - // owner_id: id, }); const stakeholder = await repo.createStakeholder(stakeholderObj, id); @@ -487,14 +410,11 @@ const createStakeholder = return StakeholderTree({ ...stakeholder }); }; -const deleteStakeholder = - (repo, org_id = null) => - async (removeStakeholder) => { - const id = await getUUID(repo, org_id); - const stakeholder = await repo.deleteStakeholder(removeStakeholder); +const deleteStakeholder = (repo) => async (removeStakeholder) => { + const stakeholder = await repo.deleteStakeholder(removeStakeholder); - return StakeholderTree({ ...stakeholder }); - }; + return StakeholderTree({ ...stakeholder }); +}; module.exports = { getAllStakeholdersById, diff --git a/server/repositories/StakeholderRepository.js b/server/repositories/StakeholderRepository.js index 60cb587..013f774 100644 --- a/server/repositories/StakeholderRepository.js +++ b/server/repositories/StakeholderRepository.js @@ -24,8 +24,6 @@ class StakeholderRepository extends BaseRepository { [organization_id], ); - // console.log('get by organization id ------> ', result.rows); - return { stakeholders: result.rows, count: +count.rows[0].count }; } @@ -53,9 +51,8 @@ class StakeholderRepository extends BaseRepository { .leftJoin('stakeholder_relation as sr', 's.id', 'sr.child_id') .whereNull('sr.child_id') .orderBy('s.org_name', 'asc'); - // .limit(Number(options.limit)) - // .offset(options.offset); + // count all the stakeholders, regardless of nesting const count = await this._session.getDB()('stakeholder as s').count('*'); // // add these lines to count only the parents and not the children: @@ -72,18 +69,13 @@ class StakeholderRepository extends BaseRepository { .select('s.*') .leftJoin('stakeholder_relation as sr', 's.id', 'sr.child_id') .where('s.id', id) - // .orWhere('s.owner_id', id) - // .andWhere('sr.child_id', null) .orderBy('s.org_name', 'asc'); - // .limit(Number(options.limit)) - // .offset(options.offset); // count all the stakeholders, regardless of nesting const count = await this._session .getDB()(this._tableName) .count('*') .where('id', id); - // .orWhere('owner_id', id); return { stakeholders: results, count: +count[0].count }; } @@ -159,62 +151,7 @@ class StakeholderRepository extends BaseRepository { } async getFilter(filter) { - const searchQuery = ( - query, - search, - // { - // id, - // type, - // org_name, - // first_name, - // last_name, - // email, - // phone, - // website, - // map, - // search, - // }, - ) => { - if (search) { - query.where('org_name', 'ilike', `%${search}%`); - query.orWhere('first_name', 'ilike', `%${search}%`); - query.orWhere('last_name', 'ilike', `%${search}%`); - query.orWhere('email', 'ilike', `%${search}%`); - query.orWhere('phone', 'ilike', `%${search}%`); - query.orWhere('website', 'ilike', `%${search}%`); - query.orWhere('map', 'ilike', `%${search}%`); - } - // if (id) { - // query.where('id', 'ilike', `%${id}%`); - // } - // if (type) { - // query.where('type', 'ilike', `%${type}%`); - // } - // if (org_name) { - // query.where('org_name', 'ilike', `%${org_name}%`); - // } - // if (first_name) { - // query.where('first_name', 'ilike', `%${first_name}%`); - // } - // if (last_name) { - // query.where('last_name', 'ilike', `%${last_name}%`); - // } - // if (email) { - // query.where('email', 'ilike', `%${email}%`); - // } - // if (phone) { - // query.where('phone', 'ilike', `%${phone}%`); - // } - // if (website) { - // query.where('website', 'ilike', `%${website}%`); - // } - // if (map) { - // query.where('map', 'ilike', `%${map}%`); - // } - }; - const { search = '', org_name = '', ...rest } = filter; - console.log('ORG_NAME', org_name); const stakeholders = await this._session .getDB()(this._tableName) @@ -235,8 +172,6 @@ class StakeholderRepository extends BaseRepository { } }) .orderBy('org_name', 'asc'); - // .limit(Number(options.limit)) - // .offset(options.offset) const count = await this._session .getDB()(this._tableName) @@ -266,52 +201,19 @@ class StakeholderRepository extends BaseRepository { const stakeholders = await this._session .getDB()(this._tableName) .select('*') - .where( - (builder) => builder.whereIn('id', relatedIds), - // .orWhere('owner_id', id), - ) + .where((builder) => builder.whereIn('id', relatedIds)) .andWhere({ ...filter }) .orderBy('org_name', 'asc'); - // .limit(Number(options.limit)) - // .offset(options.offset); const count = await this._session .getDB()(this._tableName) .count('*') - .where( - (builder) => builder.whereIn('id', relatedIds), - // .orWhere('owner_id', id), - ) + .where((builder) => builder.whereIn('id', relatedIds)) .andWhere({ ...filter }); return { stakeholders, count: +count[0].count }; } - // in progress - // async getSearch(value, options) { - // const stakeholders = await this._session - // .getDB()(this._tableName) - // .select('*') - // .whereILike('org_name', value) - // .orWhereILike('first_name', value) - // .orWhereILike('last_name', value) - // .orWhereILike('email', value) - // .orWhereILike('phone', value) - // .limit(options.limit) - // .offset(options.offset); - - // const count = await this._session - // .getDB()(this._tableName) - // .count('*') - // .whereILike('org_name', value) - // .orWhereILike('first_name', value) - // .orWhereILike('last_name', value) - // .orWhereILike('email', value) - // .orWhereILike('phone', value); - - // return { stakeholders, count: +count[0].count }; - // } - async createStakeholder(object) { const created = await this._session .getDB()(this._tableName) @@ -387,8 +289,6 @@ class StakeholderRepository extends BaseRepository { const stakeholders = await this._session .getDB()(this._tableName) .select('*') - // .whereIn('owner_id', [id, owner_id]) - // .orWhereNull('owner_id') .whereIn('id', [...ids, id]) .orWhereNull('id') .orderBy('org_name', 'asc'); @@ -401,6 +301,7 @@ class StakeholderRepository extends BaseRepository { return { stakeholders, count: +count[0].count }; } + // KEEP IN CASE WE NEED TO ADD BACK // async getNonRelations(id, owner_id) { // const relatedIds = await this.getRelatedIds(id); // const ids = relatedIds || [];