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

Release Dev to Test #1302

Merged
merged 1 commit into from
Jun 3, 2024
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
38 changes: 0 additions & 38 deletions api/src/repositories/funding-source-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,44 +375,6 @@ describe('FundingSourceRepository', () => {
});
});

describe('getSurveyFundingSourceByFundingSourceId', () => {
it('returns a single funding source basic supplementary data', async () => {
const expectedResult = {
survey_funding_source_id: 1,
survey_id: 1,
funding_source_id: 1,
amount: 1,
revision_count: 1
};

const mockResponse = { rowCount: 1, rows: [expectedResult] } as unknown as Promise<QueryResult<any>>;

const dbConnection = getMockDBConnection({ sql: async () => mockResponse });

const fundingSourceRepository = new FundingSourceRepository(dbConnection);

const response = await fundingSourceRepository.getSurveyFundingSourceByFundingSourceId(1, 1);

expect(response).to.eql(expectedResult);
});

it('throws an error if rowCount is 0', async () => {
const mockResponse = { rowCount: 0, rows: [] } as unknown as Promise<QueryResult<any>>;

const dbConnection = getMockDBConnection({ sql: async () => mockResponse });

const fundingSourceRepository = new FundingSourceRepository(dbConnection);

try {
await fundingSourceRepository.getSurveyFundingSourceByFundingSourceId(1, 1);

expect.fail();
} catch (error) {
expect((error as ApiError).message).to.equal('Failed to get survey funding source');
}
});
});

describe('getSurveyFundingSources', () => {
it('returns all survey funding sources', async () => {
const expectedResult = [
Expand Down
59 changes: 21 additions & 38 deletions api/src/repositories/funding-source-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ export class FundingSourceRepository extends BaseRepository {
survey_funding_source.funding_source_id = ${fundingSourceId}
)
SELECT
funding_source.*,
funding_source.funding_source_id,
funding_source.name,
funding_source.description,
funding_source.start_date,
funding_source.end_date,
funding_source.revision_count,
w_references.survey_reference_count,
w_references.survey_reference_amount_total
FROM
Expand All @@ -171,7 +176,7 @@ export class FundingSourceRepository extends BaseRepository {
}

/**
* Fetch all survey references to a single funding source.
* For a single funding source, fetch basic information about all surveys that reference it.
*
* @param {number} fundingSourceId
* @return {*} {(Promise<(SurveyFundingSource | SurveyFundingSourceSupplementaryData)[]>)}
Expand All @@ -182,8 +187,11 @@ export class FundingSourceRepository extends BaseRepository {
): Promise<(SurveyFundingSource | SurveyFundingSourceSupplementaryData)[]> {
const sqlStatement = SQL`
SELECT
survey_funding_source.*,
survey_funding_source.survey_funding_source_id,
survey_funding_source.survey_id,
survey_funding_source.funding_source_id,
survey_funding_source.amount::numeric::int,
survey_funding_source.revision_count,
survey.project_id,
survey.name as survey_name
FROM
Expand All @@ -198,7 +206,15 @@ export class FundingSourceRepository extends BaseRepository {

const response = await this.connection.sql(
sqlStatement,
SurveyFundingSource.extend(SurveyFundingSourceSupplementaryData.shape)
z.object({
survey_funding_source_id: z.number(),
survey_id: z.number(),
funding_source_id: z.number(),
amount: z.number(),
revision_count: z.number(),
project_id: z.number(),
survey_name: z.string()
})
);

return response.rows;
Expand Down Expand Up @@ -315,40 +331,7 @@ export class FundingSourceRepository extends BaseRepository {
*/

/**
* Fetch a single survey funding source by survey id and funding source id.
*
* @param {number} surveyId
* @param {number} fundingSourceId
* @return {*} {Promise<SurveyFundingSource>}
* @memberof FundingSourceRepository
*/
async getSurveyFundingSourceByFundingSourceId(
surveyId: number,
fundingSourceId: number
): Promise<SurveyFundingSource> {
const sqlStatement = SQL`
SELECT
*,
amount::numeric::int
FROM
survey_funding_source
WHERE
survey_id = ${surveyId}
AND
funding_source_id = ${fundingSourceId};
`;
const response = await this.connection.sql(sqlStatement, SurveyFundingSource);
if (response.rowCount !== 1) {
throw new ApiExecuteSQLError('Failed to get survey funding source', [
'FundingSourceRepository->getSurveyFundingSourceByFundingSourceId',
'rowCount was != 1, expected rowCount = 1'
]);
}
return response.rows[0];
}

/**
* Fetch all survey funding sources by survey id.
* For a single survey, fetch all of its funding sources.
*
* @param {number} surveyId
* @return {*} {Promise<SurveyFundingSource[]>}
Expand Down
28 changes: 2 additions & 26 deletions api/src/services/funding-source-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,30 +178,6 @@ describe('FundingSourceService', () => {
});
});

describe('getSurveyFundingSourceByFundingSourceId', () => {
it('returns a funding source item', async () => {
const dbConnection = getMockDBConnection();
const fundingSourceService = new FundingSourceService(dbConnection);

const expectedResult = {
funding_source_id: 1,
survey_funding_source_id: 1,
survey_id: 1,
amount: 1,
revision_count: 1
};

const getSurveyFundingSourceByFundingSourceIdStub = sinon
.stub(FundingSourceRepository.prototype, 'getSurveyFundingSourceByFundingSourceId')
.resolves(expectedResult);

const response = await fundingSourceService.getSurveyFundingSourceByFundingSourceId(1, 1);

expect(getSurveyFundingSourceByFundingSourceIdStub).to.be.calledOnce;
expect(response).to.eql(expectedResult);
});
});

describe('getSurveyFundingSources', () => {
it('returns an array of funding source items', async () => {
const dbConnection = getMockDBConnection();
Expand All @@ -217,13 +193,13 @@ describe('FundingSourceService', () => {
}
];

const getSurveyFundingSourceByFundingSourceIdStub = sinon
const getSurveyFundingSourcesStub = sinon
.stub(FundingSourceRepository.prototype, 'getSurveyFundingSources')
.resolves(expectedResult);

const response = await fundingSourceService.getSurveyFundingSources(1);

expect(getSurveyFundingSourceByFundingSourceIdStub).to.be.calledOnce;
expect(getSurveyFundingSourcesStub).to.be.calledOnce;
expect(response).to.eql(expectedResult);
});
});
Expand Down
15 changes: 0 additions & 15 deletions api/src/services/funding-source-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,6 @@ export class FundingSourceService extends DBService {
* SURVEY FUNDING SOURCE FUNCTIONS
*/

/**
* Fetch a single survey funding source by survey id and funding source id.
*
* @param {number} surveyId
* @param {number} fundingSourceId
* @return {*} {Promise<SurveyFundingSource>}
* @memberof FundingSourceService
*/
async getSurveyFundingSourceByFundingSourceId(
surveyId: number,
fundingSourceId: number
): Promise<SurveyFundingSource> {
return this.fundingSourceRepository.getSurveyFundingSourceByFundingSourceId(surveyId, fundingSourceId);
}

/**
* Fetch all survey funding sources by survey id.
*
Expand Down