Skip to content

Commit

Permalink
Revert "BHBC-2058: Edit Project as a Single Page / General UI Clean-up (
Browse files Browse the repository at this point in the history
#856)" (#857)

This reverts commit 5c1cd58.
  • Loading branch information
KjartanE authored Nov 17, 2022
1 parent 5c1cd58 commit 9dbda16
Show file tree
Hide file tree
Showing 82 changed files with 9,261 additions and 21,405 deletions.
12,568 changes: 14 additions & 12,554 deletions api/package-lock.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions api/src/models/project-create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('PostProjectObject', () => {
]
},
funding: {
fundingSources: [
funding_sources: [
{
agency_id: 1,
investment_action_category: 1,
Expand Down Expand Up @@ -522,48 +522,48 @@ describe('PostFundingData', () => {
data = new PostFundingData(null);
});

it('sets fundingSources', () => {
expect(data.fundingSources).to.eql([]);
it('sets funding_sources', () => {
expect(data.funding_sources).to.eql([]);
});
});

describe('Values provided but not valid arrays', () => {
let data: PostFundingData;

const obj = {
fundingSources: null
funding_sources: null
};

before(() => {
data = new PostFundingData(obj);
});

it('sets fundingSources', () => {
expect(data.fundingSources).to.eql([]);
it('sets funding_sources', () => {
expect(data.funding_sources).to.eql([]);
});
});

describe('Values provided but with no length', () => {
let data: PostFundingData;

const obj = {
fundingSources: []
funding_sources: []
};

before(() => {
data = new PostFundingData(obj);
});

it('sets fundingSources', () => {
expect(data.fundingSources).to.eql([]);
it('sets funding_sources', () => {
expect(data.funding_sources).to.eql([]);
});
});

describe('All values provided', () => {
let data: PostFundingData;

const obj = {
fundingSources: [
funding_sources: [
{
agency_id: 1,
investment_action_category: 1,
Expand All @@ -579,8 +579,8 @@ describe('PostFundingData', () => {
data = new PostFundingData(obj);
});

it('sets fundingSources', () => {
expect(data.fundingSources).to.eql(obj.fundingSources);
it('sets funding_sources', () => {
expect(data.funding_sources).to.eql(obj.funding_sources);
});
});
});
6 changes: 3 additions & 3 deletions api/src/models/project-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ export class PostFundingSource {
* @class PostFundingData
*/
export class PostFundingData {
fundingSources: PostFundingSource[];
funding_sources: PostFundingSource[];

constructor(obj?: any) {
defaultLog.debug({ label: 'PostFundingData', message: 'params', obj });

this.fundingSources =
(obj?.fundingSources?.length && obj.fundingSources.map((item: any) => new PostFundingSource(item))) || [];
this.funding_sources =
(obj?.funding_sources?.length && obj.funding_sources.map((item: any) => new PostFundingSource(item))) || [];
}
}

Expand Down
18 changes: 11 additions & 7 deletions api/src/models/project-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,17 @@ describe('PutFundingSource', () => {

before(() => {
data = new PutFundingSource({
id: 1,
investment_action_category: 1,
agency_project_id: 'agency project id',
funding_amount: 20,
start_date: '2020/04/04',
end_date: '2020/05/05',
revision_count: 1
fundingSources: [
{
id: 1,
investment_action_category: 1,
agency_project_id: 'agency project id',
funding_amount: 20,
start_date: '2020/04/04',
end_date: '2020/05/05',
revision_count: 1
}
]
});
});

Expand Down
33 changes: 9 additions & 24 deletions api/src/models/project-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,30 +117,15 @@ export class PutFundingSource {
constructor(obj?: any) {
defaultLog.debug({ label: 'PutFundingSource', message: 'params', obj });

this.id = obj?.id || null;
this.investment_action_category = obj?.investment_action_category || null;
this.agency_project_id = obj?.agency_project_id || null;
this.funding_amount = obj?.funding_amount || null;
this.start_date = obj?.start_date || null;
this.end_date = obj?.end_date || null;
this.revision_count = obj?.revision_count ?? null;
}
}

/**
* Processes PUT /project funding data
*
* @export
* @class PostFundingData
*/
export class PutFundingData {
fundingSources: PutFundingSource[];

constructor(obj?: any) {
defaultLog.debug({ label: 'PostFundingData', message: 'params', obj });

this.fundingSources =
(obj?.fundingSources?.length && obj.fundingSources.map((item: any) => new PutFundingSource(item))) || [];
const fundingSource = obj?.fundingSources?.length && obj.fundingSources[0];

this.id = fundingSource?.id || null;
this.investment_action_category = fundingSource?.investment_action_category || null;
this.agency_project_id = fundingSource?.agency_project_id || null;
this.funding_amount = fundingSource?.funding_amount || null;
this.start_date = fundingSource?.start_date || null;
this.end_date = fundingSource?.end_date || null;
this.revision_count = fundingSource?.revision_count ?? null;
}
}

Expand Down
21 changes: 0 additions & 21 deletions api/src/queries/project/project-delete-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,6 @@ export const deleteProjectFundingSourceSQL = (
return sqlStatement;
};

/**
* SQL query to delete all project funding source record.
*
* @param {projectId} projectId
* @returns {SQLStatement} sql query object
*/
export const deleteAllProjectFundingSourceSQL = (projectId: number | undefined): SQLStatement | null => {
if (!projectId) {
return null;
}

const sqlStatement: SQLStatement = SQL`
DELETE
from project_funding_source
WHERE
project_id = ${projectId};
`;

return sqlStatement;
};

/**
* SQL query to delete a project row (and associated data) based on project ID.
*
Expand Down
16 changes: 10 additions & 6 deletions api/src/queries/project/project-update-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,16 @@ describe('putProjectFundingSourceSQL', () => {
it('returns a SQLStatement when all fields are passed in as expected', () => {
const response = putProjectFundingSourceSQL(
new PutFundingSource({
investment_action_category: 222,
agency_project_id: 'funding source name',
funding_amount: 10000,
start_date: '2020-02-02',
end_date: '2020-03-02',
revision_count: 11
fundingSources: [
{
investment_action_category: 222,
agency_project_id: 'funding source name',
funding_amount: 10000,
start_date: '2020-02-02',
end_date: '2020-03-02',
revision_count: 11
}
]
}),
1
);
Expand Down
31 changes: 0 additions & 31 deletions api/src/queries/survey/survey-delete-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,6 @@ export const deleteSurveyFundingSourceByProjectFundingSourceIdSQL = (
return sqlStatement;
};

/**
* SQL query to delete survey funding sources rows if deleted from project level
*
* @param {(any[] | undefined)} projectFundingSourceIds
* @return {*} {(SQLStatement | null)}
*/
export const deleteSurveyFundingSourceConnectionToProjectSQL = (
projectFundingSourceIds: any[] | undefined
): SQLStatement | null => {
if (!projectFundingSourceIds) {
return null;
}

const sqlStatement: SQLStatement = SQL`
DELETE
from survey_funding_source sfs
WHERE
sfs.project_funding_source_id
NOT IN
( ${projectFundingSourceIds[0]}`;

for (let i = 1; i < projectFundingSourceIds.length; i++) {
sqlStatement.append(`, ${projectFundingSourceIds[i]}`);
}
sqlStatement.append(`);`);

console.log('sqlStatement', sqlStatement);

return sqlStatement;
};

/**
* SQL query to delete all survey species rows.
*
Expand Down
78 changes: 26 additions & 52 deletions api/src/services/project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { IPostIUCN, PostFundingSource, PostProjectObject } from '../models/proje
import {
IPutIUCN,
PutCoordinatorData,
PutFundingData,
PutFundingSource,
PutIUCNData,
PutLocationData,
Expand Down Expand Up @@ -260,8 +259,8 @@ export class ProjectService extends DBService {
}
if (entities.includes(GET_ENTITIES.funding)) {
promises.push(
this.getFundingData(projectId).then((value) => {
results.funding = value;
this.getProjectData(projectId).then((value) => {
results.project = value;
})
);
}
Expand Down Expand Up @@ -428,16 +427,14 @@ export class ProjectService extends DBService {
}

async createProject(postProjectData: PostProjectObject): Promise<number> {
console.log('CREATE PROJECTTTTTTTTTT', postProjectData);

const projectId = await this.insertProject(postProjectData);

const promises: Promise<any>[] = [];

// Handle funding sources
promises.push(
Promise.all(
postProjectData.funding.fundingSources.map((fundingSource: PostFundingSource) =>
postProjectData.funding.funding_sources.map((fundingSource: PostFundingSource) =>
this.insertFundingSource(fundingSource, projectId)
)
)
Expand Down Expand Up @@ -511,20 +508,14 @@ export class ProjectService extends DBService {
}

async insertFundingSource(fundingSource: PostFundingSource, project_id: number): Promise<number> {
console.log('fundingSource--------------------------------------', fundingSource);

const sqlStatement = queries.project.postProjectFundingSourceSQL(fundingSource, project_id);

console.log('sqlStatement', sqlStatement);

if (!sqlStatement) {
throw new HTTP400('Failed to build SQL insert statement');
}

const response = await this.connection.query(sqlStatement.text, sqlStatement.values);

console.log('response', response);

const result = (response && response.rows && response.rows[0]) || null;

if (!result || !result.id) {
Expand Down Expand Up @@ -633,9 +624,6 @@ export class ProjectService extends DBService {
async updateProject(projectId: number, entities: IUpdateProject) {
const promises: Promise<any>[] = [];

console.log('projectId', projectId);
console.log('entities', entities);

if (entities?.partnerships) {
promises.push(this.updatePartnershipsData(projectId, entities));
}
Expand Down Expand Up @@ -788,37 +776,27 @@ export class ProjectService extends DBService {
}

async updateFundingData(projectId: number, entities: IUpdateProject): Promise<void> {
const putFundingData = entities?.funding && new PutFundingData(entities.funding);

const fundingIds = putFundingData?.fundingSources.map((data) => {
return data.id;
});
const putFundingSource = entities?.funding && new PutFundingSource(entities.funding);

console.log('fundingIds', fundingIds);

const surveyFundingSourceDeleteStatement = queries.survey.deleteSurveyFundingSourceConnectionToProjectSQL(
fundingIds
const surveyFundingSourceDeleteStatement = queries.survey.deleteSurveyFundingSourceByProjectFundingSourceIdSQL(
putFundingSource?.id
);
const projectFundingSourceDeleteStatement = queries.project.deleteProjectFundingSourceSQL(
projectId,
putFundingSource?.id
);

console.log('surveyFundingSourceDeleteStatement', surveyFundingSourceDeleteStatement);

if (surveyFundingSourceDeleteStatement) {
const surveyFundingSourceDeleteResult = await this.connection.query(
surveyFundingSourceDeleteStatement.text,
surveyFundingSourceDeleteStatement.values
);

if (!surveyFundingSourceDeleteResult) {
throw new HTTP409('Failed to delete survey funding source');
}
if (!projectFundingSourceDeleteStatement || !surveyFundingSourceDeleteStatement) {
throw new HTTP400('Failed to build SQL delete statement');
}

console.log('putFundingData', putFundingData);
const projectFundingSourceDeleteStatement = queries.project.deleteAllProjectFundingSourceSQL(projectId);
console.log('projectFundingSourceDeleteStatement', projectFundingSourceDeleteStatement);
const surveyFundingSourceDeleteResult = await this.connection.query(
surveyFundingSourceDeleteStatement.text,
surveyFundingSourceDeleteStatement.values
);

if (!projectFundingSourceDeleteStatement) {
throw new HTTP400('Failed to build SQL insert statement');
if (!surveyFundingSourceDeleteResult) {
throw new HTTP409('Failed to delete survey funding source');
}

const projectFundingSourceDeleteResult = await this.connection.query(
Expand All @@ -830,21 +808,17 @@ export class ProjectService extends DBService {
throw new HTTP409('Failed to delete project funding source');
}

putFundingData?.fundingSources.forEach(async (putFundingSource: PutFundingSource) => {
const sqlInsertStatement = queries.project.putProjectFundingSourceSQL(putFundingSource, projectId);
console.log('sqlInsertStatement', sqlInsertStatement);
const sqlInsertStatement = queries.project.putProjectFundingSourceSQL(putFundingSource, projectId);

if (!sqlInsertStatement) {
throw new HTTP400('Failed to build SQL insert statement');
}
if (!sqlInsertStatement) {
throw new HTTP400('Failed to build SQL insert statement');
}

const insertResult = await this.connection.query(sqlInsertStatement.text, sqlInsertStatement.values);
console.log('insertResult', insertResult);
const insertResult = await this.connection.query(sqlInsertStatement.text, sqlInsertStatement.values);

if (!insertResult) {
throw new HTTP409('Failed to put (insert) project funding source with incremented revision count');
}
});
if (!insertResult) {
throw new HTTP409('Failed to put (insert) project funding source with incremented revision count');
}
}

async deleteProject(projectId: number): Promise<boolean | null> {
Expand Down
Loading

0 comments on commit 9dbda16

Please sign in to comment.