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

Full flow of editing objectives #176

Merged
merged 9 commits into from
Mar 29, 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
100 changes: 99 additions & 1 deletion api/src/models/project-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { describe } from 'mocha';
import {
GetCoordinatorData,
GetPartnershipsData,
GetObjectivesData,
PutCoordinatorData,
PutPartnershipsData,
PutSpeciesData
PutSpeciesData,
PutObjectivesData
} from './project-update';

describe('PutPartnershipsData', () => {
Expand Down Expand Up @@ -310,3 +312,99 @@ describe('PutSpeciesData', () => {
});
});
});

describe('GetObjectivesData', () => {
describe('No values provided', () => {
let data: GetObjectivesData;

before(() => {
data = new GetObjectivesData({});
anissa-agahchen marked this conversation as resolved.
Show resolved Hide resolved
});

it('sets objectives', () => {
expect(data.objectives).to.equal(null);
});

it('sets caveats', () => {
expect(data.caveats).to.equal(null);
});

it('sets revision_count', () => {
expect(data.revision_count).to.equal(null);
});
});

describe('all values provided', () => {
const obj = {
objectives: 'objectives',
caveats: 'caveats',
revision_count: 1
};

let data: GetObjectivesData;

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

it('sets objectives', () => {
expect(data.objectives).to.equal(obj.objectives);
});

it('sets caveats', () => {
expect(data.caveats).to.equal(obj.caveats);
});

it('sets revision_count', () => {
expect(data.revision_count).to.equal(obj.revision_count);
});
});
});

describe('PutObjectivesData', () => {
describe('No values provided', () => {
let data: PutObjectivesData;

before(() => {
data = new PutObjectivesData({});
});

it('sets objectives', () => {
expect(data.objectives).to.equal('');
});

it('sets caveats', () => {
expect(data.caveats).to.equal(null);
});

it('sets revision_count', () => {
expect(data.revision_count).to.equal(null);
});
});

describe('all values provided', () => {
const obj = {
objectives: 'objectives',
caveats: 'caveats',
revision_count: 1
};

let data: PutObjectivesData;

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

it('sets objectives', () => {
expect(data.objectives).to.equal(obj.objectives);
});

it('sets caveats', () => {
expect(data.caveats).to.equal(obj.caveats);
});

it('sets revision_count', () => {
expect(data.revision_count).to.equal(obj.revision_count);
});
});
});
14 changes: 14 additions & 0 deletions api/src/models/project-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,17 @@ export class GetIUCNClassificationData {
[];
}
}

export class GetObjectivesData {
objectives: string;
caveats: string;
revision_count: number;

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

this.objectives = obj?.objectives || null;
this.caveats = obj?.caveats || null;
this.revision_count = obj?.revision_count ?? null;
}
}
32 changes: 30 additions & 2 deletions api/src/paths/project/{projectId}/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GetCoordinatorData,
GetIUCNClassificationData,
GetPartnershipsData,
GetObjectivesData,
PutCoordinatorData,
PutPartnershipsData,
PutLocationData,
Expand All @@ -26,7 +27,8 @@ import {
getCoordinatorByProjectSQL,
putProjectSQL,
getIndigenousPartnershipsByProjectSQL,
getIUCNActionClassificationByProjectSQL
getIUCNActionClassificationByProjectSQL,
getObjectivesByProjectSQL
} from '../../../queries/project/project-update-queries';
import {
deleteIUCNSQL,
Expand Down Expand Up @@ -131,7 +133,7 @@ export interface IGetProjectForUpdate {
coordinator: GetCoordinatorData | null;
permit: any;
project: any;
objectives: any;
objectives: GetObjectivesData | null;
location: any;
species: any;
iucn: GetIUCNClassificationData | null;
Expand Down Expand Up @@ -205,6 +207,14 @@ function getProjectForUpdate(): RequestHandler {
);
}

if (entities.includes(GET_ENTITIES.objectives)) {
promises.push(
getObjectivesData(projectId, connection).then((value) => {
results.objectives = value;
})
);
}

await Promise.all(promises);

await connection.commit();
Expand Down Expand Up @@ -311,6 +321,24 @@ export const getSpeciesData = async (projectId: number, connection: IDBConnectio
return new GetSpeciesData(resultFocalSpecies, resultAncillarySpecies);
};

export const getObjectivesData = async (projectId: number, connection: IDBConnection): Promise<GetObjectivesData> => {
const sqlStatement = getObjectivesByProjectSQL(projectId);

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

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

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

if (!result) {
throw new HTTP400('Failed to get project objectives data');
}

return new GetObjectivesData(result);
};

export const PUT: Operation = [logRequest('paths/project/{projectId}/update', 'PUT'), updateProject()];

PUT.apiDoc = {
Expand Down
17 changes: 16 additions & 1 deletion api/src/queries/project/project-update-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { describe } from 'mocha';
import {
getIndigenousPartnershipsByProjectSQL,
getCoordinatorByProjectSQL,
getIUCNActionClassificationByProjectSQL
getIUCNActionClassificationByProjectSQL,
getObjectivesByProjectSQL
} from './project-update-queries';

describe('getIndigenousPartnershipsByProjectSQL', () => {
Expand Down Expand Up @@ -47,3 +48,17 @@ describe('getCoordinatorByProjectSQL', () => {
expect(response).to.not.be.null;
});
});

describe('getObjectivesByProjectSQL', () => {
it('Null projectId', () => {
const response = getObjectivesByProjectSQL((null as unknown) as number);

expect(response).to.be.null;
});

it('valid projectId', () => {
const response = getObjectivesByProjectSQL(1);

expect(response).to.not.be.null;
});
});
33 changes: 33 additions & 0 deletions api/src/queries/project/project-update-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,36 @@ export const putProjectSQL = (

return sqlStatement;
};

/**
* SQL query to get objectives information, for update purposes.
*
* @param {number} projectId
* @return {*} {(SQLStatement | null)}
*/
export const getObjectivesByProjectSQL = (projectId: number): SQLStatement | null => {
defaultLog.debug({ label: 'getObjectivesByProjectSQL', message: 'params', projectId });

if (!projectId) {
return null;
}
const sqlStatement = SQL`
SELECT
objectives,
caveats,
revision_count
FROM
project
WHERE
id = ${projectId};
`;

defaultLog.debug({
label: 'getObjectivesByProjectSQL',
message: 'sql',
'sqlStatement.text': sqlStatement.text,
'sqlStatement.values': sqlStatement.values
});

return sqlStatement;
};
2 changes: 1 addition & 1 deletion app/src/features/projects/view/ProjectDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const ProjectDetails: React.FC<IProjectDetailsProps> = (props) => {
</Box>
<Box mb={3}>
<Paper>
<ProjectObjectives projectForViewData={projectForViewData} />
<ProjectObjectives projectForViewData={projectForViewData} codes={codes} refresh={props.refresh} />
</Paper>
</Box>

Expand Down
Loading