diff --git a/__tests__/api-tests/seed-data-creation.js b/__tests__/api-tests/seed-data-creation.js index 88f8a77..b271e90 100644 --- a/__tests__/api-tests/seed-data-creation.js +++ b/__tests__/api-tests/seed-data-creation.js @@ -2,44 +2,46 @@ const { v4: uuid } = require('uuid'); const knex = require('../../database/connection'); const stakeholderOne = Object.freeze({ - id: 5000000, + id: uuid(), type: 'type', org_name: 'name', first_name: 'first_name', last_name: 'last_name', email: 'email', phone: 'phone', - // pwd_reset_required: true, + pwd_reset_required: true, website: 'website', - // wallet: 'wallet@@#', - // password: 'password', - // salt: 'salt', - // active_contract_id: 10, - // offering_pay_to_plant: true, - // tree_validation_contract_id: 11, + wallet: 'wallet@@#', + password: 'password', + salt: 'salt', + active_contract_id: uuid(), + offering_pay_to_plant: true, + tree_validation_contract_id: uuid(), logo_url: 'url', map: 'ma,e', - stakeholder_uuid: uuid(), + owner_id: uuid(), + organization_id: 5000000, }); const stakeholderTwo = Object.freeze({ - id: 5000001, + id: uuid(), type: 'type', org_name: 'name', first_name: 'first_name', last_name: 'last_name', email: 'email', phone: 'phone', - // pwd_reset_required: true, + pwd_reset_required: true, website: 'website', - // wallet: 'wallet@!#', - // password: 'password', - // salt: 'salt', - // active_contract_id: 10, - // offering_pay_to_plant: true, - // tree_validation_contract_id: 11, + wallet: 'wallet@!#', + password: 'password', + salt: 'salt', + active_contract_id: uuid(), + offering_pay_to_plant: true, + tree_validation_contract_id: uuid(), logo_url: 'url', map: 'ma,e', - stakeholder_uuid: uuid(), + owner_id: uuid(), + organization_id: 5000001, }); before(async () => { @@ -56,7 +58,7 @@ before(async () => { after(async () => { await knex.raw(` DELETE FROM stakeholder - WHERE id = '${5000000}' OR id = '${5000001}'; + WHERE organization_id = '${5000000}' OR organization_id = '${5000001}'; `); }); diff --git a/server/models/Stakeholder.js b/server/models/Stakeholder.js index e40ef9f..d2f2c95 100644 --- a/server/models/Stakeholder.js +++ b/server/models/Stakeholder.js @@ -140,6 +140,7 @@ const Stakeholder = ({ const FilterCriteria = ({ id = null, + owner_id = null, organization_id = null, type = null, orgName = null, @@ -154,6 +155,7 @@ const FilterCriteria = ({ }) => { return Object.entries({ id, + owner_id, organization_id, type, orgName, diff --git a/server/models/Stakeholder.spec.js b/server/models/Stakeholder.spec.js index 657d9fb..f3de1f2 100644 --- a/server/models/Stakeholder.spec.js +++ b/server/models/Stakeholder.spec.js @@ -17,23 +17,23 @@ describe('Stakeholder Model', () => { 'last_name', 'email', 'phone', - // 'pwd_reset_required', + 'pwd_reset_required', 'website', - // 'wallet', - // 'password', - // 'salt', - // 'active_contract_id', - // 'offering_pay_to_plant', - // 'tree_validation_contract_id', + 'wallet', + 'password', + 'salt', + 'active_contract_id', + 'offering_pay_to_plant', + 'tree_validation_contract_id', 'logo_url', 'map', - 'stakeholder_uuid', - // 'organization_id', + 'owner_id', + 'organization_id', ]); }); describe('FilterCriteria', () => { - it('filterCriteria should not return results other than id, stakeholder_uuid, organization_id, type, orgName, firstName, lastName, imageUrl, email, phone, website, logoUrl, map', () => { + it('filterCriteria should not return results other than id, owner_id, organization_id, type, orgName, firstName, lastName, imageUrl, email, phone, website, logoUrl, map', () => { const filter = FilterCriteria({ check: true }); expect(filter).to.be.empty; }); @@ -41,19 +41,19 @@ describe('Stakeholder Model', () => { it('filterCriteria should not return undefined fields', () => { const filter = FilterCriteria({ id: undefined, - stakeholder_uuid: undefined, + owner_id: undefined, organization_id: undefined, }); expect(filter).to.be.empty; }); - it('filterCriteria should return id, stakeholder_uuid', () => { + it('filterCriteria should return id, owner_id', () => { const filter = FilterCriteria({ id: 'undefined', - stakeholder_uuid: 'undefined', + owner_id: 'undefined', organization_id: undefined, }); - expect(filter).to.have.keys(['id', 'stakeholder_uuid']); + expect(filter).to.have.keys(['id', 'owner_id']); }); }); diff --git a/server/repositories/StakeholderRepository.js b/server/repositories/StakeholderRepository.js index caf5459..9f0e3f1 100644 --- a/server/repositories/StakeholderRepository.js +++ b/server/repositories/StakeholderRepository.js @@ -130,17 +130,20 @@ class StakeholderRepository extends BaseRepository { .count('*') .where('id', id); - return { stakeholders: [stakeholder], count: +count[0].count }; + return { + stakeholders: [stakeholder], + count: count ? +count[0].count : 0, + }; } async getParentIds(id) { const parents = await this._session .getDB()('stakeholder as s') .select('stakeholder_relations.parent_id') - .join('stakeholder_relations', 's.id', 'stakeholder_relations.child_id') + .join('stakeholder_relations as sr', 's.id', 'sr.child_id') .where('s.id', id); - return parents ? parents.map((parent) => parent.parent_id) : []; + return parents.length ? parents.map((parent) => parent.parent_id) : []; } async getParents(id) { @@ -160,10 +163,10 @@ class StakeholderRepository extends BaseRepository { const children = await this._session .getDB()('stakeholder as s') .select('stakeholder_relations.child_id') - .join('stakeholder_relations', 's.id', 'stakeholder_relations.parent_id') + .join('stakeholder_relations as sr', 's.id', 'sr.parent_id') .where('s.id', id); - return children ? children.map((child) => child.child_id) : []; + return children.length ? children.map((child) => child.child_id) : []; } async getChildren(parent, options) { diff --git a/server/repositories/StakeholderRepository.spec.js b/server/repositories/StakeholderRepository.spec.js index 2938dd0..281b7c4 100644 --- a/server/repositories/StakeholderRepository.spec.js +++ b/server/repositories/StakeholderRepository.spec.js @@ -21,24 +21,46 @@ describe('StakeholderRepository', () => { mockKnex.unmock(knex); }); - it('getStakeholderById', async () => { + it('getStakeholderTreeById', async () => { tracker.uninstall(); tracker.install(); - tracker.on('query', (query) => { - let bool = query.sql.match(/select.*.*id.*.*stakeholder_uuid.*/); - if (!bool) - bool = query.sql.match( - /select.*stakeholder.*id.*or.*stakeholder_uuid.*limit.*offset/, - ); - expect(bool); + tracker.on('query', (query, step) => { const stakeholder = { id: 1 }; - query.response(stakeholder); + const count = { count: 1 }; + [ + function firstQuery() { + const bool = query.sql.match(/select.*.*id.*/); + expect(bool); + query.response(stakeholder); + }, + function firstQuery() { + const bool = query.sql.match(/select.*.*id.*/); + expect(bool); + query.response([]); + }, + function firstQuery() { + const bool = query.sql.match(/select.*.*id.*/); + expect(bool); + query.response([]); + }, + function secondQuery() { + const bool = query.sql.match(/count.*.*id.*/); + expect(bool); + query.response([count]); + }, + function finalQuery() { + query.response({ stakeholders: [stakeholder], count }); + }, + ][step - 1](); }); - const { stakeholder } = await stakeholderRepository.getStakeholderById(1); - expect(stakeholder).property('id').eq(1); + + const { stakeholders, count } = + await stakeholderRepository.getStakeholderTreeById(1); + expect(stakeholders[0]).property('id').eq(1); + expect(count).eq(1); }); - it.skip('getStakeholderByOrganizationId', async () => { + it('getStakeholderByOrganizationId', async () => { tracker.uninstall(); tracker.install(); tracker.on('query', (query) => {