Skip to content

Commit

Permalink
Fix: upgrade fixes (#162)
Browse files Browse the repository at this point in the history
* fix(be):
- Suggested fix to support upgrade

* fix(be):
- Downgrade version as it is causing issues

* fix:
- Updated packahe-lock

* fix(be):
- Fix due to changes in the axios-oauth-client library's version 2.0.0

* fix(be):
- Fix due to changes in the axios-oauth-client library's version 2.0.0

* fix(be):
- Fix due to changes in the axios-oauth-client library's version 2.0.0

* fix(be):
- Trying different library

* fix(be):
- Another trial

* fix(be):
- Another trial

* fix: fixing oauth issues

* fix: fixing catch line

---------

Co-authored-by: Paulo Gomes da Cruz Junior <paulushc@gmail.com>
Co-authored-by: Paulo Gomes da Cruz Junior <paulo.cruz@gov.bc.ca>
  • Loading branch information
3 people authored Jul 13, 2023
1 parent d24ced7 commit ece6fa9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 44 deletions.
4 changes: 2 additions & 2 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ The backend for nr-old-growth project is written in [NestJS](https://github.com/
CHES_CLIENT_ID=[CHES service dev username]
CHES_CLIENT_SECRET=[CHES service dev password]
// CHES dev authentication url
# CHES dev authentication url
CHES_TOKEN_URL=https://dev.oidc.gov.bc.ca/auth/realms/jbd6rnxw/protocol/openid-connect/token
// CHES dev email url
# CHES dev email url
EMAIL_POST_URL=https://ches-dev.apps.silver.devops.gov.bc.ca/api/v1/email
CHES_EMAIL_FROM=[send from email address]
Expand Down
20 changes: 4 additions & 16 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"@nestjs/schedule": "^3.0.0",
"@nestjs/swagger": "^7.0.0",
"@nestjs/typeorm": "^10.0.0",
"axios": "^1.2.0-alpha.1",
"axios-oauth-client": "^1.5.0",
"axios": "^1.4.0",
"body-parser": "^1.20.0",
"dotenv": "^16.0.3",
"pg": "^8.9.0",
Expand Down Expand Up @@ -67,8 +66,8 @@
"@nestjs/schematics": "^9.0.3",
"@nestjs/testing": "^9.1.4",
"@types/cron": "^2.0.0",
"@types/jest": "^29.4.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.4.0",
"@types/node": "^18.14.1",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.54.1",
Expand Down
4 changes: 2 additions & 2 deletions backend/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) { }
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
}

@Get('check')
getCheck(): string {
getCheck(): Promise<string> {
return this.appService.getToken();
}
}
44 changes: 26 additions & 18 deletions backend/src/ches/services/ches.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import axios from 'axios';
import { EmailEntity } from '../../email/model/email.entity';

const oauth = require('axios-oauth-client');
import { EmailEntity } from '../../email/model/email.entity';

@Injectable()
export class ChesService {
getToken() {
const getClientCredentials = oauth.client(axios.create(), {
url: process.env.CHES_TOKEN_URL,
grant_type: 'client_credentials',
client_id: process.env.CHES_CLIENT_ID,
client_secret: process.env.CHES_CLIENT_SECRET,
});

return getClientCredentials()
.then((res) => {
if (res && res.access_token) return res.access_token;
else return null;
return axios
.request({
method: 'POST',
url: process.env.CHES_TOKEN_URL,
auth: {
username: process.env.CHES_CLIENT_ID,
password: process.env.CHES_CLIENT_SECRET,
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data: 'grant_type=client_credentials',
})
.then((res) => res.data)
.then((token) => token?.access_token)
.catch((e) => {
throw new HttpException(
{ message: 'Failed to get email auth token from API: ' + e },
Expand All @@ -41,8 +43,11 @@ export class ChesService {
!process.env.CHES_EMAIL_FROM
) {
throw new HttpException(
{ message: 'Failed to config email, server side missing config of authentication url' +
'or CHES email server url or from email address' },
{
message:
'Failed to config email, server side missing config of authentication url' +
'or CHES email server url or from email address',
},
HttpStatus.BAD_REQUEST,
);
}
Expand Down Expand Up @@ -97,14 +102,17 @@ export class ChesService {
}
}
throw new HttpException(
{ message: 'Failed to get email auth token: response or response access token is null' },
{
message:
'Failed to get email auth token: response or response access token is null',
},
HttpStatus.BAD_REQUEST,
);
})
.catch((e) => {
throw new HttpException(
{ message: e },
HttpStatus.INTERNAL_SERVER_ERROR
{ message: e },
HttpStatus.INTERNAL_SERVER_ERROR,
);
});
}
Expand Down
4 changes: 1 addition & 3 deletions backend/src/form/services/form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { EmailService } from '../../email/services/email.service';
import { EmailEntity } from '../../email/model/email.entity';
import { from } from 'rxjs';

const oauth = require('axios-oauth-client');

@Injectable()
export class FormService {
private readonly logger = new Logger(FormService.name);
Expand Down Expand Up @@ -395,7 +393,7 @@ export class FormService {
}
})
.catch((e) => {
const errorMsg = `Failed to get submissions list need notification from API: ${e}`;
const errorMsg = 'Failed to get submissions list need notification from API: ' + e;
const emailSubmissionLogEntity: EmailSubmissionLog = {
code: 'FAILED',
exceptionLog: errorMsg,
Expand Down

0 comments on commit ece6fa9

Please sign in to comment.