Skip to content

Commit

Permalink
cleaned up mpi.service.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-krenz committed Nov 19, 2024
1 parent 9f47358 commit e728551
Showing 1 changed file with 27 additions and 53 deletions.
80 changes: 27 additions & 53 deletions src/mpi/mpi.service.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,28 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { Injectable, UnauthorizedException } from '@nestjs/common';

Check warning on line 1 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
import axios from 'axios';

// type ELabsStudyResponse = {
// studyID : number;
// projectID : number;
// groupID : number;
// subGroupID : number;
// userID : number;
// name : string;
// statusChanged: string;
// description : string;
// notes : string;
// approve : string;
// created : string;
// deleted : boolean;
// };

type eLabsStatus = 'PENDING' | 'PROGRESS' | 'COMPLETED';

// type ELabsExperimentResponse = {
// bearerToken : string,
// experimentID : number,
// studyID : number,
// name : string,
// status : eLabsStatus,
// templateID? : number,
// autoCollaborate?: boolean
// }

@Injectable()
export class MPIService {
private tokenStore = new Map<string, { accessToken: string; refreshToken: string; accessTokenExpiration: Date }>();

// TODO: this should be the actual user id, got from DAMP LAB auth?
private currentUserId = 'mpitest';

// To log in use:
// NOTE THAT THIS SERVER IS RUNNING ON PORT 5100
// https://mpi-demo.us.auth0.com/authorize?response_type=code&scope=offline_access&client_id=tZSXM9f8WUiPIpNGt1kXlGqzZVYvWNEF&redirect_uri=http://127.0.0.1:5100/mpi/auth0_redirect&audience=https://mpi-demo.com
// To log in use (update uri as needed):
// https://mpi-demo.us.auth0.com/authorize?response_type=code&scope=offline_access&client_id=<MPI_CLIENT_ID>&redirect_uri=http://127.0.0.1:5100/mpi/auth0_redirect&audience=https://mpi-demo.com
// To log out use:
// https://mpi-demo.us.auth0.com/oidc/logout?post_logout_redirect_uri=http://127.0.0.1:5100/mpi/auth0_logout&client_id=tZSXM9f8WUiPIpNGt1kXlGqzZVYvWNEF
// https://mpi-demo.us.auth0.com/oidc/logout?post_logout_redirect_uri=http://127.0.0.1:5100/mpi/auth0_logout&client_id=<MPI_CLIENT_ID>

async exchangeCodeForToken(code: string): Promise<string> {
const tokenUrl = `https://mpi-demo.us.auth0.com/oauth/token`;
const tokenUrl = process.env.MPI_TOKEN_URL || '';

Check warning on line 19 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const payload = {
grant_type: 'authorization_code',
client_id: process.env.MPI_CLIENT_ID,
client_secret: process.env.MPI_CLIENT_SECRET,
code,
redirect_uri: 'http://127.0.0.1:5100/mpi/auth0_redirect' // URL in this server that was used to redirect to after Auth0 login
redirect_uri: process.env.MPI_REDIRECT_URL // URL in this server that was used to redirect to after Auth0 login
};

Check warning on line 26 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

const response = await axios.post(tokenUrl, payload, {
Expand All @@ -74,7 +48,7 @@ export class MPIService {
async exchangeRefreshTokenForToken(): Promise<void> {
const refreshToken = this.tokenStore.get(this.currentUserId)!.refreshToken;

const tokenUrl = `https://mpi-demo.us.auth0.com/oauth/token`;
const tokenUrl = process.env.MPI_TOKEN_URL || '';

Check warning on line 51 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const payload = {
grant_type: 'refresh_token',
client_id: process.env.MPI_CLIENT_ID,
Expand Down Expand Up @@ -131,9 +105,9 @@ export class MPIService {
async createSequence(): Promise<any> {
const token = await this.getAccessToken();
if (!token) {
return new BadRequestException('No token found, log in to MPI first');
return new UnauthorizedException('No token found, log in to MPI first');

Check warning on line 108 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 109 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const sequences = await axios.get('http://localhost:5000/sequences', {
const sequences = await axios.get(`${process.env.MPI_BACKEND}/sequences`, {
headers: {
Authorization: `Bearer ${token}`
}
Expand All @@ -144,9 +118,9 @@ export class MPIService {
async getSequences(): Promise<any> {
const token = await this.getAccessToken();
if (!token) {
return new BadRequestException('No token found, log in to MPI first');
return new UnauthorizedException('No token found, log in to MPI first');

Check warning on line 121 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 122 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const sequences = await axios.get('http://localhost:5000/sequences', {
const sequences = await axios.get(`${process.env.MPI_BACKEND}/sequences`, {
headers: {
Authorization: `Bearer ${token}`
}
Expand All @@ -158,9 +132,9 @@ export class MPIService {
async azentaSeqOrder(id: string): Promise<any> {
const token = await this.getAccessToken();
if (!token) {
return new BadRequestException('No token found, log in to MPI first');
return new UnauthorizedException('No token found, log in to MPI first');

Check warning on line 135 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 136 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const azentaSeqOrder = await axios.get(`http://localhost:5000/azenta/seqOrder/${id}`, {
const azentaSeqOrder = await axios.get(`${process.env.MPI_BACKEND}/azenta/seqOrder/${id}`, {
headers: {
Authorization: `Bearer ${token}`
}
Expand All @@ -171,9 +145,9 @@ export class MPIService {
async azentaSeqOrders(): Promise<any> {
const token = await this.getAccessToken();
if (!token) {
return new BadRequestException('No token found, log in to MPI first');
return new UnauthorizedException('No token found, log in to MPI first');

Check warning on line 148 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 149 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const azentaSeqOrders = await axios.get('http://localhost:5000/azenta/seqOrder', {
const azentaSeqOrders = await axios.get(`${process.env.MPI_BACKEND}/azenta/seqOrder`, {
headers: {
Authorization: `Bearer ${token}`
}
Expand All @@ -187,9 +161,9 @@ export class MPIService {
orderName: 'DAMP_Azenta_Order'
};
if (!token) {
throw new BadRequestException('No token found, log in to MPI first');
throw new UnauthorizedException('No token found, log in to MPI first');

Check warning on line 164 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 165 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const azentaSeqOrders = await axios.post('http://localhost:5000/azenta/seqOrder', order, {
const azentaSeqOrders = await axios.post(`${process.env.MPI_BACKEND}/azenta/seqOrder`, order, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
Expand All @@ -202,11 +176,11 @@ export class MPIService {
async createELabsStudy(bearerToken: string, projectID: number, name: string): Promise<number | undefined> {
const token = await this.getAccessToken();
if (!token) {
throw new BadRequestException('No token found, log in to MPI first');
throw new UnauthorizedException('No token found, log in to MPI first');

Check warning on line 179 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 180 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
try {
const response = await axios.post(
'http://localhost:5000/e-labs/create-study',
`${process.env.MPI_BACKEND}/e-labs/create-study`,
{ bearerToken, projectID, name },
{
headers: {
Expand All @@ -230,11 +204,11 @@ export class MPIService {
async createELabsExperiment(bearerToken: string, studyID: number, name: string, status: eLabsStatus, templateID?: number, autoCollaborate?: boolean): Promise<number | undefined> {
const token = await this.getAccessToken();
if (!token) {
throw new BadRequestException('No token found, log in to MPI first');
throw new UnauthorizedException('No token found, log in to MPI first');

Check warning on line 207 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 208 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
try {
const response = await axios.post(
'http://localhost:5000/e-labs/create-experiment',
`${process.env.MPI_BACKEND}/e-labs/create-experiment`,
{ bearerToken, studyID, name, status, templateID, autoCollaborate },
{
headers: {
Expand All @@ -259,9 +233,9 @@ export class MPIService {
async getAclidScreenings(): Promise<any> {
const token = await this.getAccessToken();
if (!token) {
return new BadRequestException('No token found, log in to MPI first');
return new UnauthorizedException('No token found, log in to MPI first');

Check warning on line 236 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 237 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const aclid = await axios.get('http://localhost:5000/aclid/screens', {
const aclid = await axios.get(`${process.env.MPI_BACKEND}aclid/screens`, {
headers: {
Authorization: `Bearer ${token}`
}
Expand All @@ -272,9 +246,9 @@ export class MPIService {
async getAclidScreening(id: string): Promise<any> {
const token = await this.getAccessToken();
if (!token) {
return new BadRequestException('No token found, log in to MPI first');
return new UnauthorizedException('No token found, log in to MPI first');

Check warning on line 249 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 250 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const aclid = await axios.get(`http://localhost:5000/aclid/screens/${id}/details`, {
const aclid = await axios.get(`${process.env.MPI_BACKEND}/aclid/screens/${id}/details`, {
headers: {
Authorization: `Bearer ${token}`
}
Expand All @@ -286,10 +260,10 @@ export class MPIService {
// sequences: [{name: string, sequence: string}]
const token = await this.getAccessToken();
if (!token) {
return new BadRequestException('No token found, log in to MPI first');
return new UnauthorizedException('No token found, log in to MPI first');

Check warning on line 263 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 264 in src/mpi/mpi.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const aclid = await axios.post(
'http://localhost:5000/aclid/screen',
`${process.env.MPI_BACKEND}/aclid/screen`,
{ submissionName, sequences },
{
headers: {
Expand Down

0 comments on commit e728551

Please sign in to comment.