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

Edit funding rework #207

Merged
merged 20 commits into from
Apr 9, 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
6 changes: 3 additions & 3 deletions api/src/models/project-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ export class PostFundingSource {
* @class PostFundingData
*/
export class PostFundingData {
funding_agencies: PostFundingSource[];
funding_sources: PostFundingSource[];

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

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

Expand Down
24 changes: 24 additions & 0 deletions api/src/models/project-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,27 @@ export class GetProjectData {
this.revision_count = projectData?.revision_count ?? null;
}
}

export class PutFundingSource {
id: number;
investment_action_category: number;
agency_project_id: string;
funding_amount: number;
start_date: string;
end_date: string;
revision_count: number;

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

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;
}
}
43 changes: 42 additions & 1 deletion api/src/models/project-view-update.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { describe } from 'mocha';
import { GetSpeciesData } from './project-view-update';
import { GetSpeciesData, GetFundingData } from './project-view-update';

describe('GetSpeciesData', () => {
describe('No values provided', () => {
Expand Down Expand Up @@ -75,4 +75,45 @@ describe('GetSpeciesData', () => {
expect(data.ancillary_species).to.eql(['species 3']);
});
});

describe('GetFundingData', () => {
describe('No values provided', () => {
let fundingData: GetFundingData;

before(() => {
fundingData = new GetFundingData([]);
});

it('sets project funding sources', function () {
expect(fundingData.fundingSources).to.eql([]);
});
});

describe('All values provided', () => {
let fundingData: GetFundingData;

const fundingDataObj = [
{
id: 1,
agency_id: '123',
agency_name: 'Agency name',
agency_project_id: 'Agency123',
investment_action_category: 'Investment',
investment_action_category_name: 'Investment name',
start_date: '01/01/2020',
end_date: '01/01/2021',
funding_amount: 123,
revision_count: 0
}
];

before(() => {
fundingData = new GetFundingData(fundingDataObj);
});

it('sets project funding sources', function () {
expect(fundingData.fundingSources).to.eql(fundingDataObj);
});
});
});
});
43 changes: 43 additions & 0 deletions api/src/models/project-view-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,46 @@ export class GetSpeciesData {
this.ancillary_species = (ancillary_species?.length && ancillary_species.map((item: any) => item.name)) || [];
}
}

interface IGetFundingSource {
id: number;
agency_id: number;
investment_action_category: number;
investment_action_category_name: string;
agency_name: string;
funding_amount: number;
start_date: string;
end_date: string;
agency_project_id: string;
revision_count: number;
}

export class GetFundingData {
fundingSources: IGetFundingSource[];

constructor(fundingData?: any[]) {
defaultLog.debug({
label: 'GetFundingData',
message: 'params',
fundingData: fundingData
});

this.fundingSources =
(fundingData &&
fundingData.map((item: any) => {
return {
id: item.id,
agency_id: item.agency_id,
investment_action_category: item.investment_action_category,
investment_action_category_name: item.investment_action_category_name,
agency_name: item.agency_name,
funding_amount: item.funding_amount,
start_date: item.start_date,
end_date: item.end_date,
agency_project_id: item.agency_project_id,
revision_count: item.revision_count
};
})) ||
[];
}
}
38 changes: 0 additions & 38 deletions api/src/models/project-view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { expect } from 'chai';
import { describe } from 'mocha';
import {
GetCoordinatorData,
GetFundingData,
GetIUCNClassificationData,
GetLocationData,
GetObjectivesData,
Expand Down Expand Up @@ -201,43 +200,6 @@ describe('GetIUCNClassificationData', () => {
});
});

describe('GetFundingData', () => {
describe('No values provided', () => {
let fundingData: GetFundingData;

before(() => {
fundingData = new GetFundingData([]);
});

it('sets funding agencies', function () {
expect(fundingData.fundingAgencies).to.eql([]);
});
});

describe('All values provided', () => {
let fundingData: GetFundingData;

const fundingDataObj = [
{
agency_id: '123',
agency_name: 'Agency name',
investment_action_category: 'investment',
start_date: '01/01/2020',
end_date: '01/01/2021',
funding_amount: 123
}
];

before(() => {
fundingData = new GetFundingData(fundingDataObj);
});

it('sets funding agencies', function () {
expect(fundingData.fundingAgencies).to.eql(fundingDataObj);
});
});
});

describe('GetObjectivesData', () => {
describe('No values provided', () => {
let projectObjectivesData: GetObjectivesData;
Expand Down
41 changes: 0 additions & 41 deletions api/src/models/project-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,47 +140,6 @@ export class GetIUCNClassificationData {
}
}

interface IGetFundingSource {
agency_id: string;
investment_action_category: string;
agency_name: string;
funding_amount: number;
start_date: string;
end_date: string;
}

/**
* Pre-processes GET /projects/{id} funding data
*
* @export
* @class GetFundingData
*/
export class GetFundingData {
fundingAgencies: IGetFundingSource[];

constructor(fundingData?: any[]) {
defaultLog.debug({
label: 'GetFundingData',
message: 'params',
fundingData: fundingData
});

this.fundingAgencies =
(fundingData &&
fundingData.map((item: any) => {
return {
agency_id: item.agency_id,
investment_action_category: item.investment_action_category,
agency_name: item.agency_name,
funding_amount: item.funding_amount,
start_date: item.start_date,
end_date: item.end_date
};
})) ||
[];
}
}

/**
* Pre-processes GET /projects/{id} partnerships data
*
Expand Down
2 changes: 1 addition & 1 deletion api/src/openapi/schemas/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const projectCreatePostRequestObject = {
title: 'Project funding sources',
type: 'object',
properties: {
funding_agencies: {
funding_sources: {
type: 'array',
items: {
title: 'Project funding agency',
Expand Down
4 changes: 2 additions & 2 deletions api/src/paths/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ function createProject(): RequestHandler {
)
);

// Handle funding agencies
// Handle funding sources
promises.push(
Promise.all(
sanitizedProjectPostData.funding.funding_agencies.map((fundingSource: PostFundingSource) =>
sanitizedProjectPostData.funding.funding_sources.map((fundingSource: PostFundingSource) =>
insertFundingSource(fundingSource, projectId, connection)
)
)
Expand Down
54 changes: 50 additions & 4 deletions api/src/paths/project/{projectId}/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import {
PutIUCNData,
PutSpeciesData,
IGetPutIUCN,
GetLocationData
GetLocationData,
PutFundingSource
} from '../../../models/project-update';
import { GetSpeciesData } from '../../../models/project-view-update';
import { GetSpeciesData, GetFundingData } from '../../../models/project-view-update';
import {
projectIdResponseObject,
projectUpdateGetResponseObject,
Expand All @@ -31,6 +32,7 @@ import {
getIUCNActionClassificationByProjectSQL,
getObjectivesByProjectSQL,
getProjectByProjectSQL,
putProjectFundingSourceSQL,
putProjectSQL
} from '../../../queries/project/project-update-queries';
import {
Expand All @@ -41,7 +43,8 @@ import {
deleteAncillarySpeciesSQL,
deleteIndigenousPartnershipsSQL,
deleteStakeholderPartnershipsSQL,
deleteRegionsSQL
deleteRegionsSQL,
deleteFundingSQL
} from '../../../queries/project/project-delete-queries';
import {
getStakeholderPartnershipsByProjectSQL,
Expand Down Expand Up @@ -149,7 +152,7 @@ export interface IGetProjectForUpdate {
location: any;
species: any;
iucn: GetIUCNClassificationData | null;
funding: any;
funding: GetFundingData | null;
partnerships: GetPartnershipsData | null;
}

Expand Down Expand Up @@ -242,6 +245,13 @@ function getProjectForUpdate(): RequestHandler {
})
);
}
if (entities.includes(GET_ENTITIES.funding)) {
promises.push(
getProjectData(projectId, connection).then((value) => {
results.project = value;
})
);
}

await Promise.all(promises);

Expand Down Expand Up @@ -529,6 +539,10 @@ function updateProject(): RequestHandler {
promises.push(updateProjectSpeciesData(projectId, entities, connection));
}

if (entities?.funding) {
promises.push(updateProjectFundingData(projectId, entities, connection));
}

await Promise.all(promises);

await connection.commit();
Expand Down Expand Up @@ -776,3 +790,35 @@ export const updateProjectData = async (

await Promise.all([...insertActivityPromises, ...insertClimateInitiativesPromises]);
};

export const updateProjectFundingData = async (
projectId: number,
entities: IUpdateProject,
connection: IDBConnection
): Promise<void> => {
const putFundingSource = entities?.funding && new PutFundingSource(entities.funding);

const sqlDeleteStatement = deleteFundingSQL(putFundingSource?.id);

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

const deleteResult = await connection.query(sqlDeleteStatement.text, sqlDeleteStatement.values);

if (!deleteResult) {
throw new HTTP409('Failed to delete project funding source');
}

const sqlInsertStatement = putProjectFundingSourceSQL(putFundingSource, projectId);

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

const insertResult = await connection.query(sqlInsertStatement.text, sqlInsertStatement.values);

if (!insertResult) {
throw new HTTP409('Failed to put (insert) project funding source with incremented revision count');
}
};
7 changes: 3 additions & 4 deletions api/src/paths/project/{projectId}/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import { getDBConnection } from '../../../database/db';
import { HTTP400 } from '../../../errors/CustomError';
import {
GetCoordinatorData,
GetFundingData,
GetIUCNClassificationData,
GetObjectivesData,
GetPartnershipsData,
GetProjectData,
GetLocationData
} from '../../../models/project-view';
import { GetSpeciesData } from '../../../models/project-view-update';
import { GetSpeciesData, GetFundingData } from '../../../models/project-view-update';
import { projectViewGetResponseObject } from '../../../openapi/schemas/project';
import {
getFundingSourceByProjectSQL,
getIndigenousPartnershipsByProjectSQL,
getIUCNActionClassificationByProjectSQL,
getProjectSQL
Expand All @@ -26,7 +24,8 @@ import {
getAncillarySpeciesByProjectSQL,
getLocationByProjectSQL,
getClimateInitiativesByProjectSQL,
getActivitiesByProjectSQL
getActivitiesByProjectSQL,
getFundingSourceByProjectSQL
} from '../../../queries/project/project-view-update-queries';
import { getLogger } from '../../../utils/logger';
import { logRequest } from '../../../utils/path-utils';
Expand Down
Loading